纸通宝小程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

225 lines
7.2 KiB

import { config, getUserInfo} from "../api/user"
import { zconfig, loginToken, getBaseInfo, getPopupInfo } from "../api/ztb"
import { pconfig } from "../api/payment"
import { mconfig } from "../api/moment"
import { sconfig, getOrderExists } from "../api/saas"
const util = require('../../utils/util')
const event = require('../../utils/event')
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
}
}
event.on('EventMessage', this, this.onEvent)
},
onReady: function () {
wx.showLoading({ title: '获取中', mask: true })
wx.getSystemInfo({
success: 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
},
fetchUserInfo:function(){
config.header = { 'Authorization': 'QNT ' + app.globalData.token }
getUserInfo().then(result => {
this.toIndex()
}).catch(err => {
wx.hideLoading()
})
},
defaultIndex: function(){
this.onResume()
if(this.data.path){
wx.navigateTo({ url: this.data.path })
}
getPopupInfo().then(result => {
if(result.data){
this.setData({ landInfo: result.data })
}
})
},
toIndex: function(){
if(!app.globalData.token){
wx.hideLoading()
this.defaultIndex()
return
}
loginToken({loginToken: app.globalData.token}).then(result => {
zconfig.header = { 'Authorization': 'QNT ' + result.data.token }
pconfig.header = { 'Authorization': 'QNT ' + result.data.token }
mconfig.header = { 'Authorization': 'QNT ' + result.data.token }
// 登录流程完成;
if(!util.isEmpty(result.data.userToken)){
this.handleUserToken(result.data.userToken)
return
}
getBaseInfo().then(result => {
app.globalData.userInfo = result.data
wx.hideLoading()
this.defaultIndex()
}).catch(err => {
wx.hideLoading()
this.defaultIndex()
})
}).catch(err => {
wx.hideLoading()
this.defaultIndex()
})
},
handleUserToken: function(userToken){
sconfig.header = {'user-token': userToken }
getOrderExists().then(result => {
if (Number(result.data) != 1 || !app.release) {
getBaseInfo().then(result => {
app.globalData.userInfo = result.data
wx.hideLoading()
this.defaultIndex()
}).catch(err => {
wx.hideLoading()
this.defaultIndex()
})
} else {
getBaseInfo().then(result => {
app.globalData.userInfo = result.data
wx.hideLoading()
wx.redirectTo({ url: '/pages/paper/index' })
}).catch(err => {
wx.hideLoading()
wx.redirectTo({ url: '/pages/paper/index' })
})
}
}).catch(err => {
wx.hideLoading()
util.showToast(err)
this.defaultIndex()
})
},
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 })
} else if(message.what == 884){
console.log('navigateBack>>')
wx.navigateBack()
}
},
stopTouchMove: function(e){
return false
},
// 弹出框的处理;
onClose: function(){
this.setData({ visible: false, landInfo: null })
},
onImageLoad: function(){
this.setData({ visible: true })
},
onImageClick: function(){
if(this.data.landInfo.redirectInfo.targetView == 'showH5'){
util.navigateTo('/pages/html/html/index?link=' + this.data.landInfo.redirectInfo.params.id + '?title=' + this.data.landInfo.title)
return
}
util.navigateTarget(this.data.landInfo.redirectInfo)
this.setData({ visible: false, landInfo: null })
},
toRule: function(){
if(this.data.landInfo.ruleRedirectInfo.targetView == 'showH5'){
util.navigateTo('/pages/html/html/index?link=' + this.data.landInfo.ruleRedirectInfo.params.id + '?title=' + this.data.landInfo.title)
return
}
util.navigateTarget(this.data.landInfo.ruleRedirectInfo)
},
onUnload: function(){
event.remove('EventMessage', this)
}
})