Skip to content

Instantly share code, notes, and snippets.

@Caringor Caringor/login.js 
Created Jun 26, 2018

Embed
What would you like to do?
// 点击授权按钮后触发的事件
async bindGetUserInfo(event) {
utils.zsLogin(event.detail.encryptedData, event.detail.iv)
}
/**
* 登录功能
*
* @param {getUserInfo 回调的 encryptedData} encryptedData
* @param {getUserInfo 回调的 iv} getUserInfo
*
* reject 100 缺少 getUserInfo 或 iv 参数
* reject 101 wx.login 接口调用失败
* reject 102 登录到服务端失败
*/
zsLogin(encryptedData='', iv='') {
return new Promise((resolve, reject) => {
if(encryptedData === '' || iv === '') {
reject(100)
return false
}
wx.login({
success: respond => {
this.zsRequest({
url: this.globalData.host.http + 'authorize/login.cgi',
data: {
code: respond.code,
encryptData: encryptedData,
iv: iv
},
method: 'POST'
})
.then(respond => {
if(respond.code !== 1) {
reject(102)
return false
}
// 缓存 OpenID 到本地
wx.setStorage({
key: 'openId',
data: respond.openid
}, () => {
resolve(true)
})
})
.catch(() => {
reject(102)
})
},
fail: () => {
reject(101)
}
})
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.