const formatTime = date => { const year = date.getFullYear() const month = date.getMonth() + 1 const day = date.getDate() const hour = date.getHours() const minute = date.getMinutes() const second = date.getSeconds() return `${[year, month, day].map(formatNumber).join('/')} ${[hour, minute, second].map(formatNumber).join(':')}` } const formatNumber = n => { n = n.toString() return n[1] ? n : `0${n}` } var util = {}; function getSessionId(suc,fail) { wx.login({ success: r => { // console.log(r); // 发送 r.code 到后台换取 openId, sessionKey, unionId wx.request({ url: getApp().globalData.url + '/autoLogin', data: { wxCode: r.code }, header: { "Content-Type": "application/x-www-form-urlencoded", }, method: 'POST', success: result => { let resultData = result.data; if (resultData.code===0) { let data = resultData.data; getApp().globalData.token = data.token; getApp().globalData.status = data.status; getApp().globalData.flag = data.flag; getApp().globalData.address = data.address } if(suc && typeof suc === 'function'){ suc() } } }); } }) } function myRequest(res) { let host = getApp().globalData.url; let token = getApp().globalData.token; wx.request({ url: host + res.url, data: res.data ? res.data : {}, header: { "Content-Type": "application/json", "token": token }, method: res.method ? res.method : 'POST', dataType: 'json', success: function(data) { if (data.data.code === 403) { getSessionId(); return } if (data.data.code === 500) { wx.showToast({ title: data.data.msg, icon: 'error', mask: true }) return } if (typeof res.success == "function") { res.success(data.data); } }, fail: function(data) { if (typeof res.doFail == "function") { res.doFail(); } }, complete: function() { if (typeof res.doComplete == "function") { res.doComplete(); } } }); } util.request = myRequest, util.getSessionId = getSessionId module.exports = { formatTime, util }