86 lines
3.2 KiB
JavaScript
86 lines
3.2 KiB
JavaScript
import { getDoctorWxEnv } from '@/config/app-env.js';
|
||
|
||
/** 与 newApi 医生端接口一致;环境见 config/app-env.js */
|
||
export const DOCTOR_NEWAPI_BASE = getDoctorWxEnv().newApiBase;
|
||
|
||
/** 用于拉取同站点 public/traditional.json(与 DOCTOR_NEWAPI_BASE 同源) */
|
||
export function getDoctorNewApiStaticOrigin() {
|
||
try {
|
||
return new URL(DOCTOR_NEWAPI_BASE).origin;
|
||
} catch (e) {
|
||
return '';
|
||
}
|
||
}
|
||
|
||
export async function requestConfig(ins, options, successHandler = null, failHandler = null, completeHandler = null){
|
||
//原来是:ins.baseUrl = options.baseUrl || ins.baseUrl
|
||
let baseUrl = options.baseUrl || ins.baseUrl;
|
||
// TODO 这里写dev还是生产环境
|
||
if (options.url.split('/').indexOf('newApi') !== -1) {
|
||
baseUrl = DOCTOR_NEWAPI_BASE;
|
||
}
|
||
options.url = options.url.replace('/oldApi/', '');
|
||
options.url = options.url.replace('/newApi/', '');
|
||
const defaultHeader = { 'Content-Type': 'application/x-www-form-urlencoded' }
|
||
// 每次请求独立合并 header,避免登录 isToken:false 污染单例
|
||
let config = {
|
||
url:`${baseUrl}${options.url?options.url:''}`,
|
||
header: { ...defaultHeader, ...(options.header || {}) }
|
||
}
|
||
if(ins.requestInterceptor){
|
||
let _cg = null
|
||
// 合并请求拦截器配置
|
||
try {
|
||
_cg = await ins.requestInterceptor(Object.assign({}, options, config))
|
||
} catch (e){
|
||
return false
|
||
}
|
||
// config为false或null return
|
||
if(!_cg || typeof _cg !== 'object'){
|
||
return false
|
||
}
|
||
// 更新options
|
||
Object.assign(options, _cg)
|
||
config.url = options.url
|
||
config.header = options.header
|
||
}
|
||
const type = options.type || "request"
|
||
// config 详情 如果options没有直接使用,删除props
|
||
if(type === "request"){
|
||
config["data"] = options.data || options.params || {}
|
||
config["method"] = options.method || "GET"
|
||
config["dataType"] = options.dataType || "json"
|
||
config["responseType"] = options.responseType || "text"
|
||
config['sslVerify'] = false
|
||
|
||
// 如果是需要提交复杂对象/数组的写操作(POST/PUT/PATCH),强制使用 JSON 提交
|
||
const method = (config["method"] || "GET").toUpperCase()
|
||
if (["POST", "PUT", "PATCH"].includes(method)) {
|
||
const data = config["data"]
|
||
if (data && typeof data === 'object') {
|
||
const hasComplexValue = Object.values(data).some((v) => {
|
||
return v && typeof v === 'object'
|
||
})
|
||
if (hasComplexValue) {
|
||
config.header["Content-Type"] = "application/json"
|
||
}
|
||
}
|
||
}
|
||
}else if(type === "upload"){
|
||
config["filePath"] = options.filePath
|
||
config["name"] = options.name
|
||
config["method"] = options.method || "POST"
|
||
config["formData"] = options.formData
|
||
// fileType for alipay
|
||
config["fileType"] = options.fileType || "image"
|
||
config['sslVerify'] = false
|
||
// 强制删除 Content-Type
|
||
delete config.header["Content-Type"]
|
||
}
|
||
return config
|
||
}
|
||
|
||
function _isPromise(obj) {
|
||
return obj && (typeof obj === 'object' || typeof obj === 'function') && typeof obj.then === 'function'
|
||
}
|