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
/*
 * @Author: lixiong
 * @Date: 2019-09-06 14:19:58
 * @Last Modified by: lixiong
 * @Last Modified time: 2019-12-26 17:07:54
 */
 
import { getEnvInfo } from '@/utils/utils'
import BaseFetch from '@/utils/core/baseFetch'
 
import router from '@/router/index'
import { Message } from 'element-ui'
 
export default class CommFetch extends BaseFetch {
  /**
   * constructor 构造函数
   * @param {Object} baseConf 基本配置项 @example {apiOrigin: '接口地址', apiPrefix: 'api默认前缀', isShowError: true}
   * @param {Object} fetchConf 针对底层fetch的配置
   */
  constructor(baseConf = {}, fetchConf = {}) {
    super({ credentials: 'include', ...fetchConf })
    this.specialKeys = ['isShowError']
    this.specialInfo = {
      isShowError: true
    }
    this.setConf(baseConf)
  }
 
  /**
   * setConf 设置当前配置项
   * @param {*} conf
   */
  setConf(baseConf) {
    const { apiOrigin, apiPrefix, isDev } = getEnvInfo()
    this.isDev = isDev
    this.baseConf = { apiOrigin, apiPrefix, ...baseConf }
  }
 
  // 请求前,接口地址处理(提供扩展使用,如统一添加接口前缀)
  mixApi(api) {
    const { apiOrigin, apiPrefix } = this.baseConf
 
    const isNoHttp = api.search(/^https?:\/\//) !== 0
 
    // 外网接口替换设置
    // if (location.hostname.includes('xxxfintech') && isNoHttp) {
    //   api.replace(/server/, 'openserver')
    // }
 
    if (isNoHttp) {
      api = this.joinPath([`${apiOrigin}${apiPrefix}`, api])
    }
    return this.addMenuId(api)
  }
 
  /**
   * splitSpecialConf 分离当前特殊配置项与普通配置项
   * @param {object} obj 配置项
   */
  splitSpecialConf(obj = {}) {
    const { specialKeys, specialInfo } = this
    let otherInfo = {}
    if (typeof obj === 'object') {
      if (obj instanceof FormData) {
        return {
          specialInfo,
          otherInfo: obj
        }
      }
      if (Array.isArray(obj)) {
        return {
          specialInfo,
          otherInfo: obj
        }
      }
      if (Object.keys(obj).some(key => specialKeys.includes(key))) {
        Object.keys(obj).forEach(key => {
          if (specialKeys.includes(key)) {
            specialInfo[key] = obj[key]
          } else {
            otherInfo[key] = obj[key]
          }
        })
      } else {
        otherInfo = { ...obj }
      }
      return {
        specialInfo,
        otherInfo
      }
    } else {
      return {
        specialInfo: obj,
        otherInfo
      }
    }
  }
 
  /**
   * post 请求处理
   * @param {String} api 接口地址
   * @param {Object|String} body 请求参数
   */
  post(api, body = {}, conf = {}) {
    return this.fetch(api, body, { method: 'post', ...conf })
  }
 
  /**
   * get 请求处理
   * @param {String} api 接口地址
   * @param {Object|String} body 请求参数
   * @param {Object} conf 请求配置信息
   */
  get(api, body = {}, conf = {}) {
    return this.fetch(api, body, { method: 'get', ...conf })
  }
 
  // 接口请求
  fetch(api, body = {}, conf = {}) {
    const { isDev } = this
    api = this.mixApi(api)
    const {
      specialInfo: specialBodyInfo,
      otherInfo: otherBodyInfo
    } = this.splitSpecialConf(body)
    const {
      specialInfo: specialConf,
      otherInfo: otherConf
    } = this.splitSpecialConf(body)
    const mixSpecialInfo = { ...specialBodyInfo, ...specialConf }
    const { isShowError } = mixSpecialInfo
 
    return new Promise((resolve, reject) => {
      this.toFetch(api, otherBodyInfo, { ...otherConf, ...conf })
        .then(res => {
          const { errorCode, code, data, ext, msg, errorDesc } = res
          const resCode = code || errorCode
          const resData = ext || data
          const resMsg = msg || errorDesc
          if (resCode === '003') {
            // 登陆失效
            this.logout(resData)
            reject(res)
          } else if (resCode === '00') {
            // success
            resolve(res)
          } else {
            // fail
            if (isShowError) {
              this.showError(resMsg, resCode)
            }
            if (isDev) {
              console.log(api, res)
            }
            reject(res)
          }
          resolve(res)
        })
        .catch(reson => {
          this.showError(reson)
          reject(reson)
        })
    })
  }
  /**
   * logout 登陆失效处理
   * @param {Object} data 接口返回对象
   */
  logout(data) {
    const { isDev } = this
    // 开发阶段处理
    if (isDev) {
      router.push({
        path: '/comprehensiveTransaction/login'
      })
    } else {
      parent.location.href = data.loginUrl
    }
  }
 
  /**
   * showError 错误提示处理
   * @param {string} msg 提示消息
   */
  showError(msg, resCode) {
    if (msg instanceof Error) {
      msg = msg.message
    }
    if (typeof msg !== 'string' || msg === '') {
      msg = '系统异常,请稍后重试'
    }
    const type = resCode === '99' ? 'error' : 'warning'
    Message({
      message: msg,
      type,
      duration: 6000
    })
  }
 
  /**
   * addMenuId api地址添加全局参数menuId
   * @param {String} url 接口地址
   */
  addMenuId(url) {
    const { isDev } = this
    // isDev为本地调试使用
    let 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 || '854'}`
  }
}