app.js 5.67 KB
const restapi = require('/utils/restapi.js')
//app.js
import util from './utils/util.js'
App({
  // 当小程序初始化完成时,会触发 onLaunch(全局只触发一次)
  onLaunch: function(option) {
    /**挂载到wx对象 */
    wx.$util = util
    var self = this;
    // 获取用户登录场景值
    console.log("onLaunch.option", option)
    self.globalData.app = this;
    self.globalData.scene = option.scene;
    self.globalData.appId = wx.getAccountInfoSync().miniProgram.appId;
    /**
     * 获取自定义导航栏信息
     */
    let padding = 6
    let ios = true
    wx.getSystemInfo({
      success(res) {
        console.log(res)
        if (res.platform == "android") {
          padding = 8
          ios = false
        }
        /**兼容全屏手机 */
        if (res.screenHeight - res.windowHeight > 72) {
          self.globalData.isFullSucreen = true
        }
        self.globalData.systemInfo = {
          statusBarPadding: padding,
          ios: ios,
          statusBarHeight: res.statusBarHeight,
          phoneHeight: res.screenHeight - res.statusBarHeight
        }
      }
    })

    // 打开调试
    // wx.setEnableDebug({
    //   enableDebug: true
    // })
  },

  // 当小程序启动,或从后台进入前台显示,会触发
  onShow: function(option) {
    let self = this;
    self.globalData.sceneData = option;
    self.globalData.scene = option.scene;
  },

  onHide: function() {
    let that = this
    that.globalData.clearMarqueeTime = true
  },

  login: function() {
    // 登录
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        var self = this;
        self.globalData.code = res.code;
        restapi.jscodeLogin(res.code, self.globalData.appId, function(response) {
          console.log(response)
          switch (response.returnCode) {
            case '0': // 获取微信用户信息成功
              self.globalData.userInfo = response.data;
              if (response.data.uuid) { // 已获取手机号
                // 登录成功回调
                self.loginSuccessCallback(response.data);
              } else { // 未获取手机号,授权获取
                wx.navigateTo({
                  url: '/pages/login/index/index'
                });
              }
              break;
            case '-1':
              // 获取微信用户信息为空,授权获取
              if (response.errId == '200205') {
                self.getUserInfo();
              }
              break;
            default:
              wx.showToast({
                title: response.message
              })
          }
        });
      }
    })
  },

  getUserInfo: function() {
    var self = this;
    // 获取用户信息
    wx.getSetting({
      success: res => {
        if (res.authSetting['scope.userInfo']) {
          self.userAuth();
        } else {
          wx.navigateTo({
            url: '/pages/index/index'
          });
        }
      },
      fail: res => {
        console.log(res);
      }
    });
  },

  userAuth: function() {
    var self = this;
    // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框
    wx.getUserInfo({
      withCredentials: true,
      lang: 'zh_CN',
      success: res => {
        wx.checkSession({
          success() {
            //session_key 未过期,并且在本生命周期一直有效
            self.authUserInfo(res);
          },
          fail() {
            // session_key 已经失效,需要重新执行登录流程
            wx.login({
              success: response => {
                appInstance.globalData.code = response['code'];
                self.authUserInfo(res);
              }
            })
          }
        })
      },
      fail: res => {
        console.log(res);
      }
    });
  },

  authUserInfo: function(wechatAuthSetting) {
    var self = this;
    wechatAuthSetting['code'] = self.globalData.code;
    wechatAuthSetting['appId'] = self.globalData.appId;
    console.log("wechatAuthSetting", wechatAuthSetting)
    restapi.authUserInfo(wechatAuthSetting, function(response) {
      console.log("authUserInfo", response)
      switch (response.returnCode) {
        case '0':
          self.globalData.userInfo = response.data;
          if (response.data.uuid) { // 已获取手机号
            // 登录成功回调
            self.loginSuccessCallback(response.data);
          } else { // 未获取手机号,授权获取
            wx.navigateTo({
              url: '/pages/login/index/index'
            });
          }
          break;
        default:
          wx.showToast({
            title: response.message
          })
      }
    }, function() {
      console.log("authUserInfo request fail!")
    });
  },
 
  /**
   * 提示框
   */
  showMessage: function(title) {
    wx.showToast({
      title: title,
      icon: 'none',
      duration: 2000
    })
  },

  /**
   * 登录成功回调
   */
  loginSuccessCallback: function(userInfo) {
    console.log(userInfo);
    this.globalData.userInfo = userInfo;
    this.globalData.uuid = userInfo.uuid;
    if (this.userInfoReadyCallback) {
      this.userInfoReadyCallback(userInfo)
    }
  },

  globalData: {
    clearMarqueeTime:false,
    scene: null,
    app: null,
    userInfo: null,
    code: null,
    uuid: null,
    unionId: '',
    authUserInfo: false,
    authPhone: false,
    curUserId:'',
    loginSuccessCallback: function(userInfo) {
      this.app.loginSuccessCallback(userInfo);
    },
    login: function() {
      this.app.login();
    },
    systemInfo: null,
    mapKey: '',// 先去除掉腾讯的,后续需要再改造成高德
    sceneData: null,
    appId: null,
    isFullSucreen:false
  }
})