// pages/index/bindInfo/index.js const app = getApp(); Page({ /** * 页面的初始数据 */ data: { avatarUrl: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0", nickName: "", wxCode: null, }, onChooseAvatar(res) { wx.compressImage({ src: res.detail.avatarUrl, // 图片路径 quality: 50, // 压缩质量 compressedWidth: 80, compressedHeight: 80, success: (compressRes) => { wx.getFileSystemManager().readFile({ filePath: compressRes.tempFilePath, //选择图片返回的相对路径 encoding: 'base64', //编码格式 success: base64Res => { //成功的回调 this.setData({ avatarUrl: 'data:image/png;base64,' + base64Res.data, }); } }) } }) }, formSubmit() { if (this.data.nickName == "") { return wx.showToast({ icon: "error", title: "昵称不能为空!", }); } wx.getUserInfo({ success: (infoRes) => { wx.login({ success: (res) => { app.MG.identity.loginByWeChatAppCode({ code: res.code, appRefCode: app.config.appRefCode, platform: "weChatApp", icon: this.data.avatarUrl, encryptedData: infoRes.encryptedData, iv: infoRes.iv, name: this.data.nickName, }).then(res => { if (res && res.status == "Ok") { wx.setStorageSync(app.config.tokenKey, res.token); this.getUserInfo() } }) }, }) } }) }, // 获取登录用户身份 getUserInfo() { app.MG.identity.getCurrentAppUser().then(res => { // console.log(res, "userInfo"); // 用户信息优先级:教师认证 > 微信 > 学生(注册时默认) if (res) { let defaultUser = {}; // let secretData = res.secretList.find(i => i.type == 'LoginNameAndPassword') let WeChatInfo = res.infoList.find((item) => item.type === "WeChat"); let phoneNumber = res.secretList.find(i => i.type == 'MobilePhone') if (WeChatInfo) { defaultUser = { nickName: WeChatInfo.name, avatarUrl: WeChatInfo.icon, weChatId: WeChatInfo.id } } if (phoneNumber) { defaultUser.phoneNumber = phoneNumber.credential } this.setData({ userInfo: defaultUser, loading: false }) wx.setStorageSync(app.config.userInfoKey, JSON.stringify(this.data.userInfo)); } wx.switchTab({ url: '/pages/index/index' }) }); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { console.log(options, "options"); this.setData({ wxCode: options.code }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, })