mirror of
https://gitee.com/ulthon/ulthon_admin.git
synced 2026-07-07 10:32:48 +08:00
feat: 新增source目录用于存放配套资源与多端代码
This commit is contained in:
18
source/clients/uniapp/src/utils/auth.js
Normal file
18
source/clients/uniapp/src/utils/auth.js
Normal file
@@ -0,0 +1,18 @@
|
||||
const TOKEN_KEY = 'ULTHON_ADMIN_TOKEN'
|
||||
|
||||
export function getToken() {
|
||||
return uni.getStorageSync(TOKEN_KEY) || ''
|
||||
}
|
||||
|
||||
export function setToken(token) {
|
||||
uni.setStorageSync(TOKEN_KEY, token || '')
|
||||
}
|
||||
|
||||
export function clearToken() {
|
||||
uni.removeStorageSync(TOKEN_KEY)
|
||||
}
|
||||
|
||||
export function isLoggedIn() {
|
||||
return !!getToken()
|
||||
}
|
||||
|
||||
83
source/clients/uniapp/src/utils/request.js
Normal file
83
source/clients/uniapp/src/utils/request.js
Normal file
@@ -0,0 +1,83 @@
|
||||
import { clearToken, getToken } from './auth'
|
||||
|
||||
const BASE_URL = ''
|
||||
|
||||
function buildUrl(url) {
|
||||
if (!url) return ''
|
||||
if (/^https?:\/\//i.test(url)) return url
|
||||
if (url.startsWith('/')) return `${BASE_URL}${url}`
|
||||
return `${BASE_URL}/${url}`
|
||||
}
|
||||
|
||||
function isLoginError(resData) {
|
||||
const msg = (resData && resData.msg) || ''
|
||||
const url = (resData && resData.url) || ''
|
||||
return msg.includes('请先登录') || msg.includes('登录已过期') || url.includes('/admin/login')
|
||||
}
|
||||
|
||||
export function request(options) {
|
||||
const url = buildUrl(options.url)
|
||||
const method = (options.method || 'GET').toUpperCase()
|
||||
|
||||
const header = {
|
||||
Accept: 'application/json',
|
||||
...(options.header || {}),
|
||||
}
|
||||
|
||||
const token = getToken()
|
||||
if (token && !header.Authorization) {
|
||||
header.Authorization = `Bearer ${token}`
|
||||
}
|
||||
|
||||
if (!header['content-type'] && method !== 'GET') {
|
||||
header['content-type'] = 'application/x-www-form-urlencoded'
|
||||
}
|
||||
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url,
|
||||
method,
|
||||
data: options.data || {},
|
||||
header,
|
||||
timeout: options.timeout || 60000,
|
||||
success: (res) => {
|
||||
const resData = res.data
|
||||
|
||||
if (!res || typeof res !== 'object') {
|
||||
uni.showToast({ title: '请求失败', icon: 'none' })
|
||||
reject(new Error('Invalid response'))
|
||||
return
|
||||
}
|
||||
|
||||
if (res.statusCode && res.statusCode >= 400) {
|
||||
uni.showToast({ title: `请求错误(${res.statusCode})`, icon: 'none' })
|
||||
reject(new Error(`HTTP ${res.statusCode}`))
|
||||
return
|
||||
}
|
||||
|
||||
if (resData && typeof resData === 'object' && 'code' in resData) {
|
||||
if (resData.code === 0) {
|
||||
resolve(resData)
|
||||
return
|
||||
}
|
||||
|
||||
if (isLoginError(resData)) {
|
||||
clearToken()
|
||||
uni.navigateTo({ url: '/pages/login/index' })
|
||||
}
|
||||
|
||||
uni.showToast({ title: resData.msg || '请求失败', icon: 'none' })
|
||||
reject(resData)
|
||||
return
|
||||
}
|
||||
|
||||
resolve(resData)
|
||||
},
|
||||
fail: (err) => {
|
||||
uni.showToast({ title: '网络异常', icon: 'none' })
|
||||
reject(err)
|
||||
},
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user