index.js 1.74 KB
// import util from '~/utils/util'
// import { getNavigationBarTitleText } from '~/utils/cache/storageApi'
import USER from '~/api/usersApi/index'
import {
  Storage
} from '~/utils/cache/storage'
const $Storage = new Storage()
export function setPageParams(options) {
  function setPageParmas(query) {
    if (query.merchantId) $Storage.set('srcm_shopmall_merchant_id', query.merchantId)
    if (query.customerId) $Storage.set('srcm_shopmall_customer_id', query.customerId)
  }
  Object.keys(options.query).map(e => {
    let params = decodeURIComponent(e).split('&')
    params.map(i => {
      let [key, val] = i.split('=')
      if (!val) val = options.query[key]
      setPageParmas({
        [key]: val
      })
    })
  })
}

export async function login() {
  return new Promise(async (resolve) => {
    try {
      const {
        code,
        data,
        message
      } = await USER.login()
      wx.$isGroup = data.group
      wx.$authStatus = code != 0
      if (data) wx.setStorageSync('merchant_info', {
        merchantId: data.merchantId,
        merchantName: data.merchantName
      })
      if (code == '0') {
        wx.$storage.rm('userInfo')
        wx.$storage.rm('sessionId')
        wx.$util.setUserInfo(data)
        wx.$util.setSessionId(data.sessionId)
        let customerId = wx.$storage.get('srcm_shopmall_customer_id')
        if (customerId) USER.updataCustomerPhone()
        wx.$util.customerbehavior()
        // 登录完加载权益会员信息接口(目前只针对权益商品页面)
        wx.$eventbus.emit('EQUITYGOODS', true)
      }
      resolve({
        code,
        data,
        message
      })
    } catch (error) {
      console.log('jslogin 登录失败')
      console.error(error)
      wx.$authStatus = true
    }
  })
}