|
|
@@ -2,125 +2,114 @@ import axios, { AxiosRequestConfig, Method } from 'axios'
|
|
|
//import qs from 'qs'
|
|
|
//import cryptojs from 'crypto-js'
|
|
|
//import { addPending, removePending } from './pending'
|
|
|
-import { loginStore } from '@/stores'
|
|
|
-import { HttpParams, CommonParams, HttpResponse, Payload, ResultCode } from './interface'
|
|
|
import service from '@/services'
|
|
|
+import { loginStore } from '@/stores'
|
|
|
+import { CommonResult, ResultCode } from './interface'
|
|
|
|
|
|
-const httpService = new (class {
|
|
|
- private axiosInstance = axios.create({
|
|
|
- timeout: 30000,
|
|
|
- })
|
|
|
+const axiosInstance = axios.create({
|
|
|
+ timeout: 30000,
|
|
|
+})
|
|
|
|
|
|
- constructor() {
|
|
|
- // 请求拦截器
|
|
|
- this.axiosInstance.interceptors.request.use(
|
|
|
- (config) => {
|
|
|
- //addPending(config) //将当前请求添加到列表中
|
|
|
- //请求头签名
|
|
|
- const sign = {
|
|
|
- token: loginStore.getters.token,
|
|
|
- signsecret: 'qz7qWOMXKTMT5JlDs5w4NTPwWeR3xhF1v6wqbZ9cExmP6cc3spvNAp1wJJ1SqRI5',
|
|
|
- timestamp: new Date().getTime(),
|
|
|
- }
|
|
|
- //设置请求头
|
|
|
- config.headers = {
|
|
|
- Authorization: sign.token,
|
|
|
- //Signid: 'eecd3f37625f4501b88e9f0fa14b4b51',
|
|
|
- //Sign: cryptojs.SHA256(qs.stringify(sign)).toString(),
|
|
|
- //Timestamp: sign.timestamp.toString(),
|
|
|
- }
|
|
|
- return config
|
|
|
- },
|
|
|
- (err) => {
|
|
|
- console.error(err)
|
|
|
- return Promise.reject('出现错误,请稍后再试')
|
|
|
- }
|
|
|
- )
|
|
|
- // 响应拦截器
|
|
|
- this.axiosInstance.interceptors.response.use(
|
|
|
- (res) => {
|
|
|
- //removePending(res) //在请求结束后,移除本次请求
|
|
|
- return res
|
|
|
- },
|
|
|
- (err) => {
|
|
|
- if (err.message === 'Network Error') {
|
|
|
- return Promise.reject('无网络连接,请检查网络')
|
|
|
- }
|
|
|
- if (err.response) {
|
|
|
- const { msg, message } = err.response.data ?? {}
|
|
|
- switch (err.response.status) {
|
|
|
- case 408: {
|
|
|
- return Promise.reject('请求超时,请稍后再试')
|
|
|
- }
|
|
|
- default: {
|
|
|
- return Promise.reject(msg || message)
|
|
|
- }
|
|
|
- }
|
|
|
- }
|
|
|
- return Promise.reject('出现错误,请稍后再试')
|
|
|
- }
|
|
|
- )
|
|
|
+// 请求拦截器
|
|
|
+axiosInstance.interceptors.request.use(
|
|
|
+ (config) => {
|
|
|
+ //addPending(config) //将当前请求添加到列表中
|
|
|
+ //请求头签名
|
|
|
+ const sign = {
|
|
|
+ token: loginStore.getters.token,
|
|
|
+ signsecret: 'qz7qWOMXKTMT5JlDs5w4NTPwWeR3xhF1v6wqbZ9cExmP6cc3spvNAp1wJJ1SqRI5',
|
|
|
+ timestamp: new Date().getTime(),
|
|
|
+ }
|
|
|
+ //设置请求头
|
|
|
+ config.headers = {
|
|
|
+ Authorization: sign.token,
|
|
|
+ //Signid: 'eecd3f37625f4501b88e9f0fa14b4b51',
|
|
|
+ //Sign: cryptojs.SHA256(qs.stringify(sign)).toString(),
|
|
|
+ //Timestamp: sign.timestamp.toString(),
|
|
|
+ }
|
|
|
+ return config
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ console.error(err)
|
|
|
+ return Promise.reject('出现错误,请稍后再试')
|
|
|
}
|
|
|
+)
|
|
|
|
|
|
- private request = async (url: string, method: Method, payload?: unknown) => {
|
|
|
- const config = await service.onReady()
|
|
|
- this.axiosInstance.defaults.baseURL = config.goCommonSearchUrl
|
|
|
- const requestConfig: AxiosRequestConfig = {
|
|
|
- url,
|
|
|
- method,
|
|
|
+// 响应拦截器
|
|
|
+axiosInstance.interceptors.response.use(
|
|
|
+ (res) => {
|
|
|
+ //removePending(res) //在请求结束后,移除本次请求
|
|
|
+ return res
|
|
|
+ },
|
|
|
+ (err) => {
|
|
|
+ if (err.message === 'Network Error') {
|
|
|
+ return Promise.reject('无网络连接,请检查网络')
|
|
|
}
|
|
|
- if (payload instanceof Object) {
|
|
|
- if (['post', 'POST', 'put', 'PUT', 'patch', 'PATCH'].includes(method)) {
|
|
|
- requestConfig.data = payload
|
|
|
- } else {
|
|
|
- requestConfig.params = payload
|
|
|
+ if (err.response) {
|
|
|
+ const { msg, message } = err.response.data ?? {}
|
|
|
+ switch (err.response.status) {
|
|
|
+ case 408: {
|
|
|
+ return Promise.reject('请求超时,请稍后再试')
|
|
|
+ }
|
|
|
+ default: {
|
|
|
+ return Promise.reject(msg || message)
|
|
|
+ }
|
|
|
}
|
|
|
- } else {
|
|
|
- requestConfig.url = url + (payload ?? '')
|
|
|
}
|
|
|
- return this.axiosInstance(requestConfig)
|
|
|
+ return Promise.reject('出现错误,请稍后再试')
|
|
|
}
|
|
|
+)
|
|
|
|
|
|
- commonRequest = async <T extends Payload>(url: string, method: Method, params: CommonParams<T>, errMsg?: string) => {
|
|
|
- const { data, success, fail, complete } = params
|
|
|
- return await this.request(url, method, data).then((res) => {
|
|
|
- const data = res.data as T['rsp']
|
|
|
- success && success(data)
|
|
|
- return Promise.resolve(data)
|
|
|
- }).catch((err) => {
|
|
|
- const msg = err ?? (errMsg ? '请求失败: ' + errMsg : '请求失败,请稍后重试')
|
|
|
- fail && fail(msg)
|
|
|
- return Promise.reject(msg)
|
|
|
- }).finally(() => {
|
|
|
- complete && complete()
|
|
|
- })
|
|
|
+/**
|
|
|
+ * Http 通用请求
|
|
|
+ * @param url
|
|
|
+ * @param method
|
|
|
+ * @param payload
|
|
|
+ * @param errMsg
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+export async function commonRequest<T>(url: string, method: Method, payload?: unknown, errMsg?: string) {
|
|
|
+ await service.onReady().then((config) => {
|
|
|
+ axiosInstance.defaults.baseURL = config.goCommonSearchUrl
|
|
|
+ })
|
|
|
+ const config: AxiosRequestConfig = {
|
|
|
+ url,
|
|
|
+ method,
|
|
|
}
|
|
|
-
|
|
|
- httpRequest = async <T extends Payload>(url: string, method: Method, params: HttpParams<T>, errMsg?: string) => {
|
|
|
- const { data, success, fail, complete } = params
|
|
|
- return await this.request(url, method, data).then((res) => {
|
|
|
- const data = res.data as HttpResponse<T['rsp']>
|
|
|
- switch (data.code) {
|
|
|
- case ResultCode.InvalidToken:
|
|
|
- return Promise.reject('令牌无效')
|
|
|
- case ResultCode.Success:
|
|
|
- success && success(data)
|
|
|
- return Promise.resolve(data)
|
|
|
- default:
|
|
|
- fail && fail(data.msg)
|
|
|
- return Promise.reject(data.msg)
|
|
|
- }
|
|
|
- }).catch((err) => {
|
|
|
- const msg = err ?? (errMsg ? '请求失败: ' + errMsg : '请求失败,请稍后重试')
|
|
|
- fail && fail(msg)
|
|
|
- return Promise.reject(msg)
|
|
|
- }).finally(() => {
|
|
|
- complete && complete()
|
|
|
- })
|
|
|
+ if (payload instanceof Object) {
|
|
|
+ if (['post', 'POST', 'put', 'PUT', 'patch', 'PATCH'].includes(method)) {
|
|
|
+ config.data = payload
|
|
|
+ } else {
|
|
|
+ config.params = payload
|
|
|
+ }
|
|
|
}
|
|
|
-})
|
|
|
+ return await axiosInstance(config).then((res) => {
|
|
|
+ return res.data as T
|
|
|
+ }).catch((err) => {
|
|
|
+ const msg = err ?? (errMsg ? '请求失败: ' + errMsg : '请求失败,请稍后重试')
|
|
|
+ return Promise.reject(msg)
|
|
|
+ })
|
|
|
+}
|
|
|
|
|
|
-export const { commonRequest, httpRequest } = httpService
|
|
|
+/**
|
|
|
+ * Http 请求
|
|
|
+ * @param url
|
|
|
+ * @param method
|
|
|
+ * @param payload
|
|
|
+ * @param errMsg
|
|
|
+ * @returns
|
|
|
+ */
|
|
|
+export async function httpRequest<T>(url: string, method: Method, payload?: unknown, errMsg?: string) {
|
|
|
+ const res = await commonRequest<CommonResult<T>>(url, method, payload, errMsg)
|
|
|
+ switch (res.code) {
|
|
|
+ case ResultCode.InvalidToken:
|
|
|
+ return Promise.reject('令牌无效')
|
|
|
+ case ResultCode.Success:
|
|
|
+ return res
|
|
|
+ default:
|
|
|
+ return Promise.reject(res.msg)
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
* 获取服务配置地址
|