request.js 3.55 KB
/**
 * sales 请求层:@uj/api-core 的客户端工厂。
 *
 * 一个 createClient({ kind: 'rop', 配置}) 直接拿到 client;平台(wx)、URL 拼装、签名、
 * 拦截器(业务码归一 / 登录重放 / loading / toast)全在 api-core 里,本文件只传环境参数。
 * public API 与旧壳逐签名一致:get/post/request(params, returnAll, isShowLoading, hostType)、
 * reportTrack(params)、getMock(),453 处调用点零改动。
 */
// 小程序走主入口具名导出(构建 npm 不保证解析 exports 子路径);web/Node 可用 @uj/api-core/Request。
import { createClient } from '@uj/api-core'
import { sign, domain, djDomain, merchantDomain, pathEnum, secretKey, djSecretKey, index } from './http.js'
import { login } from '~/utils/appFn/index'

const client = createClient({
  kind: 'rop',
  hosts: { scrm: domain, dj: djDomain, merchant: merchantDomain },
  pathEnum,
  secretKey,
  djSecretKey,
  // 本 app 的 qs 摊平版 SHA1(字节级对齐旧壳):直接传函数,工厂内部包成签名策略。
  signature: sign,
  defaultCredential: {
    appKey: '48e5e13229b82c1b4e6e8c96151f0637',
    secret: 'c24619ed7fef02a0ae16328146bca5f97cc6493957a2137b',
  },
  session: {
    sessionId: () => {
      const s = wx.$storage.get()
      return (s && s.sessionId) || wx.$storage.get('scrm_shopmall_sessionId') || ''
    },
  },
  getMerchantId: () => wx.$storage.get('srcm_shopmall_merchant_id') || undefined,
  // 语义化开关:20/21 登录后由页面重发,Promise 挂起(旧壳语义)。
  loginReplay: true,
  onLoginRequired: () => {
    // 登录态失效(code 20/21):重跑当前页生命周期拉起登录。逻辑原样来自旧壳。
    if (wx.$loginFlag) return
    if (!wx.$authStatus) {
      wx.$loginFlag = true
      login().then(() => {
        const thisPage = wx.$router.getPageInstance()
        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()
        wx.$loginFlag = false
      }).catch(() => (wx.$loginFlag = false))
    }
  },
})

const call = (params, returnAll = false, isShowLoading = false, hostType = 'scrm') =>
  client.post(params.method, params, { hostType, returnAll, showLoading: isShowLoading })

const api = {
  get: (params, returnAll = false, isShowLoading = false, hostType = 'scrm') =>
    call(params, returnAll, isShowLoading, hostType),
  post: (params, returnAll = false, isShowLoading = false, hostType = 'scrm') =>
    call(params, returnAll, isShowLoading, hostType),
  // 兼容个别直接调用 request(RequestMethods, params, ...) 的形态:忽略首参 HTTP method。
  request: (_httpMethod, params, returnAll = false, isShowLoading = false, hostType = 'scrm') =>
    call(params, returnAll, isShowLoading, hostType),
  reportTrack: (params) => client.post(params.method, params, { returnAll: true }),
  // 配置中心拉取,与 ROP 无关,原样保留。
  getMock() {
    const configModule = 'scrm-shopmall'
    const env = { '1': 'develop', '2': 'test', '3': 'production', '4': 'beta' }[index]
    const configUrl = `https://config-center-backend.glsx.com.cn/glsxAdmin/config?moduleUrl=${configModule}&env=${env}`
    return new Promise((resolve) => {
      wx.request({
        url: configUrl, data: {}, method: 'GET', header: {},
        dataType: 'json', responseType: 'text',
        success(response) { resolve(response.data) },
      })
    })
  },
}

export default api