// pages/stock/index.js const request = require('../../../utils/request') //导入模块 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 - 190, 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 - 190 }) 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 } } // /recycle-service/get/paper-mill-list request.get('/recycle-service/get/all-paper-mill').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 () { 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 // /article/get/article-list 查询文章列表 request.get('/information-center/article/get/Article-list/', 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 request.get('/recycle-service/get/index-info-id').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 }) } } })