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.
house/fount/views/modules/users/add-or-update.vue

413 lines
16 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<!-- 添加/编辑页面容器 -->
<div class="addEdit-block">
<!-- 表单组件 -->
<el-form
class="detail-form-content"
ref="ruleForm"
:model="ruleForm"
:rules="rules"
label-width="80px"
:style="{backgroundColor:addEditForm.addEditBoxColor}" <!-- -->
>
<el-row> <!-- 表单布局行 -->
<el-col :span="12"> <!-- 左侧列宽度为12 -->
<el-form-item class="input" v-if="type!='info'" label="用户名" prop="username"> <!-- 用户名输入框 -->
<el-input v-model="ruleForm.username"
placeholder="用户名" clearable :readonly="ro.username"></el-input>
</el-form-item>
<div v-else> <!-- 如果是查看模式,显示只读输入框 -->
<el-form-item class="input" label="用户名" prop="username">
<el-input v-model="ruleForm.username"
placeholder="用户名" readonly></el-input>
</el-form-item>
</div>
</el-col>
<el-col :span="12"> <!-- 12 -->
<el-form-item class="input" v-if="type!='info'" label="密码" prop="password"> <!-- 密码输入框 -->
<el-input v-model="ruleForm.password"
placeholder="密码" clearable :readonly="ro.password"></el-input>
</el-form-item>
<div v-else> <!-- 如果是查看模式,显示只读输入框 -->
<el-form-item class="input" label="密码" prop="password">
<el-input v-model="ruleForm.password"
placeholder="密码" readonly></el-input>
</el-form-item>
</div>
</el-col>
</el-row>
<el-form-item class="btn"> <!-- 按钮容器 -->
<el-button v-if="type!='info'" type="primary" class="btn-success" @click="onSubmit">提交</el-button> <!-- 提交按钮 -->
<el-button v-if="type!='info'" class="btn-close" @click="back()">取消</el-button> <!-- 取消按钮 -->
<el-button v-if="type=='info'" class="btn-close" @click="back()">返回</el-button> <!-- 返回按钮 -->
</el-form-item>
</el-form>
</div>
</template>
<script>
// 引入验证工具函数
import { isNumber,isIntNumer,isEmail,isPhone, isMobile,isURL,checkIdCard } from "@/utils/validate";
export default {
data() {
let self = this
// 定义身份证验证规则
var validateIdCard = (rule, value, callback) => {
if(!value){
callback();
} else if (!checkIdCard(value)) {
callback(new Error("请输入正确的身份证号码"));
} else {
callback();
}
};
// 定义URL验证规则
var validateUrl = (rule, value, callback) => {
if(!value){
callback();
} else if (!isURL(value)) {
callback(new Error("请输入正确的URL地址"));
} else {
callback();
}
};
// 定义手机号验证规则
var validateMobile = (rule, value, callback) => {
if(!value){
callback();
} else if (!isMobile(value)) {
callback(new Error("请输入正确的手机号码"));
} else {
callback();
}
};
// 定义电话号验证规则
var validatePhone = (rule, value, callback) => {
if(!value){
callback();
} else if (!isPhone(value)) {
callback(new Error("请输入正确的电话号码"));
} else {
callback();
}
};
// 定义邮箱验证规则
var validateEmail = (rule, value, callback) => {
if(!value){
callback();
} else if (!isEmail(value)) {
callback(new Error("请输入正确的邮箱地址"));
} else {
callback();
}
};
// 定义数字验证规则
var validateNumber = (rule, value, callback) => {
if(!value){
callback();
} else if (!isNumber(value)) {
callback(new Error("请输入数字"));
} else {
callback();
}
};
// 定义整数验证规则
var validateIntNumber = (rule, value, callback) => {
if(!value){
callback();
} else if (!isIntNumer(value)) {
callback(new Error("请输入整数"));
} else {
callback();
}
};
return {
// 表单样式配置
addEditForm: { /* 样式配置参数 */ },
id: '',
type: '',
ro:{
// 各字段只读状态配置
username : false,
password : false,
role : false,
},
// 表单数据模型
ruleForm: {
username: '',
password: '',
},
// 表单验证规则
rules: {
username: [
{ required: true, message: '用户名不能为空', trigger: 'blur' }, // 必填验证
],
password: [
{ required: true, message: '密码不能为空', trigger: 'blur' }, // 必填验证
],
role: [
],
}
};
},
props: ["parent"], // 定义父组件传入的属性
computed: {
},
created() {
this.addEditStyleChange() // 调用样式更新方法
this.addEditUploadStyleChange() // 调用上传组件样式更新方法
},
methods: {
// 下载文件方法
download(file){
window.open(`${file}`)
},
// 初始化方法
init(id,type) {
if (id) {
this.id = id; // 设置当前ID
this.type = type; // 设置当前类型
}
if(this.type=='info'||this.type=='else'){ // 如果是查看或编辑模式
this.info(id); // 获取详细信息
}else if(this.type=='cross'){ // 如果是关联模式
var obj = this.$storage.getObj('crossObj'); // 获取关联对象
for (var o in obj){ // 遍历关联对象属性
if(o=='username'){
this.ruleForm.username = obj[o]; // 设置用户名
this.ro.username = true; // 设置为只读
continue;
}
if(o=='password'){
this.ruleForm.password = obj[o]; // 设置密码
this.ro.password = true; // 设置为只读
continue;
}
if(o=='role'){
this.ruleForm.role = obj[o]; // 设置角色
this.ro.role = true; // 设置为只读
continue;
}
}
}
},
// 获取详细信息方法
info(id) {
this.$http({
url: `users/info/${id}`,
method: "get"
}).then(({ data }) => {
if (data && data.code === 0) {
this.ruleForm = data.data; // 更新表单数据
} else {
this.$message.error(data.msg);
}
});
},
// 提交表单方法
onSubmit() {
// 表单验证并提交
this.$refs["ruleForm"].validate(valid => {
if (valid) {
this.$http({
url: `users/${!this.ruleForm.id ? "save" : "update"}`, // 请求保存或更新接口
method: "post",
data: this.ruleForm
}).then(({ data }) => {
if (data && data.code === 0) {
this.$message({
message: "操作成功", // 显示成功消息
type: "success",
duration: 1500,
onClose: () => {
this.parent.showFlag = true; // 显示列表页
this.parent.addOrUpdateFlag = false; // 隐藏表单页
this.parent.usersCrossAddOrUpdateFlag = false; // 隐藏关联表单页
this.parent.search(); // 刷新列表
this.parent.contentStyleChange(); // 更新列表样式
}
});
} else {
this.$message.error(data.msg); // 显示错误消息
}
});
}
});
},
// 获取唯一标识符方法
getUUID () {
return new Date().getTime();
},
// 返回方法
back() {
this.parent.showFlag = true; // 显示列表页
this.parent.addOrUpdateFlag = false; // 隐藏表单页
this.parent.usersCrossAddOrUpdateFlag = false; // 隐藏关联表单页
this.parent.contentStyleChange(); // 更新列表样式
},
addEditStyleChange() { // 更新表单样式方法
this.$nextTick(()=>{ // 在DOM更新后执行
// 更新input样式
document.querySelectorAll('.addEdit-block .input .el-input__inner').forEach(el=>{
el.style.height = this.addEditForm.inputHeight;
el.style.color = this.addEditForm.inputFontColor;
el.style.fontSize = this.addEditForm.inputFontSize;
el.style.borderWidth = this.addEditForm.inputBorderWidth;
el.style.borderStyle = this.addEditForm.inputBorderStyle;
el.style.borderColor = this.addEditForm.inputBorderColor;
el.style.borderRadius = this.addEditForm.inputBorderRadius;
el.style.backgroundColor = this.addEditForm.inputBgColor;
});
document.querySelectorAll('.addEdit-block .input .el-form-item__label').forEach(el=>{
el.style.lineHeight = this.addEditForm.inputHeight;
el.style.color = this.addEditForm.inputLableColor;
el.style.fontSize = this.addEditForm.inputLableFontSize;
});
// 更新select样式
document.querySelectorAll('.addEdit-block .select .el-input__inner').forEach(el=>{
el.style.height = this.addEditForm.selectHeight;
el.style.color = this.addEditForm.selectFontColor;
el.style.fontSize = this.addEditForm.selectFontSize;
el.style.borderWidth = this.addEditForm.selectBorderWidth;
el.style.borderStyle = this.addEditForm.selectBorderStyle;
el.style.borderColor = this.addEditForm.selectBorderColor;
el.style.borderRadius = this.addEditForm.selectBorderRadius;
el.style.backgroundColor = this.addEditForm.selectBgColor;
});
document.querySelectorAll('.addEdit-block .select .el-form-item__label').forEach(el=>{
el.style.lineHeight = this.addEditForm.selectHeight;
el.style.color = this.addEditForm.selectLableColor;
el.style.fontSize = this.addEditForm.selectLableFontSize;
});
document.querySelectorAll('.addEdit-block .select .el-select__caret').forEach(el=>{
el.style.color = this.addEditForm.selectIconFontColor;
el.style.fontSize = this.addEditForm.selectIconFontSize;
});
// 更新date样式
document.querySelectorAll('.addEdit-block .date .el-input__inner').forEach(el=>{
el.style.height = this.addEditForm.dateHeight;
el.style.color = this.addEditForm.dateFontColor;
el.style.fontSize = this.addEditForm.dateFontSize;
el.style.borderWidth = this.addEditForm.dateBorderWidth;
el.style.borderStyle = this.addEditForm.dateBorderStyle;
el.style.borderColor = this.addEditForm.dateBorderColor;
el.style.borderRadius = this.addEditForm.dateBorderRadius;
el.style.backgroundColor = this.addEditForm.dateBgColor;
});
document.querySelectorAll('.addEdit-block .date .el-form-item__label').forEach(el=>{
el.style.lineHeight = this.addEditForm.dateHeight;
el.style.color = this.addEditForm.dateLableColor;
el.style.fontSize = this.addEditForm.dateLableFontSize;
});
document.querySelectorAll('.addEdit-block .date .el-input__icon').forEach(el=>{
el.style.color = this.addEditForm.dateIconFontColor;
el.style.fontSize = this.addEditForm.dateIconFontSize;
el.style.lineHeight = this.addEditForm.dateHeight;
});
// 更新upload样式
let iconLineHeight = parseInt(this.addEditForm.uploadHeight) - parseInt(this.addEditForm.uploadBorderWidth) * 2 + 'px';
document.querySelectorAll('.addEdit-block .upload .el-upload--picture-card').forEach(el=>{
el.style.width = this.addEditForm.uploadHeight;
el.style.height = this.addEditForm.uploadHeight;
el.style.borderWidth = this.addEditForm.uploadBorderWidth;
el.style.borderStyle = this.addEditForm.uploadBorderStyle;
el.style.borderColor = this.addEditForm.uploadBorderColor;
el.style.borderRadius = this.addEditForm.uploadBorderRadius;
el.style.backgroundColor = this.addEditForm.uploadBgColor;
});
document.querySelectorAll('.addEdit-block .upload .el-form-item__label').forEach(el=>{
el.style.lineHeight = this.addEditForm.uploadHeight;
el.style.color = this.addEditForm.uploadLableColor;
el.style.fontSize = this.addEditForm.uploadLableFontSize;
});
document.querySelectorAll('.addEdit-block .upload .el-icon-plus').forEach(el=>{
el.style.color = this.addEditForm.uploadIconFontColor;
el.style.fontSize = this.addEditForm.uploadIconFontSize;
el.style.lineHeight = iconLineHeight;
el.style.display = 'block';
});
// 更新textarea样式
document.querySelectorAll('.addEdit-block .textarea .el-textarea__inner').forEach(el=>{
el.style.height = this.addEditForm.textareaHeight;
el.style.color = this.addEditForm.textareaFontColor;
el.style.fontSize = this.addEditForm.textareaFontSize;
el.style.borderWidth = this.addEditForm.textareaBorderWidth;
el.style.borderStyle = this.addEditForm.textareaBorderStyle;
el.style.borderColor = this.addEditForm.textareaBorderColor;
el.style.borderRadius = this.addEditForm.textareaBorderRadius;
el.style.backgroundColor = this.addEditForm.textareaBgColor;
});
document.querySelectorAll('.addEdit-block .textarea .el-form-item__label').forEach(el=>{
// el.style.lineHeight = this.addEditForm.textareaHeight
el.style.color = this.addEditForm.textareaLableColor;
el.style.fontSize = this.addEditForm.textareaLableFontSize;
});
// 更新按钮样式
document.querySelectorAll('.addEdit-block .btn .btn-success').forEach(el=>{
el.style.width = this.addEditForm.btnSaveWidth;
el.style.height = this.addEditForm.btnSaveHeight;
el.style.color = this.addEditForm.btnSaveFontColor;
el.style.fontSize = this.addEditForm.btnSaveFontSize;
el.style.borderWidth = this.addEditForm.btnSaveBorderWidth;
el.style.borderStyle = this.addEditForm.btnSaveBorderStyle;
el.style.borderColor = this.addEditForm.btnSaveBorderColor;
el.style.borderRadius = this.addEditForm.btnSaveBorderRadius;
el.style.backgroundColor = this.addEditForm.btnSaveBgColor;
});
// 更新取消按钮样式
document.querySelectorAll('.addEdit-block .btn .btn-close').forEach(el=>{
el.style.width = this.addEditForm.btnCancelWidth;
el.style.height = this.addEditForm.btnCancelHeight;
el.style.color = this.addEditForm.btnCancelFontColor;
el.style.fontSize = this.addEditForm.btnCancelFontSize;
el.style.borderWidth = this.addEditForm.btnCancelBorderWidth;
el.style.borderStyle = this.addEditForm.btnCancelBorderStyle;
el.style.borderColor = this.addEditForm.btnCancelBorderColor;
el.style.borderRadius = this.addEditForm.btnCancelBorderRadius;
el.style.backgroundColor = this.addEditForm.btnCancelBgColor;
});
});
},
addEditUploadStyleChange() { // 更新上传组件样式方法
this.$nextTick(()=>{ // 在DOM更新后执行
document.querySelectorAll('.addEdit-block .upload .el-upload-list--picture-card .el-upload-list__item').forEach(el=>{
el.style.width = this.addEditForm.uploadHeight;
el.style.height = this.addEditForm.uploadHeight;
el.style.borderWidth = this.addEditForm.uploadBorderWidth;
el.style.borderStyle = this.addEditForm.uploadBorderStyle;
el.style.borderColor = this.addEditForm.uploadBorderColor;
el.style.borderRadius = this.addEditForm.uploadBorderRadius;
el.style.backgroundColor = this.addEditForm.uploadBgColor;
});
});
},
}
};
</script>
<style lang="scss">
.editor{
height: 500px; <!-- -->
& /deep/ .ql-container { <!-- 编辑器内容区域 -->
height: 310px; <!-- 设置内容区域高度 -->
}
}
.amap-wrapper { <!-- 地图容器 -->
width: 100%; <!-- 宽度为100% -->
height: 500px; <!-- 高度为500px -->
}
.search-box { <!-- 搜索原高度 -->
position: absolute; <!-- 绝对定位 -->
}
.addEdit-block { <!-- 表单容器 -->
margin: -10px; <!-- 外边距 -->
}
.detail-form-content { <!-- 表单内容 -->
padding: 12px; <!-- 内边距 -->
}
.btn .el-button { <!-- 按钮样式 -->
padding: 0; <!-- 无内边距 -->
}
</style>