-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
99 lines (98 loc) · 2.62 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
//app.js
App({
onLaunch: function () {
this.login();
},
//登陆
login:function(){
var that = this;
var token = this.globalData.token;
if(token){
//检查token是否存在
wx.request({
url: that.globalData.serverPath +'/wxapplet/member/checktoken',
data:{
token: token
},
method:'GET',
success:function(res){
if(!res.data.success){
this.globalData.token = null;
that.login();
}
}
})
//验证成功,直接登陆后台
return ;
}
//如果不存在token,则需要
wx.login({
success:function(res){
wx.request({
url: that.globalData.serverPath +'/wxapplet/member/login',
data:{code:res.code},
success:function(res){
if(res.data.success){
if (res.data.attributes.token){
that.globalData.token = res.data.attributes.token;
that.globalData.appletMember = res.data.attributes.appletMember;
}else{
that.register();
}
return;
}
else{
wx.hideLoading();
wx.showModal({
title: '提示',
content: '无法登录,请重试',
showCancel: false
})
return;
}
}
})
}
})
},
register:function(){
var that = this;
wx.login({
success:function(res){
var code = res.code;
wx.getUserInfo({
success:function(res){
//将用户信息注册
var iv = res.iv;
var encryptedData = res.encryptedData;
wx.request({
url: that.globalData.serverPath+ '/wxapplet/member/register',
data: { code: code, encryptedData: encryptedData, iv: iv }, // 设置请求的 参数
success: (res) => {
if(res.data.success){
that.login();
wx.hideLoading();
}
else{
wx.showModal({
title: '提示',
content: '服务器出现问题',
showCancel: false
})
}
return ;
}
})
}
})
}
})
},
globalData: {
userInfo: null,
// serverPath: 'http://localhost:8080/yuansheng-weixinshop-server',
serverPath: 'https://www.ysdevelop.cn/yuansheng-weixinshop-server',
token:null,
appletMember: null
}
})