loading.js 6.54 KB
import {
  setRouteData,
  setLoginInfo,
  loginModal,
  selectAccountLogin
} from '~/lib/index'
import {
  getSessionId,
  removeSystemStorage
} from '~/utils/cache/storageApi'
import USERAPI from '~/api/usersApi/index'
import {
  shopmallApi,
  ssoApi
} from '~/api/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.drectLogin()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
  init() {
    if (this.initFlag) return
    const sessionId = getSessionId(),
      {
        url,
        targetPath = url ? decodeURIComponent(url) : ''
      } = this.options
    if (this.options && this.options.scene) return;
    // if(this.options.missionId&&!this.options.expire){
    //   wx.nextTick(()=>{
    //     wx.$router.reLaunch('/packageStoreAssistant/packageActivity/pages/taskDetail/taskDetail', { missionId: this.options.missionId,loadingPage:true })
    //   })
    //   return
    // }
    this.initFlag = true
    wx.nextTick(() => {
      let path = '/pages/tab/storeAssistant/index',
        obj = {}
      if (targetPath) obj.targetPath = targetPath
      if (sessionId) {
        USERAPI.checkSession().then(({
          code
        }) => {
          if (code == '0') {
            if (targetPath) {
              path = setRouteData(targetPath).url
              obj = setRouteData(targetPath).obj
            }
            wx.$router.reLaunch(path, obj)
          } else wx.$router.reLaunch('/packageStoreAssistant/packageAuth/pages/authentication/login/login', obj)
        })
      } else {
        if (obj && obj.inType == 1) {
          if (targetPath) {
            path = setRouteData(targetPath).url
            obj = setRouteData(targetPath).obj
          }
          wx.$router.reLaunch(path, obj)
          return
        }
        wx.$router.reLaunch('/packageStoreAssistant/packageAuth/pages/authentication/login/login', obj)
      }
    })
  },
  // autoLogin(obj) {
  //   const autoLoginObject = wx.$storage.get('auto_login_object')
  //   if (autoLoginObject) {
  //     const {
  //       failureTime,
  //       userName,
  //       password
  //     } = autoLoginObject
  //     if (Date.now() < failureTime) {
  //       USERAPI.accountLogin({
  //         userName,
  //         password
  //       }).then(async ({
  //         data,
  //         code,
  //         message
  //       }) => {
  //         if (code == '0') {
  //           setLoginInfo(data, data.sessionId)
  //         } else {
  //           try {
  //             const reload = await loginModal({
  //               message: message || '登录失败请重试',
  //               cancelText: '返回'
  //             })
  //             if (reload) this.onShow()
  //           } catch (error) {
  //             wx.$router.reLaunch('login.index', obj)
  //           }
  //         }
  //       })
  //     } else wx.$router.reLaunch('login.index', obj)
  //   } else wx.$router.reLaunch('login.index', obj)
  // },
  // 判断是否是从其他小程序跳转过来
  drectLogin() {
    const params = wx.getEnterOptionsSync()
    console.log(params);
    if (params.referrerInfo.extraData && params.referrerInfo.extraData.inType == 'direct') {
      const C_USERINFO = wx.$util.getUserInfo() || {}
      const {
        phone,
        smscode,
        merchantId
      } = params.referrerInfo.extraData
      Object.assign(this.options, {
        merchantId,
        inType: 'zh'
      })
      if (C_USERINFO.phone) {
        ssoApi.logout().then(res => {
          wx.$authStatus = true
          removeSystemStorage()
          this.freeHandleLogin(phone, smscode)
        })
      } else {
        this.freeHandleLogin(phone, smscode)
      }
    } else if (params.query.scene) {
      this.getLiveActivityId(decodeURIComponent(params.query.scene))
    } else {
      this.init()
    }
  },
  // 免登录操作流程
  freeHandleLogin(phone, smscode) {
    const para = {
      mobile: phone,
      code: smscode
    }
    USERAPI.phoneSmsLogin(para).then(({
      code,
      data,
      message,
      errId
    }) => {
      if (code == '0') {
        this.checkLoginUserInfo(data)
      } else {
        wx.$api.showToast(message || '系统异常')
      }
    }).catch(err => {
      wx.$api.showToast('系统异常')
    })
  },
  checkLoginUserInfo({
    loginUserInfoList,
    sessionId
  }) {
    // if (loginUserInfoList.length > 1) wx.$router.reLaunch('/packageStoreAssistant/packageAuth/pages/authentication/accountItems/accountItems', {
    //   sessionId,
    //   ...this.options
    // })
    if (loginUserInfoList.length > 1) this.toAutoMutiStore({
      sessionId,
      ...this.options
    })
    /**只有一个账号 */
    else selectAccountLogin({
      id: loginUserInfoList[0].uuid,
      sessionId,
      missionId: this.options.missionId || '',
      tab: this.options.tab || '',
      merchantId: this.options.merchantId || '',
    })
  },
  // 获取scene
  getLiveActivityId(str) {
    console.log(str)
    shopmallApi
      .getLiveActivityId({
        id: str.split('=')[1]
      })
      .then(({
        code,
        data
      }) => {
        if (code == 0) {
          let op = {}
          data.scene.split('&').map(item => {
            const [key, val] = item.split('=')
            op[key] = val
          })
          Object.assign(this.options, op)
          this.freeHandleLogin(op.phone, 8888)
        }
      })
  },
  toAutoMutiStore(query) {
    wx.$api.showLoading('加载中...')
    USERAPI.getAccountList(query.sessionId).then(({
      code,
      data,
      message
    }) => {
      wx.hideLoading()
      if (code == 0) {
        let {
          loginUserInfoList,
          sessionId
        } = data
        loginUserInfoList.forEach(el => {
          if (el.merchantId == query.merchantId) {
            selectAccountLogin({
              id: el.uuid,
              sessionId,
              missionId: query.missionId,
              tab: query.tab,
              merchantId: query.merchantId,
            })
            wx.setStorageSync('showServicesMerchantId', query.merchantId);
          }
        })
      } else wx.$api.showToast(message)
    })
  },
})