binquan.qiu | 7f2665f | 2020-03-27 17:19:57 +0800 | [diff] [blame] | 1 | //app.js |
| 2 | var server = 'https://yy.dlsmk.cn/payapi/mobileapi'; |
| 3 | |
| 4 | wx.$doCountdown=function(that){ |
| 5 | var inter = setInterval(function () { |
| 6 | that.setData({ |
| 7 | smsFlag: false, |
| 8 | sendColor: '#cccccc', |
| 9 | sendTime: that.data.snsMsgWait + 's后重发', |
| 10 | snsMsgWait: that.data.snsMsgWait - 1 |
| 11 | }); |
| 12 | if (that.data.snsMsgWait < 0) { |
| 13 | clearInterval(inter) |
| 14 | that.setData({ |
| 15 | smsFlag: true, |
| 16 | sendColor: 'rgb(124, 255, 112)', |
| 17 | sendTime: '获取验证码', |
| 18 | snsMsgWait: 30 |
| 19 | |
| 20 | }); |
| 21 | } |
| 22 | }.bind(that), 1000); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | wx.$checkBankcard=function() { |
| 27 | var userid = wx.getStorageSync("userid"); |
| 28 | if (wx.$isEmpty(userid)) { |
| 29 | wx.showModal({ |
| 30 | title: '提示', |
| 31 | content: '使用该功能前请先绑卡', |
| 32 | }) |
| 33 | return false; |
| 34 | } |
| 35 | return true; |
| 36 | } |
| 37 | |
| 38 | wx.$doPost = function(url, param, callback) { |
| 39 | var token = wx.getStorageSync("token") || ''; |
| 40 | var tenantid = wx.getStorageSync("tenantid") || ''; |
| 41 | var auth = 'Bearer ' + token; |
| 42 | var tenantid = tenantid; |
binquan.qiu | 7f2665f | 2020-03-27 17:19:57 +0800 | [diff] [blame] | 43 | wx.request({ |
| 44 | url: server + url, |
| 45 | timeout: 10000, |
| 46 | method: 'POST', |
| 47 | header: { |
| 48 | 'content-type': 'application/x-www-form-urlencoded', |
| 49 | 'Authorization': auth, |
| 50 | 'X-TENANT-ID': tenantid |
| 51 | }, |
| 52 | data: param, |
| 53 | success: function(result) { |
| 54 | console.log(result); |
| 55 | if (callback) { |
| 56 | callback(true, result) |
| 57 | } |
| 58 | }, |
| 59 | fail: function(result) { |
| 60 | console.log(result); |
| 61 | var status=result.status; |
| 62 | |
| 63 | if (status && status == 401) { |
| 64 | wx.redirectTo({ |
| 65 | url: '../wxlogin/wxlogin' |
| 66 | }) |
| 67 | } else { |
| 68 | if (callback) { |
| 69 | callback(false, result) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | }) |
| 74 | } |
| 75 | |
| 76 | wx.$isEmpty = function(str) { |
| 77 | if (!str || str == 'undefined' || str == null || str == '') { |
| 78 | return true; |
| 79 | } |
| 80 | return false; |
| 81 | } |
| 82 | |
| 83 | wx.$getUUID = function(str) { |
| 84 | var d = new Date().getTime(); |
| 85 | if (window.performance && typeof window.performance.now === "function") { |
| 86 | d += performance.now(); //use high-precision timer if available |
| 87 | } |
| 88 | var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) { |
| 89 | var r = (d + Math.random() * 16) % 16 | 0; |
| 90 | d = Math.floor(d / 16); |
| 91 | return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16); |
| 92 | }); |
| 93 | return uuid; |
| 94 | } |
| 95 | |
| 96 | App({ |
| 97 | |
| 98 | onLaunch: function() { |
| 99 | // 展示本地存储能力 |
| 100 | var logs = wx.getStorageSync('logs') || [] |
| 101 | logs.unshift(Date.now()) |
| 102 | wx.setStorageSync('logs', logs) |
| 103 | |
| 104 | // 登录 |
| 105 | wx.login({ |
| 106 | success: res => { |
| 107 | // 发送 res.code 到后台换取 openId, sessionKey, unionId |
| 108 | } |
| 109 | }) |
| 110 | // 获取用户信息 |
| 111 | wx.getSetting({ |
| 112 | success: res => { |
| 113 | if (res.authSetting['scope.userInfo']) { |
| 114 | // 已经授权,可以直接调用 getUserInfo 获取头像昵称,不会弹框 |
| 115 | wx.getUserInfo({ |
| 116 | success: res => { |
| 117 | // 可以将 res 发送给后台解码出 unionId |
| 118 | this.globalData.userInfo = res.userInfo |
| 119 | |
| 120 | // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回 |
| 121 | // 所以此处加入 callback 以防止这种情况 |
| 122 | if (this.userInfoReadyCallback) { |
| 123 | this.userInfoReadyCallback(res) |
| 124 | } |
| 125 | } |
| 126 | }) |
| 127 | } |
| 128 | } |
| 129 | }) |
| 130 | }, |
| 131 | globalData: { |
| 132 | userInfo: null |
| 133 | } |
| 134 | |
| 135 | }) |