const app = getApp() var url = require('url.js') var util = require('md5.js') var isShowFailModal = false var prefix = 'a' var suffix = prefix //封装微信request请求 function connect(obj, flag) { var now = new Date().getTime() signature(obj) wx.request({ url: obj.url, data: obj.data, method: obj.method, header: { 'content-type': 'application/x-www-form-urlencoded', 'My-User-Agent': app.globalData.userAgent }, success: res => { app.log('time : ' + (new Date().getTime() - now) + ' : ' + obj.url) app.log(res.data) if (res.data.code == 200) { obj.success(res) } else if (res.data.code == 601 || res.data.code == 10004) { //KEY失效,重新获取KEY,重跑接口 login(obj, flag) } else { //若返回500错误码 if (flag) { //不作操作,返回给初始函数处理 obj.success(res) } else { //默认操作,弹出错误提示 wx.hideLoading() wx.showToast({ duration: 2000, icon: 'none', title: res.data.msg || '', }) } } }, fail: e => { fail(obj, flag) }, complete: obj.complete }) } function signature(obj){ obj.data = obj.data || {} obj.data.key = app.server.key var time = new Date().getTime() + app.globalData.timeDifference var uri = obj.url var len = uri.indexOf('?') len = len >= 0 ? len : uri.length uri = uri.substring(uri.indexOf('/bnl'), len) var signature = prefix + uri + obj.data.key + time + suffix signature = util.md5(signature) obj.data.mytimestamp = time obj.data.mysignature = signature } function fail(obj, flag) { wx.hideLoading() if (isShowFailModal){ return } isShowFailModal = true wx.showModal({ title: '', content: '网络连接不稳定,请稍后重试', cancelText: '忽略', success: function (res) { isShowFailModal = false if (res.confirm) { connect(obj, flag) } } }) } function login(obj, flag) { wx.login({ success: res => { app.globalData.code = res.code getUserInfo(obj, flag) }, fail: e => { fail(obj, flag) } }) } function getUserInfo(obj, flag) { wx.getUserInfo({ success: res => { app.globalData.userInfo = res.userInfo getKey(obj, res, flag) }, fail: e => { fail(obj, flag) } }) } function getKey(obj, res, flag) { var state = '' if (app.globalData.identify.recomCode) { state = app.globalData.identify.shareIdentify + '#' + app.globalData.identify.recomCode } connect({ url: url.host + url.login, data: { code: app.globalData.code, rawData: res.rawData, signature: res.signature, state: state }, success: res => { if (res.data.code == 200) { var _result = res.data.data var nowTime = res.data.data.nowTime || new Date().getTime() app.globalData.timeDifference = nowTime - new Date().getTime() app.server = _result connect(obj, flag) } else { fail(obj, flag) } } }, true) } function a(p, s){ prefix = p suffix = s } function b(p) { prefix = p } function c(s) { suffix = s } function e(p){ return prefix } function f(s){ return suffix } //微信下单支付流程 function wxpay(obj) { getOrder(obj) } //获取订单接口 function getOrder(obj) { connect({ url: obj.url, data: obj.data, method: 'POST', success: res => { order(obj, res.data.data) } }) } //微信支付统一调用接口 function order(obj, order) { if (app.globalData.debug) { connect({ url: url.host + url.testPay, data: { out_trade_no: order }, success: res => { obj.success(res) } }) return } connect({ url: url.host + url.unified, data: { out_trade_no: order }, method: 'POST', success: res => { pay(obj, order, res.data.data) } }) } //微信支付 function pay(obj, no, order) { wx.requestPayment({ timeStamp: order.timeStamp, nonceStr: order.nonceStr, package: order.package, signType: 'MD5', paySign: order.paySign, success: res => { wx.showLoading({ title: '', }) setTimeout(function () { query(obj, no) }.bind(this), 2000); }, fail: e => { wx.hideLoading() wx.showModal({ title: '', content: '支付失败', showCancel: false }) } }) } //查询返回结果 function query(obj, no, end) { connect({ url: url.host + url.wx_query, method: 'POST', data: { out_trade_no: no }, success: res => { if (res.data.code == 200 && res.data.data == 1) { obj.success(res) } else { if (end) { wx.hideLoading() wx.showModal({ title: '', content: res.data.msg, showCancel: false, success: res => { } }) } else { setTimeout(function () { query(obj, no, true) }.bind(this), 2000); } } } }, false) } module.exports = { a: a, b: b, c: c, e: e, f: f, connect: connect, wxpay: wxpay, }