常熟
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.

294 lines
7.0 KiB

7 months ago
import Vue from 'vue';
import store from '@/store';
// 定义 API URL
7 months ago
const CS_API_URL = 'https://changshu.js-dyyj.com';
6 months ago
const OLD_API_URL = 'https://qyly1.csly-travel.com';
7 months ago
const JDSZ_API_URL = 'https://api.cloud.sz-trip.com';
const NEWAPIURL = process.env.NODE_ENV === 'development' ? CS_API_URL : CS_API_URL;
// 获取token
const getToken = () => {
6 months ago
const userInfoFromStorage = uni.getStorageSync('userInfo');
if (userInfoFromStorage) {
const userInfo = JSON.parse(userInfoFromStorage);
if (userInfo.token) {
return userInfo.token;
}
}
return store.state.user.userInfo.token;
7 months ago
};
// 定义错误处理函数
const handleError = (res, reject) => {
6 months ago
setTimeout(() => {
uni.showToast({
title: res.data?.msg || res.msg,
icon: 'none'
});
reject(res);
}, 0);
if (res.data?.code === 401) {
store.commit('changeLoginPath');
}
7 months ago
};
// 挂载到 Vue 原型上
Vue.prototype.NEWAPIURL = NEWAPIURL;
// #ifdef H5
Vue.prototype.NEWAPIURL = '/api';
// #endif
Vue.prototype.Post = (params = {}, apiurl) => {
6 months ago
const token = getToken();
if (token) {
params.token = token;
}
return new Promise((resolve, reject) => {
uni.showLoading({
title: '加载中'
});
let apiHttp = ''
switch (params.apiType) {
case 'jdsz':
apiHttp = JDSZ_API_URL
// #ifdef H5
apiHttp = Vue.prototype.NEWAPIURL;
// #endif
break;
case 'old':
apiHttp = OLD_API_URL
break;
default:
apiHttp = CS_API_URL
break;
}
uni.request({
method: params.method || 'GET',
url: apiHttp + apiurl,
data: params,
header: {
'content-type': 'application/json',
'token': token || ''
},
success: (res) => {
7 months ago
uni.hideLoading()
6 months ago
if (res.data.code === 200 || res.data.code === 1 || res.statusCode == 200) {
resolve(res.data);
} else {
handleError(res, reject);
}
},
fail: (err) => {
7 months ago
uni.hideLoading()
6 months ago
console.log('err', err);
handleError(err, reject);
}
});
});
7 months ago
};
6 months ago
Vue.prototype.httpPost = (that, murl, mpara, callbs, callbf) => {
// var pages = getCurrentPages();
// if(pages.length>0){
// var page = pages[pages.length - 1].route;
// }
// //此时key应该有值,无值说明用户未给权限
var key = '';
try {
key = uni.getStorageSync('session');
} catch (e) {
}
// console.log('获取key'+key)
mpara.wx = 'wx';
uni.request({
url: OLD_API_URL + murl, //用户信息交换接口
method: 'POST',
data: JSON.stringify(mpara),
header: {
"Content-Type": "text/html;charset=UTF-8",
'Cookie': 'WXSESSIONID=' + 'key'
},
success: function(res) {
if (res.data.status == 200) {
typeof callbs == "function" && callbs(res);
} else if (res.data.status == 2001) {
//未登录状态
// uni.removeStorageSync('session')
// try {
// if (page == 'pages/my/my') {
// typeof callbs == "function" && callbs(res);
// return
// }
// uni.showModal({
// title: '',
// content: '未登录,是否登录',
// showCancel: true,
// cancelText: '否',
// confirmText: '是',
// success: res => {
// if (res.confirm) {
// uni.navigateTo({
// url: '/pages/login/index'
// });
// } else {
// uni.navigateBack({
// delta: 1
// });
// }
// }
// });
// } catch (e) {
// var error = {
// msg: '删除KEY失败,请稍后重试!wx',
// errormsg: e
// }
// typeof callbf == "function" && callbf(error);
// }
} else if (res.data.status == 2002) {
//无用户状态,跳转绑定页面
//uni.redirectTo({ url: "/pages/user/mobile/mobile" });
} else {
var error = {
msg: 'jy出错:' + res.data.message,
errormsg: res
}
typeof callbs == "function" && callbs(res);
}
},
fail: function(e) {
var error = {
msg: '获取数据失败,请稍后重试!jy',
errormsg: e
}
typeof callbf == "function" && callbf(error);
}
});
}
Vue.prototype.httpGetxin = (that, murl, mpara, callbs, callbf) => {
console.log(123)
var key = '';
try {
key = uni.getStorageSync('session');
} catch (e) {
console.log(e);
}
// #ifndef APP-PLUS
mpara.wx = 'wx';
// #endif
uni.request({
url: murl, //用户信息交换接口
method: mpara.method || 'GET',
data: JSON.stringify(mpara),
header: {
"Content-Type": "text/html;charset=UTF-8",
'Cookie': 'WXSESSIONID=' + key
},
success: function(res) {
console.log('res', res)
if (res.data.status == 200) {
typeof callbs == "function" && callbs(res);
} else {
var error = {
msg: 'jy出错:' + res.data.message,
errormsg: res
}
typeof callbs == "function" && callbs(res);
}
},
fail: function(e) {
console.log('err', e)
var error = {
msg: '获取数据失败,请稍后重试!jy',
errormsg: e
}
typeof callbf == "function" && callbf(error);
}
});
}
Vue.prototype.httpPostxin = (that, murl, mpara, callbs, callbf) => {
var pages = getCurrentPages();
if (pages.length > 0) {
var page = pages[pages.length - 1].route;
}
//此时key应该有值,无值说明用户未给权限
var key = '';
try {
key = uni.getStorageSync('session');
} catch (e) {
}
console.log('获取key' + key)
// mpara.wx = 'wx';
// mpara.lng=that.$store.getters.lon==''?0:that.$store.getters.lon;
// mpara.lat=that.$store.getters.lat==''?0:that.$store.getters.lat;
uni.request({
url: murl, //用户信息交换接口
method: 'POST',
data: JSON.stringify(mpara),
header: {
"Content-Type": "application/json;charset=UTF-8"
// 'Cookie': 'WXSESSIONID=' + key
},
success: function(res) {
if (res.data.status == 200) {
typeof callbs == "function" && callbs(res);
} else if (res.data.status == 2001) {
//未登录状态
// uni.removeStorageSync('session')
// try {
// if (page == 'pages/my/my') {
// typeof callbs == "function" && callbs(res);
// return
// }
// uni.showModal({
// title: '',
// content: '未登录,是否登录',
// showCancel: true,
// cancelText: '否',
// confirmText: '是',
// success: res => {
// if (res.confirm) {
// uni.navigateTo({
// url: '/pages/login/index'
// });
// } else {
// uni.navigateBack({
// delta: 1
// });
// }
// }
// });
// } catch (e) {
// var error = {
// msg: '删除KEY失败,请稍后重试!wx',
// errormsg: e
// }
// typeof callbf == "function" && callbf(error);
// }
} else if (res.data.status == 2002) {
//无用户状态,跳转绑定页面
//uni.redirectTo({ url: "/pages/user/mobile/mobile" });
} else {
var error = {
msg: 'jy出错:' + res.data.message,
errormsg: res
}
typeof callbs == "function" && callbs(res);
}
},
fail: function(e) {
var error = {
msg: '获取数据失败,请稍后重试!jy',
errormsg: e
}
typeof callbf == "function" && callbf(error);
}
});
}
export default NEWAPIURL;