wxOnAppRoute.js 3.43 KB
import util from '../utils/util.js'
import { getUserInfo } from '~/utils/cache/userInfo'
import { IMG_URL } from '~/utils/constantData'

/**
 * wx.onAppRoute钩子方法,
 * 所有的参数都是路由类上的参数
 * @param {*} obj.query
 * @param {*} obj.routeHooks
 * @param {*} obj.routerList
 * @param {*} obj.getPageInstance
 * @param {*} obj.getRootName
 * @param {*} obj.setNavigationBarTitleText
 * @param {*} obj.getRouterList
 */
export const wxOnAppRouteFn = obj => {
  wx.onAppRoute &&
    wx.onAppRoute(e => {
      let thisPage = obj.getPageInstance()
      const rootName = obj.getRootName()
      if (rootName) {
        const selectedColor = rootName == 'packageEquity' ? '#f7dcbc' : '#FC5B32'
        wx.setTabBarStyle({
          selectedColor,
          color: '#979899',
          backgroundColor: '#FFFFFF',
          borderStyle: 'white'
        })
      }
      if (thisPage) {
        obj.setNavigationBarTitleText(thisPage)
        let onRoute = thisPage.onRoute
        thisPage.$query = obj.query
        typeof onRoute === 'function' && onRoute(obj.query)
        thisPage.rootName = rootName
        obj.getRouterList().find(item => {
          if (`/${thisPage.__route__}` === item.url) {
            const authStatus = wx.$authStatus
            // if (item.isTab) thisPage.setData({
            //   showLogin: authStatus
            // })
          }
        })
        if (!wx.$version)
          util.getImgVersion().then(({ version }) => {
            wx.$version = version || '1.0.0'
            thisPage.setData({
              img_version: wx.$version
            })
          })
        else
          thisPage.setData({
            img_version: wx.$version
          })
        // console.log(app)
        thisPage.setData({
          IMGURL: IMG_URL
        })
        let userInfo = getUserInfo()
        if (userInfo && userInfo.phone) {
          if (wx.$authStatus) {
            thisPage.setData({
              userInfo: null
            })
          } else {
            thisPage.setData({
              userInfo
            })
            if (thisPage.init) thisPage.init(userInfo)
          }
        }
      }
      obj.routeHooks.forEach(fn => {
        try {
          typeof fn === 'function' && fn(e, obj.query, obj.routerList)
        } catch (err) {
          console.log(err)
        }
      })
      obj.query = {}
    })
}

/**
 * 生成特定格式路由
 *
 * @param {*} routerList 路由参数
 * @returns
 */
export const generateRoutList = routerList => {
  let _routerList = routerList
  try {
    __wxConfig &&
      __wxConfig.tabBar &&
      __wxConfig.tabBar.list &&
      (_routerList = routerList.map(item => {
        let obj = {}
        if (item.parent != '__plugin__') {
          obj = {
            ...item,
            isTab: __wxConfig.tabBar.list.some(({ pagePath }) => item.url.includes(pagePath.replace('.html', '')))
          }
          if (item.parent !== 'pages') {
            let parentList = item.url.split(`/`)
            if (parentList.length > 5) {
              item.url = item.url.replace(/\/__plugin__\/[a-z0-9A-Z]+/, '')
              obj.subPages = item.url.split(`/${item.parent}/pages/`)[1].split('/')[0]
            }
          }
          if (obj.isTab) {
            __wxConfig.tabBar.list.map(e => {
              if (obj.url === `/${e.pagePath.replace('.html', '')}`) obj.rootName = e.rootName
            })
          }
        }
        return obj
      }))
  } catch (err) {}
  return _routerList
}