mirror of
https://gitee.com/likeadmin/likeadmin_java.git
synced 2026-06-05 19:25:23 +08:00
feat 同步前端代码
feat 同步PC代码 feat 增加1.9DB feat 同步逻辑
This commit is contained in:
@@ -1,19 +1,14 @@
|
||||
import { getClient } from '@/utils/env'
|
||||
import { FetchOptions } from 'ohmyfetch'
|
||||
import { FetchOptions } from 'ofetch'
|
||||
import { RequestCodeEnum, RequestMethodsEnum } from '@/enums/requestEnums'
|
||||
import feedback from '@/utils/feedback'
|
||||
import { merge } from 'lodash-es'
|
||||
import { Request } from './request'
|
||||
import { getApiPrefix, getApiUrl, getVersion } from '../env'
|
||||
import { useUserStore } from '@/stores/user'
|
||||
import {
|
||||
PopupTypeEnum,
|
||||
useAccount
|
||||
} from '~~/layouts/components/account/useAccount'
|
||||
|
||||
export function createRequest(opt?: Partial<FetchOptions>) {
|
||||
const userStore = useUserStore()
|
||||
const { setPopupType, toggleShowPopup } = useAccount()
|
||||
// const { setPopupType, toggleShowPopup } = useAccount()
|
||||
const defaultOptions: FetchOptions = {
|
||||
// 基础接口地址
|
||||
baseURL: getApiUrl(),
|
||||
@@ -22,17 +17,6 @@ export function createRequest(opt?: Partial<FetchOptions>) {
|
||||
version: getVersion()
|
||||
},
|
||||
retry: 2,
|
||||
async onRequest({ options }) {
|
||||
const { withToken } = options.requestOptions
|
||||
const headers = options.headers || {}
|
||||
// 添加token
|
||||
if (withToken) {
|
||||
const token = userStore.token
|
||||
headers['like-token'] = token
|
||||
}
|
||||
options.headers['terminal'] = getClient()
|
||||
options.headers = headers
|
||||
},
|
||||
requestOptions: {
|
||||
apiPrefix: getApiPrefix(),
|
||||
isTransformResponse: true,
|
||||
@@ -40,8 +24,8 @@ export function createRequest(opt?: Partial<FetchOptions>) {
|
||||
withToken: true,
|
||||
isParamsToData: true,
|
||||
requestInterceptorsHook(options) {
|
||||
console.log(options)
|
||||
const { apiPrefix, isParamsToData } = options.requestOptions
|
||||
const { apiPrefix, isParamsToData, withToken } =
|
||||
options.requestOptions
|
||||
// 拼接请求前缀
|
||||
if (apiPrefix) {
|
||||
options.url = `${apiPrefix}${options.url}`
|
||||
@@ -56,6 +40,12 @@ export function createRequest(opt?: Partial<FetchOptions>) {
|
||||
options.body = params
|
||||
options.params = {}
|
||||
}
|
||||
const headers = options.headers || {}
|
||||
if (withToken) {
|
||||
const token = userStore.token
|
||||
headers['token'] = token
|
||||
}
|
||||
options.headers = headers
|
||||
return options
|
||||
},
|
||||
async responseInterceptorsHook(response, options) {
|
||||
@@ -69,32 +59,21 @@ export function createRequest(opt?: Partial<FetchOptions>) {
|
||||
if (!isTransformResponse) {
|
||||
return response._data
|
||||
}
|
||||
const { code, data, msg } = response._data
|
||||
const { code, data, show, msg } = response._data
|
||||
switch (code) {
|
||||
case RequestCodeEnum.SUCCESS:
|
||||
if (show) {
|
||||
msg && feedback.msgSuccess(msg)
|
||||
}
|
||||
return data
|
||||
case RequestCodeEnum.PARAMS_TYPE_ERROR:
|
||||
case RequestCodeEnum.PARAMS_VALID_ERROR:
|
||||
case RequestCodeEnum.REQUEST_METHOD_ERROR:
|
||||
case RequestCodeEnum.ASSERT_ARGUMENT_ERROR:
|
||||
case RequestCodeEnum.ASSERT_MYBATIS_ERROR:
|
||||
case RequestCodeEnum.LOGIN_ACCOUNT_ERROR:
|
||||
case RequestCodeEnum.LOGIN_DISABLE_ERROR:
|
||||
case RequestCodeEnum.NO_PERMISSTION:
|
||||
case RequestCodeEnum.FAILED:
|
||||
case RequestCodeEnum.SYSTEM_ERROR:
|
||||
if (msg) {
|
||||
case RequestCodeEnum.FAIL:
|
||||
if (show) {
|
||||
msg && feedback.msgError(msg)
|
||||
}
|
||||
return Promise.reject(msg)
|
||||
|
||||
case RequestCodeEnum.TOKEN_INVALID:
|
||||
case RequestCodeEnum.TOKEN_EMPTY:
|
||||
case RequestCodeEnum.LOGIN_FAILURE:
|
||||
userStore.logout()
|
||||
setPopupType(PopupTypeEnum.LOGIN)
|
||||
toggleShowPopup(true)
|
||||
return Promise.reject()
|
||||
|
||||
return Promise.reject(data)
|
||||
default:
|
||||
return data
|
||||
}
|
||||
|
||||
@@ -4,11 +4,13 @@ import {
|
||||
$Fetch,
|
||||
FetchResponse,
|
||||
RequestOptions,
|
||||
FileParams
|
||||
} from 'ohmyfetch'
|
||||
FileParams,
|
||||
RequestEventStreamOptions
|
||||
} from 'ofetch'
|
||||
import { merge } from 'lodash-es'
|
||||
import { isFunction } from '../validate'
|
||||
import { RequestMethodsEnum } from '@/enums/requestEnums'
|
||||
import { objectToQuery } from '../util'
|
||||
|
||||
export class Request {
|
||||
private requestOptions: RequestOptions
|
||||
@@ -79,7 +81,6 @@ export class Request {
|
||||
this.requestOptions,
|
||||
requestOptions
|
||||
)
|
||||
|
||||
const {
|
||||
requestInterceptorsHook,
|
||||
responseInterceptorsHook,
|
||||
@@ -101,6 +102,7 @@ export class Request {
|
||||
response,
|
||||
mergeOptions
|
||||
)
|
||||
|
||||
resolve(response)
|
||||
} catch (error) {
|
||||
reject(error)
|
||||
|
||||
@@ -45,3 +45,19 @@ export function getNormalPath(path: string) {
|
||||
}
|
||||
return newPath
|
||||
}
|
||||
|
||||
/**
|
||||
* @description对象格式化为Query语法
|
||||
* @param { Object } params
|
||||
* @return {string} Query语法
|
||||
*/
|
||||
export function objectToQuery(params: Record<string, any>): string {
|
||||
let query = ''
|
||||
for (const props of Object.keys(params)) {
|
||||
const value = params[props]
|
||||
if (!isEmpty(value)) {
|
||||
query += props + '=' + value + '&'
|
||||
}
|
||||
}
|
||||
return query.slice(0, -1)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user