import Request from './request'
const http = new Request();
http.setConfig((config) => { /* 设置全局配置 */
config.baseUrl = 'https://www.baozusao.com/app'; /* 打包 */
config.header = {
'Content-Type': 'application/json;charset=UTF-8',
"Access-Control-Allow-Origin": "*",
}
return config
})
http.interceptor.request((config, cancel) => { /* 请求之前拦截器 */
return config;
})
http.interceptor.response((response) => { /* 请求之后拦截器 */
// #ifdef MP-WEIXIN
if (response.statusCode == undefined) {
// if (response.statusCode == 401) {
// #endif
// #ifdef H5
if (response.statusCode == 401) {
// #endif
// 登入超时处理
uni.showToast({
title: '身份认证过期,请重新登陆',
icon: 'none'
});
setTimeout(function() {
uni.reLaunch({
url: '../../pages/login/login'
})
}, 500);
}
if (response.statusCode == 200) {
// 请求正常返回
return response.data
} else {
console.log('response.error.message',response.data.error.message)
if (response.data.error.message.indexOf('用户不存在')>-1) {
return {code:401}
} else if(response.data.error.message.indexOf('not cipher or manager or user')>-1){
return {code:401}
} else {
// 异常提醒
uni.showToast({
title: response.data.error.message,
icon: 'none'
});
}
return response.data.error;
}
})
export {
http
};
/**
* Request 0.0.5
* @Class uni-app request网络请求库
* @Author lu-ch
* @Date 2019-06-05
* @Email webwork.s@qq.com
* **/
export default class Request {
config = {
baseUrl: '',
header: {
'Content-Type': 'application/json;charset=UTF-8',
"Access-Control-Allow-Origin":"*",
'cookie':'111'
},
method: 'GET',
dataType: 'json',
responseType: 'text',
success () {
},
fail () {
},
complete () {
}
}
static posUrl (url) { /* 判断url是否为绝对路径 */
return /(http|https):\/\/([\w.]+\/?)\S*/.test(url)
}
interceptor = {
request (f) {
if (f) {
Request.requestBeforeFun = f
}
},
response (f) {
if (f) {
Request.requestComFun = f
}
}
}
static requestBeforeFun (config) {
return config
}
static requestComFun (response) {
return response
}
setConfig (f) {
this.config = f(this.config)
}
request (options = {}) {
options.baseUrl = options.baseUrl || this.config.baseUrl
options.dataType = options.dataType || this.config.dataType
options.url = Request.posUrl(options.url) ? options.url : (options.baseUrl + options.url)
options.data = options.data || {}
options.header = options.header || this.config.header
options.method = options.method || this.config.method
const token = uni.getStorageSync('token');
if(token != undefined && token != '' && token != null) {
options.header['token'] = token
}
return new Promise((resolve, reject) => {
let next = true
let _config = null
options.complete = (response) => {
let statusCode = response.statusCode
response.config = _config
response = Request.requestComFun(response)
if (statusCode === 200) { // 成功
resolve(response)
} else {
reject(response)
}
}
let cancel = (t = 'handle cancel') => {
let err = {
errMsg: t,
config: afC
}
reject(err)
next = false
}
let afC = { ...this.config, ...options }
_config = { ...afC, ...Request.requestBeforeFun(afC, cancel) }
if (!next) return
uni.request(_config)
})
}
get (url, data, options = {}) {
options.url = url
options.data = data
options.method = 'GET'
return this.request(options)
}
post (url, data, options = {}) {
options.url = url
options.data = data
options.method = 'POST'
return this.request(options)
}
del (url, data, options = {}) {
options.url = url
options.data = data
options.method = 'DELETE'
return this.request(options)
}
put (url, data, options = {}) {
options.url = url
options.data = data
options.method = 'PUT'
return this.request(options)
}
}
import Request from './request'
const http = new Request();
网友评论