import { config, getUserInfo} from "../api/user" import { zconfig, loginToken } from "../api/ztb" import { mconfig } from "../api/moment" const event = require('../../utils/event.js') const storage = require('../../utils/storage') const app = getApp() Page({ data: { TabList: [ { index: 0, value: 'article', badge: 0, name: '情报' }, { index: 1, value: 'mall', badge: 0, name: '交易' }, { index: 2, value: 'home', badge: 0, name: '我的' } ], pageIndex: 1, safeBottom: app.globalData.safeBottom, inited: false, path: null, landInfo: null, visible: false }, /************************************** 初始化流程 ********************************************/ onLoad: function (options) { const updateManager = wx.getUpdateManager() updateManager.onCheckForUpdate(function (res) { // 请求完新版本信息的回调 // console.log('updateManager>>>' + res.hasUpdate) }) updateManager.onUpdateReady(function () { // 新的版本已经下载好,调用 applyUpdate 应用新版本并重启 updateManager.applyUpdate() }) if(options.url){ this.data.path = options.url if(options.key && options.value){ this.data.path += '?' + options.key + '=' + options.value } } }, onReady: function () { wx.showLoading({ title: '获取中', mask: true }) wx.getSystemInfo({ success: e => { app.globalData.isIPhoneX = this.checkIPhoneX(e) app.globalData.isIos = this.checkIos(e) app.globalData.dev = e.platform == 'devtools' let custom = wx.getMenuButtonBoundingClientRect() app.globalData.Custom = custom // 顶部操作栏高度 app.globalData.CustomBar = custom.bottom + custom.top - e.statusBarHeight + (app.globalData.isIos ? 4 : 0) let windowHeight = e.windowHeight * (750 / e.windowWidth) let safeBottom = e.windowHeight - e.safeArea.bottom if(safeBottom > e.windowHeight){ safeBottom = 34 } app.globalData.safeBottom = safeBottom * (750 / e.windowWidth) // 状态栏高度 app.globalData.StatusBar = e.statusBarHeight * (750 / e.windowWidth) let statusBarHeight = app.globalData.CustomBar * (750 / e.windowWidth) app.globalData.statusBarHeight = statusBarHeight // fragmentHeight:指的是整个页面statusBarHeight以下的高度 app.globalData.fragmentHeight = windowHeight - statusBarHeight app.globalData.safeFragmentHeight = windowHeight - statusBarHeight - app.globalData.safeBottom app.globalData.windowWidth = e.windowWidth this.data.inited = true const fheght = (app.globalData.safeFragmentHeight + app.globalData.statusBarHeight) - 100 event.emit('InitMessage', { what: 8, desc: 'Logined' }) this.setData({fragment: fheght, safeBottom: app.globalData.safeBottom }) var authorization = app.globalData.token || storage.get('Authorization') if(authorization){ app.globalData.token = authorization this.fetchUserInfo() } else { this.toIndex() } } }) }, checkIos: function (e) { if ('ios' === e.platform) { return true } if (e.system.startsWith('iOS')) { return true } return false }, checkIPhoneX: function (e) { var t = e.model return (/iPhone 11/.test(t) || /iPhone X/.test(t) || /iPhone 12/.test(t)) && this.checkIos(e) }, fetchUserInfo:function(){ config.header = { 'Authorization': 'QNT ' + app.globalData.token } getUserInfo().then(result => { app.globalData.userInfo = result.data this.toIndex() }).catch(err => { wx.hideLoading() }) }, toIndex: function(){ if(!app.globalData.token){ wx.hideLoading() this.onResume() if(this.data.path){ wx.navigateTo({ url: this.data.path }) } return } loginToken({loginToken: app.globalData.token}).then(result => { zconfig.header = { 'Authorization': 'QNT ' + result.data.token } mconfig.header = { 'Authorization': 'QNT ' + result.data.token } // pconfig.header = { 'Authorization': 'QNT ' + result.data } wx.hideLoading() this.onResume() if(this.data.path){ wx.navigateTo({ url: this.data.path }) } // event.emit('EventMessage', { what: 888, desc: 'Logined' }) }).catch(err => { console.log(err) wx.hideLoading() this.onResume() if(this.data.path){ wx.navigateTo({ url: this.data.path }) } }) }, onResume: function(){ var pageView = this.selectComponent('#' + this.data.TabList[this.data.pageIndex].value) if (pageView && this.data.inited) { pageView.onRestart() } }, //事件处理函数 onNavChange: function(e) { // 处理用户的登录校验 this.setData({ pageIndex: Number(e.currentTarget.dataset.tab) }) this.onResume() }, /** * 生命周期函数--监听页面显示 */ onShow: function () { if(this.data.inited){ this.onResume() } }, onEvent: function (message) { if(message.what == 250){ this.setData({ pageIndex: 0 }) } }, stopTouchMove: function(e){ return false }, // 弹出框的处理; onClose: function(){ this.setData({ visible: false, landInfo: null }) }, onImageLoad: function(){ this.setData({ visible: true }) }, onImageClick: function(){ console.log(this.data.landInfo) this.setData({ visible: false, landInfo: null }) }, toRule: function(){ console.log('toRule>>>') } })