request.js 3.6 KB
import {
  secretKey,
  pathEnum,
  domain,
  sign
} from './http.js'
// import util from '../util'
import {
  getSessionId
} from '~/utils/cache/storageApi'
let requestTimes = 0
/**接口 */
class Request {
  get(params, returnAll = false, isShowLoading = false) {
    return this.request('GET', params, returnAll, isShowLoading)
  }
  post(params, returnAll = false, isShowLoading = false) {
    return this.request('POST', params, returnAll, isShowLoading)
  }
  request(RequestMethods, params, returnAll, isShowLoading) {
    return new Promise((resolve, reject) => {
      wx.showNavigationBarLoading()
      let header =
        RequestMethods === 'POST' ?
        {
          'content-type': 'application/x-www-form-urlencoded'
        } :
        {}
      let projectName = this.getRopProject(params.method)
      if (!projectName) projectName = this.getRopProject(params.method)
      const path = pathEnum[projectName]
      params = this.getData(params)
      requestTimes++
      if (isShowLoading) {
        wx.showLoading({
          title: '加载中'
        })
      }
      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.$storage.rm('sessionId')
            wx.$storage.rm('userInfo')
            const thisPage = wx.$router.getPageInstance()
            if(thisPage.route == 'pages/authentication/loading/loading') {
              resolve(data)
              return
            } else wx.$router.reLaunch('/pages/authentication/login/login')
          } else {
            if (returnAll) {
              resolve(data)
              return
            }
            if (code == '0') {
              resolve(data.data)
            } else {
              wx.showToast({
                title: message || '系统异常',
                icon: 'none'
              })
              reject(data)
            }
          }
        },
        fail(error) {
          wx.hideLoading()
          console.error(`接口异常:`, error)
          wx.showToast({
            title: '系统异常',
            icon: 'error'
          })
          reject(error)
        },
        complete() {
          requestTimes--;
          if (requestTimes == 0 && isShowLoading) {
            wx.hideLoading()
          }
          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 = getSessionId()
    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 = 'dj-wechat'
    params.appKey = appKey
    return sign(params, secret, noSignAttr)
  }
  getRopProject(method) {
    const projectName = method.split('.')[2]
    return projectName
  }
}
export default new Request()