// pages/administ/index.js import Scene from './scene' import { sconfig, getMessageList } from "../../api/saas" import { headerFactoryId } from "../../api/user" const event = require('../../utils/event') const storage = require('../../utils/storage') const app = getApp() Scene({ /** * 页面的初始数据 */ data: { safeBottom: app.globalData.safeBottom, fragment: app.globalData.safeFragmentHeight, userInfo: null, tabList: [ { index: 0, value: 'process', badge: 0, name: '采购', blue: '/assets/tabbar/process-blue.png', gray: '/assets/tabbar/process-gray.png' }, { index: 1, value: 'storage', badge: 0, name: '库存', blue: '/assets/tabbar/storage-blue.png', gray: '/assets/tabbar/storage-gray.png' }, { index: 2, value: 'agent', badge: 0, name: '代卖', blue: '/assets/tabbar/mall-blue.png', gray: '/assets/tabbar/mall-gray.png' }, { index: 3, value: 'home', badge: 0, name: '我的', blue: '/assets/tabbar/home-blue.png', gray: '/assets/tabbar/home-gray.png' } ], tabIndex: 0, zIndex: 9, setInter: null, factoryName: '', items: [], mActiveIndex: 0, popIndex: 0, factoryId: '', arrow: false, visible: false }, /************************************** 初始化流程 ********************************************/ onLoad: function (options) { if (options.url) { var path = options.url if (options.key && options.value) { path += '?' + options.key + '=' + options.value } wx.navigateTo({ url: path }) } this.data.items = [] for (let mIndex = 0; mIndex < app.userInfo.enterpriseInfos.length; mIndex++) { const element = app.userInfo.enterpriseInfos[mIndex] // var enterprise = { text: util.substring(element.enterpriseName, 8) } var enterprise = { text: element.enterpriseName } var children = [] if(mIndex >= 1){ this.data.arrow = true } for (let nIndex = 0; nIndex < element.factoryInfos.length; nIndex++) { const item = element.factoryInfos[nIndex] if(item.factoryId == app.userInfo.factoryId){ this.data.mActiveIndex = mIndex app.userInfo.factoryName = item.factoryName if(element.isEnterprisePrimary){ app.userInfo.type = 'ADMIN' } else { app.userInfo.type = item.type || '' } } if(nIndex >= 1){ this.data.arrow = true } var factory = { id: item.factoryId } if(!item.type && !element.isEnterprisePrimary){ factory.text = item.factoryName + '(申请中)' factory.disabled = true } else { factory.text = item.factoryName } factory.index = nIndex children.push(factory) } enterprise.children = children this.data.items.push(enterprise) } this.data.popIndex = this.data.mActiveIndex this.setData({ userInfo: app.userInfo, items: this.data.items, mActiveIndex: this.data.mActiveIndex, factoryId: app.userInfo.factoryId, factoryName: app.userInfo.factoryName, arrow: this.data.arrow, customBar: app.globalData.CustomBar || (app.globalData.isIos ? 64 : 60), fragment: app.globalData.safeFragmentHeight, safeBottom: app.globalData.safeBottom }) event.on('EventMessage', this, this.onEvent) // this.worker = wx.createWorker('/workers/process.js') this.data.setInter = setInterval(this.fetchMessage, 10000) }, /************************************** 生命周期函数--监听页面初次渲染完成 ********************************************/ onShow: function () { var pageView = this.selectComponent('#' + this.data.tabList[this.data.tabIndex].value) if (pageView) { pageView.onRestart() } }, onChange: function ({detail}) { this.setData({ tabIndex: detail }) this.onShow() }, stopTouchMove: function(e){ return false }, onEvent: function (message) { if (message.what == 888) { this.onUnload() } }, fetchMessage: function(){ // var params = { url: sconfig.baseUrl + '/message-center/message/get/message-list', xAppId: app.xAppId} // params.xFactoryId = app.userInfo.factoryId // params.authorization = app.globalData.authorization // this.worker.postMessage({ type: 1, params }) // this.worker.onMessage(function(res){ // console.log(res) // }) getMessageList({ productType: 'ZTB_FACTORY', status: 0 }).then(result => { if(result.data && result.data.records && result.data.records.length){ let pages = getCurrentPages() //当前页面栈 let nowPage = pages[pages.length - 1] //当前页面 if(nowPage.onNotice){ nowPage.onNotice(result.data.records[0]) } } }) }, /************************************** changeFactory ********************************************/ onPopupChange: function({detail}){ this.setData({zIndex: detail}) }, showFactoryList: function(){ if(!this.data.arrow){ return } this.setData({ visible: !this.data.visible, mActiveIndex: this.data.popIndex }) if (this.data.tabIndex == 0) { var pageView = this.selectComponent('#' + this.data.tabList[this.data.tabIndex].value) pageView.onClose() } }, onHide: function(){ this.setData({ visible: false }) }, onClickNav({ detail }) { this.setData({ mActiveIndex: detail.index || 0 }) }, changeFactory({ detail }) { if(app.userInfo.factoryId == detail.id){ this.setData({ visible: false }) return } wx.showLoading({ title: '正在切换', mask: true }) app.userInfo.factoryId = detail.id storage.put('X-FACTORY-ID' + app.userInfo.userId, detail.id) this.data.popIndex = this.data.mActiveIndex const element = app.userInfo.enterpriseInfos[this.data.mActiveIndex] if(element.isEnterprisePrimary){ app.userInfo.type = 'ADMIN' } else { app.userInfo.type = element.factoryInfos[detail.index].type || '' } headerFactoryId(detail.id) var pageView = this.selectComponent('#' + this.data.tabList[this.data.tabIndex].value) if (pageView && this.data.tabIndex != 3) { pageView.fetchStatisticsInfo(true) } event.emit('EventMessage', { what: 999, desc: 'changeFactory' }) this.setData({ visible: false, factoryId: detail.id, factoryName: detail.text }) }, onUnload: function(){ event.remove('EventMessage', this) if(this.data.setInter) { clearInterval(this.data.setInter) this.data.setInter = null } } })