平板端快衣店
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.
 
 
 
 
 

382 lines
12 KiB

<template>
<view>
<view class="formBox">
<uni-forms ref="form" :modelValue="formData" :rules="rules" labelWidth="85px">
<uni-forms-item label="商品名称" name="productName" required>
<uni-easyinput type="text" v-model="formData.productName" placeholder="请输入商品名称" trim="all" />
</uni-forms-item>
<uni-forms-item label="商品图片" name="productPicture">
<!-- <uni-file-picker limit="9" title="最多选择9张图片" ref="files" :auto-upload="false" v-model="productPicture" mode="grid"
@select="select"
@delete="delIMG"></uni-file-picker>
<button @click="upload">上传文件</button> -->
<u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple
:maxCount="10"></u-upload>
</uni-forms-item>
<uni-forms-item label=" 供应商" required name="supplierId">
<uni-easyinput type="text" v-model="formData.supplierId" placeholder="请输入供应商" trim="all" />
<!-- <uni-easyinput type="text" v-model="formData.name" /> -->
</uni-forms-item>
<uni-forms-item label="货号" name="productSn" required>
<uni-easyinput type="text" v-model="formData.productSn" placeholder="请输入货号" trim="all" />
</uni-forms-item>
<uni-forms-item label="条码" name="barcode" required>
<uni-easyinput type="text" v-model="formData.barcode" placeholder="请输入条码" trim="all" />
</uni-forms-item>
<uni-forms-item label="采购价" name="purchasePrice" required>
<uni-easyinput type="digit" v-model="formData.purchasePrice" placeholder="请输入采购价" trim="all" />
</uni-forms-item>
<uni-forms-item label="批发价" name="wholesalePrice" required>
<uni-easyinput type="digit" v-model="formData.wholesalePrice" placeholder="请输入批发价" trim="all" />
</uni-forms-item>
<uni-forms-item label="市场价" name="price" required>
<uni-easyinput type="digit" v-model="formData.price" placeholder="请输入零售价" trim="all" />
</uni-forms-item>
<uni-forms-item label="销售周期" name="salesWeek">
<!-- <uni-easyinput type="digit" v-model="formData.salesWeek" placeholder="请输入零售价" trim="all" /> -->
<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.salesWeek" />
</uni-forms-item>
<uni-forms-item label="尾货预警" name="tailWarn" required>
<uni-easyinput type="digit" v-model="formData.tailWarn" placeholder="请设置尾货预警天数" trim="all" />
</uni-forms-item>
<uni-forms-item label="分类" name="categoryId" required>
<uni-data-select class="uni-data-select" :localdata="categoryList" v-model="formData.categoryId"
placeholder="请选择分类" @change="categoryChange"></uni-data-select>
</uni-forms-item>
<view class="attr">
<view class="content" v-for="(items,index) in arrtChange" :key="items.id">
<view class="attrName">
{{items.attributeName }}
</view>
<view class="attrs">
<text v-for="(itemTag, index1) in items.attrNameRange" :key="index1"
@tap.stop="attrNameChange(index,index1) "
:class="itemTag.isChecked ? 'checkedText' : '' "> {{itemTag.text}}</text>
</view>
</view>
</view>
<uni-forms-item label="货品简介" name="productIntro">
<uni-easyinput type="textarea" autoHeight v-model="formData.productIntro" placeholder="请输入货品简介"
trim="all" />
</uni-forms-item>
<!-- <uni-forms-item label="库存数量" name="name">
<uni-easyinput type="number" v-model="formData.name" placeholder="请输入库存数量" trim="all"/>
</uni-forms-item> -->
<!-- <uni-forms-item label="上架日期" name="name">
<uni-datetime-picker type="date" :clear-icon="false" v-model="formData.single" @maskClick="maskClick" />
</uni-forms-item> -->
<!-- <uni-forms-item label="销售周期" name="name">
<uni-data-select
class="uni-data-select"
:localdata="range"
v-model="formData.manufacturer"
placeholder="请选择销售周期"
></uni-data-select>
</uni-forms-item> -->
</uni-forms>
<u-button type="primary" @tap.stop="submit(ret)">保存</u-button>
</view>
<!-- <u-modal v-model="show" :content="content"></u-modal> -->
</view>
</template>
<script>
export default {
data() {
return {
action: 'http://www.example.com/upload',
// 表单数据
formData: {
name: '',
manufacturer: '',
price: '',
single: '',
classification: '',
// domains 字段下会有多个结果
domains: {},
productPicture: []
},
categoryList: [],
arrtList: [],
arrtChange: [],
attributeList: {},
rules: {
// 对name字段进行必填验证
categoryId: {
rules: [{
required: true,
errorMessage: '请选择商品分类',
}, ],
},
price: {
rules: [{
required: true,
errorMessage: '请填写市场价',
}, ],
},
wholesalePrice: {
rules: [{
required: true,
errorMessage: '请填写批发价',
}, ],
},
purchasePrice: {
rules: [{
required: true,
errorMessage: '请填写采购价',
}, ],
},
barcode: {
rules: [{
required: true,
errorMessage: '请填写条码',
}, ],
},
productSn: {
rules: [{
required: true,
errorMessage: '请填写货号',
}, ],
},
supplierId: {
rules: [{
required: true,
errorMessage: '请填写姓名',
}, ],
},
productName: {
rules: [{
required: true,
errorMessage: '请填写商品名称',
}, ],
},
fileList1: [],
}
}
},
onShow() {
let that = this
uni.$on('classForm', function(data) {
that.formData.classification = data
})
},
onLoad() {
this.tui.request("/app/productCategory/list", "get", {
// shopId: 1,
}, false, true).then((res) => {
if (res.code == 200) {
this.categoryList = res.result.map(item => {
return {
value: item.id,
text: item.categoryName
}
})
}
})
},
methods: {
// 删除图片
deletePic(event) {
this[`fileList${event.name}`].splice(event.index, 1)
},
// 新增图片
async afterRead(event) {
console.log(event);
// 当设置 multiple 为 true 时, file 为数组格式,否则为对象格式
let lists = [].concat(event.file)
if(this.fileList1 == undefined) this.fileList1 = [];
let fileListLen = this[`fileList${event.name}`].length
console.log(lists,'lists');
lists.map((item) => {
this[`fileList${event.name}`].push({
...item,
status: 'uploading',
message: '上传中'
})
})
console.log(this.fileList1);
for (let i = 0; i < lists.length; i++) {
const result = await this.uploadFilePromise(lists[i].url)
let item = this[`fileList${event.name}`][fileListLen]
this[`fileList${event.name}`].splice(fileListLen, 1, Object.assign(item, {
status: 'success',
message: '',
url: result
}))
fileListLen++
}
},
uploadFilePromise(url) {
console.log(url);
return new Promise((resolve, reject) => {
let a = uni.uploadFile({
url: 'http://154.8.162.157:8888/hiver/upload/file', // 仅为示例,非真实的接口地址
filePath: url,
name: 'file',
formData: {
user: 'test'
},
success: (res) => {
setTimeout(() => {
resolve(res.data.data)
}, 1000)
}
});
})
},
upload() {
this.$refs.files.upload()
},
//图片上传
select(e) {
const tempFilePaths = e.tempFilePaths;
const imgUrl = tempFilePaths[0]
uni.uploadFile({
url: 'http://154.8.162.157:8888/hiver/upload/file?categoryId=' + this.formData.categoryId,
filePath: imgUrl,
name: 'imgUrl',
header: {
"Content-Type": "multipart/form-data"
},
success: (uploadFileRes) => {
console.log('uploadFileRes', JSON.parse(uploadFileRes.data));
let path = JSON.parse(uploadFileRes.data)
//后端返回的地址,存入图片地址
this.imageValue.push({
url: this.imgURL + path.imgUrl,
name: ''
})
console.log('imageValue', this.imageValue)
}
});
},
//图片删除
delIMG(e) {
console.log('456', e)
const num = this.imageValue.findIndex(v => v.url === e.tempFilePath);
this.imageValue.splice(num, 1);
},
upload() {
this.$refs.files.upload()
},
attrNameChange(index, index1) {
if (!this.arrtList[index].attrNameRange[index1].isChecked) this.arrtList[index].attrNameRange[index1]
.isChecked = false
this.arrtList[index].attrNameRange[index1].isChecked = this.arrtList[index].attrNameRange[index1]
.isChecked ? false : true;
this.arrtChange = JSON.parse(JSON.stringify(this.arrtList));
},
categoryChange(e) {
this.tui.request("/app/productAttribute/selectByCategoryId", "get", {
categoryId: e
}, false, true).then((res) => {
if (res.code == 200) {
this.arrtList = res.result;
if (this.arrtList.length > 0) {
this.arrtList.forEach((items) => {
console.log(items, 'items');
this.tui.request("/app/productAttributeValue/selectByAttributeId", "get", {
id: items.id
}, false, true).then((res1) => {
if (res1.code == 200) {
items.attrNameRange = res1.result.map(val => {
return {
text: val.value,
value: val.id
}
})
}
}).catch((res) => {})
})
setTimeout(() => {
this.arrtChange = this.arrtList
}, 500)
}
}
}).catch((res) => {})
},
/**
* 复写 binddata 方法,如果只是为了校验,无复杂自定义操作,可忽略此方法
* @param {String} name 字段名称
* @param {String} value 表单域的值
*/
// binddata(name,value){
// 通过 input 事件设置表单指定 name 的值
// this.$refs.form.setValue(name, value)
// },
// 触发提交表单
submit() {
this.arrtList.forEach((item, index) => {
item.value = [];
item.attrNameRange.forEach((item2, index2) => {
if (item2.isChecked) {
item.value.push(item2.text)
}
})
})
this.attributeList = this.arrtList.reduce((obj, item) => ({
...obj,
[item.attributeName]: item.value
}), {})
for (let val in this.attributeList) {
if (this.attributeList[val].length == 0) {
return uni.showToast({
title: "请添加属性",
icon: "none"
})
}
}
this.$refs.form.validate().then(res => {
res.attributeList = JSON.stringify(this.attributeList);
this.tui.request("/app/product/save", "post", res, false, true).then((res1) => {
if (res1.code == 200) {
uni.navigateBack({
delta: 1
})
} else {
uni.showToast({
title: "添加失败",
icon: 'none'
})
}
})
}).catch(err => {
console.log('表单错误信息:', err);
})
}
}
}
</script>
<style lang="scss" scoped>
.formBox {
margin: 20rpx;
padding: 20rpx 26rpx;
border-radius: 14rpx;
box-shadow: 0px 1px 10px 2px #e2e2e2;
background-color: #fff;
.uni-data-select {
width: 100%;
}
.attrs {
text {
display: inline-block;
border: 1px solid $u-primary;
padding: 7px 20rpx;
margin: 20rpx;
background-color: #fff;
color: $u-primary;
}
.checkedText {
background-color: $u-primary;
color: #fff;
}
}
}
</style>