封装了一些公共的方法 仅供参考
const api = require('./api.config').api;
//页面跳转
function gopage(e) {
let url = e.currentTarget.dataset.url;
wx.navigateTo({
url: url,
})
}
function Ajax(url,type, data, callback) {
wx.request({
url: api[url],
data: data,
header:{
'content-type': 'application/x-www-form-urlencoded'
},
method: type,
success: res => {
if (res.data.code == 1) {
callback(res);
} else {
callback(res);
toast(res.data.msg, false)
}
}
})
}
//支付
function wxPay(url, data, callback) {
var callback = callback
wx.request({
url: api[url],
method: 'POST',
header: {
'content-type': 'application/x-www-form-urlencoded'
},
data: data,
success: function (res) {
console.log(res)
if (res.statusCode == 200) {
wx.requestPayment({
timeStamp: res.data.timeStamp,
nonceStr: res.data.nonceStr,
package: res.data.package,
signType: res.data.signType,
paySign: res.data.paySign,
success(res) {
console.log('成功', res)
toast('支付成功', false);
callback(res)
},
fail(res) {
console.log('失败', res);
toast('支付失败', false);
}
})
} else {
toast(res.data.msg, false)
}
}
})
}
//toast
function toast(res, b) {
wx.showToast({
title: res,
icon: b ? 'success' : 'none'
})
};
function formatMsgTime(dateTimeStamp) {
var result = ''
var minute = 1000 * 60
var hour = minute * 60
var day = hour * 24
var month = day * 30
var now = new Date().getTime()
var diffValue = now - dateTimeStamp
if (diffValue < 0) return
var monthC = diffValue / month
var weekC = diffValue / (7 * day)
var dayC = diffValue / day
var hourC = diffValue / hour
var minC = diffValue / minute
console.log(monthC, weekC, dayC, hourC)
if (monthC >= 1) {
result = "" + parseInt(monthC) + "月前"
}
else if (weekC >= 1) {
result = "" + parseInt(weekC) + "周前"
}
else if (dayC >= 1) {
result = "" + parseInt(dayC) + "天前"
}
else if (hourC >= 1) {
result = "" + parseInt(hourC) + "小时前"
}
else if (minC >= 1) {
result = "" + parseInt(minC) + "分钟前"
} else {
result = "刚刚"
}
return result
};
/* 上传照片 */
function uploadImg(that, tempFilePaths, successImgCount, failImgCount, imgLength, i) {
var that = that;
// console.log(tempFilePaths);
// console.log("上传成功照片:"+successImgCount);
// console.log("上传失败照片"+failImgCount);
// console.log("图片长度"+imgLength);
// console.log("上传第"+i+"张");
wx.uploadFile({
filePath: tempFilePaths[i],
name: 'file',
url: api.upload,
formData: {
file: '',
filetype: 'image',
token: wx.getStorageSync('token')
},
success: function (res) {
console.log(JSON.parse(res.data));
if (res.statusCode == 200) {//图片上传成功
++successImgCount;
that.data.imgUrl.push(JSON.parse(res.data).data.url)
that.setData({
imgUrl: that.data.imgUrl
})
} else {
wx.showModal({
title: '提示',
content: res.data.msg,
showCancel: false
})
}
},
fail: function (res) {
console.log(res);
wx.hideLoading();
++failImgCount;
},
complete: function (res) {
console.log("上传成功照片:" + successImgCount);
console.log("上传失败照片" + failImgCount);
console.log("图片长度" + imgLength);
console.log("上传第" + i + "张");
i++;
if (i == imgLength) {
console.log("结束");
wx.hideLoading();
} else {
uploadImg(that, tempFilePaths, successImgCount, failImgCount, imgLength, i);
}
}
})
}
module.exports = {
bindgopage,
Ajax,
formatMsgTime,
toast,
uploadImg,
wxPay
}