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.
345 lines
9.5 KiB
345 lines
9.5 KiB
import Config from './config.js'
|
|
import Util from './util.js'
|
|
import siteConfig from './siteConfig.js';
|
|
|
|
// 获取应用类型和名称的函数
|
|
function getAppInfo() {
|
|
let app_type, app_type_name;
|
|
// #ifdef H5
|
|
app_type = 'h5';
|
|
app_type_name = 'H5';
|
|
// #endif
|
|
|
|
// #ifdef MP-WEIXIN
|
|
app_type = 'weapp';
|
|
app_type_name = '微信小程序';
|
|
// #endif
|
|
|
|
// #ifdef APP-PLUS
|
|
app_type = '1';
|
|
app_type_name = 'PDA小程序版';
|
|
// #endif
|
|
return { app_type, app_type_name };
|
|
}
|
|
|
|
// 检查配置是否存在
|
|
function checkConfig(configUrl, message) {
|
|
if (!configUrl) {
|
|
uni.showToast({
|
|
title: message,
|
|
'icon': 'none',
|
|
duration: 10000
|
|
});
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|
|
// 处理请求错误
|
|
function handleRequestError(res, configUrl) {
|
|
uni.hideLoading();
|
|
if (res.errMsg && res.errMsg == 'request:fail url not in domain list') {
|
|
uni.showToast({
|
|
title: configUrl + '不在request 合法域名列表中',
|
|
'icon': 'none',
|
|
duration: 10000
|
|
});
|
|
} else if ((res.errMsg && res.errMsg != "request:ok") || (res.statusCode && [200, 500].indexOf(res.statusCode) == -1)) {
|
|
uni.showToast({
|
|
title: configUrl + '请求失败',
|
|
'icon': 'none',
|
|
duration: 10000
|
|
});
|
|
}
|
|
}
|
|
|
|
// 处理401状态码
|
|
function handle401(role) {
|
|
switch (role) {
|
|
case 'dealer':
|
|
uni.removeStorageSync('token');
|
|
uni.removeStorageSync('dealer_info');
|
|
uni.redirectTo({
|
|
url: "/agentSystem/pages/login"
|
|
});
|
|
break;
|
|
case 'api':
|
|
uni.removeStorageSync('lrusertoken');
|
|
break;
|
|
case 'pda':
|
|
uni.removeStorageSync('pdatoken');
|
|
uni.reLaunch({
|
|
url: "/PDA/pages/login"
|
|
});
|
|
break;
|
|
case 'salesman':
|
|
uni.removeStorageSync('lrsalesmantoken');
|
|
uni.reLaunch({
|
|
url: "/brandSystem/pages/login"
|
|
});
|
|
break;
|
|
case 'engineer':
|
|
uni.removeStorageSync('lrengineertoken');
|
|
uni.reLaunch({
|
|
url: "/pages/staff/login"
|
|
});
|
|
break;
|
|
}
|
|
}
|
|
// 检查网络
|
|
function checkNetwork() {
|
|
uni.getNetworkType({
|
|
success: (res)=> {
|
|
const networkType = res.networkType;
|
|
if (networkType === 'none') {
|
|
console.log('当前无网络连接');
|
|
uni.showToast({
|
|
title: '无网络连接,请检查网络设置',
|
|
icon: 'none',
|
|
duration: 2000
|
|
});
|
|
return false
|
|
} else {
|
|
// console.log(`当前网络类型: ${networkType}`);
|
|
return true
|
|
}
|
|
},
|
|
fail: function (err) {
|
|
console.error('获取网络类型失败:', err);
|
|
}
|
|
});
|
|
}
|
|
|
|
// 公共的发送请求方法
|
|
function commonSendRequest(role, params, baseUrl, tokenKey) {
|
|
if (!checkConfig(baseUrl, '未配置请求域名')) {
|
|
return;
|
|
}
|
|
if(checkNetwork()){
|
|
return;
|
|
}
|
|
const token = uni.getStorageSync(tokenKey) || '';
|
|
// 确保 params.header 已经被初始化
|
|
if (!params.header) {
|
|
params.header = {};
|
|
}
|
|
params.header[`lr-${role == 'api'?'user':role}-token`] = token;
|
|
params.baseUrl = baseUrl + params.url;
|
|
return this.sendRequest(role, params);
|
|
}
|
|
|
|
export default {
|
|
async getUserInfo() {
|
|
const dealer_info = uni.getStorageSync('dealer_info');
|
|
if (dealer_info) {
|
|
// return dealer_info;
|
|
}
|
|
const res = await this.sendDealer({
|
|
url: 'Index/getuser'
|
|
});
|
|
if (res.code === 1) {
|
|
uni.setStorageSync('dealer_info', res.data);
|
|
return res.data;
|
|
} else {
|
|
uni.removeStorageSync('token');
|
|
uni.removeStorageSync('dealer_info');
|
|
uni.reLaunch({
|
|
url: "/pages/login/login"
|
|
});
|
|
}
|
|
},
|
|
|
|
sendDealer(params) {
|
|
return commonSendRequest.call(this, 'dealer', params, Config.baseDealerUrl, 'token');
|
|
},
|
|
|
|
sendPda(params) {
|
|
return commonSendRequest.call(this, 'pda', params, Config.basePdaUrl, 'pdatoken');
|
|
},
|
|
|
|
sendApi(params) {
|
|
return commonSendRequest.call(this, 'api', params, Config.baseApiUrl, 'lrusertoken');
|
|
},
|
|
|
|
sendSalesman(params) {
|
|
return commonSendRequest.call(this, 'salesman', params, Config.baseSalesmanUrl, 'lrsalesmantoken');
|
|
},
|
|
|
|
sendEngineer(params){
|
|
return commonSendRequest.call(this, 'engineer', params, Config.baseEngineerUrl, 'lrengineertoken');
|
|
},
|
|
|
|
sendRequest(role, params) {
|
|
const { app_type, app_type_name } = getAppInfo();
|
|
let isNonLoading = params.isNonLoading ? params.isNonLoading : false;
|
|
if (!isNonLoading) {
|
|
uni.showLoading({
|
|
title: '加载中',
|
|
mask: true
|
|
});
|
|
}
|
|
const method = params.data != undefined ? 'POST' : 'GET';
|
|
const url = params.baseUrl;
|
|
|
|
// #ifdef H5
|
|
const env = process.env.NODE_ENV
|
|
const redirect_url = location.href
|
|
const data = {
|
|
app_type,
|
|
app_type_name,
|
|
env,
|
|
redirect_url
|
|
};
|
|
// #endif
|
|
// #ifndef H5
|
|
const data = {
|
|
app_type,
|
|
app_type_name,
|
|
};
|
|
// #endif
|
|
|
|
let header = params.header;
|
|
if (!header) {
|
|
header = {};
|
|
}
|
|
header['content-type'] = 'application/json';
|
|
params.async = false;
|
|
|
|
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: header,
|
|
dataType: params.dataType || 'json',
|
|
responseType: params.responseType || 'text',
|
|
success: (res) => {
|
|
uni.hideLoading();
|
|
|
|
if (res.data.code == 401) {
|
|
handle401(role);
|
|
// #ifdef H5
|
|
if(role == 'api'){
|
|
let code = this.getUrlCode('code')
|
|
if(code == null){
|
|
window.location.href = res.data.data.url
|
|
return false;
|
|
}
|
|
}
|
|
// #endif
|
|
}
|
|
resolve(res.data);
|
|
},
|
|
fail: (res) => {
|
|
handleRequestError(res, Config.baseUrl);
|
|
reject(res);
|
|
},
|
|
complete: (res) => {
|
|
handleRequestError(res, Config.baseUrl);
|
|
reject(res.data);
|
|
}
|
|
});
|
|
});
|
|
} else {
|
|
uni.request({
|
|
url: url,
|
|
method: method,
|
|
data: data,
|
|
header: header,
|
|
dataType: params.dataType || 'json',
|
|
responseType: params.responseType || 'text',
|
|
success: (res) => {
|
|
uni.hideLoading();
|
|
if (res.data.code == 401) {
|
|
handle401(role);
|
|
// #ifdef H5
|
|
if(role == 'api'){
|
|
let code = this.getUrlCode('code')
|
|
if(code == null){
|
|
window.location.href = res.data.data.url
|
|
return false;
|
|
}
|
|
}
|
|
// #endif
|
|
}
|
|
typeof params.success == 'function' && params.success(res.data);
|
|
},
|
|
fail: (res) => {
|
|
handleRequestError(res, Config.baseUrl);
|
|
typeof params.fail == 'function' && params.fail(res);
|
|
},
|
|
complete: (res) => {
|
|
handleRequestError(res, Config.baseUrl);
|
|
typeof params.complete == 'function' && params.complete(res.data);
|
|
}
|
|
});
|
|
}
|
|
},
|
|
|
|
upload(params) {
|
|
const { app_type, app_type_name } = getAppInfo();
|
|
uni.showLoading({
|
|
title: '上传中...',
|
|
mask: true
|
|
});
|
|
uni.uploadFile({
|
|
url: Config.baseUrl + params.url + '?driver=' + siteConfig.upload.mode,
|
|
filePath: params.filePath,
|
|
name: params.name || 'file',
|
|
fileType: params.fileType || 'image',
|
|
formData: {
|
|
app_type,
|
|
app_type_name,
|
|
},
|
|
header: {
|
|
'lr-dealer-token': uni.getStorageSync('token'),
|
|
'lr-user-token': uni.getStorageSync('lrusertoken')
|
|
},
|
|
success: (res) => {
|
|
uni.hideLoading();
|
|
typeof params.success == 'function' && params.success(JSON.parse(res.data));
|
|
},
|
|
fail: (res) => {
|
|
uni.hideLoading();
|
|
typeof params.fail == 'function' && params.fail(res);
|
|
}
|
|
});
|
|
},
|
|
|
|
getUrlCode(name) {
|
|
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.href) ||[, ''])[1].replace(/\+/g, '%20')) || null
|
|
},
|
|
async h5Login(){
|
|
this.isLogin()
|
|
let code = this.getUrlCode('code')
|
|
this.handleToLogin(code)
|
|
},
|
|
async isLogin(){
|
|
const res = await this.sendApi({
|
|
url:'auth/islogin'
|
|
})
|
|
},
|
|
async handleToLogin(code){
|
|
if(uni.getStorageSync('lrusertoken')=='' && code !=null){
|
|
const res = await this.sendApi({
|
|
url: 'auth/wechatLogin',
|
|
data:{
|
|
code: code
|
|
}
|
|
})
|
|
if(res.code == 1){
|
|
uni.setStorageSync('lrusertoken',res.data.token)
|
|
window.location.reload()
|
|
}else if(res.code == 0){
|
|
uni.showToast({
|
|
title: res.msg,
|
|
icon: 'none'
|
|
})
|
|
}
|
|
}
|
|
},
|
|
|
|
}; |