260720
This commit is contained in:
139
utils/request.js
Normal file
139
utils/request.js
Normal file
@@ -0,0 +1,139 @@
|
||||
export const API_BASE_URL = (() => {
|
||||
if (process.env.NODE_ENV === 'development') {
|
||||
return 'http://localhost:10051/api'
|
||||
}
|
||||
return 'https://tuanwei-api.gxwebsoft.com/api'
|
||||
})()
|
||||
|
||||
export const TOKEN_STORAGE_KEYS = ['access_token', 'Authorization', 'uni_id_token', 'token']
|
||||
export const DEFAULT_TENANT_ID = 10049
|
||||
export const TOKEN_HEADER_NAME = 'Authorization'
|
||||
export const LOGIN_PAGE_URL = '/pages/login/index'
|
||||
|
||||
export function getToken() {
|
||||
for (let index = 0; index < TOKEN_STORAGE_KEYS.length; index += 1) {
|
||||
const value = uni.getStorageSync(TOKEN_STORAGE_KEYS[index])
|
||||
if (value) {
|
||||
return value
|
||||
}
|
||||
}
|
||||
return ''
|
||||
}
|
||||
|
||||
export function hasToken() {
|
||||
return !!getToken()
|
||||
}
|
||||
|
||||
export function clearAuthStorage() {
|
||||
TOKEN_STORAGE_KEYS.forEach((key) => {
|
||||
try {
|
||||
uni.removeStorageSync(key)
|
||||
} catch (error) {}
|
||||
})
|
||||
try {
|
||||
uni.removeStorageSync('user_info')
|
||||
} catch (error) {}
|
||||
}
|
||||
|
||||
export function setAuthInfo(loginResult) {
|
||||
const result = loginResult || {}
|
||||
const token = result.access_token || result.accessToken || result.token || ''
|
||||
const user = result.user || {}
|
||||
if (token) {
|
||||
uni.setStorageSync('access_token', token)
|
||||
uni.setStorageSync('Authorization', token)
|
||||
uni.setStorageSync('token', token)
|
||||
}
|
||||
uni.setStorageSync('user_info', user)
|
||||
return {
|
||||
token,
|
||||
user
|
||||
}
|
||||
}
|
||||
|
||||
export function getStoredUserInfo() {
|
||||
return uni.getStorageSync('user_info') || {}
|
||||
}
|
||||
|
||||
export function redirectToLogin(redirectUrl) {
|
||||
const query = redirectUrl ? `?redirect=${encodeURIComponent(redirectUrl)}` : ''
|
||||
uni.reLaunch({
|
||||
url: `${LOGIN_PAGE_URL}${query}`
|
||||
})
|
||||
}
|
||||
|
||||
export function ensureLoggedIn(options = {}) {
|
||||
const { redirectUrl } = options
|
||||
if (hasToken()) {
|
||||
return true
|
||||
}
|
||||
redirectToLogin(redirectUrl)
|
||||
return false
|
||||
}
|
||||
|
||||
export function request({ url, method = 'GET', data, header = {}, auth = true }) {
|
||||
const token = getToken()
|
||||
return new Promise((resolve, reject) => {
|
||||
uni.request({
|
||||
url: /^https?:\/\//.test(url) ? url : `${API_BASE_URL}${url}`,
|
||||
method,
|
||||
data,
|
||||
header: {
|
||||
'Content-Type': 'application/json',
|
||||
...(auth && token ? { [TOKEN_HEADER_NAME]: token } : {}),
|
||||
...header
|
||||
},
|
||||
success: (response) => {
|
||||
const responseHeader = (response && response.header) || {}
|
||||
const nextToken =
|
||||
responseHeader[TOKEN_HEADER_NAME] ||
|
||||
responseHeader[TOKEN_HEADER_NAME.toLowerCase()]
|
||||
if (nextToken) {
|
||||
uni.setStorageSync('access_token', nextToken)
|
||||
uni.setStorageSync('Authorization', nextToken)
|
||||
uni.setStorageSync('token', nextToken)
|
||||
}
|
||||
const body = (response && response.data) || {}
|
||||
const code = Number(body.code)
|
||||
if (code === 0 || code === 200) {
|
||||
resolve(body.data !== undefined ? body.data : body)
|
||||
return
|
||||
}
|
||||
if (code === 401) {
|
||||
clearAuthStorage()
|
||||
}
|
||||
reject(new Error(body.message || '请求失败'))
|
||||
},
|
||||
fail: (error) => {
|
||||
reject(new Error((error && error.errMsg) || '网络请求失败'))
|
||||
}
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
export function get(url, data, options = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'GET',
|
||||
data,
|
||||
...options
|
||||
})
|
||||
}
|
||||
|
||||
export function post(url, data, options = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'POST',
|
||||
data,
|
||||
...options
|
||||
})
|
||||
}
|
||||
|
||||
export function put(url, data, options = {}) {
|
||||
return request({
|
||||
url,
|
||||
method: 'PUT',
|
||||
data,
|
||||
...options
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user