request.js 4.04 KB
import { secretKey, djSecretKey, pathEnum, domain, sign } from './http.js'
import { getSessionId } from '~/utils/cache/storageApi'
import network from '~/utils/network'
/**接口 */
class Request {
  get(params, returnAll = false, requestPath) {
    return this.request('GET', params, returnAll, requestPath)
  }
  post(params, returnAll = false, requestPath) {
    // console.log(requestPath, 'requestPath')
    return this.request('POST', params, returnAll, requestPath)
  }
  async request(RequestMethods, params, returnAll, requestPath = 'scrm') {
    const type = await network.getNetworkType()
    if (type == 'none') return
    return new Promise((resolve, reject) => {
      wx.showNavigationBarLoading()
      let header =
        RequestMethods === 'POST'
          ? {
              'content-type': 'application/x-www-form-urlencoded'
            }
          : {}
      let projectName = params.apiProjectName || this.getRopProject(params.method)
      // console.log(projectName, 'projectName')
      if (!projectName) projectName = this.getRopProject(params.method)
      let path = pathEnum[projectName]
      if (!path) {
        path = requestPath == 'dj' ? `/rop-dj-${projectName}/` : `/rop-scrm-${projectName}/`
      }
      params = this.getData(params, requestPath)
      // console.log(params,'params');
      wx.request({
        url: `${domain[requestPath]}${path}router?method=${params.method}`,
        data: params,
        method: RequestMethods,
        header,
        dataType: 'json',
        responseType: 'text',
        success(response) {
          // console.log(response,'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(),
              autoLoginObject = wx.$storage.get('auto_login_object')
            if (thisPage.route == 'login/loading/loading') {
              resolve(data)
              return
            } else {
              if (autoLoginObject) wx.$router.reLaunch('login.loading')
              else wx.$router.reLaunch('login.index')
            }
          } else {
            if (returnAll) {
              resolve(data)
              return
            }
            if (code == '0') {
              resolve(data.data)
            } else {
              // console.trace(data)
              wx.showToast({
                title: message || '网络请求失败',
                icon: 'none'
              })
            }
          }
        },
        fail(error) {
          wx.hideLoading()
          console.log(`接口异常:`, error)
          wx.showToast({
            title: '网络请求失败',
            icon: 'error'
          })
          reject(error)
        },
        complete() {
          wx.stopPullDownRefresh()
          wx.hideNavigationBarLoading()
        }
      })
    })
  }
  getData(params, requestPath) {
    let projectName = params.projectName
    if (!projectName) projectName = this.getRopProject(params.method)
    else delete params.projectName
    let appKey = '48e5e13229b82c1b4e6e8c96151f0637'
    let secret = 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b'
    if (requestPath == 'scrm' && secretKey[projectName]) {
      appKey = secretKey[projectName].appKey
      secret = secretKey[projectName].secret
    }
    if (requestPath == 'dj') {
      appKey = djSecretKey.appKey
      secret = djSecretKey.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()