var app = getApp();
/**
* 公共微信https请求封装
* @param url
* @param type
* @param data
* @param callBack
*/
function https(url, type, data, callBack, header) {
if (!data.isHideLoad) {
wx.showLoading({
title: '加载中',
})
}
wx.showNavigationBarLoading();
wx.request({
url: url,
method: type,
data: data,
header: header ? header : ( {
"Content-Type": "application/json",
"Authorization": "Bearer " + wx.getStorageSync('token')
}),
success: function (res) {
callBack(res.data);
},
fail: function (error) {
showToast("收收请求失败");
},
complete: function (res) {
console.log(res);
if (res.statusCode === 401) {
showToast("收收请求未授权");
}
wx.hideLoading();
wx.stopPullDownRefresh();
wx.hideNavigationBarLoading();
}
})
}
/**
* 接口API授权 type 1.是公共授权 2.登录授权 immediately立刻执行授权
*/
function authorization(type, callback, immediately) {
var timePromise1 = undefined;
var timePromise2 = undefined;
var that = this;
if (type == 1) { //1.是公共授权
var auth1 = function () {
//获取公共接口授权token 公共接口授权token两个小时失效 超过两个小时重新请求
if (!wx.getStorageSync("userid") && (immediately || (!wx.getStorageSync("token") || wx.getStorageSync("token") == "undefined" || ((new Date().getTime() - new Date(wx.getStorageSync("expires_in")).getTime()) / 1000) > 7199))) {
clearInterval(timePromise2);
that.https(app.globalData.api + "/token", "POST", {grant_type: 'client_credentials'},
function (data) {
if (data.access_token) {
wx.setStorageSync('token', data.access_token);//公共接口授权token
wx.setStorageSync('expires_in', new Date());//公共接口授权token 有效时间
}
callback.call(that, data)
}, {
'Authorization': 'Basic MTcwNjE0MDAwMTozNzliYjljNi1kNTYwLTQzMjUtYTQxMi0zMmIyMjRlMjg3NDc=',
'Content-Type': 'application/x-www-form-urlencoded'
}
)
} else { //没有执行授权
callback.call(that)
}
}
auth1();
timePromise1 = setInterval(auth1, 7199000);
} else if (type == 2) { //2.登录授权
var auth2 = function () {
//获取登录接口授权token 登录接口授权token两个小时失效 超过两个小时重新请求
if (wx.getStorageSync("userid") && (immediately || ((new Date().getTime() - new Date(wx.getStorageSync("expires_in")).getTime()) / 1000) > 7199)) {
clearInterval(timePromise1);
that.https(app.globalData.api + "/token", "POST", {
grant_type: 'password',
username: wx.getStorageSync("userid"),
password: wx.getStorageSync("usersecret")
},
function (data) {
if (data.access_token) {
wx.setStorageSync('token', data.access_token);//登录接口授权token
wx.setStorageSync('expires_in', new Date());//登录接口授权token 有效时间
}
callback.call(that, data)
}, {
'Authorization': 'Basic MTcwNjE0MDAwMTozNzliYjljNi1kNTYwLTQzMjUtYTQxMi0zMmIyMjRlMjg3NDc=',
'Content-Type': 'application/x-www-form-urlencoded'
}
)
} else { //没有执行授权
callback.call(that)
}
}
auth2();
timePromise2 = setInterval(auth2, 7199000);
}
}
/**
* 是否登录
*/
function isLogin() {
return wx.getStorageSync("userid") ? true : false;
}
/**
* 是否登录提示
*/
function isLoginModal(isShow) {
if (!wx.getStorageSync("userid")) {
if (isShow) {
wx.showModal({
title: '收收提示',
content: "登录收收,体验更完善功能",
showCancel: true,
confirmColor: "#00ACFF",
confirmText: "登录",
success: function (res) {
if (res.confirm) {
wx.navigateTo({
url: '/pages/account/login'
})
console.log('用户点击确定');
} else if (res.cancel) {
//返回上一页
wx.navigateBack({
delta: 1
})
}
}
})
} else {
wx.navigateTo({
url: '/pages/account/login'
})
}
}
}
/**
* Toast提示框
*/
function showToast(title, icon, duration) {
wx.showToast({
title: title || "",
icon: icon || 'success',
duration: duration || 2000
})
}
/**
* Modal显示模态弹窗
*/
function showModal(title, content, confirmText, cancelText, callback, showCancel) {
var that = this;
wx.showModal({
title: title,
content: content,
confirmText: confirmText,
cancelText: cancelText,
showCancel: showCancel || true,
confirmColor: "#00ACFF",
cancelColor: "#33cd5f",
success: function (res) {
callback.call(that, res)
/* if (res.confirm) {
console.log('用户点击确定')
} else if (res.cancel) {
console.log('用户点击取消')
}*/
}
})
}
/**
* toolTip方法 type 1是提示色 2是警告色
*/
function toolTip(that, msg, type) {
//提示字段值
that.setData(
{
popMsg: msg,
popType: type == 1 ? "tool-tip-message-success" : "tool-tip-message" || "tool-tip-message"
}
);
}
/**
* 调用验证表单方法
*/
function wxValidate(e, that, callback) {
toolTip(that, "")
const params = e.detail.value
/* console.log(params);*/
if (!that.WxValidate.checkForm(e)) {
const error = that.WxValidate.errorList
//提示字段值
toolTip(that, error[0].msg)
/* console.log(error)*/
return false
} else {
callback.call(this)
}
}
/**
* 改变验证码按钮状态
*/
function verifyCodeBtn(e, that) {
if (e.currentTarget.id == 'user' && (/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value))) {
that.setData({
vcdisabled: false
})
} else if (e.currentTarget.id == 'user' && !(/^1(3|4|5|7|8)\d{9}$/.test(e.detail.value))) {
that.setData({
vcdisabled: true
})
}
}
/**
* 获取验证码公共方法
*/
function getVerifyCode(account, that, callback) {
var second = 120,
timePromise = undefined;
timePromise = setInterval(function () {
if (second <= 0) {
clearInterval(timePromise);
that.setData({
paracont: "重发验证码",
vcdisabled: false
})
} else {
that.setData({
paracont: second + "秒后重试",
vcdisabled: true
})
second--;
}
}, 1000, 122);
this.https(app.globalData.api + "/api/util/send_sms_validcode/" + account, "GET", {},
function (data) {
if (data.code == 1001) {
callba