QYF-GitLab1
2024-12-20 69b3b282b6398b22d65601a4d9dc94bdde557f31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// 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() {
    let that = this;
    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));
      }
      if (that.data.redirectPage == "" || that.data.redirectPage == "/pages/home/home" || that.data.redirectPage == "/pages/bookServices/assort/index" || that.data.redirectPage == "/pages/study/index" || that.data.redirectPage == "/pages/cart/index" || that.data.redirectPage == "/pages/personalCenter/index") {
        wx.switchTab({
          url: that.data.redirectPage != "" ? that.data.redirectPage : '/pages/home/home'
        })
      } else {
        wx.navigateTo({
          url: that.data.redirectPage
        })
      }
    });
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options, "options");
    console.log(decodeURIComponent(options.page));
    this.setData({
      redirectPage: decodeURIComponent(options.page)
    })
  },
 
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
 
  },
 
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
 
  },
 
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
 
  },
 
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
 
  },
 
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
 
  },
 
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
 
  },
})