// pages/login/login.js const app = getApp() const $request = require('../../utils/request.js'); Page({ /** * 页面的初始数据 */ data: { userInfo: null, // 是否获取到了用户信息 hasUserInfo: false, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const userInfo = wx.getStorageSync('userInfo') || null; if (app.globalData.userInfo || userInfo) { this.setData({ userInfo }) } }, // 获取手机号授权 getPhoneNumber(e) { console.log(e); if (!!e.detail.iv && !!e.detail.encryptedData) { const iv = e.detail.iv const encryptedData = e.detail.encryptedData this.appletLogin(iv, encryptedData) } else { wx.showToast({ title: '请允许授权', icon: "none", duration: 3000 }) } }, // 保存用户信息 appletLogin(iv, encryptedData) { let { nickName, gender, country, city, province, avatarUrl } = this.data.userInfo; $request.get('/com/appletLogin.action', { // openid: app.globalData.openid, iv: iv, encryptedData: encryptedData, sessionKey: app.globalData.sessionKey, loginState: "业务交流通", avatarUrl, gender, nickName, country, province, city }).then(res => { // console.log(res,'保存用户信息返回的Res') this.getUserSession() }).catch(error => { console.log(error, 'error appletLogin') }) }, // 获取用户信息弹框 onCheckboxChange() { if (!this.data.hasUserInfo) { if(!this.data.userInfo){ this.getUserProfile() }else{ this.setData({ hasUserInfo: true }) } } else { this.setData({ hasUserInfo: false }) } }, // 获取用户信息 getUserProfile(e) { // 推荐使用wx.getUserProfile获取用户信息,开发者每次通过该接口获取用户个人信息均需用户确认 // 开发者妥善保管用户快速填写的头像昵称,避免重复弹窗 wx.getUserProfile({ desc: '用于完善会员资料', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { this.setData({ userInfo: res.userInfo, hasUserInfo: true }) wx.setStorageSync('userInfo', res.userInfo) }, fail: () => { wx.showToast({ title: '请允许授权', icon: "none", duration: 3000 }) } }) }, // 保存个人信息后拿到新数据 getUserSession() { let that = this wx.showLoading({ title: '加载中', mask: true }) $request.post('/script/getSession.action', {}).then(res => { console.log(res, 'getUserSession---res') wx.hideLoading() app.globalData.customerId = res.data.customer.customerId // wx.navigateTo({ // url: "/pages/mine/mine", //跳转页面的路径,可带参数 ?隔开,不同参数用 & 分隔;相对路径,不需要.wxml后缀 // success: function () {}, //成功后的回调; // fail: function () {}, //失败后的回调; // complete: function () {} //结束后的回调(成功,失败都会执行) // }) }).catch(error => { console.log(error, "获取error") }) }, /** * 用户点击右上角分享 */ onShareAppMessage: function () { } })