import { paperCategoryList, getStorePurchaseList, deletePurchase, updatePurchaseStatus } from "../../../../api/ztb" const util = require('../../../../utils/util') const event = require('../../../../utils/event') const app = getApp() Page({ /** * 页面的初始数据 */ data: { height: app.globalData.fragmentHeight, kg: app.globalData.kg, loading: true, requesting: false, finished: false, cateList: [], cateScroll: false, tabiList: [], pageIndex: 0, top: 0, orderList: [], form: { firstCategoryId: '', pageNum: 1, pageSize:15 } }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { event.on('EventMessage', this, this.onEvent) this.wuxDialog = this.wuxDialog || this.selectComponent('#wux-dialog') if(options.storeId){ this.data.form['storeId'] = options.storeId } this.fetchCateList() this.fetchGoodsList() }, onEvent: function (message) { if (message.what == 412) { this.onRefreshList() } }, onRefreshList: function () { this.setData({ orderList: [], ['form.firstCategoryId']: this.data.form.firstCategoryId, ['form.pageNum']: 1, loading: true, finished: false }) this.fetchGoodsList() }, fetchCateList: function(){ paperCategoryList({ type: 1 }).then(result => { var tabList = ['全部'] var size = 0 for (let index = 0; index < result.data.length; index++) { tabList.push(result.data[index].name) size += result.data[index].name.length } this.data.cateList = result.data this.setData({ tabiList: tabList, cateScroll: size > 14, height: app.globalData.fragmentHeight - 90, kg: app.globalData.kg, CustomBar: app.globalData.CustomBar }) }) }, onCategoryChange: function ({detail}) { if(this.data.pageIndex == Number(detail.index)){ return } this.data.pageIndex = Number(detail.index) if(this.data.pageIndex == 0){ this.data.form.firstCategoryId = '' } else { this.data.form.firstCategoryId = this.data.cateList[this.data.pageIndex - 1].id } this.onRefreshList() }, fetchGoodsList: function () { if (this.data.requesting || this.data.finished) { return } if (this.data.loading) { this.data.requesting = true } else { this.setData({ requesting: true }) } getStorePurchaseList(this.data.form).then(result => { if (result.data.records.length) { var respList = result.data.records let nowList = `orderList[${this.data.orderList.length}]` var num = this.data.form.pageNum var finished = this.data.form.pageNum >= 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({ finished: true, requesting: false, loading: false }) } }).catch(err => { //异常回调 this.setData({ requesting: false, loading: false }) util.showToast(err) }) }, editItem: function (e) { var item = this.data.orderList[e.currentTarget.dataset.page][e.currentTarget.dataset.index] wx.navigateTo({ url: '/submodel/pages/purchase/create/index?id=' + item.id }) }, shelvesItem: function (e) { var page = e.currentTarget.dataset.page var index = e.currentTarget.dataset.index var item = this.data.orderList[page][index] var that = this if(item.status == 1){ this.wuxDialog.open({ resetOnClose: true, title: '温馨提示', content: '确定下架该商品?', buttons: [{ text: '取消' }, { text: '确定', type: 'primary', onTap(e) { that.statusItem(item, page, index, 2) } }] }) } else { that.statusItem(item, page, index, 1) } }, statusItem: function(item, page, index, status){ wx.showLoading({ title: '处理中', mask: true }) updatePurchaseStatus({productId: item.id, status}).then(result => { this.setData({ ['orderList[' + page + '][' + index + '].status']: status }) wx.hideLoading() if(status == 2){ util.showToast('采购下架成功!') } else { util.showToast('采购上架成功!') } }).catch(err => { //异常回调 wx.hideLoading() util.showToast(err) }) }, removeItem: function (e) { var item = this.data.orderList[e.currentTarget.dataset.page][e.currentTarget.dataset.index] var that = this this.wuxDialog.open({ resetOnClose: true, title: '温馨提示', content: '确定删除该采购信息?', buttons: [{ text: '取消' }, { text: '确定', type: 'primary', onTap(e) { that.deleteItem(item) } }] }) }, deleteItem: function(item){ wx.showLoading({ title: '处理中', mask: true }) deletePurchase({productId: item.id}).then(result => { wx.hideLoading() this.onRefreshList() util.showToast('商品删除成功!') }).catch(err => { wx.hideLoading() util.showToast(err) }) }, onTabChange: function (e) { if (this.data.form.bidType == e.detail.key) { return } this.data.form.bidType = e.detail.key this.data.form.firstCategoryId = '' this.onRefreshList() }, onUnload: function(){ event.remove('EventMessage', this) } })