Skip to content

Instantly share code, notes, and snippets.

@Caringor
Created June 26, 2018 10:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Caringor/7fdf75db64819bdcef9dab6360e7dada to your computer and use it in GitHub Desktop.
Save Caringor/7fdf75db64819bdcef9dab6360e7dada to your computer and use it in GitHub Desktop.
// 点击授权按钮后触发的事件
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