This commit is contained in:
quantulr
2023-08-30 17:27:21 +08:00
commit 0288146b0d
85 changed files with 26508 additions and 0 deletions

View File

@ -0,0 +1,12 @@
{
"usingComponents": {
"t-popup": "tdesign-miniprogram/popup/popup",
"t-input": "tdesign-miniprogram/input/input",
"t-upload": "tdesign-miniprogram/upload/upload",
"t-picker": "tdesign-miniprogram/picker/picker",
"t-cascader": "tdesign-miniprogram/cascader/cascader",
"t-tree-select": "tdesign-miniprogram/tree-select/tree-select",
"t-picker-item": "tdesign-miniprogram/picker-item/picker-item",
"t-date-time-picker": "tdesign-miniprogram/date-time-picker/date-time-picker"
}
}

View File

@ -0,0 +1,24 @@
/* pages/add-stock/add-stock.wxss */
.add-stock {
padding: 32rpx 0;
.add-stock-form {
border-radius: 18rpx;
overflow: hidden;
margin: 0 32rpx;
.upload-wrapper {
padding: 32rpx 32rpx;
background-color: #fff;
.label {
font-size: 32rpx;
line-height: 44rpx;
padding-bottom: 16rpx;
}
.body {
margin-top: 24rpx;
}
}
}
.submit-button {
margin: 32rpx 32rpx 0;
}
}

View File

