// pages/stock/index.js import { getPreferList } from "../../api/moment" import { getAllFactoryList, getIndexInfoId } from "../../api/ztb" const util = require('../../../utils/util') const storage = require('../../../utils/storage') const event = require('../../../utils/event.js') const app = getApp() Component({ options: { addGlobalClass: true, multipleSlots: true }, /** * 页面的初始数据 */ data: { height: app.globalData.safeFragmentHeight - 90, chartList: null, momentList: [], requesting: false, finished: false, idList: '', form: { pageNum: 1 }, noticeList: [], loopIndex: 0, noticeIndex: 0, loopRequesting: false, content: '', factoryList: [] }, lifetimes: { // 生命周期函数,可以为函数,或一个在methods段中定义的方法名 attached: function () { event.on('EventMessage', this, this.onEvent) }, detached: function () { event.remove('EventMessage', this) } }, methods: { onRestart: function () { if (!this.data.firstShow) { this.setData({ height: app.globalData.safeFragmentHeight - 90 }) this.onRefreshList() } if(!this.data.noticeList.length && !this.data.status){ this.fetchNoticeList() } this.data.firstShow = true }, onEvent: function (message) { if (message.what == 570 || message.what == 571 || message.what == 572 || message.what == 573) { // 帖子发布刷新 this.onRefreshList() } }, //***********************************参与纸厂讨论*****************************************// fetchFactoryList: function(){ if(app.globalData.userInfo){ var recommendedString = storage.get('recommended' + app.globalData.userInfo.userId) if(!util.isEmpty(recommendedString)){ this.setData({ factoryList: JSON.parse(recommendedString) }) return } } getAllFactoryList().then(result => { result.data.sort((a, b)=> b.isRecommended - a.isRecommended) this.setData({ factoryList: result.data.slice(0, 8) }) }) }, toFactoryList: function(){ wx.navigateTo({ url: '/pages/article/factory/index' }) }, toFactory: function(e){ var item = this.data.factoryList[e.currentTarget.dataset.index] util.navigateTo('/pages/agent/factory/index?tabIndex=1&id=' + item.paperMillId) }, toPaperList: function(){ wx.navigateTo({ url: '/pages/moment/paper/index' }) }, //*************************************************momentList************************************************// onRefreshList: function () { if (this.data.requesting) { return } this.setData({ momentList: [], factoryList: [], ['form.pageNum']: 1, finished: false }) this.fetchFactoryList() this.fetchMomentList() }, fetchMomentList: function(){ if (this.data.requesting || this.data.finished) { return } this.data.requesting = true getPreferList(this.data.form).then(result => { if (result.data.records.length) { var respList = result.data.records let nowList = `momentList[${this.data.momentList.length}]` var num = this.data.form.pageNum var finished = this.data.form.pageNum >= result.data.pages this.setData({ [nowList]: respList, ['form.pageNum']: (num + 1), finished, requesting: false }) } else { this.setData({ finished: true, requesting: false }) } }).catch(err => { this.setData({ requesting: false }) }) }, //*************************************************noticeList************************************************// fetchNoticeList: function(){ if (this.data.loopRequesting) { return } this.data.loopRequesting = true getIndexInfoId().then(result => { this.data.loopRequesting = false this.data.noticeList = result.data if(result.data && result.data.length){ this.data.noticeIndex = 0 this.data.loopIndex = 0 this.setData({ content: result.data[0].title}) } }).catch(err => { this.data.loopRequesting = false }) }, onLoop: function(){ if(!this.data.noticeList.length){ this.data.loopIndex++ if(this.data.loopIndex >= 600){ this.fetchNoticeList() } return } this.data.noticeIndex++ if(this.data.noticeIndex >= this.data.noticeList.length){ this.data.loopIndex++ this.data.noticeIndex = 0 } if(this.data.loopIndex >= 60){ this.fetchNoticeList() } this.setData({ content: this.data.noticeList[this.data.noticeIndex].title }) this.notice = this.notice || this.selectComponent('#wux-notice-bar') this.notice.resetAnimation() }, onClick: function(){ wx.navigateTo({ url: '/pages/html/notice/index?id=' + this.data.noticeList[this.data.noticeIndex].id }) } } })