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.
419 lines
10 KiB
419 lines
10 KiB
//!!!!!!!!!此处不该这样写,只是临时方案。参照纯函数编程以及ES6的Promise
|
|
|
|
//封装的网络请求方法
|
|
var wxurl = 'https://cs.tour-ma.com/';
|
|
// var wxurl = '';
|
|
// var wxurl = 'https://cs.tour-ma.com/';
|
|
// var wxurl = 'http://120.92.133.72/'
|
|
var wxtitle = '';
|
|
|
|
function loginUser(para, callbs, callbf) {
|
|
uni.login({ //1.请求微信登录
|
|
provider: 'weixin',
|
|
success: function(res) {
|
|
//通过微信code,从服务器请求session
|
|
var url = 'minapps/login.jspx';
|
|
var code = res.code;
|
|
uni.request({
|
|
url: wxurl + url, //拿code请求服务器
|
|
data: {
|
|
code: code,
|
|
},
|
|
success: function(res) {
|
|
if (res.data.status == 200) {
|
|
typeof callbs == "function" && callbs(res); //获取key成功
|
|
//getUser(); //回调微信用户
|
|
} else if (res.data.status == 2002) {
|
|
//无用户状态,跳转绑定页面
|
|
} else {
|
|
var error = {
|
|
msg: '与服务器换取session失败,请稍后重试!jy',
|
|
errormsg: res
|
|
}
|
|
typeof callbf == "function" && callbf(error); //获取key失败
|
|
}
|
|
},
|
|
fail: function(e) {
|
|
var error = {
|
|
msg: '与服务器换取session失败。请稍后重试!jy',
|
|
errormsg: e
|
|
}
|
|
typeof callbf == "function" && callbf(error); //获取key失败
|
|
}
|
|
})
|
|
//通过微信code,从服务器请求session。 end//
|
|
}
|
|
})
|
|
}
|
|
|
|
function httpPostai(that, murl, mpara, callbs, callbf) {
|
|
// #ifdef MP-WEIXIN
|
|
mpara.wx = 'wx';
|
|
// #endif
|
|
uni.request({
|
|
url: murl, //用户信息交换接口
|
|
method: 'POST',
|
|
data: JSON.stringify(mpara),
|
|
header: {
|
|
"Content-Type": "text/html;charset=UTF-8"
|
|
},
|
|
success: function(res) {
|
|
if (res.data.status == 200) {
|
|
typeof callbs == "function" && callbs(res);
|
|
}
|
|
},
|
|
fail: function(e) {
|
|
var error = {
|
|
msg: '获取数据失败,请稍后重试!jy',
|
|
errormsg: e
|
|
}
|
|
typeof callbf == "function" && callbf(error);
|
|
}
|
|
});
|
|
}
|
|
|
|
//post请求,参数分别为:当前页面、url、参数对象、成功回调、失败回调
|
|
function 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';
|
|
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: wxurl + 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);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
//post请求,参数分别为:当前页面、url、参数对象、成功回调、失败回调
|
|
function 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);
|
|
}
|
|
});
|
|
|
|
}
|
|
|
|
//post请求,参数分别为:当前页面、url、参数对象、成功回调、失败回调
|
|
function httpGet(that, murl, mpara, callbs, callbf) {
|
|
var key = '';
|
|
try {
|
|
key = uni.getStorageSync('session');
|
|
} catch (e) {
|
|
console.log(e);
|
|
}
|
|
// #ifndef APP-PLUS
|
|
mpara.wx = 'wx';
|
|
// #endif
|
|
uni.request({
|
|
url: wxurl + 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 {
|
|
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);
|
|
}
|
|
});
|
|
}
|
|
//post请求,参数分别为:当前页面、url、参数对象、成功回调、失败回调
|
|
function httpGetxin(that, murl, mpara, callbs, callbf) {
|
|
var key = '';
|
|
try {
|
|
key = uni.getStorageSync('session');
|
|
} catch (e) {
|
|
console.log(e);
|
|
}
|
|
// #ifndef APP-PLUS
|
|
mpara.wx = 'wx';
|
|
// #endif
|
|
uni.request({
|
|
url: murl, //用户信息交换接口
|
|
method: 'GET',
|
|
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 {
|
|
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);
|
|
}
|
|
});
|
|
}
|
|
function httpGetg(that, murl, mpara, callbs, callbf) {
|
|
var url = wxurl + murl;
|
|
var len = 0;
|
|
for (var key in mpara) {
|
|
if (len == 0) {
|
|
url += '?' + key + '=' + mpara[key];
|
|
} else {
|
|
url += '&' + key + '=' + mpara[key];
|
|
}
|
|
len++
|
|
}
|
|
uni.request({
|
|
url: url, //用户信息交换接口
|
|
method: 'GET',
|
|
header: {
|
|
"Content-Type": "text/html;charset=UTF-8"
|
|
},
|
|
success: function(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) {
|
|
var error = {
|
|
msg: '获取数据失败,请稍后重试!jy',
|
|
errormsg: e
|
|
}
|
|
typeof callbf == "function" && callbf(error);
|
|
}
|
|
});
|
|
}
|
|
|
|
function httpGejqr(that, murl, mpara, callbs, callbf) {
|
|
var url = wxurl + murl;
|
|
uni.request({
|
|
url: url, //用户信息交换接口
|
|
method: 'GET',
|
|
header: {
|
|
"Content-Type": "text/html;charset=UTF-8"
|
|
},
|
|
success: function(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) {
|
|
var error = {
|
|
msg: '获取数据失败,请稍后重试!jy',
|
|
errormsg: e
|
|
}
|
|
typeof callbf == "function" && callbf(error);
|
|
}
|
|
});
|
|
}
|
|
|
|
function getWeek(date) {
|
|
var week;
|
|
if (date.getDay() == 0) week = "周日"
|
|
if (date.getDay() == 1) week = "周一"
|
|
if (date.getDay() == 2) week = "周二"
|
|
if (date.getDay() == 3) week = "周三"
|
|
if (date.getDay() == 4) week = "周四"
|
|
if (date.getDay() == 5) week = "周五"
|
|
if (date.getDay() == 6) week = "周六"
|
|
return week;
|
|
}
|
|
// 计算两地距离
|
|
function distance(la1, lo1, la2, lo2) {
|
|
var La1 = la1 * Math.PI / 180.0;
|
|
var La2 = la2 * Math.PI / 180.0;
|
|
var La3 = La1 - La2;
|
|
var Lb3 = lo1 * Math.PI / 180.0 - lo2 * Math.PI / 180.0;
|
|
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(La3 / 2), 2) + Math.cos(La1) * Math.cos(La2) * Math.pow(Math.sin(
|
|
Lb3 / 2), 2)));
|
|
s = s * 6378.137;
|
|
s = Math.round(s * 10000) / 10000;
|
|
s = s.toFixed(2);
|
|
return s;
|
|
}
|
|
module.exports.httpGetg = httpGetg;
|
|
module.exports.loginUser = loginUser;
|
|
module.exports.httpPost = httpPost;
|
|
module.exports.httpGet = httpGet;
|
|
module.exports.httpPostai = httpPostai;
|
|
module.exports.httpGejqr = httpGejqr;
|
|
module.exports.getWeek = getWeek;
|
|
module.exports.distance=distance;
|
|
module.exports.httpGetxin = httpGetxin;
|
|
module.exports.httpPostxin = httpPostxin;
|