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

539 lines
14 KiB

// pages/goods/create/index.js
import {
paperCategoryList,
getProductInfo,
createProductInfo,
updateProductInfo,
getWaterRate,
getDopantRate,
getBiddingInterval
} 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,
kg: app.globalData.kg,
height: app.globalData.safeFragmentHeight,
options: regions,
region: [440000, '', ''],
cateList: [],
dopantList: [],
waterList: [],
intervalList: [{
text: '24小时',
value: 1
}, {
text: '48小时',
value: 2
}],
videoList: [],
imgLength: 0,
fileList: [],
form: {
bidType: 1,
imgList: [],
secondCategoryId: null,
stock: null,
dopantRate: null,
waterRate: null
},
cateName: '',
dopantName: '',
waterName: '',
intervalName: '',
sumAmount: 0,
keyboard: 0,
toView: '',
focus: false,
picking: false
},
/**
* 生命周期函数--监听页面加载
*/
onLoad: function (options) {
this.setData({
['form.bidType']: options.type ? Number(options.type) : 1,
kg: app.globalData.kg,
safeBottom: app.globalData.safeBottom,
height: app.globalData.safeFragmentHeight
})
this.cascaderView = this.cascaderView || this.selectComponent('#wux-cascader')
this.pickerView = this.pickerView || this.selectComponent('#picker-view')
wx.showLoading({
title: '加载中',
mask: true
})
getDopantRate().then(result => {
var list = []
result.data.forEach((item) => {
list.push({
text: item.name,
value: item.id
})
})
this.setData({
dopantList: list
})
})
getWaterRate().then(result => {
var list = []
result.data.forEach((item) => {
list.push({
text: item.name,
value: item.id
})
})
this.setData({
waterList: list
})
})
getBiddingInterval().then(result => {
var list = []
result.data.forEach((item) => {
list.push({
text: item.name,
value: item.id
})
})
this.setData({
intervalList: list
})
})
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
})
if (!options.id) {
wx.hideLoading()
}
}).catch(err => {
//异常回调
if (!options.id) {
wx.hideLoading()
}
})
if (options.id) {
getProductInfo(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 videoList = []
if (result.data.videoUrl) {
videoList.push({
videoUrl: result.data.videoUrl,
status: 8
})
}
var region = []
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)
}
this.data.cateName = result.data.firstCategoryName + ' ' + result.data.secondCategoryName
if (this.data.dopantList && this.data.dopantList.length) {
this.data.dopantList.forEach(element => {
if (Number(element.value) == Number(result.data.dopantRate)) {
this.data.dopantName = element.text
}
})
}
if (this.data.waterList && this.data.waterList.length) {
this.data.waterList.forEach(element => {
if (Number(element.value) == Number(result.data.waterRate)) {
this.data.waterName = element.text
}
})
}
if (result.data.biddingInterval) {
this.data.intervalList.forEach(element => {
if (Number(element.value) == Number(result.data.biddingInterval)) {
this.data.intervalName = element.text
}
})
}
if (result.data.unitPrice) {
var unitPrice = app.globalData.kg ? result.data.unitPrice : math.times(result.data.unitPrice, 1000)
} else {
var unitPrice = ''
}
var stock = app.globalData.kg ? result.data.stock : math.divide(result.data.stock, 1000)
var lowestNum = ''
if (!util.isEmpty(result.data.lowestNum)) {
lowestNum = app.globalData.kg ? result.data.lowestNum : math.divide(result.data.lowestNum, 1000)
}
this.setData({
form: result.data,
['form.unitPrice']: unitPrice,
['form.stock']: stock,
['form.lowestNum']: lowestNum,
fileList: fileList,
videoList: videoList,
region: region,
['form.areaStr']: result.data.locProvinceName + ' ' + result.data.locCityName + ' ' + result.data.locDistrictName,
cateValue: [result.data.firstCategoryId, result.data.secondCategoryId],
cateName: this.data.cateName,
dopantName: this.data.dopantName,
waterName: this.data.waterName,
imgLength: fileList.length,
intervalName: this.data.intervalName
})
if (util.isEmpty(this.data.form.areaStr)) {
this.cascaderView.getCurrentOptions(region, true)
}
this.sumAmount(null)
this.data.form['coverImgUrl'] = fileList[0].url
wx.hideLoading()
}).catch(err => {
//异常回调
wx.hideLoading()
util.showToast(err)
})
}
},
InputFocus(e) {
this.setData({
focus: true,
toView: 'textarea',
keyboard: e.detail.height
})
},
InputBlur(e) {
this.setData({
keyboard: 0,
focus: false,
toView: null
})
},
textareaFocus: function (e) {
this.setData({
focus: true,
toView: 'textarea'
})
},
onVideoChange: function (e) {
if (e.detail && e.detail.length) {
this.data.form['videoUrl'] = e.detail[0].url
}
},
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) {
if (e.target.id == 'description') {
this.setData({
['form.' + e.target.id]: e.detail.value
})
} else {
this.data.form[e.target.id] = e.detail.value
}
},
onLoadOptions: function (e) {
// /paperCategoryList 获取纸品列表
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]
}
},
onPaperChange: function (e) {
this.setData({
picking: false
})
if (!e.detail) {
return
}
if (e.detail.type == 0) {
this.data.form['secondCategoryId'] = e.detail.value
this.data.form['firstCategoryId'] = e.detail.value
this.setData({
cateName: e.detail.text
})
} else if (e.detail.type == 1) {
this.data.form['dopantRate'] = e.detail.value
this.setData({
dopantName: e.detail.text
})
} else if (e.detail.type == 2) {
this.data.form['waterRate'] = e.detail.value
this.setData({
waterName: e.detail.text
})
} else if (e.detail.type == 3) {
this.data.form['biddingInterval'] = e.detail.value
this.setData({
intervalName: e.detail.text
})
}
},
bindinput: function (e) {
this.data.form[e.target.id] = e.detail.value
},
sumAmount: function (e) {
if (Number(this.data.form.unitPrice) > 0 && parseInt(this.data.form.stock)) {
this.setData({
sumAmount: math.times(this.data.form.unitPrice, this.data.form.stock)
})
}
},
templatDescription: function () {
},
showRadioPicker: function (e) {
if (e.currentTarget.id == 'dopantRate') {
this.pickerView.showPicker(this.data.dopantList, this.data.form.dopantRate, 1)
} else if (e.currentTarget.id == 'waterRate') {
this.pickerView.showPicker(this.data.waterList, this.data.form.waterRate, 2)
} else if (e.currentTarget.id == 'biddingInterval') {
this.pickerView.showPicker(this.data.intervalList, this.data.form.biddingInterval, 3)
}
this.setData({
picking: true
})
},
submitForm: function () {
if (util.isEmpty(this.data.form.secondCategoryId)) {
util.showToast('请选择废纸品类')
return
}
if (this.data.form.bidType == 2) {
if (util.isEmpty(this.data.form.unitPrice)) {
if (this.data.form.bidType == 1) {
util.showToast('请输入商品单价')
} else {
util.showToast('请输入起拍单价')
}
return
}
if (app.globalData.kg) {
if (parseFloat(this.data.form.unitPrice) > 100) {
if (this.data.form.bidType == 1) {
util.showToast('请检查您输入的商品单价是否合理')
} else {
util.showToast('请检查您输入的起拍单价是否合理')
}
return
}
} else {
if (parseFloat(this.data.form.unitPrice) < 100) {
if (this.data.form.bidType == 1) {
util.showToast('请检查您输入的商品单价是否合理')
} else {
util.showToast('请检查您输入的起拍单价是否合理')
}
return
}
}
}
if (util.isEmpty(this.data.form.stock)) {
util.showToast('请输入库存')
return
}
if (util.isEmpty(this.data.form.biddingInterval) && this.data.form.bidType == 2) {
util.showToast('请选择竞价有效时')
return
}
if (util.isEmpty(this.data.form.dopantRate)) {
util.showToast('请选择含杂比例')
return
}
if (util.isEmpty(this.data.form.waterRate)) {
util.showToast('请选择湿水比例')
return
}
if (util.isEmpty(this.data.form.locDistrictId)) {
util.showToast('请选择店铺所在区域')
return
}
if (util.isEmpty(this.data.form.locDetails)) {
util.showToast('请输入店铺详细地址')
return
}
if (this.data.form.imgList.length < 2) {
util.showToast('请至少上传2张商品图片')
return
}
this.data.form['coverImgUrl'] = this.data.form.imgList[0]
if (util.isEmpty(this.data.form.description)) {
util.showToast('请输入商品描述')
return
}
this.setData({
requesting: true
})
wx.showLoading({
title: '加载中',
mask: true
})
if (!app.globalData.kg) {
if (this.data.form.unitPrice > 0) {
this.data.form.unitPrice = math.divide(this.data.form.unitPrice, 1000)
} else {
this.data.form.unitPrice = ''
}
this.data.form.stock = math.times(this.data.form.stock, 1000)
if (!util.isEmpty(this.data.form.lowestNum)) {
this.data.form.lowestNum = math.times(this.data.form.lowestNum, 1000)
}
}
if (this.data.form.productId) {
updateProductInfo(this.data.form).then(result => {
wx.hideLoading()
event.emit('EventMessage', {
what: 102,
desc: 'GoodsCreate'
})
util.showBackToast('商品发布成功!')
}).catch(err => {
//异常回调
wx.hideLoading()
this.setData({
requesting: false
})
util.showToast(err)
})
} else {
createProductInfo(this.data.form).then(result => {
wx.hideLoading()
util.showBackToast('商品创建成功!')
}).catch(err => {
wx.hideLoading()
this.setData({
requesting: false
})
util.showToast(err)
})
}
}
})