login.js
3.73 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
import { LoginService } from './loginService.js'
const login = new LoginService().yohoApi();
export const loginAction = (callBack) => {
return wx.login({
success(res) {
if (res.code) {
login.checkLogin(res.code).then((data) => {
// 获取 srd_session
const loginData = data;
handleLoginData(loginData);
callBack(null, data);
}).catch((error) => {
callBack(error);
});
}
},
fail(error) {
callBack(error);
}
})
};
const handleLoginData = (loginData) => {
// 上报接口返回的结果,线上debug用
// 缓存 srd_session
setStorageWithValueForKey('srd_session', loginData.srd_session);
// 缓存 openid
setStorageWithValueForKey('openid', loginData.openid);
// 如果 unionid 不存在(未使用过任何有货微信产品的全新用户),调用getUnionID函数,再次获取unionID
if (loginData.unionid) {
// 对于已经授权过的用户,拿到unionid之后获取一次userinfo更新个人信息
setStorageWithValueForKey('unionid', loginData.unionid);
}
}
/**
* 判断微信用户是否绑定
* @param srd_session
* @param result
* @returns {Promise.}
*/
export const decodeUnionId = (srd_session, result) => {
let res = result.detail;
// 存储 userInfo
if (res && res.userInfo) {
setStorageWithValueForKey('userInfo', res.userInfo);
} else {
return;
}
const nickName = res.userInfo.nickName;
const avatarUrl = res.userInfo.avatarUrl;
if (!srd_session) return;
return login.getUnionID(srd_session, res.encryptedData, res.iv).then((data) => {
let union_id;
// 本地保存 union_id
if (data.union_id) {
setStorageWithValueForKey('union_id', data.union_id);
union_id = data.union_id;
} else {
union_id = wx.getStorageSync('union_id');
}
if (!union_id) {
throw new Error('union_id is null');
}
// 网络上传头像,union_id,昵称
login.sendWeChatUserDataWithUnionId(data.union_id, nickName, avatarUrl).catch(error => {});
return {
union_id: union_id,
userInfo: res.userInfo
};
});
};
/**
* 判断微信用户是否绑定
* @param unionID
* @param userInfo
* @returns {Promise.}
*/
export const wechatUserIsBind = (union_id, userInfo) => {
return login.wechatUserIsBind(union_id, userInfo.nickName).then(data => {
if (!isStringEmpty(data.is_bind) && data.is_bind === "Y") {
const newUserInfo = {
is_bind: data.is_bind,
mobile: data.mobile,
session_key: data.session_key,
uid: data.uid,
ssouid: data.ssouid
};
const assignUserInfo = Object.assign(newUserInfo, userInfo);
setStorageWithValueForKey('userInfo', assignUserInfo);
return Promise.resolve({
code: 10003,
data: userInfo,
message: '微信用户已绑定手机号'
});
} else {
return Promise.resolve({
code: 10004,
message: '微信用户未绑定手机号'
});
}
});
}
const setStorageWithValueForKey = (key, value) => {
const globalData = getGlobalData();
wx.setStorage({
key: key,
data: value,
})
globalData[key] = value;
}
const getStorageWithValueForKey = (key) => {
wx.getStorage({
key: key,
success: function(res) {
},
})
}
const getGlobalData = () => {
const app = getApp();
let globalData;
if (app && app.globalData) {
// 原生小程序的 globalData 获取方式
globalData = app && app.globalData;
} else {
// Taro 下 globalData 获取方式
globalData = app && app.props && app.props.globalData;
}
return globalData;
}
const isStringEmpty = (str) => {
if (str === undefined || str === null || str === '') {
return true
} else {
return false
}
}