// components/getPhoneNumer/getPhoneNumber.js const app = getApp() Component({ /** * 组件的属性列表 */ properties: { appid:{ type: String, value:'' }, secret: { type: String, value:'' } }, // 可以定义外部样式类 影响组件的样式 externalClasses: ['class'], options: { // 配置样式是否可以被父组件影响 详细见文档 styleIsolation: 'apply-shared' }, /** * 组件的初始数据 */ data: { // session_key session_key:'', // 用户openid openid:'' }, /** * 组件的方法列表 */ methods: { getPhoneNumber(e) { console.log(e); const that = this wx.checkSession({ success: (res) => { console.log(res); if(e.detail.errMsg == 'getPhoneNumber:fail user deny'){ console.log("用户拒绝了授权"); return }else{ console.log("用户同意了授权"); let key = that.data.session_key let phone = e.detail.encryptedData; let iv = e.detail.iv; const RdWXBizDataCrypt = require('./utils/RdWXBizDataCrypt'); const pc = new RdWXBizDataCrypt(that.data.appid, key); const data = pc.decryptData(phone, iv); console.log(data.phoneNumber); //当前手机号码 that.triggerEvent("myevent",data) } }, fail:(err) => { console.log("登录状态失效,请重新登录",err); } }) }, }, /** * 组件生命周期 */ lifetimes: { attached(){ const that = this wx.login({ timeout: 2000, success(res){ wx.request({ url: `https://api.weixin.qq.com/sns/jscode2session?appid=${that.data.appid}&secret=${that.data.secret}&js_code=${res.code}&grant_type=authorization_code`, success(result){ that.setData({ session_key:result.data.session_key, openid:result.data.openid }) } }) } }) } } })