You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
188 lines
4.6 KiB
188 lines
4.6 KiB
import Config from './config.js'
|
|
import Util from './util.js'
|
|
import store from '@/store/index.js'
|
|
import { showLoading, hideLoading } from "./prompt-ui.js"
|
|
|
|
// #ifdef H5
|
|
const app_type = Util.isWeiXin() ? 'wechat' : 'h5';
|
|
const app_type_name = Util.isWeiXin() ? '微信公众号' : 'H5';
|
|
// #endif
|
|
|
|
// #ifdef MP-WEIXIN
|
|
const app_type = 'wechat';
|
|
const app_type_name = 'WECHAT';
|
|
// #endif
|
|
|
|
// #ifdef MP-ALIPAY
|
|
const app_type = 'aliapp';
|
|
const app_type_name = '支付宝小程序';
|
|
// #endif
|
|
|
|
// #ifdef MP-BAIDU
|
|
const app_type = 'baiduapp';
|
|
const app_type_name = '百度小程序';
|
|
// #endif
|
|
|
|
// #ifdef MP-TOUTIAO
|
|
const app_type = 'MP-TOUTIAO';
|
|
const app_type_name = '头条小程序';
|
|
// #endif
|
|
|
|
// #ifdef MP-QQ
|
|
const app_type = 'MP-QQ';
|
|
const app_type_name = 'QQ小程序';
|
|
// #endif
|
|
|
|
// #ifdef APP-PLUS
|
|
const app_type = 'weapp';
|
|
const app_type_name = 'WEAPP';
|
|
// #endif
|
|
|
|
|
|
const createLog = (url, req, res) => {
|
|
console.log(
|
|
`%c ${url}`,
|
|
'padding: 2px 4px; border-radius: 3px 0 0 3px; color: #fff; background: #b23ff7; font-weight: bold;',
|
|
req,
|
|
res
|
|
)
|
|
}
|
|
|
|
export default {
|
|
sendRequest(params) {
|
|
var method = params.data != undefined ? 'POST' : 'GET', // 请求方式
|
|
url = Config.baseUrl + params.url, // 请求路径
|
|
data = {
|
|
app_type,
|
|
app_type_name
|
|
};
|
|
|
|
// token
|
|
if (uni.getStorageSync('token')) data.token = uni.getStorageSync('token');
|
|
// 店铺id
|
|
if (uni.getStorageSync('myStore')) data.store_id = uni.getStorageSync('myStore').id;
|
|
// 参数
|
|
if (params.data != undefined) Object.assign(data, params.data);
|
|
|
|
showLoading();
|
|
if (params.async === false) {
|
|
//同步
|
|
return new Promise((resolve, reject) => {
|
|
uni.request({
|
|
url: url,
|
|
method: method,
|
|
data: data,
|
|
header: params.header || {
|
|
'Accept': 'application/json',
|
|
'content-type': 'application/x-www-form-urlencoded;application/json'
|
|
},
|
|
dataType: params.dataType || 'json',
|
|
responseType: params.responseType || 'text',
|
|
success: (res) => {
|
|
try {
|
|
res.data = JSON.parse(res.data);
|
|
createLog(params.url, data, res.data)
|
|
} catch (e) {
|
|
|
|
}
|
|
if (res.data.code == -3 && store.state.siteState > 0) {
|
|
store.commit('setSiteState', -3)
|
|
Util.redirectTo('/pages_tool/storeclose/storeclose', {}, 'reLaunch');
|
|
return;
|
|
}
|
|
if (res.data.refreshtoken) {
|
|
uni.setStorage({
|
|
key: 'token',
|
|
data: res.data.refreshtoken
|
|
});
|
|
}
|
|
if (res.data.code == -10009 || res.data.code == -10010) {
|
|
uni.removeStorage({
|
|
key: 'token'
|
|
})
|
|
}
|
|
resolve(res.data);
|
|
},
|
|
fail: (res) => {
|
|
reject(res);
|
|
},
|
|
complete: (res) => {
|
|
hideLoading();
|
|
reject(res);
|
|
}
|
|
});
|
|
});
|
|
} else {
|
|
//异步
|
|
uni.request({
|
|
url: url,
|
|
method: method,
|
|
data: data,
|
|
header: params.header || {
|
|
'Accept': 'application/json',
|
|
'content-type': 'application/x-www-form-urlencoded;application/json'
|
|
},
|
|
dataType: params.dataType || 'json',
|
|
responseType: params.responseType || 'text',
|
|
success: (res) => {
|
|
try {
|
|
res.data = JSON.parse(res.data);
|
|
createLog(params.url, data, res.data)
|
|
} catch (e) {
|
|
//TODO handle the exception
|
|
// console.log('api error:', e);
|
|
}
|
|
|
|
if (res.data.code != 0) {
|
|
console.log('-------', url)
|
|
}
|
|
if (res.data.code == -3 && store.state.siteState > 0) {
|
|
store.commit('setSiteState', -3)
|
|
Util.redirectTo('/pages_tool/storeclose/storeclose', {}, 'reLaunch');
|
|
return;
|
|
}
|
|
if (res.data.refreshtoken) {
|
|
uni.setStorage({
|
|
key: 'token',
|
|
data: res.data.refreshtoken
|
|
});
|
|
}
|
|
if (res.data.code == -10009 || res.data.code == -10010) {
|
|
uni.removeStorage({
|
|
key: 'token'
|
|
})
|
|
}
|
|
if (res.data.code == 8 || res.data.code == 1) {
|
|
|
|
return
|
|
}
|
|
if (res.data.code == -1 && res.data.message == "您尚未登录,请先进行登录") {
|
|
uni.showToast({
|
|
title: res.data.message || '您尚未登录,请先进行登录',
|
|
icon: "none",
|
|
})
|
|
setTimeout(() => {
|
|
Util.redirectTo('/pages_tool/login/login');
|
|
}, 1000)
|
|
}
|
|
if (res.data.code != 0 || res.data.code == -1) {
|
|
uni.showToast({
|
|
title: res.data.message || '系统错误',
|
|
icon: "none",
|
|
})
|
|
return
|
|
}
|
|
|
|
typeof params.success == 'function' && params.success(res.data);
|
|
},
|
|
fail: (res) => {
|
|
typeof params.fail == 'function' && params.fail(res);
|
|
},
|
|
complete: (res) => {
|
|
hideLoading();
|
|
typeof params.complete == 'function' && params.complete(res);
|
|
}
|
|
});
|
|
}
|
|
}
|
|
}
|