loading.js 3.32 KB
// pages/loading/loading.js
import { systemApi } from '~/api/index'
Page({
  /**
   * 页面的初始数据
   */
  data: {},
  /**
   * 生命周期函数--监听页面加载
   */
  async onLoad(options) {
    console.log('---------------- 页面初始化 ----------------')
    console.log('options参数:', options)
    systemApi.getImageVersion() //获取图片版本号
    this.tabList = __wxConfig.tabBar.list
    wx.p.wxApi.showLoading()
    //scenc获取页面参数并跳转
    if (options.scene) {
      return this.getSceneById(options.scene)
    }
    if (options.url) {
      return this.login(options)
    }
    this.login(options)
  },
  async login(options = {}) {
    console.log(options, 'options.merchantId')
    // wx.p.router.goHome()
    if (options.merchantId){
      wx.$merchant_id = options.merchantId
      wx.p.storage.set('merchant_info', {
        merchantId: options.merchantId,
      })
      wx.p.storage.set('first_enter_merchantId', options.merchantId)
      wx.p.storage.set('first_enter_miniprograme', true)
    } 
    try {
      const { code, data, message } = await wx.p.api.userApi.login()
      wx.$LoginStatus = code != 0
      if (data)
        wx.p.storage.set('merchant_info', {
          merchantId: data.merchantId,
          merchantName: data.merchantName
        })
      if (code == '0') {
        wx.p.storage.set('userInfo', data)
        wx.p.storage.set('sessionId', data.sessionId)
        if (!data) throw message
      } else {
        wx.p.storage.rm('userInfo')
        wx.p.storage.rm('sessionId')
      }
      console.log('登陆信息:', data)
      if (options.url) {
        return this.jumpPage(options)
      }
      // 默认跳转到首页
      wx.p.router.goHome()
    } catch (error) {
      wx.$LoginStatus = true //请求失败默认未登录
      console.log(error)
      wx.p.wxApi.showToast(String(error))
      wx.p.router.goHome()
    }
  },
  async getSceneById(scene) {
    const id = decodeURIComponent(scene).split('=').at(-1)
    console.log(decodeURIComponent(scene),'decodeURIComponent(scene)');
    try {
      const { code, data } = await wx.p.api.wechatApi.getSceneById({
        id
      })
      const params = data.scene.split('url='),options = {}
      data.scene.split('&').map(item => {
        const [key, val] = item.split('=')
        options[key] = val
      })
      if (params.length > 1) options.url = params.at(-1)
      if (code == 0) this.login(options)
      else throw {}
    } catch (error) {
      wx.p.wxApi.showToast(error || '获取scene失败')
      this.login()
    }
  },
  jumpPage(options) {
    console.log(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 || ''
    wx.p.router[routeName](url, events)
  },
  setRouteData(targetPath) {
    let [url, query] = targetPath.split('?'),
      events = {}
    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
    }
  }
})