filter.js 6.34 KB
const restapi = require('restapi.js')
const app = getApp();
const whitePage = ['pages/tab/index/index'];
/**
 * 身份验证过滤器
 * @param pageObj 页面对象
 */
function identityFilter(pageObj) {  
  var enterTime = new Date();
  if(pageObj.onLoad){
    let _onLoad = pageObj.onLoad;
    pageObj.onLoad = function(op){
      console.log(op);
      let currentInstance = getPageInstance();
      if(op.scene){
        var scene = decodeURIComponent(op.scene);
        var qrCodeScene = {
          id: scene.split('=')[1]
        }
        console.log(qrCodeScene);
        restapi.qrCodeScene(qrCodeScene, function(res){
          if(res.returnCode =='0'){
            var scenestr = res.data;
            if (scenestr){
              scenestr.split('&').forEach((item) => {
                const key = item.split("=")[0]
                const val = item.split("=")[1];
                op[key] = val;
              })
              console.log(op);
              console.log('filter op', op);
            }
          }
          _onLoad.call(currentInstance, op);
        })
      }else{
        _onLoad.call(currentInstance, op);
      }
    }
  }
  if (pageObj.onShow) {
    let _onShow = pageObj.onShow;
    pageObj.onShow = function () {
      let currentInstance = getPageInstance();
      var flag = true;
      for (var i in whitePage){
        if (currentInstance.route == whitePage[i]){
          flag = false;
          break;
        }
      }
      if (!flag){
        _onShow.call(currentInstance);
      }
       
      identityCheck(() => {
        // 关闭授权窗

        currentInstance.setData({
          authToast: false
        });
        currentInstance.onLogin();
        if(flag){
          _onShow.call(currentInstance);
        }
      }, authType => {
        // 弹出授权窗口
        if(!(app.globalData.authUserInfo && app.globalData.authPhone)) {
          currentInstance.setData({
            authToast: true,
            authType: authType
          });
        }
        // 完成授权后回调
        app.userInfoReadyCallback = userInfo => {
          currentInstance.onLogin();
          if(flag){
            _onShow.call(currentInstance);
          }
        }
      });
    }
  }
  if (pageObj.onHide) {
    let _onHide = pageObj.onHide;
    pageObj.onHide = function () {
      let currentInstance = getPageInstance();
      pageVisitCollect(currentInstance.route, enterTime);
      _onHide.call(currentInstance);
    }
    
  }
  if (pageObj.onUnload) {
    let _onUnload = pageObj.onUnload;
    pageObj.onUnload = function () {
      let currentInstance = getPageInstance();
      pageVisitCollect(currentInstance.route, enterTime);
      _onUnload.call(currentInstance);
    }
  }
  return pageObj;
}

/**
 * 身份验证
 */
function identityCheck(success, fail){
  
  // 获取用户信息
  wx.login({
    success: response => {
      app.globalData.code = response.code;
      userAuth(success, fail);
    }
  })
}

/**
 * 用户授权
 */
function userAuth(success, fail){

  let currentInstance = getPageInstance();
  console.log(app.globalData)
  // 已授权微信用户信息
  if (app.globalData.authUserInfo) {
    // 已授权手机号
    if (app.globalData.authPhone) {
      // 停掉检测任务
      clearInterval(currentInstance.data.authInterval);
      // 授权成功
      success();
    } else {
      // 开启授权检测任务
      checkAuthInterval();
      // 未授权手机号
      fail(false);
    }
    return;
  } else {
    fail(true);
  }
  // 开启新的授权检测任务
  checkAuthInterval();

  restapi.jscodeLogin(app.globalData.code, app.globalData.appId, function (response) {
    console.log(response);
    switch (response.returnCode) {
      case '0': // 获取微信用户信息成功
        app.globalData.userInfo = response.data;
        if(app.globalData.userInfo.isLogin == 'Y'){
          // 退出登录,重新授权用户信息(单独授权手机号,由于code失效导致解密异常,后期处理)
          app.globalData.authUserInfo = true;
          if (response.data.accountId) { // 已获取手机号
            app.globalData.authPhone = true;
            // 登录成功回调
            app.globalData.loginSuccessCallback(response.data);
            success();
          } else { // 未获取手机号,授权获取
            fail(false);
          }
        }else{
          // 已退出登录,授权获取用户信息
          fail(true);
        }
        break;
      case '-1':
        // 获取微信用户信息为空,授权获取
        if (response.errId == '200205') {
          fail(true);
        }
        break;
      default:
        wx.showToast({
          title: response.message
        })
    }
  });
}

/**
 * 轮询检测授权
 */
function checkAuthInterval(){
  let currentInstance = getPageInstance();
  // 停掉原有检测任务
  clearInterval(currentInstance.data.authInterval);

  var authInterval = setInterval(function(){
    // 已授权微信用户信息
    if (app.globalData.authUserInfo) {
      // 已授权手机号
      if (app.globalData.authPhone) {
        // 关闭授权窗口
        currentInstance.setData({
          authToast: false
        });
        console.log("clear interval:" + currentInstance.data.authInterval);
        clearInterval(currentInstance.data.authInterval);
        // 登录成功回调
        app.globalData.loginSuccessCallback(app.globalData.userInfo);
      } else {
        // 切换到手机号授权窗口
        currentInstance.setData({
          authType: false
        });
      }
    }
  }, 1000);
  // 保存任务ID
  currentInstance.setData({
    authInterval: authInterval
  });
}

function getPageInstance() {
  var pages = getCurrentPages();
  return pages[pages.length - 1];
}

/**
 * 页面访问收集
 * @param {页面路径} page 
 * @param {进入时间} enterTime 
 */
function pageVisitCollect(page, enterTime){
  var data = {
    path: page,
    enterTime: enterTime,
    leaveTime: new Date,
    merchantId: app.globalData.extConfig.merchantId,
    appId: app.globalData.appId
  }
  restapi.pageVisitCollect(JSON.stringify(data), function(res){});
}

exports.identityFilter = identityFilter;