authorize.js 1.73 KB
let app = getApp();
let config = require('../../config');

Page({
  data: {
    authorized: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onShareAppMessage() {},
  onReady() {
    wx.showLoading();
    this.autoAuth();
  },
  onLoad(options) {},
  onShow() {},
  bindGetUserInfo(e) {
    wx.showLoading();
    this.getUserInfo(e.detail);
  },
  getUserInfo(e) {
    let _this = this;
    app.globalData.userInfo = e.userInfo;
    app.post({
      url: app.api.register,
      sid: false,
      data: {
        encryptedData: e.encryptedData,
        iv: e.iv,
        code: app.globalData.wxcode,
        tlMemberCode: app.globalData.tlMemberCode
      }
    }).then((res2) => {
      _this.autoAuth();
    }).catch((err) => {})
  },
  // 刷新首页数据
  refreshIndexData() {
    return new Promise((resolve, reject) => {
      app.post({
        url: app.api.index,
        data: {}
      }).then((res) => {
        app.globalData.indexInfo = res;
        resolve(res)
      }).catch((err) => {
        console.log("index err:", err);
      });
    });
  },
  // 自动授权
  autoAuth() {
    let _this = this;
    this.refreshIndexData().then((result) => {
      if (result.isNeedAuth == 1) {
        // 未授权
        wx.login({
          success: function (res) {
            app.store.setItem("wxcode", res.code);
          }
        });
        wx.hideLoading();
        _this.setData({
          authorized: false
        })
      } else {
        // 已经授权
        // setTimeout(() => {
        _this.toIndex();
        wx.hideLoading();
        // }, 2000);
      }
    });
  },
  // 跳转到首页
  toIndex() {
    app.router.push({
      path: "index",
      openType: "redirect"
    })
    wx.hideLoading();
  }

})