// pages/index/bindInfo/index.js import { setSessionGuid } from "../../assets/js/userAction" const app = getApp(); Page({ /** * 页面的初始数据 */ data: { avatarUrl: "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0", nickName: "", redirectPage: "" }, 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: "WeChatAppCustom", icon: this.data.avatarUrl, encryptedData: infoRes.encryptedData, iv: infoRes.iv, name: this.data.nickName, }).then(res => { if (res && res.status == "Ok") { // 储存token wx.setStorageSync(app.config.tokenKey, res.token); // 获取用户信息 this.getUserInfo() // 记录登录统计 setSessionGuid() } else { wx.showToast({ icon: "error", title: res.message, }); } }) }, }) } }) }, // 获取登录用户身份 getUserInfo() { app.MG.identity.getCurrentAppUser().then(res => { // 用户信息优先级:教师认证 > 微信 > 学生(注册时默认) if (res) { let defaultUser = {}; 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 } wx.setStorageSync(app.config.userInfoKey, JSON.stringify(defaultUser)); } wx.navigateTo({ url: this.data.redirectPage ? this.data.redirectPage : '/pages/home/home' }) }); }, /** * 生命周期函数--监听页面加载 */ onLoad(options) { console.log(options, "options"); console.log(decodeURIComponent(options.page)); this.setData({ redirectPage: decodeURIComponent(options.page) }) }, /** * 生命周期函数--监听页面初次渲染完成 */ onReady() { }, /** * 生命周期函数--监听页面显示 */ onShow() { }, /** * 生命周期函数--监听页面隐藏 */ onHide() { }, /** * 生命周期函数--监听页面卸载 */ onUnload() { }, /** * 页面相关事件处理函数--监听用户下拉动作 */ onPullDownRefresh() { }, /** * 页面上拉触底事件的处理函数 */ onReachBottom() { }, })