@ -0,0 +1,378 @@
// pages/add-stock/add-stock.ts
// const geneTree = async () => {
// return [...Array(10).keys()].map((el) => ({
// value: el,
// label: el,
// }));
import Message from "tdesign-miniprogram/message/index";
import { uploadFile } from "../../api/file";
import { addStockLog, modelList, productList, specList } from "../../api/stock";
import httpClient from "../../utils/request";
const dayjs = require("dayjs");
// };
Page({
/**
* 页面的初始数据
*/
data: {
prodPicList: [],
totalErrMsg: "",
defaultTime: dayjs(dayjs().format("YYYY-MM-DD")).valueOf(),
specPickerVisible: false,
productPickerVisible: false,
modelPickerVisible: false,
typePickerVisible: false,
dateVisible: false,
stockLimit: 0,
picList: [],
productOptions: [
{
label: "323q",
value: "313213",
children: [],
},
],
modelOptions: [],
specOptions: [],
types: [
{
label: "入库",
value: "1",
},
{
label: "出库",
value: "2",
},
],
form: {
type: undefined,
productId: undefined,
modelId: undefined,
specId: undefined,
picList: [],
total: undefined,
date: dayjs(dayjs().format("YYYY-MM-DD")).format("YYYY-MM-DD HH:mm:ss"),
},
pickerSubTitles: ["选择产品", "选择型号", "选择规格"],
},
onPick(e: any) {
console.log(e);
this.setData({
["productOptions[0].children"]: {
value: "fdsf",
label: "sdfa",
},
});
},
onChange(e: any) {
const field = e.currentTarget.dataset.field;
this.setData({
[`form.${field}`]: e.detail.value,
});
if (field === "total") {
if (
parseInt(e.detail.value) > this.data.stockLimit &&
this.data.form.type == 2
) {
this.setData({
// @ts-ignore
totalErrMsg: `出库数量不能大于库存数 : ` + this.data.stockLimit,
});
} else {
this.setData({
// @ts-ignore
totalErrMsg: "",
});
}
}
},
onPickerChange(e: any) {
console.log(e.detail);
const field = e.currentTarget.dataset.field;
this.setData({
[`form.${field}`]: e.detail.value[0],
});
if (field === "productId") {
// load model options
modelList({
productId: this.data.form.productId,
}).then((resp: any) => {
this.setData({
modelOptions: resp.rows.map((el: any) => ({
label: el.name,
value: el.modelId,
})),
});
});
} else if (field === "modelId") {
// load spec options
specList({
modelId: this.data.form.modelId,
}).then((resp: any) => {
this.setData({
specOptions: resp.rows.map((el: any) => ({
...el,
label: el.name,
value: el.specId,
})),
});
});
} else if (field === "specId") {
const spec: any = this.data.specOptions.find(
(el: any) => el.value == e.detail.value[0]
);
console.log(spec);
this.setData({
stockLimit: spec?.stock ?? 0,
prodPicList:
spec?.pic?.split(",")?.map((el: string) => {
let url;
if (el.startsWith("https://") || el.startsWith("http://")) {
url = el;
} else {
url = `${httpClient.baseUrl}${el}`;
}
return {
url,
};
}) ?? [],
});
}
},
onPickerCancel(e: any) {
console.log(e);
},
showPicker(e: any) {
const { pickerKey } = e.currentTarget.dataset;
this.setData({
[`${pickerKey}`]: true,
});
},
loadProductionOptions() {
productList().then((resp: any) => {
this.setData({
productOptions: resp.rows.map((el: any) => ({
label: el.name,
value: el.productId,
})),
});
});
},
handleAdd(e: any) {
const { picList } = this.data;
const { files } = e.detail;
this.setData({
// @ts-ignore
picList: [
...picList,
...files.map((el: any) => ({ ...el, status: "loading" })),
],
});
// uploadFile(files[0]);
for (const file of files) {
uploadFile(file)
.then((resp: any) => {
const { picList } = this.data;
const index = picList.findIndex((el: any) => el.url === file.url);
if (index != -1) {
this.setData({
[`picList[${index}].url`]: `${httpClient.baseUrl}${resp.url}`,
[`picList[${index}].percent`]: 100,
[`picList[${index}].status`]: null,
});
}
})
.catch(() => {
const { picList } = this.data;
const index = picList.findIndex((el: any) => el.url === file.url);
if (index != -1) {
picList.splice(index, 1);
this.setData({
picList,
});
}
});
}
},
hidePicker() {
this.setData({
dateVisible: false,
});
},
handleRemove(e: any) {
const { index } = e.detail;
const { picList } = this.data;
picList.splice(index, 1);
this.setData({
picList,
});
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(option: any) {
if (option.type) {
this.setData({
["form.type"]: option.type,
});
this.loadProductionOptions();
} else {
wx.navigateBack();
}
},
validate() {
const { form } = this.data;
const rules = {
specId: [
{
required: true,
message: "请选择规格",
},
],
type: [
{
required: true,
message: "请选择类别",
},
],
date: [
{
required: true,
message: "请选择日期",
},
],
total: [
{
required: true,
message: "请输入数量",
},
{
validation: () => {
const { total } = this.data.form;
if (this.data.form.type == 2) {
// @ts-ignore
return total > this.data.stockLimit;
} else {
return false;
}
},
message: "出库数量不能超过总库存",
},
],
provePic: [
{
validation: () => {
return this.data.picList.length == 0;
},
message: "请上传凭证照片",
},
],
};
for (const field of Object.keys(rules)) {
// @ts-ignore
for (const validation of rules[field]) {
if (validation.validation) {
console.log(validation.validation());
if (validation.validation()) {
return validation.message;
}
}
if (validation.required) {
// @ts-ignore
if (typeof form[field] === "array") {
// @ts-ignore
if (form[field].length <= 0) {
return validation.message;
// throw new Error(validation.message);
}
} else {
// @ts-ignore
if (!form[field]) {
return validation.message;
// throw new Error(validation.message);
}
}
}
}
}
},
handleSubmitStock() {
let message;
message = this.validate();
if (message) {
Message.error({
context: this,
offset: [20, 32],
duration: 1000,
content: message,
});
return;
}
const { form } = this.data;
addStockLog({
specId: form.specId,
total: form.total,
date: form.date,
provePic: this.data.picList.map((el: any) => el.url).join(","),
type: form.type,
}).then(() => {
const pages = getCurrentPages();
const prevPage = pages[pages.length - 2];
prevPage.handleRefresh();
Message.success({
context: this,
offset: [20, 32],
duration: 3000,
content: "修改成功",
});
setTimeout(() => {
wx.switchTab({
url: "/pages/stock/stock",
});
}, 500);
});
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {},
/**
* 生命周期函数--监听页面显示
*/
onShow() {},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {},
});

View File

@ -0,0 +1,51 @@
<!--pages/add-stock/add-stock.wxml-->
<wxs module="dict" src="../../utils/dict.wxs" />
<wxs module="times" src="../../utils/time.wxs" />
<t-message id="t-message" />
<view class="add-stock">
<view class="add-stock-form">
<t-cell title="产品" note="{{dict.getDictLabel(form.productId, productOptions)}}" data-picker-key="productPickerVisible" bind:click="showPicker" arrow />
<t-cell title="型号" note="{{dict.getDictLabel(form.modelId, modelOptions)}}" data-picker-key="modelPickerVisible" bind:click="showPicker" arrow />
<t-cell title="规格" note="{{dict.getDictLabel(form.specId, specOptions)}}" data-picker-key="specPickerVisible" bind:click="showPicker" arrow />
<view class="upload-wrapper" wx:if="{{form.specId}}">
<text class="label">产品图片</text>
<view class="body">
<view class="prod-image-list">
<t-image wx:for="{{prodPicList}}" wx:key="url" shape="round" width="150rpx" height="150rpx" src="{{item.url}}"></t-image>
</view>
</view>
</view>
<t-cell title="类型" note="{{dict.getDictLabel(form.type, types)}}" data-picker-key="typePickerVisible" bind:click="showPicker" arrow />
<t-cell title="选择日期" hover note="{{times.formatDate(form.date) || ''}}" arrow data-mode="date" data-picker-key="dateVisible" bindtap="showPicker" class="test" t-class="panel-item" />
<view class="upload-wrapper">
<text class="label">凭证照片</text>
<view class="body">
<t-upload class="upload" mediaType="{{['image']}}" files="{{picList}}" bind:add="handleAdd" bind:remove="handleRemove">
</t-upload>
</view>
</view>
<t-input label="数量" type="number" bind:change="onChange" data-field="total" value="{{form.total}}" placeholder="请输入数量" tips="{{totalErrMsg}}" status="{{totalErrMsg?'error':''}}" />
</view>
<view class="submit-button">
<t-button bind:tap="handleSubmitStock" theme="primary" block>确认提交</t-button>
</view>
<!-- 产品选择器 -->
<t-picker default-value="{{[form.productId]}}" visible="{{productPickerVisible}}" value="{{form.productId}}" data-key="productId" data-field="productId" title="选择产品" cancelBtn="取消" confirmBtn="确认" bindchange="onPickerChange" bindcancel="onPickerCancel">
<t-picker-item options="{{productOptions}}" />
</t-picker>
<!-- 型号选择器 -->
<t-picker default-value="{{[form.modelId]}}" visible="{{modelPickerVisible}}" value="{{form.modelId}}" data-key="modelId" data-field="modelId" title="选择型号" cancelBtn="取消" confirmBtn="确认" bindchange="onPickerChange" bindcancel="onPickerCancel">
<t-picker-item options="{{modelOptions}}" />
</t-picker>
<!-- 规格选择器 -->
<t-picker default-value="{{[form.specId]}}" visible="{{specPickerVisible}}" value="{{form.specId}}" data-key="specId" data-field="specId" title="选择规格" cancelBtn="取消" confirmBtn="确认" bindchange="onPickerChange" bindcancel="onPickerCancel">
<t-picker-item options="{{specOptions}}" />
</t-picker>
<!-- 类型选择器 -->
<t-picker default-value="{{[form.type]}}" visible="{{typePickerVisible}}" value="{{form.type}}" data-key="type" data-field="type" title="选择类别" cancelBtn="取消" confirmBtn="确认" bindchange="onPickerChange" bindcancel="onPickerCancel">
<t-picker-item options="{{types}}" />
</t-picker>
<!-- 日期 -->
<t-date-time-picker title="选择日期" visible="{{dateVisible}}" mode="date" data-field="date" defaultValue="{{form.date || defaultTime}}" format="YYYY-MM-DD HH:mm:ss" bindchange="onChange" bindcancel="hidePicker" />
</view>