You've already forked guangan-mp
1.0.0
This commit is contained in:
118
common/ajax.js
Normal file
118
common/ajax.js
Normal file
@ -0,0 +1,118 @@
|
||||
import ajax from '@/uni_modules/uni-ajax/js_sdk/index.js';
|
||||
import {
|
||||
TaCache
|
||||
} from './cache';
|
||||
import {
|
||||
TaToast
|
||||
} from './toast';
|
||||
import {
|
||||
objectHasKey
|
||||
} from './util';
|
||||
|
||||
// 执行队列
|
||||
let _tasks = []
|
||||
let _status = true
|
||||
|
||||
export const baseUrl = 'https://www.gafzgc.com/'
|
||||
// export const baseUrl = 'http://127.0.0.1:8000/'
|
||||
|
||||
// 创建请求实例,检查登录状态及业务异常,返回 Body 内容( 建议使用 TaPost 处理)
|
||||
const TaHttp = ajax.create({
|
||||
baseURL: baseUrl
|
||||
})
|
||||
|
||||
// 创建请求实例,不检查登录状态及业务异常,返回 Body 内容
|
||||
export const TaAjax = ajax.create({
|
||||
baseURL: baseUrl
|
||||
})
|
||||
|
||||
// 通用 POST 提交,检查登录状态及业务异常,返回 Body 内容,可中断请求
|
||||
export function TaPost(uri, data = {}, load = '', options = {}) {
|
||||
load.length > 0 && TaToast.loading(load)
|
||||
return new Promise((resolve, reject) => {
|
||||
if (_status) {
|
||||
_tasks.push(options.fetcher = new ajax.Fetcher())
|
||||
TaHttp.post(uri, data, options).then((ret) => {
|
||||
ret.code === 1 ? resolve(ret) : reject(ret)
|
||||
}).finally(() => {
|
||||
load.length > 0 && TaToast.loadhide()
|
||||
_tasks.splice(_tasks.indexOf(options.fetcher), 1)
|
||||
})
|
||||
} else {
|
||||
load.length > 0 && TaToast.loadhide()
|
||||
reject({
|
||||
code: 0,
|
||||
info: '请求已被阻止!'
|
||||
})
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// 阻止 POST 请求 ( 20毫秒内阻止请求 )
|
||||
export function TaPostAbort() {
|
||||
_status = false;
|
||||
_tasks.forEach((x, i) => (x.abort(), _tasks.splice(i, 1)))
|
||||
return new Promise(resolve => setTimeout(() => resolve(_status = true), 20))
|
||||
}
|
||||
|
||||
// 添加请求拦截器
|
||||
TaHttp.interceptors.request.use(useRequestInterceptor, error => Promise.reject(error))
|
||||
TaAjax.interceptors.request.use(useRequestInterceptor, error => Promise.reject(error))
|
||||
|
||||
// 添加响应拦截器
|
||||
TaHttp.interceptors.response.use(response => useResponseInterceptor(response, true), error => Promise.reject(error))
|
||||
TaAjax.interceptors.response.use(response => useResponseInterceptor(response, false), error => Promise.reject(error))
|
||||
|
||||
// 定义请求拦截函数
|
||||
function useRequestInterceptor(config) {
|
||||
// 检查是否有 Api-Token 配置,没有则自动追加配置
|
||||
let token = TaCache.get('auth.token')
|
||||
if (token) config.header['Api-Token'] = token
|
||||
// 检查是否有 Content-Type 配置,没有则自动追加配置
|
||||
if (!objectHasKey(config.header, 'content-type')) {
|
||||
config.header['Content-Type'] = 'application/x-www-form-urlencoded'
|
||||
}
|
||||
return config
|
||||
}
|
||||
|
||||
// 定义结果拦截函数
|
||||
function useResponseInterceptor(response, checkAuth) {
|
||||
// 获取返回的数据
|
||||
let body = response.data || {
|
||||
code: 0,
|
||||
info: 'unknow',
|
||||
data: {}
|
||||
}
|
||||
// 网络请求异常处理
|
||||
if (response.statusCode === 404) {
|
||||
TaToast.error(body.info || '资源不存在!')
|
||||
return Promise.reject(body)
|
||||
} else if (response.statusCode === 500) {
|
||||
TaToast.error(body.info || '服务异常!')
|
||||
return Promise.reject(body)
|
||||
} else if (response.statusCode !== 200) {
|
||||
TaToast.error(body.info || '请求异常!' + body.code)
|
||||
return Promise.reject(body)
|
||||
}
|
||||
// 接口返回异常处理
|
||||
if (checkAuth) {
|
||||
if (body.code === 402) {
|
||||
TaPostAbort(), TaToast.error(body.info || '请完善资料!').then(() => uni.navigateTo({
|
||||
url: '/pages/user/bind/bind'
|
||||
}))
|
||||
return Promise.reject(body)
|
||||
} else if ((body.code === 401 || body.code === 403)) {
|
||||
TaPostAbort(), TaToast.error(body.info || '需要登录授权!').then(() => DataUser.logout())
|
||||
return Promise.reject(body)
|
||||
} else if (body.code !== 1) {
|
||||
body.info && TaToast.error(body.info)
|
||||
return Promise.reject(body)
|
||||
}
|
||||
}
|
||||
// 任何请求,如果有返回 Token 都进行保存授权令牌
|
||||
if (body.data && typeof body.data.token === 'string') {
|
||||
TaCache.set('auth.token', body.data.token)
|
||||
}
|
||||
// 返回主体内容
|
||||
return Promise.resolve(body);
|
||||
}
|
Reference in New Issue
Block a user