Files

269 lines
5.8 KiB
TypeScript
Raw Normal View History

2023-08-30 17:27:21 +08:00
import Message from "tdesign-miniprogram/message/index";
import { getDict } from "../../api/dict";
import { addFinance, getFinanceInfo, updateFinance } from "../../api/finance";
2023-08-31 16:24:37 +08:00
import { getInfo } from "../../api/login";
2023-08-30 17:27:21 +08:00
// pages/add-affairs/add-affairs.ts
const dayjs = require("dayjs");
Page({
/**
*
*/
data: {
2023-08-31 16:24:37 +08:00
permissions: [],
2023-08-30 17:27:21 +08:00
defaultTime: dayjs(dayjs().format("YYYY-MM-DD")).valueOf(),
types: [],
form: {
financeId: undefined,
event: undefined, //事务
type: undefined, // 类别
date: undefined, // 时间
oppositeCompany: undefined, // 对方账户
amount: undefined, // 金额
},
dateVisible: false,
pickerVisible: false,
},
onChange(e: any) {
const field = e.currentTarget.dataset.field;
this.setData({
[`form.${field}`]: e.detail.value,
});
},
showTypePicker() {
this.setData({
pickerVisible: true,
});
},
hidePicker() {
this.setData({
pickerVisible: false,
});
},
// 显示时间选择
showTimePicker() {
this.setData({
dateVisible: true,
});
},
hideTimePicker() {
this.setData({
dateVisible: false,
});
},
handleConfirmTime(e: any) {
console.log(e);
this.setData({
[`form.date`]: e.detail.value,
});
},
2023-08-31 16:24:37 +08:00
loadUserInfo() {
const permissions = getApp().globalData.permissions;
if (permissions) {
this.setData({
permissions,
});
} else {
const token = getApp().globalData.authToken;
getInfo(token).then((resp: any) => {
this.setData({
permissions: resp.permissions,
});
getApp().globalData.permissions = resp.permissions;
});
}
},
2023-08-30 17:27:21 +08:00
getTypeLabel(dictValue: string) {
// @ts-ignore
return this.data.types.find((item: any) => item.value === dictValue)?.label;
},
validate() {
const { form } = this.data;
const rules = {
event: [
{
required: true,
message: "请输入事项",
},
],
type: [
{
required: true,
message: "请选择类别",
},
],
date: [
{
required: true,
message: "请选择日期",
},
],
oppositeCompany: [
{
required: true,
message: "请输入对方账户",
},
],
amount: [
{
required: true,
message: "请输入金额",
},
],
};
for (const field of Object.keys(rules)) {
// @ts-ignore
for (const validation of rules[field]) {
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);
}
}
}
}
}
},
2023-08-31 16:24:37 +08:00
2023-08-30 17:27:21 +08:00
handleSubmitFinance() {
const { form } = this.data;
const result = this.validate();
if (result) {
Message.error({
context: this,
offset: [20, 32],
duration: 1000,
content: result,
});
return;
}
if (form.financeId) {
// 修改
updateFinance({
...form,
//@ts-ignore
type: form.type[0],
}).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/index/index",
});
}, 500);
});
} else {
// 新增
addFinance({
...form,
//@ts-ignore
type: form.type[0],
}).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/index/index",
});
}, 500);
});
}
},
onPickerChange(e: any) {
this.setData({
["form.type"]: e.detail.value,
["form.typeLabel"]: e.detail.label,
});
},
getTypeOptions() {
getDict("finance_type").then((resp: any) => {
const options =
resp.data?.map((el: any) => ({
label: el.dictLabel,
value: el.dictValue,
})) ?? [];
this.setData({
types: options,
});
});
},
getFinanceDetail(financeId: string) {
getFinanceInfo(financeId).then((resp: any) => {
this.setData({
form: { ...resp.data, type: [resp.data.type] },
});
});
},
/**
* --
*/
onLoad(option: any) {
if (option.financeId) {
this.getFinanceDetail(option.financeId);
}
this.getTypeOptions();
2023-08-31 16:24:37 +08:00
this.loadUserInfo();
2023-08-30 17:27:21 +08:00
},
/**
* --
*/
onReady() {},
/**
* --
*/
onShow() {},
/**
* --
*/
onHide() {},
/**
* --
*/
onUnload() {},
/**
* --
*/
onPullDownRefresh() {},
/**
*
*/
onReachBottom() {},
/**
*
*/
onShareAppMessage() {},
});