/* * @Author: Pengjiantian * @Date: 2020-07-09 14:28:31 * @Last Modified by: Pengjiantian * @Last Modified time: 2020-07-09 14:42:18 */ 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('xxx') && 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 || ''}` } }