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

241 lines
7.5 KiB

// pages/goods/create/index.js
import { updatePurchase, createPurchase, paperCategoryList, getPurchaseInfo } from "../../../api/ztb"
import regions from '../../../utils/region'
const math = require('../../../utils/math')
const util = require('../../../utils/util')
const event = require('../../../utils/event.js')
const app = getApp()
Page({
/**
* 页面的初始数据
*/
data: {
safeBottom: app.globalData.safeBottom,
height: app.globalData.safeFragmentHeight,
kg: app.globalData.kg,
options: regions,
region: [440000, '', ''],
cateList: [],
imgLength: 0,
fileList: [],
form: {
imgList: [],
secondCategoryId: null
},
cateName: '',
focus: false,
picking: false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.cascaderView = this.cascaderView || this.selectComponent('#wux-cascader')
wx.showLoading({ title: '加载中', mask: true })
if (options.id) {
getPurchaseInfo(options.id).then(result => {
var fileList = []
if (result.data.imgList && result.data.imgList.length) {
result.data.imgList.forEach(element => {
fileList.push({ url: element, status: 8 })
})
}
var region = []
var areaStr = ''
if(result.data.locDistrictId){
region[0] = parseInt(result.data.locDistrictId / 10000) * 10000
region[1] = parseInt(result.data.locDistrictId / 100) * 100
region[2] = parseInt(result.data.locDistrictId)
areaStr = result.data.locProvinceName + ' ' + result.data.locCityName + ' ' + result.data.locDistrictName
}
this.data.cateName = result.data.firstCategoryName + ' ' + result.data.secondCategoryName
var unitPrice = ''
if(result.data.unitPrice){
unitPrice = math.times(result.data.unitPrice, 1000)
}
var purchaseQuantity = ''
if(result.data.purchaseQuantity){
purchaseQuantity = math.divide(result.data.purchaseQuantity, 1000)
}
this.setData({
cateValue: [result.data.firstCategoryId, result.data.secondCategoryId],
cateName: this.data.cateName,
fileList: fileList,
imgLength: fileList.length,
form: result.data,
['form.unitPrice']: unitPrice,
['form.purchaseQuantity']: purchaseQuantity,
region: region,
['form.areaStr']: areaStr
})
wx.hideLoading()
}).catch(err => {
//异常回调
wx.hideLoading()
util.showToast(err)
})
}
paperCategoryList({ type: 1 }).then(result => {
var cateList = []
result.data.forEach((item) => {
cateList.push({ label: item.name, value: item.id, isLeaf: false })
})
this.setData({ cateList: cateList, safeBottom: app.globalData.safeBottom, kg: app.globalData.kg, height: app.globalData.safeFragmentHeight })
if (!options.id) {
wx.hideLoading()
}
}).catch(err => {
if (!options.id) {
wx.hideLoading()
}
})
},
textareaFocus: function (e) {
this.setData({ focus: true })
},
InputFocus(e) {
this.setData({ focus: true })
},
InputBlur(e) {
this.setData({ focus: false, ['form.description'] : this.data.form.description })
},
onImageChange: function (e) {
if (e.detail) {
var list = []
e.detail.forEach(element => {
if (!util.isEmpty(element.url)) {
list.push(element.url)
}
})
this.data.form['imgList'] = list
this.setData({ imgLength: list.length })
}
},
bindInput: function (e) {
this.data.form[e.currentTarget.id] = e.detail.value
if(e.currentTarget.id == 'description'){
// this.setData({ ['form.description'] : e.detail.value})
}
},
onLoadOptions: function (e) {
const { value } = e.detail
const options = [...this.data.cateList]
var typeId = parseInt(value[value.length - 1])
wx.showLoading({ title: '加载中', mask: true })
paperCategoryList({ type: 2, firstCategoryId: typeId }).then(result => {
var list = []
result.data.forEach((item) => {
list.push({ label: item.name, value: item.id })
})
options.forEach((element) => {
if (parseInt(element.value) === typeId) {
element.children = list
}
})
this.setData({ cateList: options })
wx.hideLoading()
}).catch(err => {
wx.hideLoading()
})
},
showRegion: function () {
if (this.data.disabled) {
return
}
this.cascaderView.showPicker(this.data.region)
this.setData({ picking: true })
},
onRegionInit: function (e) {
if (this.data.form.locDistrictId && e.detail.length == 3) {
this.setData({
['form.areaStr']: e.detail.map((n) => n.label).join(' ')
})
}
},
onRegionChange: function (e) {
if (e.detail && e.detail.options) {
this.setData({
['form.areaStr']: e.detail.options.map((n) => n.label).join(' '),
region: e.detail.value
})
this.data.form['locProvinceId'] = e.detail.value[0]
this.data.form['locProvinceName'] = e.detail.options[0].label
this.data.form['locCityId'] = e.detail.value[1]
this.data.form['locCityName'] = e.detail.options[1].label
this.data.form['locDistrictId'] = e.detail.value[2]
this.data.form['locDistrictName'] = e.detail.options[2].label
}
this.setData({
picking: false
})
},
showPaperPicker: function (e) {
this.setData({ visible: true, picking: true })
var list = [this.data.cateList[0].value]
if (!this.data.cateList[0].children) {
this.onLoadOptions({ detail: { value: list } })
}
},
onCategory: function (e) {
this.setData({ picking: false })
if (e.detail && e.detail.options) {
this.setData({
cateName: e.detail.options.map((n) => n.label).join(' '),
cateValue: e.detail.value
})
this.data.form['firstCategoryId'] = e.detail.value[0]
this.data.form['secondCategoryId'] = e.detail.value[1]
}
},
submitForm: function () {
if (util.isEmpty(this.data.form.secondCategoryId)) {
util.showToast('请选择废纸品类')
return
}
if (util.isEmpty(this.data.form.description)) {
util.showToast('请输入采购描述')
return
}
if (this.data.form.imgList.length >= 1) {
this.data.form['coverImgUrl'] = this.data.form.imgList[0]
}
if(!util.isEmpty(this.data.form.unitPrice) && !this.data.kg){
this.data.form.unitPrice = math.divide(this.data.form.unitPrice, 1000)
}
if(!util.isEmpty(this.data.form.purchaseQuantity) && !this.data.kg){
this.data.form.purchaseQuantity = math.times(this.data.form.purchaseQuantity, 1000)
}
this.setData({ requesting: true })
wx.showLoading({ title: '加载中', mask: true })
if (this.data.form.productId) {
updatePurchase(this.data.form).then(result => {
//成功回调
wx.hideLoading()
event.emit('EventMessage', {
what: 412,
desc: 'GoodsCreate'
})
util.showBackToast('修改成功!')
}).catch(err => {
//异常回调
wx.hideLoading()
this.setData({ requesting: false })
util.showToast(err)
})
} else {
createPurchase(this.data.form).then(result => {
//成功回调
wx.hideLoading()
util.showBackToast('发布采购成功!')
}).catch(err => {
//异常回调
wx.hideLoading()
this.setData({ requesting: false })
util.showToast(err)
})
}
}
})