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

235 lines
6.4 KiB

// pages/goods/index/index.js
import { paperCategoryList, getGoodProductList, statusProductInfo, deleteProduct } from "../../api/ztb"
const util = require('../../../utils/util')
const event = require('../../../utils/event')
const app = getApp()
const statusList = [1, 2]
Page({
/**
* 页面的初始数据
*/
data: {
height: app.globalData.fragmentHeight,
kg: app.globalData.kg,
loading: true,
requesting: false,
finished: false,
tabList: ['一口价商品', '竞价商品'],
tabIndex: 0,
tabScroll: false,
cateList: [],
cateScroll: false,
tabiList: [],
pageIndex: 0,
top: 0,
orderList: [],
form: {
firstCategoryId: '',
bidType: 1,
pageNum: 1
}
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.setData({
height: app.globalData.fragmentHeight - 180,
kg: app.globalData.kg,
CustomBar: app.globalData.CustomBar
})
event.on('EventMessage', this, this.onEvent)
this.wuxDialog = this.wuxDialog || this.selectComponent('#wux-dialog')
this.fetchCateList()
this.fetchGoodsList()
},
onEvent: function (message) {
console.log('goods>>index>>onEvent', message)
if (message.what == 102) {
this.onRefreshList()
}
},
onRefreshList: function () {
this.setData({
orderList: [],
['form.firstCategoryId']: this.data.form.firstCategoryId,
['form.bidType']: this.data.form.bidType,
['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 })
})
},
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 })
}
getGoodProductList(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)
})
},
lookItem: function (e) {
var item = this.data.orderList[e.currentTarget.dataset.page][e.currentTarget.dataset.index]
// wx.navigateTo({
// url: '/pages/goods/detail/index?id=' + item.id
// })
},
editItem: function (e) {
var item = this.data.orderList[e.currentTarget.dataset.page][e.currentTarget.dataset.index]
wx.navigateTo({ url: '/pages/goods/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 })
statusProductInfo({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 })
deleteProduct({productId: item.id}).then(result => {
wx.hideLoading()
this.onRefreshList()
util.showToast('商品删除成功!')
}).catch(err => {
//异常回调
wx.hideLoading()
util.showToast(err)
})
},
onTabChange: function ({detail}) {
if (this.data.tabIndex == detail.index) {
return
}
this.data.tabIndex = detail.index
this.data.form.bidType = statusList[this.data.tabIndex]
this.data.form.firstCategoryId = ''
this.setData({ pageIndex: 0, ['form.bidType']: statusList[this.data.tabIndex] })
this.onRefreshList()
},
onUnload: function(){
event.remove('EventMessage', this)
}
})