// pages/setting/paper-detial/index.js import Scene from '../../index/scene' import Dialog from '../../../components/dialog/dialog' import { getPaperInfo, getCategoryList, savePaperPrice, editPaperPrice, delePaperPrice } from "../../../api/saas" const event = require('../../../utils/event') const util = require('../../../utils/util') Scene({ /** * 页面的初始数据 */ data: { paperList: null, form: {}, columns: [], visible: false, }, /** * 生命周期函数--监听页面加载 */ onLoad: function (options) { if(options.id){ wx.showLoading({ title: '正在获取', mask: true }) getCategoryList().then(result => { this.data.paperList = result.data this.data.columns = [] for (let index = 0; index < this.data.paperList.length; index++) { this.data.columns.push(this.data.paperList[index].categoryName) } this.setData({ columns: this.data.columns }) }).catch(err => { util.showToast(err) }) getPaperInfo(options.id).then(result => { wx.hideLoading() this.setData({ form: result.data }) }).catch(err => { wx.hideLoading() util.showToast(err) }) } else { wx.showLoading({ title: '正在获取', mask: true }) getCategoryList().then(result => { this.data.paperList = result.data this.data.columns = [] for (let index = 0; index < this.data.paperList.length; index++) { this.data.columns.push(this.data.paperList[index].categoryName) } this.setData({ columns: this.data.columns }) wx.hideLoading() }).catch(err => { wx.hideLoading() util.showToast(err) }) } }, chooseMethod: function(e){ this.setData({ visible: true }) }, onConfirm: function({detail}){ var nowItem = this.data.paperList[detail.index] this.data.form.categoryId = nowItem.categoryId this.data.form.categoryName = nowItem.categoryName this.setData({ visible: false, ['form.productName']: detail.value, ['form.categoryName']: detail.value }) }, onHide: function(){ this.setData({ visible: false }) }, bindInput: function(e){ this.data.form[e.currentTarget.id] = e.detail }, deleteForm: function(e){ Dialog.confirm({ title: '温馨提示', message: '确定删除该纸品?' }).then(() => { wx.showLoading({ title: '正在处理', mask: true }) delePaperPrice(this.data.form.id).then(result => { wx.hideLoading() util.showBackToast('删除成功') event.emit('OrderMessage', { what: 201 }) }).catch(err => { wx.hideLoading() util.showToast(err) }) }).catch(err => { }) }, submitForm: function(e){ if(util.isEmpty(this.data.form.categoryId)){ util.showToast('请选择一个纸品名称') return } if(util.isEmpty(this.data.form.defaultUnitPrice) || Number(this.data.form.defaultUnitPrice) <= 0){ util.showToast('请输入纸品价格') return } wx.showLoading({ title: '正在处理', mask: true }) if(util.isEmpty(this.data.form.id)){ savePaperPrice(this.data.form).then(result => { wx.hideLoading() util.showBackToast('添加成功') event.emit('OrderMessage', { what: 201 }) }).catch(err => { wx.hideLoading() util.showToast(err) }) } else { editPaperPrice(this.data.form).then(result => { wx.hideLoading() util.showBackToast('修改成功') event.emit('OrderMessage', { what: 202, detail: this.data.form }) }).catch(err => { wx.hideLoading() util.showToast(err) }) } } })