mirror of
https://gitee.com/niucloud-team/niucloud-admin.git
synced 2025-12-13 03:12:47 +00:00
update web
This commit is contained in:
parent
ffa8719298
commit
83152a469a
@ -30,14 +30,18 @@ export function logout() {
|
|||||||
* 用户名注册
|
* 用户名注册
|
||||||
*/
|
*/
|
||||||
export function usernameRegister(data: AnyObject) {
|
export function usernameRegister(data: AnyObject) {
|
||||||
return request.post('register', data)
|
let url = 'register'
|
||||||
|
data.pid && (url += `?pid=${data.pid}`)
|
||||||
|
return request.post(url, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 手机号注册
|
* 手机号注册
|
||||||
*/
|
*/
|
||||||
export function mobileRegister(data: AnyObject) {
|
export function mobileRegister(data: AnyObject) {
|
||||||
return request.post('register/mobile', data)
|
let url = 'register/mobile'
|
||||||
|
data.pid && (url += `?pid=${data.pid}`)
|
||||||
|
return request.post(url, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -58,7 +62,9 @@ export function weappLogin(data: AnyObject) {
|
|||||||
* 绑定手机号
|
* 绑定手机号
|
||||||
*/
|
*/
|
||||||
export function bind(data: AnyObject) {
|
export function bind(data: AnyObject) {
|
||||||
return request.post('bind', data)
|
let url = 'bind'
|
||||||
|
data.pid && (url += `?pid=${data.pid}`)
|
||||||
|
return request.post(url, data)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -2,7 +2,7 @@
|
|||||||
* 获取验证码
|
* 获取验证码
|
||||||
*/
|
*/
|
||||||
export function getCaptcha() {
|
export function getCaptcha() {
|
||||||
return request.get('captcha', {})
|
return request.get('captcha', { time: (new Date().getTime()) })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@ -1,28 +1,27 @@
|
|||||||
import { breakpointsTailwind } from '@vueuse/core'
|
import { breakpointsTailwind } from '@vueuse/core'
|
||||||
import { ElMessage } from 'element-plus'
|
import { ElMessage } from 'element-plus'
|
||||||
|
import useMemberStore from '@/stores/member'
|
||||||
|
|
||||||
interface ShowMessageConfig {
|
interface ConfigOption {
|
||||||
showErrorMessage?: boolean
|
showErrorMessage?: boolean
|
||||||
showSuccessMessage?: boolean
|
showSuccessMessage?: boolean,
|
||||||
|
headers?: Headers
|
||||||
}
|
}
|
||||||
|
|
||||||
interface FetchOptions {
|
interface FetchOptions {
|
||||||
baseURL: string
|
baseURL: string
|
||||||
method: string
|
headers: Record<string, any>
|
||||||
query?: Record<string, any>,
|
onRequest?: (data: any) => void
|
||||||
body?: RequestInit['body'] | Record<string, any>
|
onResponse?: (data: any) => void
|
||||||
headers: Record<string, any>,
|
onResponseError?: (data: any) => void
|
||||||
onRequest?: (data: any) => void,
|
showErrorMessage?: boolean
|
||||||
onResponse?: (data: any) => void,
|
showSuccessMessage?: boolean
|
||||||
onResponseError?: (data: any) => void,
|
|
||||||
showMessageConfig?: ShowMessageConfig,
|
|
||||||
watch: boolean
|
watch: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
class Http {
|
class Http {
|
||||||
private options: FetchOptions = {
|
private options: FetchOptions = {
|
||||||
baseURL: '',
|
baseURL: '',
|
||||||
method: '',
|
|
||||||
headers: {},
|
headers: {},
|
||||||
watch: false
|
watch: false
|
||||||
}
|
}
|
||||||
@ -38,9 +37,6 @@ class Http {
|
|||||||
this.options.headers[runtimeConfig.public.VITE_REQUEST_HEADER_SITEID_KEY] = useCookie('siteId').value || runtimeConfig.public.VITE_SITE_ID
|
this.options.headers[runtimeConfig.public.VITE_REQUEST_HEADER_SITEID_KEY] = useCookie('siteId').value || runtimeConfig.public.VITE_SITE_ID
|
||||||
this.options.headers[runtimeConfig.public.VITE_REQUEST_HEADER_CHANNEL_KEY] = 'pc'
|
this.options.headers[runtimeConfig.public.VITE_REQUEST_HEADER_CHANNEL_KEY] = 'pc'
|
||||||
if (getToken()) this.options.headers[runtimeConfig.public.VITE_REQUEST_HEADER_TOKEN_KEY] = getToken()
|
if (getToken()) this.options.headers[runtimeConfig.public.VITE_REQUEST_HEADER_TOKEN_KEY] = getToken()
|
||||||
|
|
||||||
delete this.options.body
|
|
||||||
delete this.options.query
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -51,31 +47,32 @@ class Http {
|
|||||||
this.handleNetworkError(response)
|
this.handleNetworkError(response)
|
||||||
if (data.code != undefined) {
|
if (data.code != undefined) {
|
||||||
if (data.code == 1) {
|
if (data.code == 1) {
|
||||||
if (options.showMessageConfig.showSuccessMessage) ElMessage({ message: data.msg, type: 'success' })
|
if (options.showSuccessMessage) ElMessage({ message: data.msg, type: 'success' })
|
||||||
} else {
|
} else {
|
||||||
ElMessage({ message: data.msg, type: 'error' })
|
if (data.code == 0) {
|
||||||
|
ElMessage({ message: data.msg, type: 'error' })
|
||||||
|
} else {
|
||||||
|
this.handleAuthError(data.code)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public get(url: string, query = {}, showMessageConfig: ShowMessageConfig = {}) {
|
public get(url: string, query = {}, config: ConfigOption = {}) {
|
||||||
this.options.query = query
|
return this.request(url, 'GET', { query }, config)
|
||||||
return this.request(url, 'GET', showMessageConfig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public post(url: string, body = {}, showMessageConfig: ShowMessageConfig = {}) {
|
public post(url: string, body = {}, config: ConfigOption = {}) {
|
||||||
this.options.body = body
|
return this.request(url, 'POST', { body }, config)
|
||||||
return this.request(url, 'POST', showMessageConfig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public put(url: string, body = {}, showMessageConfig: ShowMessageConfig = {}) {
|
public put(url: string, body = {}, config: ConfigOption = {}) {
|
||||||
this.options.body = body
|
return this.request(url, 'PUT', { body }, config)
|
||||||
return this.request(url, 'PUT', showMessageConfig)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public delete(url: string, showMessageConfig: ShowMessageConfig = {}) {
|
public delete(url: string, config: ConfigOption = {}) {
|
||||||
return this.request(url, 'DELETE', showMessageConfig)
|
return this.request(url, 'DELETE', {}, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -85,10 +82,7 @@ class Http {
|
|||||||
* @param showMessageConfig
|
* @param showMessageConfig
|
||||||
* @returns
|
* @returns
|
||||||
*/
|
*/
|
||||||
private request(url: string, method: string, showMessageConfig: ShowMessageConfig = {}) {
|
private request(url: string, method: string, param: AnyObject = {}, config: ConfigOption = {}) {
|
||||||
this.options.method = method
|
|
||||||
this.options.showMessageConfig = showMessageConfig
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
setTimeout(() => {
|
setTimeout(() => {
|
||||||
// 处理首次请求baseurl空的问题
|
// 处理首次请求baseurl空的问题
|
||||||
@ -96,14 +90,27 @@ class Http {
|
|||||||
!this.options.baseURL && (this.options.baseURL = runtimeConfig.public.VITE_APP_BASE_URL || `${location.origin}/api/`)
|
!this.options.baseURL && (this.options.baseURL = runtimeConfig.public.VITE_APP_BASE_URL || `${location.origin}/api/`)
|
||||||
this.options.baseURL.substr(-1) != '/' && (this.options.baseURL += '/')
|
this.options.baseURL.substr(-1) != '/' && (this.options.baseURL += '/')
|
||||||
|
|
||||||
useFetch(url, { ...this.options }).then((response) => {
|
// 处理数组格式
|
||||||
const { data: { value }, error } = response
|
for (const key in param.query) {
|
||||||
|
if (param.query[key] instanceof Array) {
|
||||||
|
param.query[key].forEach((item, index) => {
|
||||||
|
param.query[`${key}[${index}]`] = item
|
||||||
|
});
|
||||||
|
delete param.query[key]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
useFetch(url, { ...this.options, method, ...config, ...param }).then((response) => {
|
||||||
|
const { data: { value }, error } = response
|
||||||
if (value) {
|
if (value) {
|
||||||
if (value.code == 1) {
|
if (value.code && value.code == 1) {
|
||||||
resolve(value)
|
resolve(value)
|
||||||
} else {
|
} else {
|
||||||
reject(value)
|
if (value.type && value.type == 'application/zip') {
|
||||||
|
resolve(value)
|
||||||
|
} else {
|
||||||
|
reject(value)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
reject(error)
|
reject(error)
|
||||||
@ -115,6 +122,14 @@ class Http {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private handleAuthError(code: number) {
|
||||||
|
switch (code) {
|
||||||
|
case 401:
|
||||||
|
useMemberStore().logout()
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private handleNetworkError(err: any) {
|
private handleNetworkError(err: any) {
|
||||||
if (err.status && err.status != 200) {
|
if (err.status && err.status != 200) {
|
||||||
let errMessage = ''
|
let errMessage = ''
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user