// pages/moment/paper/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() Page({ /** * 页面的初始数据 */ data: { factoryList: [], recommendedList: null, recommendedString: null, type: 0, idString: null }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if(options.type){ this.data.type = Number(options.type) } wx.showLoading({ title: '加载中', mask: true }) if(this.data.type == 0 && app.globalData.userInfo){ this.data.recommendedString = storage.get('recommended' + app.globalData.userInfo.userId) if(!util.isEmpty(this.data.recommendedString)){ this.data.recommendedList = JSON.parse(this.data.recommendedString) } } if(options.ids){ this.data.idString = options.ids } request.get('/recycle-service/get/all-paper-mill').then(result => { //成功回调 if(!util.isEmpty(this.data.recommendedString)){ var that = this result.data.forEach(element => { if(!element.isRecommended && that.checkCategory(element.paperMillId)){ element.checked = true } }) } if(!util.isEmpty(this.data.idString)){ var that = this result.data.forEach(element => { if(that.data.idString.indexOf(element.paperMillId) >= 0){ element.checked = true } }) } this.setData({ factoryList: result.data, type: this.data.type }) wx.hideLoading() }).catch(err => { //异常回调 wx.hideLoading() }) }, checkCategory: function(id){ if(!this.data.recommendedList || !this.data.recommendedList.length){ return false } for (let index = 0; index < this.data.recommendedList.length; index++) { if(id == this.data.recommendedList[index].paperMillId){ return true } } return false }, checkPaper: function(e) { var index = e.target.dataset.index var item = this.data.factoryList[index] if(this.data.type == 1){ // 纸厂单选 event.emit('EventMessage', { what: 555, obj: item, desc: 'PaperFactoryChoose' }) wx.navigateBack({ delta: 1 }) return } else if(this.data.type == 2){ // 排队纸厂单选 event.emit('EventMessage', { what: 556, obj: item, desc: 'PaperFactoryChoose' }) wx.navigateBack({ delta: 1 }) return } else if(this.data.type == 4){ // 纸厂价格多选 this.setData({ ['factoryList[' + index + '].checked']: !item.checked}) return } if(item.isRecommended){ return } if(item.checked){ this.setData({ ['factoryList[' + index + '].checked']: !item.checked}) return } var sum = 0 for (let i = 0; i < this.data.factoryList.length; i++) { if(this.data.factoryList[i].checked || this.data.factoryList[i].isRecommended){ sum++ } } if(sum >= 8){ util.showToast('最多只能关注8个纸厂') return } this.setData({ ['factoryList[' + index + '].checked']: !item.checked}) }, onUnload: function(){ if(this.data.type == 0){ var fString = '' var fList = [] this.data.factoryList.forEach(element => { if(element.checked || element.isRecommended){ fList.push({paperMillId: element.paperMillId, shortName: element.shortName, logoImg: element.logoImg}) } }) fString = JSON.stringify(fList) if(fList.length <= 4){ return } if(fString != this.data.recommendedString && app.globalData.userInfo){ storage.put('recommended' + app.globalData.userInfo.userId, fString) event.emit('EventMessage', { what: 505, desc: 'RecommendedChange' }) } } else if(this.data.type == 4){ var fIdString = '' this.data.factoryList.forEach(element => { if(element.checked){ if(!util.isEmpty(fIdString)){ fIdString += ',' + element.paperMillId } else { fIdString = element.paperMillId } } }) if(!util.isEmpty(fIdString) && fIdString != this.data.idString){ event.emit('EventMessage', { what: 508, obj: fIdString, desc: 'RecommendedChange' }) } } }, toFactory: function(event){ util.navigateTo('/pages/agent/factory/index?id=' + event.currentTarget.dataset.fid) } })