freight-web/src/pagesApp/components/addShipmentProduct.vue

280 lines
7.0 KiB
Vue
Raw Normal View History

2024-03-18 01:52:18 +00:00
<template>
<view class="c-card">
<u-form
labelPosition="left"
:model="model1"
:rules="rules"
ref="form"
:labelWidth="100"
:labelStyle="{ padding: '0rpx 10rpx' }"
:errorType="'border-bottom'"
2024-03-18 01:52:18 +00:00
>
<u-form-item
:prop="`formData.${item.key}`"
2024-03-18 01:52:18 +00:00
:label="item.name"
:required="item.required"
v-for="(item, index) in formAttrList"
:key="index"
@click="item.fn"
>
<u-textarea
v-if="item.type === 'textarea'"
v-model="(model1.formData as any)[item.key]"
:placeholder="`请输入${item.name}`"
></u-textarea>
2024-04-26 06:13:57 +00:00
<up-input
2024-03-18 01:52:18 +00:00
v-if="
item.type === 'select' ||
item.type === 'input' ||
item.type === 'cascader'
"
v-model="(model1.formData as any)[item.key]"
:placeholder="`${item.type === 'select' ? '选择' : '输入'}${
item.name
}`"
2024-04-26 06:13:57 +00:00
clearable
2024-03-18 01:52:18 +00:00
:customStyle="{}"
border="none"
:disabled="item.type === 'cascader'"
:disabledColor="['出货分类'].indexOf(item.name) > -1?'#ffffff':'#f5f7fa'"
2024-04-26 06:13:57 +00:00
@clear="handleClear(item)"
2024-03-18 01:52:18 +00:00
>
<template #suffix>
<text>
{{ item.unit }}
</text>
</template>
2024-04-26 06:13:57 +00:00
</up-input>
2024-03-18 01:52:18 +00:00
<template
#right
v-if="item.type === 'select' || item.type === 'cascader'"
>
<u-icon name="arrow-right"></u-icon>
</template>
</u-form-item>
</u-form>
<!-- <block v-for="(item, index) in formAttrList" :key="index">
<u-action-sheet
v-if="item.type === 'select'"
:actions="contrlModalParams[item.childKey].list"
:title="contrlModalParams[item.childKey].title"
:show="contrlModalParams[item.childKey].isShow"
@select="(v: any) => handleSelect(item.childKey, v)"
@close="contrlModalParams[item.childKey].isShow = false"
:closeOnClickAction="true"
></u-action-sheet>
</block> -->
<u-picker
:show="contrlModalParams['reCategory'].isShow"
:title="contrlModalParams['reCategory'].title"
ref="uPicker"
:columns="contrlModalParams['reCategory'].list"
@cancel="contrlModalParams['reCategory'].isShow = false"
@change="handleChange"
keyName="shmCategoryName"
@confirm="confirm"
></u-picker>
</view>
<view class="btn-box">
<u-button type="primary" text="保存" @click="save()"></u-button>
</view>
</template>
<script setup lang="ts">
import { GoodsApi } from "@/services";
import { formatDate } from "@/utils";
import { DeviceType, ImagesType, OrderType } from "@/utils/enum";
import { onLoad } from "@dcloudio/uni-app";
import _ from "underscore";
2024-04-26 06:13:57 +00:00
const handleClear = (item:any) => {
setTimeout(() => {
(model1.formData as any)[item.key] = '';
}, 100);
}
const { proxy }: any = getCurrentInstance();
2024-03-18 01:52:18 +00:00
const model1 = reactive<any>({
formData: {},
});
const rules = ref({
"formData.shmProductsName": {
2024-03-18 01:52:18 +00:00
type: "string",
required: true,
message: "请输入出货产品",
2024-03-18 01:52:18 +00:00
trigger: ["blur", "change"],
},
"formData.reCategoryName": {
2024-03-18 01:52:18 +00:00
type: "string",
required: true,
message: "请选择出货分类",
2024-03-18 01:52:18 +00:00
trigger: ["blur", "change"],
},
});
const contrlModalParams = reactive<any>({
reCategory: {
isShow: false,
title: "标题",
list: [],
},
});
const formAttrList = reactive<any>([
{
name: "出货产品",
key: "shmProductsName",
type: "input",
required: true,
unit: "",
},
{
name: "出货分类",
key: "reCategoryName",
type: "cascader",
childKey: "reCategory",
required: true,
unit: "",
fn: () => {
contrlModalParams.reCategory.isShow = true;
contrlModalParams.reCategory.title = "出货分类";
},
},
]);
// const handleSelect = (key: string, v: any) => {
// contrlModalParams[key].isShow = false;
// if (key === "reCategory") {
// model1.formData.reCategoryName = v.name;
// model1.formData.reCategoryId = v.id;
// }
// };
const handleChange = (e: any) => {
const {
columnIndex,
value,
values, // values为当前变化列的数组内容
index,
picker = proxy.$refs.uPicker,
} = e;
console.log(columnIndex, value, values, index, picker);
if (columnIndex === 0) {
// picker为选择器this实例变化第二列对应的选项
picker.setColumnValues(1, value[0].childrenList || []);
}
};
const confirm = (e: any) => {
if (e.value[1]) {
model1.formData.shmCategoryId = e.value[1].id;
model1.formData.reCategoryName = e.value[1].shmCategoryName;
contrlModalParams["reCategory"].isShow = false;
2024-03-18 01:52:18 +00:00
}
};
const form = ref();
const check = () => {
return new Promise((resolve) => {
form.value
.validate()
.then((res: boolean) => {
resolve(res);
})
.catch((errors: any) => {
resolve(false);
uni.showToast({
icon: "none",
title: errors[0].message || "校验失败",
});
});
});
};
2024-03-18 01:52:18 +00:00
const save = () => {
check().then((res) => {
if (res) {
startSave();
}
});
};
const startSave = () => {
2024-03-18 01:52:18 +00:00
if (model1.formData.id) {
GoodsApi.editShipmentProduct(model1.formData).then((res) => {
if (res.code === 200) {
2024-04-25 08:30:50 +00:00
uni.navigateBack()
}
});
2024-03-18 01:52:18 +00:00
} else {
GoodsApi.addShipmentProduct(model1.formData).then((res) => {
if (res.code === 200) {
2024-04-25 08:30:50 +00:00
uni.navigateBack()
2024-03-18 01:52:18 +00:00
}
});
}
};
const getTypeList = () => {
GoodsApi.getShipmentCategory().then((res) => {
if (res.code === 200) {
contrlModalParams.reCategory.list = [
(res.data as any).map((item: any) => {
return { ...item, name: item.reCategoryName };
}),
];
contrlModalParams.reCategory.list.push(
contrlModalParams.reCategory.list[0][0].childrenList
);
}
});
};
onMounted(() => {
getTypeList();
});
onLoad((option) => {
// 接收传递的标题参数
const title = (option as any).title;
if ((option as any).item) {
model1.formData = JSON.parse((option as any).item) || {};
if (option) {
if (model1.formData.shmCategoryName) {
model1.formData.reCategoryName = model1.formData.shmCategoryName;
}
2024-03-18 01:52:18 +00:00
}
}
// 设置页面标题
if (title) {
uni.setNavigationBarTitle({
title: title || "新增出货产品",
2024-03-18 01:52:18 +00:00
});
}
});
</script>
<style lang="scss" scoped>
.c-card {
background: #ffffff;
// box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(5, 68, 37, 0.12);
border-radius: 13rpx;
margin: 30rpx 25rpx;
2024-03-21 05:52:15 +00:00
padding: 0rpx 20rpx;
2024-03-18 01:52:18 +00:00
::v-deep .u-form-item {
height: auto;
}
::v-deep .u-form-item + .u-form-item {
border-top: 1rpx solid rgba(233, 233, 233, 0.76);
}
}
.btn-box {
margin-top: 60rpx;
display: flex;
background: #ffffff;
box-shadow: 0rpx 0rpx 10rpx 0rpx rgba(0, 56, 93, 0.12);
border-radius: 13rpx 13rpx 0rpx 0rpx;
padding: 25rpx 50rpx;
position: sticky;
bottom: 0rpx;
z-index: 999;
::v-deep button {
border-radius: 43rpx;
}
}
</style>