/* * 本时生活 */ const app = getApp() Page({ /** * 页面的初始数据 */ data: { wechatUserId: "", loginCode : "", userInfo : "", isLogin : false, iv : '', enData : '', loginTel : [] //用户手机号 }, /** * 生命周期函数--监听页面加载 */ onLoad (options) { console.log(options) // 登录方式-活动 this.setData({ way : options.way }) if(options.type == "mobiles") { wx.$api.user.mobiles().then(res=>{ this.setData({ loginTel : res.data }) }) }else { // 获取用户手机列表 const logintel = wx.getStorageSync("users") this.setData({ loginTel : logintel }) } }, /** * 生命周期函数--页面显示 */ onShow() { wx.login({ success: res => { this.setData({ loginCode: res.code }) } }) }, /** * 绑定手机号码 */ mobile(e){ if(e.detail.errMsg == "getPhoneNumber:ok"){ this.setData({ isLogin : true, iv : e.detail.iv, enData : e.detail.encryptedData }) this.binMobel() }else{ wx.showToast({ title: '拒绝了手机号码授权', icon : 'none' }) } }, /** * 绑定手机号码 */ binMobel(){ let code = this.data.loginCode, iv = this.data.iv, mobile = this.data.enData wx.$api.enroll.bindmobile(code, iv, mobile).then(res=>{ this.setData({ isLogin: false }) getApp().globalData.token = res.data.token // 更新全局存储器用户状态 getApp().globalData.isUser = true // 写入缓存 wx.setStorage({ key : 'token', data : res.data.token }) if(this.data.way == "activity") { // 回到活动首页 wx.reLaunch({ url: '/pages/activityInfo/activityInfo' }) } else { // 回到个人中心 wx.switchTab({ url: '/pages/user/user' }) } }) }, /** * 点击账号登录 */ loginGo(e) { let username = e.currentTarget.dataset.name, wechatUser_id = app.globalData.wechatUser wx.$api.enroll.tel(wechatUser_id, username).then(res=>{ app.globalData.token = res.data.token app.globalData.isUser = true // 写入缓存 wx.setStorage({ key : 'token', data : res.data.token }) if(this.data.way == "activity") { // 回到活动首页 wx.reLaunch({ url: '/pages/activityInfo/activityInfo' }) } else { // 回到个人中心 wx.switchTab({ url: '/pages/user/user' }) } }) } })