auth.js
9.98 KB
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
var t = require("./common.js");
module.exports = {
app: function() {
return getApp();
},
auth: function(t) {
var e = this.app(), o = this;
wx.checkSession({
success: function() {
!e.globalData.wechatUser && o.wxLogin(t);
},
fail: function() {
o.wxLogin(t);
}
});
},
isAuth: function() {
return !!this.app().globalData.wechatUser;
},
clearAuth: function() {
this.app().globalData.wechatUser = null, wx.setStorageSync("isAuth", !1);
},
hadAuth: function() {
try {
return !!wx.getStorageSync("isAuth");
} catch (t) {
return wx.setStorageSync("isAuth", !1), !1;
}
},
login_fir: function (e, o, n) {
var a = this.app(), app = a, i = this;
var userinfo= wx.getStorageSync("userinfo");
if(userinfo){
a.globalData.userInfo=userinfo;
a.globalData.user_id=userinfo.user_id;
"function" == typeof n && n(a.globalData.userInfo, a.globalData.wechatUser);
return true;
}
if (void 0 === e || "" == e) return a.globalData.wechatUser = null, i.alertLoginErrorAndGoHome("登录码为空,请重新尝试"),
!1;
var r = o.userInfo, s = a.globalData.setting.versionCode;
console.log("thirdLogin");
console.log(e);
a.request.get("/api/weshop/users/openidandkey", {
data: {
js_code: e,
store_id: a.globalData.setting.stoid,
},
success: function (e) {
console.log("openidandkey");
console.log(e);
if (e.data.code == 0) {
if (e.data.data.user_id != undefined) {
app.globalData.user_id = e.data.data.user_id;
app.globalData.openid = e.data.data.weapp_openid;
app.globalData.sessionKey = e.data.data.sessionKey;
wx.setStorageSync("userinfo",e.data.data);
wx.setStorageSync("isAuth", !0), a.globalData.userInfo = e.data.data, a.globalData.userInfo.head_pic = t.getFullUrl(a.globalData.userInfo.head_pic);
"function" == typeof n && n(a.globalData.userInfo, a.globalData.wechatUser);
}else{
app.globalData.sessionKey = e.data.data.sessionKey;
app.globalData.openid = e.data.data.openid;
app.globalData.getu = r;
"function" == typeof n && n();
return false;
}
} else {
return app.showWarning("登入失败!"+e.data.msg);
}
}
});
},
login: function(e, o, n) {
var a = this.app(),app=a, i = this;
if (void 0 === e || "" == e) return a.globalData.wechatUser = null, i.alertLoginErrorAndGoHome("登录码为空,请重新尝试"),
!1;
var r = o.userInfo, s = a.globalData.setting.versionCode;
console.log("thirdLogin");
console.log(e);
a.request.get("/api/weshop/users/openidandkey", {
data: {
js_code: e,
store_id: a.globalData.setting.stoid,
},
success: function (e) {
console.log("openidandkey");
console.log(e);
if(e.data.code==0){
if (e.data.data.user_id==undefined){
app.globalData.sessionKey = e.data.data.sessionKey;
app.globalData.openid = e.data.data.openid;
app.globalData.getu = r;
wx.navigateTo({
url: '/pages/getphone/getphone',
})
}else{
app.globalData.user_id = e.data.data.user_id;
app.globalData.openid = e.data.data.weapp_openid;
app.globalData.sessionKey = e.data.data.sessionKey;
wx.setStorageSync("userinfo",e.data.data);
wx.setStorageSync("isAuth", !0), a.globalData.userInfo = e.data.data, a.globalData.userInfo.head_pic = t.getFullUrl(a.globalData.userInfo.head_pic);
"function" == typeof n && n(a.globalData.userInfo, a.globalData.wechatUser);
}
}else{
return app.showWarning("登入失败!"+e.data.msg);
}
}
});
},
//-------获取用户信息----
get_u(t){
var app_d = this.app().globalData, oo = app_d.setting;
var ab = this.app().request;
ab.get("/api/weshop/users/get/" + oo.stoid + "/" + app_d.user_id, {
success: function (e) {
app_d.userInfo = e.data.data;
"function" == typeof t && t(e.data.data);
},
});
},
//-------微信登录--------
wxLogin: function(t) {
var app_d = this.app().globalData, oo = app_d.setting;
if (app_d.user_id != "" && app_d.user_id != null ){
var ab = this.app().request;
ab.get("/api/weshop/users/get/" + oo.stoid+"/"+app_d.user_id, {
success: function (e) {
app_d.userInfo = e.data.data;
"function" == typeof t && t(e.data.data);
},
});
}else{
var e = this;
wx.login({
success: function(o) {
console.log('wxLogin');
console.log(o);
o.code ? e.doGetWxUser(o.code, t) : wx.showModal({
title: "获取用户登录态失败",
content: o.errMsg,
showCancel: !1,
complete: function() {
e.goHome();
}
});
},
fail: function(t) {
console.log(t);
}
});
}
},
wxLogin_fir: function (t) {
var app_d = this.app().globalData, oo = app_d.setting;
if (app_d.user_id != "" && app_d.user_id != null) {
var ab = this.app().request;
ab.get("/api/weshop/users/get/" + oo.stoid + "/" + app_d.user_id, {
success: function (e) {
app_d.userInfo = e.data.data;
"function" == typeof t && t(e.data.data);
},
});
} else {
var e = this;
wx.login({
success: function (o) {
console.log('wxLogin');
console.log(o);
o.code ? e.doGetWxUser(o.code, t,1) : wx.showModal({
title: "获取用户登录态失败",
content: o.errMsg,
showCancel: !1,
complete: function () {
e.goHome();
}
});
},
fail: function (t) {
console.log(t);
}
});
}
},
doGetWxUser: function(t, e,ind) {
var o = this, n = o.app();
n.globalData.code = t;
try {
//获取本地存储
var a = wx.getStorageSync("wx_user_info");
var userInfo = a.userInfo;
if (userInfo==undefined){ //判断本地是数据是否正确
wx.getUserInfo({
success: function (res)
{
console.log('doGetWxUser');
console.log(res);
console.log(ind);
//组装用户
var ar = {'nickName':res.userInfo.nickName,
'avatarUrl':res.userInfo.avatarUrl,
'gender': res.userInfo.gender};
//----会员记录到全局------
n.globalData.getu = ar;
var uu = {'userInfo': ar};
a=uu;
if(ind==1){
a && void 0 != a ? (n.globalData.wechatUser = a, o.login_fir(t, a, e)) : o.goGetUserInfo();
}else{
a && void 0 != a ? (n.globalData.wechatUser = a, o.login(t, a, e)) : o.goGetUserInfo();
}
},
fail: function () {
//---获取用户信息失败后。请跳转授权页面--
wx.showModal({
title: '警告',
content: '尚未进行授权,请点击确定跳转到授权页面进行授权。',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定')
wx.navigateTo({
url: '/pages/togoin/togoin',
})
}
}
})
}
})
}else{
a && void 0 != a ? (n.globalData.wechatUser = a, o.login(t, a, e)) : o.goGetUserInfo();
}
} catch (t) {
o.goGetUserInfo();
}
},
failGetWxUser: function(t, e) {
var o = this;
wx.showModal({
title: "请先授权登录哦",
success: function(t) {
t.confirm ? o.goGetUserInfo() : t.cancel && o.alertNoAuthAndGoHome();
},
fail: function(t) {
o.goHome();
}
});
},
//一些提示的方法
alertNoAuthAndGoHome: function() {
var t = this;
this.app().showWarning("你尚未授权登录", function() {
t.goHome();
}, null, !0);
},
alertLoginErrorAndGoHome: function(t) {
"string" == typeof t && "" != t || (t = "登录时发生错误");
var e = this;
this.app().showWarning(t, function() {
e.goHome();
}, null, !0);
},
goHome: function() {
wx.navigateTo({
url: "/pages/index/index/index"
});
},
goGetUserInfo: function() {
wx.navigateTo({
url: "/pages/user/get_user_info/get_user_info"
});
}
};