App({ onLaunch: function () { let token=my.getStorageSync({'key':'token'}).data let data = Math.round(new Date().getTime()/1000).toString();; let tokenTime=my.getStorageSync({'key':'token_time'}).data let cha=data-tokenTime let data1=60*60*24 if(!token){ this.token() }else if(cha>data1){ my.removeStorageSync('token') this.token() } this.token() // 登录 var that = this; my.getSystemInfo({ success: res => { console.log(res) var system = res.system.trim().split(/\s+/)[1]; // var intNumber = system.substring(0, system.indexOf(".")); // if (res.platform == 'ios' && intNumber < 13 || res.SDKVersion < '2.1.0') { // console.log('低版本') // that.globalData.compatible = true; // my.reLaunch({ // url: '/pages/compatible/index/index', // }) // } // that.globalData.platform = res.platform // that.globalData.version = res.version // console.log(res.safeArea.bottom) // console.log('手机信息res' + res.model) // if (res.model == 'iPhone X' || res.model == 'iPhone XR' || res.model == 'iPhone XS Max') { // that.globalData.screenHeight = 'xSeries' // } else if (res.safeArea.bottom >= 780) { // that.globalData.screenHeight = 'xSeries' // } } }) if (my.getStorageSync({'key':'midTimes'}).data != '') { var time = my.getStorageSync({'key':'midTimes'}).data; var timestamp = Date.parse(new Date()) / 1000; if (time <= timestamp) { console.log('midtimes过期') my.setStorageSync({'key':'midTimes',data:''}) } } var time = my.getStorageSync({'key':'token_time'}).data; var timestamp = Date.parse(new Date()); if (time <= timestamp / 1000) { console.log('token过期') my.removeStorageSync('token_time') } if (my.canIUse('getUpdateManager')) { const updateManager = my.getUpdateManager() updateManager.onCheckForUpdate(function (res) { if (res.hasUpdate) { updateManager.onUpdateReady(function () { my.alert({ title: '更新提示', content: '新版本已经准备好,是否重启应用?', success: function (res) { updateManager.applyUpdate() } }) }) updateManager.onUpdateFailed(function () { my.alert({ title: '已经有新版本了哟~', content: '新版本已经上线啦~,请您删除当前小程序,重新搜索打开哟~' }) }) } }) } else { my.alert({ title: '提示', content: '当前微信版本过低,无法使用该功能,请升级到最新微信版本后重试。' }) } //登录 }, globalData: { statusBarHeight: my.getSystemInfoSync()['statusBarHeight'], windowHeight: my.getSystemInfoSync()['windowHeight'], imgUrl: 'http://resource.bike.hanyiyun.com/', screenHeight: '', login: false, platform: '', version: '', req: true, id: 0, times: 60, invalid: false, compatible: '', title: '禹见你', //全局修改此小程序名称 logoUrl: '/img/dingding.png', //logo图片路径 网络图片/本地图片 dejin.png shanxianLg.png yuzhouLg.png dingding.png appid: '2021002147603404', //小程序的appid wxc5f328a92356183f--遇见你appid merchant_id: '1', url: 'https://api.mtu.ximengnaikang.com/api', //线上域名 // url: 'http://api.yutu.site.ximengnaikang.com/api', //本地域名 mapKey: 'GTLBZ-F6BKO-5EDWH-SDHIX-K2ART-O2FRQ' //地图key值 }, request(api, params, method, req1) { var that = this; if (req1 == false) return; if (req1) { that.globalData.req = false; } if (my.getStorageSync({'key':'token'}).data != '' || my.getStorageSync({'key':'token'}).data != undefined) { if (req1 == true || req1 == undefined) { return new Promise((resolve, reject) => { my.request({ url: this.globalData.url + api, data: params, headers: { 'content-type': 'application/json', 'Accept': 'application/json', 'Cache-Control': 'no-cache', 'Authorization': my.getStorageSync({'key':'token'}).data, 'merchant-id': this.globalData.merchant_id, 'source-type': 'alipay' }, method: method, success: (res) => { resolve(res) // console.log(res) if (res.status == 450) { return; } if (res.status != 200) { if (res.data.message == '订单不存在或订单已结算') { if (this.globalData.compatible) { my.reLaunch({ url: '/pages/compatible/index/index', }) } else { my.reLaunch({ url: '/pages/index/index', }) } return false; } else if (res.data.message == '运动中不能关锁') { return false; } if (res.message != undefined) { my.showToast({ content: res.message, icon: 'none', duration: 3000, mask: true }) } else if (res.data.message != undefined) { my.showToast({ content: res.data.message, icon: 'none', duration: 3000, mask: true }) } } if (res.status == 401) { var timeout; my.removeStorageSync('token') my.removeStorageSync('token_time') // this.token() my.reLaunch({ url: '/pages/index/index', }) } else if (res.status == 404) { my.navigateTo({ url: '/pages/undefind/undefind', }) } }, fail: (err) => { my.showToast({ content: err.data.message || err.data, icon: 'none', mask: true, duration: 3000, }); console.log(err,'qingqiushibai') reject(err,"请求失败") }, complete: () => { that.globalData.req = true } }) }) } } }, token:function(){ let that=this my.getAuthCode({ success: function (res) { my.getSystemInfo({ success(res) { console.log(arr) var arr = [{ label: '品牌', type: 'brand', value: res.brand }, { label: '型号', type: 'model', value: res.model }, { label: '系统平台', type: 'platform', value: res.platform }, { label: '系统版本', type: 'system', value: res.system }, { label: '微信版本', type: 'version', value: res.version }, { label: '小程序版本', type: 'SDKVersion', value: res.SDKVersion }, { label: '定位状态', type: 'locationEnabled', value: res.locationEnabled ? '开' : '关' }, { label: '定位授权', type: 'locationAuthorized', value: res.locationAuthorized ? '已授权' : '未授权' } ] } }) // 发送 res.code 到后台换取 openId, sessionKey, unionId console.log(res.code) var data = { 'code': res.authCode, 'appid': that.globalData.appid, 'type': 1 }; my.request({ url: that.globalData.url + '/auth/weapp-login', data: data, headers: { 'content-type': 'application/json', 'Accept': 'application/json', 'Cache-Control': 'no-cache', 'Authorization': my.getStorageSync({'key':'token'}).data, 'merchant-id': that.globalData.merchant_id }, method:'POST', success:(res)=>{ my.setStorageSync({'key':'token',data:res.data.token}); my.setStorageSync({'key':'session_key',data:res.data.session_key}); my.setStorageSync({'key':'token_time',data:res.data.exp}); my.setStorageSync({'key':'user_ID',data:res.data.user.id}); },err:(err=>{ console.log(err,'登录错误---------------------------------') }) }) } }) } })