初始化商家端
This commit is contained in:
171
common/api/request.js
Normal file
171
common/api/request.js
Normal file
@ -0,0 +1,171 @@
|
||||
import { navigateToLogin } from '@/common/utils/utils.js';
|
||||
import store from '@/store/index';
|
||||
import { dev_host, mock_host, product_host, needProductUrl } from '../config.js';
|
||||
export default class Request {
|
||||
config = {
|
||||
baseUrl: '',
|
||||
header: {
|
||||
'Content-Type': 'application/json; charset=utf-8',
|
||||
},
|
||||
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) {
|
||||
this.requestBeforeFun = f
|
||||
}
|
||||
},
|
||||
response: (f) => {
|
||||
if (f) {
|
||||
this.requestComFun = f
|
||||
}
|
||||
}
|
||||
}
|
||||
static requestBeforeFun(config) {
|
||||
return config
|
||||
}
|
||||
static requestComFun(response) {
|
||||
return response
|
||||
}
|
||||
|
||||
setConfig(f) {
|
||||
this.config = f(this.config)
|
||||
return this
|
||||
}
|
||||
testNeedProductUrl(url) {
|
||||
return needProductUrl.findIndex(ele => ele == url) != -1;
|
||||
}
|
||||
request(options = {}) {
|
||||
if (this.testNeedProductUrl(options.url)) {
|
||||
options.baseUrl = product_host + '/api';
|
||||
} else {
|
||||
options.baseUrl = options.baseUrl || this.config.baseUrl;
|
||||
}
|
||||
console.log('options.url=', options.baseUrl)
|
||||
options.dataType = options.dataType || this.config.dataType;
|
||||
if (options.url == undefined) return Promise.reject();
|
||||
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;
|
||||
if (options['Content-Type']) options.header['Content-Type'] = options['Content-Type'];
|
||||
return new Promise((resolve, reject) => {
|
||||
let next = true
|
||||
let _config = null
|
||||
options.complete = (response) => {
|
||||
let statusCode = response.statusCode
|
||||
response.config = _config
|
||||
response = this.requestComFun(response)
|
||||
if (statusCode != 200) {
|
||||
let errMessage = '';
|
||||
switch (statusCode) {
|
||||
case 400:
|
||||
errMessage = global.i18n.t('请求错误(400)');
|
||||
break;
|
||||
case 401:
|
||||
errMessage = global.i18n.t('未授权,请重新登录(401)');
|
||||
uni.showToast({
|
||||
title: global.i18n.t('登录失效'),
|
||||
icon: 'none',
|
||||
position: 'bottom',
|
||||
duration: 1500
|
||||
})
|
||||
store.commit('logout');
|
||||
break;
|
||||
case 403:
|
||||
errMessage = global.i18n.t('拒绝访问(403)');
|
||||
break;
|
||||
case 404:
|
||||
errMessage = global.i18n.t('请求出错(404)');
|
||||
break;
|
||||
case 408:
|
||||
errMessage = global.i18n.t('请求超时(408)');
|
||||
break;
|
||||
case 500:
|
||||
errMessage = global.i18n.t('服务器错误(500)');
|
||||
break;
|
||||
case 501:
|
||||
errMessage = global.i18n.t('服务未实现(501)');
|
||||
break;
|
||||
case 502:
|
||||
errMessage = global.i18n.t('网络错误(502)');
|
||||
break;
|
||||
case 503:
|
||||
errMessage = global.i18n.t('服务不可用(503)');
|
||||
break;
|
||||
case 504:
|
||||
errMessage = global.i18n.t('网络超时(504)');
|
||||
break;
|
||||
case 505:
|
||||
errMessage = global.i18n.t('HTTP版本不受支持(505)');
|
||||
break;
|
||||
default:
|
||||
// errMessage = global.i18n.t("服务器错误!");
|
||||
errMessage = '';
|
||||
break;
|
||||
}
|
||||
|
||||
if (statusCode != 401) {
|
||||
if (errMessage) {
|
||||
uni.showToast({
|
||||
title: errMessage,
|
||||
icon: 'none',
|
||||
position: 'bottom',
|
||||
duration: 1500
|
||||
})
|
||||
}
|
||||
uni.$emit('netWorkError', { msg: global.i18n.t('服务器太拥挤了~请您稍后重试') })
|
||||
}
|
||||
reject({ statusCode, errMessage })
|
||||
} else {
|
||||
let _code = response.data.code;
|
||||
if (_code == '-201' || _code == '-202' || _code == '-203') {
|
||||
uni.showToast({
|
||||
title: global.i18n.t('登录失效,请重新登录'),
|
||||
icon: 'none',
|
||||
})
|
||||
store.commit("logout");
|
||||
navigateToLogin();
|
||||
} else {
|
||||
resolve(response)
|
||||
}
|
||||
}
|
||||
uni.stopPullDownRefresh();
|
||||
}
|
||||
let cancel = (t = 'handle cancel') => {
|
||||
let err = {
|
||||
errMsg: t,
|
||||
config: afC
|
||||
}
|
||||
reject(err)
|
||||
next = false
|
||||
}
|
||||
let afC = { ...this.config, ...options }
|
||||
_config = { ...afC, ...this.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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user