loading.js 3.3 KB
// pages/loading/loading.js
import {
  IMG_VERSION
} from '~/utils/constantData'
import {
  commonApi
} from "~/api/api.js";
import {
  removeSystemStorage
} from '~/utils/cache/storageApi'
import USER from '~/api/usersApi/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {},
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    console.log('---------------- 页面初始化 ----------------')
    console.log('options参数:', options)
    IMG_VERSION() //获取图片版本号
    this.tabList = []
    wx.$api.showLoading()
    //scenc获取页面参数并跳转
    if (options.scene) {
      return this.getSceneById(options.scene)
    }
    if (options.url) {
      return this.login(options)
    }
    this.login(options)
  },
  async login(options = {}) {
    if (options.merchantId) wx.$storage.set('srcm_shopmall_merchant_id', options.merchantId)
    try {
      const {
        code,
        data,
        message
      } = await USER.login()
      wx.$authStatus = code != 0
      if (data)
        wx.setStorageSync('merchant_info', {
          merchantId: data.merchantId,
          merchantName: data.merchantName
        })
      if (code == '0') {
        wx.$util.setUserInfo(data)
        wx.$util.setSessionId(data.sessionId)
        if (!data) throw message
      } else removeSystemStorage()
      console.log('登陆信息:', data)
      if (options.url) {
        return this.jumpPage(options)
      }
      // 默认跳转到首页
      wx.$router.goHome()
    } catch (error) {
      wx.$authStatus = true //请求失败默认未登录
      console.log(error)
      wx.$api.showToast(String(error))
      wx.$router.goHome()
    }
  },
  async getSceneById(scene) {
    const id = decodeURIComponent(scene).split('=').at(-1)
    try {
      const {
        code,
        data
      } = await commonApi.getQrcodeParams({
        id
      })
      const params = data.scene.split('url='),
        options = {},
        {
          options: _options
        } = this
      data.scene.split('&').map(item => {
        const [key, val] = item.split('=')
        options[key] = val
      })
      if (params.length > 1) options.url = params.at(-1)
      if (!options.url && _options.url) options.url = _options.url
      if (code == 0) this.login(options)
      else throw {}
    } catch (error) {
      wx.$api.showToast(error || '获取scene失败')
      this.login()
    }
  },
  jumpPage(options) {
    const targetPath = decodeURIComponent(options.url),
      {
        url,
        events
      } = this.setRouteData(targetPath),
      routeName = this.tabList.findIndex(e => `/${e.pagePath}` == `${url}.html`) > -1 ? 'switchTab' : 'redirectTo'
    events.notIntercept = true
    events.merchantId = options.merchantId || ''
    Object.keys(options).map(key => {
      if (key != 'url' && options[key]) events[key] = options[key]
    })
    wx.$router[routeName](url, events)
  },
  setRouteData(targetPath) {
    let [url, query] = targetPath.split('?'),
      events = {
        isLogin: 1
      }
    if (query) {
      let arr = query.split('&')
      arr.map(e => {
        let [key, val] = e.split('=')
        if (val == 'false' || val == 'true') val = JSON.parse(val)
        Object.assign(events, {
          [key]: val
        })
      })
    }
    return {
      url,
      events
    }
  }
})