纸通宝小程序
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.
 

92 lines
2.6 KiB

// pages/moment/paper/index.js
import { getCategoryList } from "../../api/moment"
const util = require('../../../utils/util')
const storage = require('../../../utils/storage')
const event = require('../../../utils/event.js')
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
categoryList: [],
cateIds: null,
cateIdList: null
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
wx.showLoading({ title: '加载中', mask: true })
this.data.cateIds = storage.get('cateIdList' + app.globalData.userInfo.userId)
if(!util.isEmpty(this.data.cateIds)){
this.data.cateIdList = this.data.cateIds.toString().split(',')
}
getCategoryList().then(result => {
//成功回调
if(!util.isEmpty(this.data.cateIds)){
for (let index = 0; index < result.data.records.length; index++) {
if(this.checkCategory(result.data.records[index].id)){
result.data.records[index].checked = true
}
}
}
this.setData({categoryList: result.data.records})
wx.hideLoading()
}).catch(err => {
//异常回调
wx.hideLoading()
})
},
checkCategory: function(id){
if(!this.data.cateIdList || !this.data.cateIdList.length){
return false
}
for (let index = 0; index < this.data.cateIdList.length; index++) {
if(id == this.data.cateIdList[index]){
return true
}
}
return false
},
checkPaper: function(e) {
var index = e.target.dataset.index
var item = this.data.categoryList[index]
if(item.checked){
this.setData({ ['categoryList[' + index + '].checked']: !item.checked})
return
}
var sum = 0
for (let i = 0; i < this.data.categoryList.length; i++) {
if(this.data.categoryList[i].checked){
sum++
}
}
if(sum >= 5){
util.showToast('最多只能关注5个纸品')
return
}
this.setData({ ['categoryList[' + index + '].checked']: !item.checked})
},
onUnload: function(){
var cateString = ''
for (let i = 0; i < this.data.categoryList.length; i++) {
if(this.data.categoryList[i].checked){
if(util.isEmpty(cateString)){
cateString = this.data.categoryList[i].id
} else {
cateString += ',' + this.data.categoryList[i].id
}
}
}
if(util.isEmpty(cateString) && util.isEmpty(this.data.cateIds)){
return
}
if(cateString != this.data.cateIds){
storage.put('cateIdList' + app.globalData.userInfo.userId, cateString)
event.emit('EventMessage', { what: 502, desc: 'CateChange' })
}
}
})