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

292 lines
6.9 KiB
Vue
Raw Normal View History

2024-03-18 06:17:04 +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 06:17:04 +00:00
>
<u-form-item
:prop="`formData.${item.key}`"
2024-03-18 06:17:04 +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>
<u-input
v-if="item.type === 'select' || item.type === 'input'"
:password="item.key === 'password'"
2024-03-18 06:17:04 +00:00
v-model="(model1.formData as any)[item.key]"
:placeholder="`${item.type === 'select' ? '选择' : '输入'}${
item.name
}`"
:clearable="true"
:customStyle="{}"
border="none"
:maxlength="item.key === 'password' ? 6 : -1"
@change="(e:any) => {handleInput(e, item)}"
:disabled="item.type === 'select'"
:disabledColor="['性别', '用户角色'].indexOf(item.name) > -1?'#ffffff':'#f5f7fa'"
2024-03-18 06:17:04 +00:00
>
<template #suffix>
<text>
{{ item.unit }}
</text>
</template>
</u-input>
<template #right v-if="item.type === 'select'">
<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>
</view>
<view class="btn-box">
<u-button type="primary" text="保存" @click="save()"></u-button>
</view>
</template>
<script setup lang="ts">
2024-03-19 02:15:47 +00:00
import { ProfileApi, StockCardApi } from "@/services";
2024-03-18 06:17:04 +00:00
import { formatDate } from "@/utils";
import { DeviceType, ImagesType, OrderType, StockCardType } from "@/utils/enum";
import valid from "@/utils/validate";
2024-03-18 06:17:04 +00:00
import { onLoad } from "@dcloudio/uni-app";
import _ from "underscore";
const handleInput = (e: any, item: any) => {
if (item.key === "phone") {
const temp = e?.replace(valid.valid_number, "");
setTimeout(() => {
model1.formData[item.key] = temp;
}, 100);
}
};
2024-03-18 06:17:04 +00:00
const model1 = reactive<any>({
formData: {
},
2024-03-18 06:17:04 +00:00
});
const rules = ref({
"formData.userName": {
2024-03-18 06:17:04 +00:00
type: "string",
required: true,
message: "请输入用户名",
2024-03-18 06:17:04 +00:00
trigger: ["blur", "change"],
},
"formData.password": {
2024-03-18 06:17:04 +00:00
type: "string",
required: true,
message: "请输入密码",
trigger: ["blur", "change"],
},
});
const contrlModalParams = reactive<any>({
2024-03-19 02:15:47 +00:00
role: {
isShow: false,
title: "标题",
list: [],
},
gender: {
2024-03-18 06:17:04 +00:00
isShow: false,
title: "标题",
list: [
{
id: 1,
name: "男",
},
{
id: 2,
name: "女",
},
],
2024-03-19 02:15:47 +00:00
},
2024-03-18 06:17:04 +00:00
});
const formAttrList = reactive<any>([
{
name: "姓名",
2024-03-19 02:15:47 +00:00
key: "name",
2024-03-18 06:17:04 +00:00
type: "input",
},
{
name: "性别",
2024-03-19 02:15:47 +00:00
key: "genderName",
2024-03-18 06:17:04 +00:00
type: "select",
2024-03-19 02:15:47 +00:00
childKey: "gender",
2024-03-18 06:17:04 +00:00
fn: () => {
2024-03-19 02:15:47 +00:00
contrlModalParams.gender.isShow = true;
contrlModalParams.gender.title = "选择性别";
2024-03-18 06:17:04 +00:00
},
},
{
name: "用户角色",
2024-03-19 02:15:47 +00:00
key: "roleName",
2024-03-18 06:17:04 +00:00
type: "select",
2024-03-19 02:15:47 +00:00
childKey: "role",
2024-03-18 06:17:04 +00:00
fn: () => {
2024-03-19 02:15:47 +00:00
contrlModalParams.role.isShow = true;
contrlModalParams.role.title = "选择角色";
2024-03-18 06:17:04 +00:00
},
},
{
name: "用户名",
2024-03-19 02:15:47 +00:00
key: "userName",
2024-03-18 06:17:04 +00:00
type: "input",
required: true,
},
{
name: "密码",
2024-03-19 02:15:47 +00:00
key: "password",
2024-03-18 06:17:04 +00:00
type: "input",
required: true,
},
{
name: "联系手机",
2024-03-19 02:15:47 +00:00
key: "phone",
2024-03-18 06:17:04 +00:00
type: "input",
},
]);
const handleSelect = (key: string, v: any) => {
contrlModalParams[key].isShow = false;
2024-03-19 02:15:47 +00:00
if (key === "role") {
model1.formData.roleName = v.name;
model1.formData.roleIds = [v.id];
}
if (key === "gender") {
model1.formData.genderName = v.name;
model1.formData.gender = v.id;
2024-03-18 06:17:04 +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 06:17:04 +00:00
const save = () => {
if (model1.formData.phone) {
if (!valid.mobile.pattern.test(model1.formData.phone)) {
uni.showToast({ icon: "none", title: "请输入正确的手机号" });
return;
}
}
check().then((res) => {
if (res) {
startSave();
}
});
};
const startSave = () => {
2024-03-18 06:17:04 +00:00
if (model1.formData.id) {
2024-03-20 02:43:46 +00:00
ProfileApi.updateUserById(model1.formData).then((res) => {
2024-03-18 06:17:04 +00:00
if (res.code === 200) {
uni.redirectTo({
2024-03-20 02:43:46 +00:00
url: "/pagesApp/user", // 要跳转到的页面路径
2024-03-18 06:17:04 +00:00
});
}
});
} else {
ProfileApi.addUser({ userType: 1, ...model1.formData }).then((res) => {
2024-03-18 06:17:04 +00:00
if (res.code === 200) {
uni.redirectTo({
2024-03-19 02:15:47 +00:00
url: "/pagesApp/user", // 要跳转到的页面路径
2024-03-18 06:17:04 +00:00
});
}
});
}
};
2024-03-19 02:15:47 +00:00
const getRoleList = () => {
2024-03-20 02:43:46 +00:00
ProfileApi.getRoleList({}).then((res) => {
2024-03-19 02:15:47 +00:00
if (res.code === 200) {
contrlModalParams.role.list = (res.data as any).map((item: any) => {
return { ...item, name: item.roleName };
});
console.log(contrlModalParams.role.list);
}
});
};
onMounted(() => {
getRoleList();
});
2024-03-18 06:17:04 +00:00
onLoad((option) => {
2024-03-19 02:15:47 +00:00
// 接收传递的标题参数;
2024-03-18 06:17:04 +00:00
const title = (option as any).title;
const obj = JSON.parse((option as any).item)
model1.formData = {...obj, genderName: ['未知','男','女'][obj.gender]};
if (obj.roleVos.length > 0) {
model1.formData.roleIds = [obj.roleVos[0].id]
model1.formData.roleName = obj.roleVos[0].roleName
2024-03-19 02:15:47 +00:00
}
// 设置页面标题;
if (title) {
uni.setNavigationBarTitle({
title: title,
});
2024-03-18 06:17:04 +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 06:17:04 +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>