zhaoxiaoqiang1
2026-01-04 f1d30d03186c79ca2cbcfe60d6d2ce7d73fba97b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import BaseAxios from "./core/baseAxios"
 
import router from "@/router/index"
import { Message } from "element-ui"
 
// 默认接口origin地址
let defaultOrigin
if (location.hostname === "scp.jycash.cn") {
  defaultOrigin = process.env.VUE_APP_XXXFINTECH_API_ORIGIN
} else if (location.host === '10.10.16.114') {
  defaultOrigin = process.env.VUE_APP_HTTPS_API_ORIGIN;
} else {
  // defaultOrigin = process.env.VUE_APP_API_ORIGIN
  defaultOrigin = window.location.origin
 
}
 
// 默认接口类型前缀
const defaultApiPath = "/rlc-cts/"
 
// 判断是否为本地开发环境
const isDev = process.env.NODE_ENV === "development"
 
export default class CommAxios extends BaseAxios {
  /**
   * constructor 构造函数
   * @param {Object} conf 请求基本配置
   * @param {Object} currentConf 当前配置项(isShowError 返回结果为非正确结果的时候,是否自动提示错误)
   * @example {apiOrigin: '接口origin地址', apiPath: '接口类型前缀', isShowError: true}
   */
  constructor(conf = {}, currentConf = {}) {
    super(conf)
    this.currentParamsKeys = ["isShowError"]
    this.setConf(currentConf)
  }
 
  /**
   * setConf 设置当前配置项
   * @param {*} conf
   */
  setConf(conf) {
    const {
      apiOrigin = defaultOrigin,
      apiPath = defaultApiPath,
      ...other
    } = conf
    this.baseURL = `${apiOrigin}${apiPath}`
    this.otherConf = other ? { ...other } : {}
  }
 
  /**
   * mixParams 参数混入处理
   * @param {Object|String} params 请求参数
   */
  mixParams(params) {
    const { specialParamsKeys, currentParamsKeys } = this
    const keys = [...currentParamsKeys, ...specialParamsKeys]
    if (typeof params === "string") {
      params = { data: params }
    }
    if (typeof params === "object") {
      // 若参数中包含配置信息,则不需要特殊处理(此时的实际参数为params的data字段)
      if (!Object.keys(params).some(key => keys.includes(key))) {
        params = { data: params }
      }
    }
    return params
  }
 
  /**
   * 请求统一入口
   * @param {String} api 接口地址
   * @param {Object|String} params 请求参数
   * @param {Object} conf 请求配置信息
   */
  request(api, params = {}, conf = {}) {
    const { baseURL, otherConf } = this
    const mixConf = this.mixConf({ baseURL, ...conf }, params)
    const url = this.getUrl(api, mixConf)
    const data = this.getParams(mixConf)
    const { isShowError = true } = { ...otherConf, ...mixConf }
    return new Promise((resolve, reject) => {
      this.axios({
        ...mixConf,
        url: this.addMenuId(url),
        data
      })
        .then(res => {
          const { data: result } = res
          const { errorCode, code, data, ext, msg, errorDesc } = result
          const resCode = code || errorCode
          const resData = ext || data
          const resMsg = msg || errorDesc
          if (resCode === "003") {
            // 登陆失效
            this.logout(resData)
            reject(result)
          } else if (resCode === "00" || resCode === "200") {
            // success
            if (resCode === "200") {
              resolve(result.data)
            } else {
              resolve(result)
            }
          } else {
            // fail
            if (resCode === "99") {
              if (isShowError) {
                this.showError(resMsg)
              }
            } else {
              //如果是该接口,code会在页面判断是否逾期
              if (res.config.url.indexOf('/batchClaimCheck') != -1) {
                resolve(result)
                return
              }
              //如果接口为getLoanInfo,特殊处理,因为该接口数据格式不统一
              if (res.config.url.indexOf('/getUserInfo') == -1) {
                // resCode非99的错误提示统一用黄色
                Message({
                  message: resMsg,
                  type: "warning",
                  duration: 6000
                })
              }else {
 
                if (res.data.resultCode == '1') {
                  resolve(res.data.data)
                }else {
                  Message({
                    message: '获取申请人失败',
                    type: "warning",
                    duration: 6000
                  })
                }
              }
            }
            reject(result)
          }
          resolve(res)
        })
        .catch(reson => {
          reject(reson)
        })
    })
  }
 
  upload(api, params = {}) {
    return this.request(api, params, {
      method: "post",
      headers: {
        "Content-Type": "multipart/form-data"
      }
    })
  }
 
  /**
   * 以表单形式提交数据
   * @param {String} api 接口地址
   * @param {Object} params 请求参数
   */
  submit(api, params = {}) {
    const { baseURL } = this
    const mixConf = this.mixConf({ baseURL }, params)
    const url = this.getUrl(api, mixConf)
    const tempForm = document.createElement("form")
    tempForm.method = "post"
    tempForm.action = this.addMenuId(url)
    tempForm.target = "_blank"
 
    document.body.appendChild(tempForm)
 
    Object.keys(params).forEach(key => {
      const tempInput = document.createElement("input")
      tempInput.type = "hidden"
      tempInput.name = key
      tempInput.value = params[key]
      tempForm.appendChild(tempInput)
    })
    tempForm.submit()
    document.body.removeChild(tempForm)
  }
 
  /**
   * logout 登陆失效处理
   * @param {Object} data 接口返回对象
   */
  logout(data) {
    // 开发阶段处理
    if (isDev) {
      router.push({
        path: "/comprehensiveTransaction/login"
      })
    } else {
      parent.location.href = data.loginUrl
    }
  }
 
  /**
   * showError 错误提示处理
   * @param {string} msg 提示消息
   */
  showError(msg) {
    if (msg instanceof Error) {
      msg = msg.message
    }
    if (typeof msg !== "string" || msg === "") {
      msg = "系统异常,请稍后重试"
    }
    Message({
      message: msg,
      type: "error",
      duration: 6000
    })
  }
 
  /**
   * addMenuId api地址添加全局参数menuId
   * @param {String} url 接口地址
   */
  addMenuId(url) {
    // isDev为本地调试使用
    const menuId = isDev
      ? sessionStorage.getItem("g_menu_id")
      : parent.g_menu_id || ""
    const contactStr = url.includes("?") ? "&" : "?"
    if (url.includes("menuId=")) {
      return url
    }
    return `${url}${contactStr}menuId=${menuId || ""}`
  }
}