request.js 4.3 KB
import {
  secretKey,
  pathEnum,
  domain,
  index,
  sign
} from './http.js'
import util from '../utils/util.js'
let INTERRUPT = false
/**接口 */
class Request {
  get(params, returnAll = false) {
    return this.request('GET', params, returnAll)
  }
  post(params, returnAll = false) {
    return this.request('POST', params, returnAll)
  }
  reportTrack(params) {
    return this.request('POST', params, true)
  }
  request(RequestMethods, params, returnAll) {
    return new Promise((resolve, reject) => {
      wx.showNavigationBarLoading()
      let header = RequestMethods === 'POST' ? {
        'content-type': 'application/x-www-form-urlencoded'
      } : {'Cache-Control': 'no-cache'}
      // const merchantId = wx.$storage.get('srcm_shopmall_merchant_id')
      // if (merchantId) header.merchantId = merchantId
      let projectName = this.getRopProject(params.method)
      if (!projectName) projectName = this.getRopProject(params.method)
      const path = pathEnum[projectName]
      params = this.getData(params)
      wx.request({
        url: `${domain}${path}router?method=${params.method}`,
        data: params,
        method: RequestMethods,
        header,
        dataType: 'json',
        responseType: 'text',
        success(response) {
          /**业务代码 */
          let {
            data: {
              code,
              message
            },
            data
          } = response
          if (code == 20 || code == 21) {
            wx.hideLoading()
            const thisPage = wx.$router.getPageInstance()
            if (thisPage && thisPage.pageRefresh) thisPage.pageRefresh()
            if (!wx.$authStatus) {
              if (INTERRUPT) return
              INTERRUPT = true
              // wx.showLoading()
              getApp().login().then(() => {
                if (thisPage.init && thisPage.data.userInfo) thisPage.init()
                if (thisPage.loginCallBack && thisPage.data.userInfo) thisPage.loginCallBack(thisPage.data.userInfo)
                if (thisPage.onRoute) thisPage.onRoute()
                thisPage.onLoad()
                thisPage.onShow()
                INTERRUPT = false
              })
            }
          } else {
            if (returnAll) {
              resolve(data)
              return
            }
            if (code == '0') {
              resolve(data.data)
            } else {
              wx.showToast({
                title: message || '系统异常',
                icon: 'none'
              })
            }
          }
        },
        fail(error) {
          // wx.hideLoading()
          wx.showToast({
            title: '系统异常',
            icon: 'error'
          })
          reject(error)
        },
        complete() {
          wx.stopPullDownRefresh()
          wx.hideNavigationBarLoading()
        }
      })
    })
  }
  getData(params) {
    let projectName = params.projectName
    if (!projectName) projectName = this.getRopProject(params.method)
    else delete params.projectName
    let appKey = '48e5e13229b82c1b4e6e8c96151f0637'
    let secret = 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b'
    if (secretKey[projectName]) {
      appKey = secretKey[projectName].appKey
      secret = secretKey[projectName].secret
    }
    // const sessionId = util.getSessionId()
    const sessionId = wx.$storage.get("aicard_info").sessionId
    let noSignAttr = []
    if (!params.sessionId && sessionId) params.sessionId = sessionId
    if (params.noSignAttr) {
      noSignAttr = params.noSignAttr
      delete params.noSignAttr
    }
    params.v = '1.0.0'
    params.format = 'json'
    params.locale = 'zh_CN'
    params.source = 'Aicard-wechat'
    params.appKey = appKey
    return sign(params, secret, noSignAttr)
  }
  getRopProject(method) {
    const projectName = method.split('.')[2]
    return projectName
  }
  getMock() {
    let url = 'https://oss-config.dev.glsx.com.cn/mock/6264ff4760f415276f6dfe62/aicard-config'
    if (index == 4) url = 'https://oss-config.test.glsx.com.cn/mock/6274e3050da5034a58680aa6/aicard-config'
    return new Promise((resolve) => {
      wx.request({
        url,
        data: {},
        method: 'GET',
        header: {},
        dataType: 'json',
        responseType: 'text',
        success(response) {
          resolve(response.data)
        }
      })
    })
  }
}
export default new Request()