90 lines
2.3 KiB
JavaScript
90 lines
2.3 KiB
JavaScript
import Config from './config.js'
|
|
import Util from './util.js'
|
|
|
|
const app_type = 'pc';
|
|
const app_type_name = 'PC';
|
|
|
|
export default {
|
|
sendRequest(params) {
|
|
var method = params.method ?? 'POST', // 请求方式
|
|
url = Config.baseUrl + params.url, // 请求路径
|
|
data = {
|
|
app_type,
|
|
app_type_name
|
|
};
|
|
|
|
// token
|
|
if (uni.getStorageSync('cashier_token')) data.token = uni.getStorageSync('cashier_token');
|
|
// siteid
|
|
if (uni.getStorageSync('site_id')) data.site_id = uni.getStorageSync('site_id');
|
|
//store_id
|
|
if (uni.getStorageSync('store_id')) data.store_id = uni.getStorageSync('store_id');
|
|
// 参数
|
|
if (params.data != undefined) Object.assign(data, params.data);
|
|
|
|
if (params.async === false) {
|
|
//同步
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: url,
|
|
method: method,
|
|
data: data,
|
|
header: params.header || {
|
|
'content-type': 'application/x-www-form-urlencoded;application/json'
|
|
},
|
|
dataType: params.dataType || 'json',
|
|
responseType: params.responseType || 'json',
|
|
success: (res) => {
|
|
if (res.data.code == -10009 || res.data.code == -10010) {
|
|
uni.removeStorage({
|
|
key: 'cashier_token'
|
|
})
|
|
if (Util.getCurrRoute() != 'pages/login/login') {
|
|
Util.redirectTo('/pages/login/login', {}, 'reLaunch');
|
|
return;
|
|
}
|
|
}
|
|
resolve(res.data);
|
|
},
|
|
fail: (res) => {
|
|
reject(res);
|
|
},
|
|
complete: (res) => {
|
|
reject(res);
|
|
}
|
|
});
|
|
}).catch((e) => {});
|
|
} else {
|
|
//异步
|
|
uni.request({
|
|
url: url,
|
|
method: method,
|
|
data: data,
|
|
header: params.header || {
|
|
'content-type': 'application/x-www-form-urlencoded;application/json'
|
|
},
|
|
dataType: params.dataType || 'json',
|
|
responseType: params.responseType || 'text',
|
|
success: (res) => {
|
|
if (res.data.code == -10009 || res.data.code == -10010) {
|
|
uni.removeStorage({
|
|
key: 'cashier_token'
|
|
})
|
|
if (Util.getCurrRoute() != 'pages/login/login') {
|
|
Util.redirectTo('/pages/login/login', {}, 'reLaunch');
|
|
return;
|
|
}
|
|
}
|
|
typeof params.success == 'function' && params.success(res.data);
|
|
},
|
|
fail: (res) => {
|
|
typeof params.fail == 'function' && params.fail(res);
|
|
},
|
|
complete: (res) => {
|
|
typeof params.complete == 'function' && params.complete(res);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|