// index.js // 获取应用实例 const app = getApp() Page({ data: { ischecked: true, bindInfo: false, avatarModal: false, phoneModal: false, phoneModal1: false, showBg: false, step: 0,// 头像授权1, 手机授权2 nickNamebind: false, phonebind: false, userInfo: null, userid:'', token: '' }, onLoad() { this.loginGettoken() }, backhome(){ wx.navigateTo({ url: '/pages/index/index' }) }, checkFun(){ this.setData({ ischecked: !this.data.ischecked }) }, // 协议 toxieyi(){ }, shouquan(){ if(this.data.step==1){ this.setData({ avatarModal: true, showBg: true }) } if(this.data.step==2){ this.setData({ phoneModal: true, showBg: true }) } }, closeModal(){ this.setData({ showBg: false }) }, leave(){ wx.navigateTo({ url: '../out/index?index=1', }) }, //登录-获取token loginGettoken(){ let that = this wx.login({ success (res) { if (res.code) { //发起网络请求 wx.request({ url:'https://cktest.2weisou.com/meta/auth/mobile/token/social?grant_type=mobil&mobile=MINI_C@'+res.code+'@'+'', method: "POST", data: { }, header: { Authorization: "Basic YXBwbGV0OmFwcGxldA==", }, success(res1) { if (res1.statusCode == 200) { let userInfo = res1.data.user_info that.setData({ token: res1.data.access_token, userid: userInfo.id, userInfo: userInfo }) if(!!userInfo.userWxName&&!!userInfo.phone){ that.leave() return; } // console.log(userInfo) if(!userInfo.userWxName||!userInfo.phone){ that.setData({ showBg: true }) }else{ that.setData({ showBg: false }) } that.setData({ avatarModal: !!userInfo.userWxName?false:true, phoneModal1: !!userInfo.phone?false:true, step: 1 }) if(!that.data.avatarModal){ that.setData({ step: 2, phoneModal: that.data.phoneModal1 }) } } } }) } else { console.log('登录失败!' + res.errMsg) } } }) }, // 绑定用户头像信息 bindNickname(obj) { let that = this let params = { id: that.data.userid, userWxHeadImgUrl: obj.avatarUrl, userWxName: obj.nickName, userVxGender: obj.gender } wx.request({ url: 'https://cktest.2weisou.com/meta/ar/user/bindNickname', method: "POST", data: params, header: { Authorization: 'bearer '+ that.data.token }, success(res) { if (res.statusCode == 200) { that.setData({ avatarModal: false, step: 2 }) if(that.data.phoneModal1){ that.setData({ phoneModal: true }) } } } }) }, getUserInfo(){ // if(!this.data.ischecked){ // wx.showToast({ // title: '请勾选协议', // icon: 'none', // duration: 2000 // }) // return; // } let that = this wx.getUserProfile({ desc: '展示用户信息', // 声明获取用户个人信息后的用途,后续会展示在弹窗中,请谨慎填写 success: (res) => { // console.log(res) that.bindNickname(res.userInfo) } }) }, // 授权手机号 getPhoneNumber(e) { console.log(e); let that = this if (e.detail.errMsg == "getPhoneNumber:ok") { const { iv, encryptedData } = e.detail; wx.request({ url: 'https://cktest.2weisou.com/meta/ar/user/bindMini', method: "POST", data: { encrypted: encryptedData, iv }, header: { Authorization: 'bearer '+ that.data.token }, success(res) { console.log(res) if (res.data.code == 0) { // console.log(res) that.setData({ phoneModal: false, showBg: false, step: 0 }) that.leave() } } }) } else { wx.showToast({ icon: "none", title: "请授权手机号" }) } } })