role-edit.vue 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. <script setup lang="ts">
  2. import { reactive, ref } from "vue";
  3. import { enableOptions } from "@/constants/constants";
  4. import * as roleApi from "@/api/sys/role";
  5. import { message } from "@/utils/message";
  6. import type { FormInstance } from "element-plus";
  7. import { cloneDeep } from "@pureadmin/utils";
  8. const checkRoleCode = (rule: any, value: any, callback: any) => {
  9. if (value === "") {
  10. callback(new Error("请输入角色编码"));
  11. } else {
  12. const id = pageData.formData.id;
  13. roleApi.checkCode(value, id).then((res: any) => {
  14. if (res.success) {
  15. if (res.result) {
  16. callback(new Error("编码重复"));
  17. }
  18. }
  19. });
  20. callback();
  21. }
  22. };
  23. const formRef = ref<FormInstance>();
  24. const pageData: any = reactive({
  25. dialogVisible: false,
  26. title: "新增角色",
  27. formLoading: false,
  28. isUpdate: false,
  29. formData: {
  30. roleCode: "",
  31. roleName: "",
  32. description: "",
  33. enable: 1
  34. },
  35. formRules: {
  36. roleCode: [
  37. { required: true, message: "请输入角色编码", trigger: "blur" },
  38. { validator: checkRoleCode, trigger: "blur" }
  39. ],
  40. roleName: [{ required: true, message: "请输入角色名称", trigger: "blur" }],
  41. enable: [{ required: true, message: "请选择状态", trigger: "change" }]
  42. }
  43. });
  44. const emits = defineEmits(["ok", "close"]);
  45. const open = (data?: any, title?: string) => {
  46. pageData.formData = data || {
  47. roleCode: "",
  48. roleName: "",
  49. description: "",
  50. enable: 1
  51. };
  52. pageData.title = title || "新增角色";
  53. pageData.isUpdate = !!pageData.formData.id;
  54. pageData.dialogVisible = true;
  55. };
  56. const handleClose = () => {
  57. pageData.dialogVisible = false;
  58. emits("close");
  59. };
  60. const handleConfirm = () => {
  61. formRef.value!.validate((isValid: boolean) => {
  62. if (isValid) {
  63. const { id } = pageData.formData;
  64. if (id) {
  65. _update();
  66. } else {
  67. _save();
  68. }
  69. } else {
  70. message("表单校验失败", { type: "warning" });
  71. }
  72. });
  73. };
  74. const _save = () => {
  75. pageData.formLoading = true;
  76. const _data = cloneDeep(pageData.formData);
  77. roleApi
  78. .roleSave(_data)
  79. .then((res: any) => {
  80. if (res.success) {
  81. _confirm();
  82. } else {
  83. message(res.message, { type: "warning" });
  84. }
  85. })
  86. .finally(() => {
  87. pageData.formLoading = false;
  88. });
  89. };
  90. const _update = () => {
  91. pageData.formLoading = true;
  92. const _data = cloneDeep(pageData.formData);
  93. roleApi
  94. .roleUpdate(pageData.formData.id, _data)
  95. .then((res: any) => {
  96. if (res.success) {
  97. _confirm();
  98. } else {
  99. message(res.message, { type: "warning" });
  100. }
  101. })
  102. .finally(() => {
  103. pageData.formLoading = false;
  104. });
  105. };
  106. const _confirm = () => {
  107. pageData.dialogVisible = false;
  108. emits("ok");
  109. };
  110. defineExpose({ open });
  111. defineOptions({ name: "RoleEdit" });
  112. </script>
  113. <template>
  114. <el-dialog
  115. v-model="pageData.dialogVisible"
  116. :title="pageData.title"
  117. destroy-on-close
  118. :width="800"
  119. >
  120. <div class="el-dialog-content">
  121. <el-form
  122. :model="pageData.formData"
  123. style="margin-top: 20px; width: 70%"
  124. label-width="auto"
  125. :rules="pageData.formRules"
  126. ref="formRef"
  127. :loading="pageData.formLoading"
  128. >
  129. <el-form-item label="角色编码" prop="roleCode">
  130. <el-input
  131. v-model="pageData.formData.roleCode"
  132. clearable
  133. placeholder="请输入角色编码"
  134. :disabled="pageData.isUpdate"
  135. />
  136. </el-form-item>
  137. <el-form-item label="角色名称" prop="roleName">
  138. <el-input
  139. v-model="pageData.formData.roleName"
  140. clearable
  141. placeholder="请输入角色名称"
  142. />
  143. </el-form-item>
  144. <el-form-item label="描述" prop="description">
  145. <el-input
  146. type="textarea"
  147. v-model="pageData.formData.description"
  148. :autosize="{ minRows: 3 }"
  149. clearable
  150. placeholder="文本输入,不得超过200"
  151. max="200"
  152. />
  153. </el-form-item>
  154. <el-form-item label="状态" prop="enable">
  155. <el-radio-group v-model="pageData.formData.enable">
  156. <el-radio
  157. v-for="(item, index) in enableOptions"
  158. :label="item.value"
  159. :key="index"
  160. >{{ item.label }}</el-radio
  161. >
  162. </el-radio-group>
  163. </el-form-item>
  164. </el-form>
  165. </div>
  166. <template #footer>
  167. <el-button @click="handleClose">取消</el-button>
  168. <el-button type="primary" @click="handleConfirm">确认</el-button>
  169. </template>
  170. </el-dialog>
  171. </template>
  172. <style scoped></style>