移除各文件中重复的平台检测代码,统一封装到platform.js模块,添加缓存优化,支持多端平台检测、app_type和显示名称获取等能力,简化后续平台相关代码维护
224 lines
6.0 KiB
JavaScript
224 lines
6.0 KiB
JavaScript
import Config from './config.js'
|
||
import Util from './util.js'
|
||
import store from '@/store/index.js'
|
||
import Platform from './platform.js'
|
||
|
||
const { app_type, app_type_name } = Platform.getPlatformInfo();
|
||
|
||
export default {
|
||
sendRequest(params) {
|
||
if (!Config.baseUrl) {
|
||
uni.showToast({
|
||
title: '未配置请求域名',
|
||
'icon': 'none',
|
||
duration: 10000
|
||
});
|
||
return;
|
||
}
|
||
|
||
var method = params.data != undefined ? 'POST' : 'GET', // 请求方式
|
||
url = (Config.baseUrl + params.url).replace(/(?<!:)\/+/g, '/'), // 请求路径
|
||
data = {
|
||
app_type,
|
||
app_type_name
|
||
};
|
||
|
||
// token
|
||
data.token = store.state.token || '';
|
||
data.uniacid = uni.getStorageSync('uniacid') || Config.uniacid; // 从缓存中获取uniacid,或使用默认uniacid,支持快应用
|
||
|
||
// 门店id
|
||
var default_store_info = store.state.defaultStoreInfo;
|
||
if (default_store_info) {
|
||
data.store_id = default_store_info.store_id;
|
||
}
|
||
|
||
var store_info = store.state.globalStoreInfo;
|
||
|
||
if (store_info) data.store_id = store_info.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 || {
|
||
// '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);
|
||
// } catch (e) {
|
||
// //TODO handle the exception
|
||
// console.log('api error:', 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) {
|
||
store.commit('setToken', res.data.refreshtoken);
|
||
}
|
||
if (res.data.code == -10009 || res.data.code == -10010) {
|
||
store.commit('setToken', '');
|
||
store.commit('setMemberInfo', '');
|
||
}
|
||
resolve(res.data);
|
||
},
|
||
fail: (res) => {
|
||
if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
|
||
uni.showToast({
|
||
title: Config.baseUrl + '不在request 合法域名列表中',
|
||
'icon': 'none',
|
||
duration: 10000
|
||
});
|
||
return;
|
||
}
|
||
reject(res);
|
||
},
|
||
complete: (res) => {
|
||
if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200, 500].indexOf(res.statusCode) == -1)) {
|
||
uni.showToast({
|
||
title: Config.baseUrl + '请求失败',
|
||
'icon': 'none',
|
||
duration: 10000
|
||
});
|
||
return;
|
||
}
|
||
reject(res.data);
|
||
}
|
||
});
|
||
});
|
||
} 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);
|
||
// } catch (e) {
|
||
// //TODO handle the exception
|
||
// console.log('api error:', 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) {
|
||
store.commit('setToken', res.data.refreshtoken);
|
||
}
|
||
if (res.data.code == -10009 || res.data.code == -10010) {
|
||
store.commit('setToken', '');
|
||
store.commit('setMemberInfo', '');
|
||
}
|
||
typeof params.success == 'function' && params.success(res.data);
|
||
},
|
||
fail: (res) => {
|
||
if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
|
||
uni.showToast({
|
||
title: Config.baseUrl + '不在request 合法域名列表中',
|
||
'icon': 'none',
|
||
duration: 10000
|
||
});
|
||
return;
|
||
}
|
||
typeof params.fail == 'function' && params.fail(res);
|
||
},
|
||
complete: (res) => {
|
||
if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200, 500].indexOf(res.statusCode) == -1)) {
|
||
uni.showToast({
|
||
title: Config.baseUrl + '请求失败',
|
||
'icon': 'none',
|
||
duration: 10000
|
||
});
|
||
return;
|
||
}
|
||
typeof params.complete == 'function' && params.complete(res.data);
|
||
}
|
||
});
|
||
}
|
||
},
|
||
uploadBase64(params) {
|
||
uni.request({
|
||
url: Config.baseUrl + '/api/upload/headimgBase64',
|
||
method: 'POST',
|
||
header: {
|
||
'content-type': 'application/x-www-form-urlencoded;application/json'
|
||
},
|
||
data: {
|
||
app_type,
|
||
app_type_name,
|
||
images: params.base64
|
||
},
|
||
dataType: 'json',
|
||
responseType: 'text',
|
||
success: res => {
|
||
typeof params.success == 'function' && params.success(res.data);
|
||
},
|
||
fail: () => {
|
||
typeof params.fail == 'function' && params.fail(res);
|
||
}
|
||
});
|
||
},
|
||
pullImage(params) {
|
||
uni.request({
|
||
url: Config.baseUrl + '/api/upload/headimgPull',
|
||
method: 'POST',
|
||
header: {
|
||
'content-type': 'application/x-www-form-urlencoded;application/json'
|
||
},
|
||
data: {
|
||
app_type,
|
||
app_type_name,
|
||
path: params.path
|
||
},
|
||
dataType: 'json',
|
||
responseType: 'text',
|
||
success: res => {
|
||
typeof params.success == 'function' && params.success(res.data);
|
||
},
|
||
fail: () => {
|
||
typeof params.fail == 'function' && params.fail(res);
|
||
}
|
||
});
|
||
},
|
||
upload(params) {
|
||
uni.uploadFile({
|
||
url: Config.baseUrl + params.url,
|
||
filePath: params.filePath,
|
||
name: params.name || 'file',
|
||
fileType: params.fileType || 'image',
|
||
formData: {
|
||
app_type,
|
||
app_type_name,
|
||
},
|
||
header: {
|
||
'content-type': 'application/x-www-form-urlencoded;application/json'
|
||
},
|
||
success: (res) => {
|
||
typeof params.success == 'function' && params.success(JSON.parse(res.data));
|
||
},
|
||
fail: (res) => {
|
||
typeof params.fail == 'function' && params.fail(res);
|
||
}
|
||
});
|
||
}
|
||
} |