// pages/agent/index/index.js import util from "../../../utils/util" import { getFactoryPrice, hasActivity } from "../../../api/ztb" const event = require('../../../utils/event') const app = getApp() Component({ options: { addGlobalClass: true, multipleSlots: true }, properties: { cityList: { type: Array, value: [], observer: function(vals){ if(util.isEmpty(vals) || vals.length <= 0){ return } this.data.cityList = [{ cityId: '', cityName: '全部' }].concat(vals) for (let index = 0; index < this.data.cityList.length; index++) { this.data.tabList.push(this.data.cityList[index].cityName) } this.setData({ tabList: this.data.tabList, scrolled: this.data.cityList.length >= 5 }) } } }, /** * 页面的初始数据 */ data: { height: app.globalData.safeFragmentHeight - 190, CustomBar: app.globalData.CustomBar, kg: app.globalData.kg, loading: true, requesting: false, finished: false, tabList: [], tabIndex: 0, scrolled: false, top: 0, orderList: [], form: { cityId: '', enableSalesAgent: 1, pageNum: 1, pageSize:15, listType: 0 }, triggered: false, firstShow: false, hasActivity: false }, lifetimes: { // 生命周期函数,可以为函数,或一个在methods段中定义的方法名 attached: function () { event.on('EventMessage', this, this.onEvent) if(app.globalData.userInfo){ hasActivity().then(result => { this.setData({ hasActivity: result.data }) }) } }, detached: function () { event.remove('EventMessage', this) } }, methods: { onRestart: function(){ if (!this.data.firstShow) { this.setData({ height: app.globalData.safeFragmentHeight - 190, StatusBar: app.globalData.StatusBar || 40, customHeight: app.globalData.customHeight, tabIndex: 0, kg: app.globalData.kg, firstShow: true }) // this.fetchRegionList() this.fetchPapersList() } this.data.firstShow = true }, onEvent: function (message) { console.log('mall>>index>>onEvent', message) if (message.what == 10 && app.globalData.isVIP == 1) { // this.setData({ token: app.globalData.token }) // this.onRefreshList() } else if(message.what == 1001){ this.setData({ kg: app.globalData.kg }) } else if(message.what == 888){ this.setData({ orderList: [], tabIndex: this.data.tabIndex, ['form.pageNum']: 1, loading: true, finished: false }) this.fetchPapersList() } }, searchList: function(){ util.navigateTo('/pages/mall/search-list/index') }, chooseCity: function(){ this.triggerEvent("picker", {type: 2, cityId: this.data.form.cityId}) }, toAbility: function(){ util.navigateTo('/pages/agent/ability/index') }, onCityChange: function({ detail }){ for (let index = 0; index < this.data.cityList.length; index++) { if(this.data.cityList[index].cityId == detail.cityId){ detail.index = index this.onTabChange({detail}) break } } }, onTabChange: function ({ detail }) { if (this.data.tabIndex == Number(detail.index)) { return } this.data.tabIndex = Number(detail.index) this.data.form.cityId = this.data.cityList[this.data.tabIndex].cityId this.onRefreshList() }, // 下拉刷新... onRefreshList: function () { if (this.data.requesting) { return } this.setData({ orderList: [], tabIndex: this.data.tabIndex, ['form.pageNum']: 1, loading: true, finished: false }) this.fetchPapersList() }, // 获取特价列表 fetchPapersList: function () { if (this.data.requesting || this.data.finished) { return } this.setData({ requesting: true }) getFactoryPrice(this.data.form).then(result => { if (result.data.records.length) { var respList = result.data.records let nowList = `orderList[${this.data.orderList.length}]` var num = result.data.current var finished = result.data.current >= result.data.pages if (this.data.form.pageNum == 1) { this.setData({ [nowList]: respList, total: result.data.total, ['form.pageNum']: (num + 1), top: 0, finished, requesting: false, loading: false }) } else { this.setData({ [nowList]: respList, total: result.data.total, ['form.pageNum']: (num + 1), finished, requesting: false, loading: false }) } } else { this.setData({ requesting: false, finished: true, loading: false, }) } }).catch(err => { //异常回调 this.setData({ requesting: false, finished: true, loading: false, }) }) } } })