// pages/myModule/components/myCollect/myAttention.js const $request = require('../../../../utils/request.js'); const app = getApp() Page({ /** * 页面的初始数据 */ data: { pageLoading: false, types: [], selectedTags: [], }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { const eventChannel = this.getOpenerEventChannel() // 监听acceptDataFromOpenerPage事件,获取上一页面通过eventChannel传送到当前页面的数据 eventChannel.on('customerid', data => { console.log(data) this.setData({ customerId: data.customerid, pageLoading: true, }) this.getAllType(); }) }, // 获取tab数据 getAllType() { $request.get('/businessCommunicationType/getAllType.action').then(res => { if (res.status == 0) { let datas = res.data; this.setData({ types: datas, }) this.getAttentionTypeIds(); } }).catch(error => { console.log(error, 'error appletLogin') }) }, // 获取关注类型 getAttentionTypeIds() { let customerId = this.data.customerId; $request.get(`/businessCommunicationCustomer/getAttentionTypeIds.action`, { customerId } ).then(res => { // console.log(res); if (res.status == 0) { let selectedTags = res.data ? res.data.split(',') : []; this.setData({ selectedTags }) this.initSelectedTags(); } this.setData({ pageLoading: false, }) }).catch(error => { console.log(error, 'error appletLogin') }) }, // 选中类型变化 changeTags(e) { let typename = e.currentTarget.dataset.typename; let selectedTags = this.data.selectedTags; // 1 如果选中的数组里没有当前type if (selectedTags.indexOf(typename) == -1) { // 2 如果选中的数组的长度小于3 直接push if (selectedTags.length < 5) { selectedTags.push(typename) } } // 3 如果选中的数组里有它,直接删除 else { selectedTags.forEach((el, inx) => { if (el == typename) { selectedTags.splice(inx, 1); } }) } this.setData({ selectedTags }) this.initSelectedTags(); }, // 初始化选中 initSelectedTags() { let { types, selectedTags } = this.data; types.forEach(el => { el.isActive = false; if (selectedTags.indexOf(el.typeName) != -1) { el.isActive = true; } }) this.setData({ types }) console.log(types, 'types') }, // 设置关注类型 setAttentionTypeIds() { let { customerId, selectedTags } = this.data; wx.showLoading({ title: '数据保存中...', mask: true }) $request.post(`/businessCommunicationCustomer/setAttentionTypeIds.action`, { customerId, type: selectedTags.join(',') } ).then(res => { console.log(res); if (res.status == 0) { const businessCommunicationCustomer = wx.getStorageSync('businessCommunicationCustomer'); if(businessCommunicationCustomer){ businessCommunicationCustomer.attentionTypeIds = selectedTags.join(','); wx.setStorageSync('businessCommunicationCustomer',businessCommunicationCustomer); app.globalData.businessCommunicationCustomer = businessCommunicationCustomer; } wx.showToast({ title: '保存成功', icon: 'success', duration: 2000 }) wx.navigateBack() } wx.hideLoading() }).catch(error => { console.log(error, 'error appletLogin') wx.hideLoading() }) }, })