user.js
2.95 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
import http from '../request/http'
const { appId } = wx.getAccountInfoSync().miniProgram
let loginCount = null,
loginCache = null,
loginTimer = null
class UsersApi {
/**调用后台接口获取用户信息 */
async login() {
const appEnter = wx.getEnterOptionsSync()
try {
if (appEnter.scene === 1154 && appEnter.mode === 'singlePage') throw 'login:fail rejected due to no permission currently'
if (!loginCount) {
loginCache = null
loginCount = Date.now()
const {
code
} = await this.getWxCode(),
data = await http.fetch('scrm.api.sso.weapp.jscode.getOpenIdAndLogin', {
appId,
code,
delSessionId: true
})
loginCache = data
loginCount = null
return Promise.resolve(data)
} else {
return new Promise((resolve) => {
loginTimer = setInterval(() => {
if (loginCache) {
clearInterval(loginTimer)
loginCount = null
resolve(loginCache)
}
}, 1e3)
})
}
} catch (error) {
if (loginTimer) clearInterval(loginTimer)
loginCount = null
return Promise.reject(error)
}
}
/**调用接口获取登录凭证(code)。通过凭证进而换取用户登录态信息,包括用户在当前小程序的唯一标识(openid)、微信开放平台帐号下的唯一标识(unionid,若当前小程序已绑定到微信开放平台帐号)及本次登录的会话密钥(session_key)等。用户数据的加解密通讯需要依赖会话密钥完成。 */
getWxCode() {
return new Promise((resolve, reject) => {
wx.login({
success: res => resolve(res),
fail: err => reject(err)
})
})
}
/**授权手机号 */
async authUserPhone(detail, sessionId) {
if (!sessionId) {
wx.p.wxApi.showToast('缺少sessionId')
Promise.reject({
errMsg: '缺少sessionId'
})
}
if (detail.errMsg && detail.errMsg == 'getPhoneNumber:fail user deny') Promise.reject()
delete detail.errMsg
if (detail.iv && detail.encryptedData) {
wx.p.wxApi.showLoading()
detail.sessionId = sessionId
return http.fetch('scrm.api.sso.weapp.auth.phone', detail)
} else Promise.reject(detail.errMsg)
}
/**退出登录 */
logout() {
return http.fetch('scrm.api.sso.user.logout')
}
/** 手机号获取短信验证码 */
getSmsCode(mobile) {
return http.fetch('scrm.api.sso.wechat.applets.sms.get.code', {
mobile
})
}
/** 手机号短信验证码登录 */
phoneSmsLogin(data = {}) {
return http.fetch('scrm.api.sso.wechat.applets.sms.login', {
...data,
source: 'MINIAPP',
platformEntrance: 'STORE_ASSISTANT'
})
}
/*
*C端手机号短信登录
*/
phoneLogin(data) {
return http.fetch('scrm.api.sso.weapp.auth.phone.mini', {
source: 'MINIAPP',
...data
})
}
}
export default new UsersApi()