#先说下获取手机号的步骤,首先要调用wx.login拿到code,把code发送到我们的服务器(开发者服务器)上,后台通过appid、appsecret(小程序后台那里生成的)、code向微信接口服务拿到session_key、openid等信息,到这里其实我们已经可以直接去获取手机号了,自定义登录态,如果项目需要可以做
https://developers.weixin.qq.com/miniprogram/dev/framework/open-ability/login.html
#获取手机号和用户信息的代码如下面的permissions.js
#checkSession
主要方法是checkSession,这个呢是封装了下wx.checkSession,首先是看走success区间呢还是fail区间,如果是success区间,先从本地拿code,如果这个code存在且未被使用过就直接返回这个code,否则重新wx.login获取code(fail区间一样的逻辑)
#getSessionKeyByCode
这个呢是调用后台的接口,通过code换取seesion_key的,拿到数据后,重新set wx_code到本地中,并添加上use为true,代表这个code已经被使用了
#getPhoneNumber
这个是获取手机号的方法,获取手机号需使用button组件,并添加上open-type="getPhoneNumber"属性,并绑定上bindgetphonenumber="getPhoneNumber",点击按钮的时候会返回加密的数据encryptedData、iv,加密需要我们传sessionKey(如果后台有存那不必传),这个sessionKey我们在上一步中已经存在本地了,所以直接从本地拿wx_code里的session_key
#获取用户信息,相对获取手机号简单些,直接调即可,可以看getUserInfo方法
const api = require('./api.js');
const util = require('./util.js');function checkSession() {return new Promise((resolve, reject) => {wx.checkSession({success() {console.log('success:有效的');//session_key 未过期,并且在本生命周期一直有效let res = wx.getStorageSync('wx_code');// resolve(res);console.log(res);if (!res || res.use) {console.log('无效的code');wx.login({success: res => {// 发送 res.code 到后台换取 openId, sessionKey, unionIdwx.setStorageSync('wx_code', res);resolve(res);},fail: res => {reject(res);}})} else {console.log('有效的code');resolve(res);}},fail() {console.log('fail:失败了');// session_key 已经失效,需要重新执行登录流程// 登录wx.login({success: res => {// 发送 res.code 到后台换取 openId, sessionKey, unionIdwx.setStorageSync('wx_code', res);resolve(res);},fail: res => {reject(res);}})}})})
}function checkSession_blank() {return new Promise((resolve, reject) => {wx.checkSession({success() {console.log('success:有效的');//session_key 未过期,并且在本生命周期一直有效let res = wx.getStorageSync('wx_code');// resolve(res);console.log(res);if (!res || res.use) {console.log('无效的code');wx.login({success: res => {// 发送 res.code 到后台换取 openId, sessionKey, unionIdwx.setStorageSync('wx_code', res);resolve(res);},fail: res => {reject(res);}})} else {console.log('有效的code');resolve(res);}},fail() {console.log('fail:失败了');// session_key 已经失效,需要重新执行登录流程// 登录wx.login({success: res => {// 发送 res.code 到后台换取 openId, sessionKey, unionIdwx.setStorageSync('wx_code', res);resolve(res);},fail: res => {reject(res);}})}})})
}function getSessionKeyByCode(res) {return util.wxRequestGet({url: api.getSessionKeyByCode.url,data: {code: res.code}}).then(res => {let wxCode = wx.getStorageSync('wx_code');wx.setStorageSync('wx_code', {...res.data.data,...wxCode,use: true})})
}function getPhoneNumber(e) {console.log(e);// return checkSession().then(res => {let detail = e.detail;let wxCode = wx.getStorageSync('wx_code');// console.log('wx_code: ', res);return new Promise((resolve, reject) => {util.wxRequest({url: api.getUserDetails.url,data: {encryptedData: detail.encryptedData,iv: detail.iv,jsCode: wxCode.code,sessionKey: wxCode.session_key},method: 'POST',success: function (res) {console.log(res);// 把用户信息和手机号信息等set在一起if (res.data.success) {let userInfo = wx.getStorageSync('wx_userinfo');wx.setStorageSync('wx_userinfo', {...userInfo,...res.data.data,openId: wxCode.openid});// wx.setStorageSync('wx_code', {// ...wxCode,// use: true// });return resolve(res);} else {wx.showToast({icon: 'none',title: res.data.msg})}reject(res);},fail: function (res) {reject(res);}})})// })
}function getUserInfo() {return new Promise((resolve, reject) => {const wxUserInfo = wx.getStorageSync('wx_userinfo') || {}if (wxUserInfo.nickName) {resolve(wxUserInfo)} else {wx.authorize({scope: 'userInfo',success() {wx.getUserInfo({success: (result) => {wx.setStorageSync({data: { ...wxUserInfo, ...result.userInfo },key: 'wx_userinfo',})resolve(result.userInfo)},// fail: (res) => reject(res),complete: () => resolve({})})}})}})
}function setUserInfo(data) {const userInfo = wx.getStorageSync('wx_userinfo');wx.setStorageSync('wx_userinfo', { ...userInfo, ...data })
}module.exports = {checkSession: checkSession,getPhoneNumber: getPhoneNumber,getUserInfo: getUserInfo,setUserInfo: setUserInfo,getSessionKeyByCode: getSessionKeyByCode
}