authorize.js 1.43 KB
let app = getApp();

Page({
  data: {
    barHeight: app.globalData.barHeight,
    navbarData: {
      showCapsule: 0, //是否显示左上角图标   1表示显示    0表示不显示
      title: '微信授权' //导航栏 中间的标题
    },
    loginData: null,
    authorized: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
  },
  onShareAppMessage() {},
  onLoad(options) {},
  onReady() {
    // app.store.clear("sessionId");
    let _this = this;
    // wx.login({
    //   success: function (res) {
    //     _this.data.loginData = res;
    //   }
    // });
  },
  bindGetUserInfo(e) {
    wx.showLoading();
    this.getUserInfo(e.detail);
  },
  getUserInfo(e) {
    let _this = this;
    app.globalData.userInfo = e.userInfo;
    // console.log("loginData:",_this.data.loginData);
    app.post({
      url: app.api.register,
      sid: false,
      data: {
        encryptedData: e.encryptedData,
        iv: e.iv,
        code: _this.data.loginData.code,
      }
    }).then((res2) => {
      app.store.setItem('sessionId', res2.sessionId);
      _this.setData({
        authorized: true
      })
      _this.toIndex();
    }).catch((err) => {
      console.log("@authorize || err:", err);
    })
  },
  toIndex() {
    console.log("@authorize || toIndex ");
    setTimeout(() => {
      app.router.push({
        path: "index",
        query: {},
        openType: "redirect"
      })
      wx.hideLoading();
    }, 2000);
  }
})