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 = window.location.origin
|
// }
|
|
let defaultOrigin = window.location.origin
|
|
// 默认接口类型前缀
|
const defaultApiPath = "/sms-core/"
|
|
// 判断是否为本地开发环境
|
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 === "200") {
|
// success
|
resolve(result)
|
} else {
|
// fail
|
if (resCode === "99") {
|
if (isShowError) {
|
this.showError(resMsg)
|
}
|
} else {
|
// resCode非99的错误提示统一用黄色
|
Message({
|
message: resMsg,
|
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=") || url.includes("/ChannelTemp/tempName/")) {
|
return url
|
}
|
return `${url}${contactStr}menuId=${menuId || ""}`
|
}
|
}
|