dialog.vue 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <template>
  2. <div class="system-role-dialog-container">
  3. <el-dialog :title="state.dialog.title" v-model="state.dialog.isShowDialog" width="769px">
  4. <el-form ref="roleDialogFormRef" :model="state.ruleForm" :rules="state.rules" size="default" label-width="90px">
  5. <el-row :gutter="35">
  6. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  7. <el-form-item label="角色名称" prop="roleName">
  8. <template #label>
  9. <el-tooltip effect="dark" content="用于标识日常使用的角色名称" placement="top-start">
  10. <span>角色名称</span>
  11. </el-tooltip>
  12. </template>
  13. <el-input v-model="state.ruleForm.roleName" placeholder="请输入角色标识" clearable></el-input>
  14. </el-form-item>
  15. </el-col>
  16. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  17. <el-form-item label="角色别名" prop="roleDesc">
  18. <el-input v-model="state.ruleForm.roleDesc" placeholder="请输入角色别名" clearable></el-input>
  19. </el-form-item>
  20. </el-col>
  21. <!--
  22. <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  23. <el-form-item label="排序">
  24. <el-input-number v-model="state.ruleForm.weight" :min="0" :max="999" controls-position="right" placeholder="请输入排序" class="w100"/>
  25. </el-form-item>
  26. </el-col>-->
  27. <!-- <el-col :xs="24" :sm="12" :md="12" :lg="12" :xl="12" class="mb20">
  28. <el-form-item label="角色状态">
  29. <el-switch v-model="state.ruleForm.status" inline-prompt active-text="启" inactive-text="禁"></el-switch>
  30. </el-form-item>
  31. </el-col>-->
  32. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
  33. <el-form-item label="权限组">
  34. <el-input readonly v-model="state.ruleForm.permissions" type="textarea" placeholder="请输入角色描述" ></el-input>
  35. </el-form-item>
  36. </el-col>
  37. <!-- <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
  38. <el-form-item label="菜单权限">
  39. <el-tree :data="state.menuData" :props="state.menuProps" show-checkbox class="menu-data-tree"/>
  40. </el-form-item>
  41. </el-col>-->
  42. <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24" class="mb20">
  43. <span style="margin-left: 5px">
  44. <el-button v-auth="'role.delete'" type="danger" @click="handleDelete" size="default">删除角色</el-button>
  45. </span>
  46. </el-col>
  47. </el-row>
  48. </el-form>
  49. <template #footer>
  50. <span class="dialog-footer">
  51. <el-button @click="onCancel" size="default">取 消</el-button>
  52. <el-button type="primary" @click="onSubmit(roleDialogFormRef)" size="default">{{ state.dialog.submitTxt }}</el-button>
  53. </span>
  54. </template>
  55. </el-dialog>
  56. </div>
  57. </template>
  58. <script setup lang="ts" name="systemRoleDialog">
  59. import {reactive, ref} from 'vue';
  60. import {Msg} from "/@/utils/message";
  61. import {$body, $get} from "/@/utils/request";
  62. import u from "/@/utils/u";
  63. import type {FormInstance, FormRules} from 'element-plus'
  64. // 定义子组件向父组件传值/事件
  65. const emit = defineEmits(['refresh']);
  66. // 定义变量内容
  67. const roleDialogFormRef = ref();
  68. const state = reactive({
  69. readonly:false,
  70. action:'add',
  71. ruleForm: {
  72. id:0,
  73. name: '', // 角色名称
  74. roleSign: '', // 角色标识
  75. sort: 0, // 排序
  76. status: true, // 角色状态
  77. describe: '', // 角色描述
  78. },
  79. rules: {
  80. roleName: [u.validator.required, u.validator.length32],
  81. },
  82. menuData: [] as TreeType[],
  83. menuProps: {
  84. children: 'children',
  85. label: 'label',
  86. },
  87. dialog: {
  88. isShowDialog: false,
  89. type: '',
  90. title: '',
  91. submitTxt: '',
  92. },
  93. });
  94. // 打开弹窗
  95. const openDialog = (id: number, readonly: boolean) => {
  96. state.readonly = readonly;
  97. if (readonly) {
  98. if (id > 0) {
  99. state.dialog.title = '角色详情';
  100. state.dialog.submitTxt = '确定';
  101. loadData(id);
  102. } else {
  103. Msg.message('角色数据错误', 'error')
  104. return;
  105. }
  106. } else {
  107. if (id > 0) {
  108. state.dialog.title = '修改角色';
  109. state.dialog.submitTxt = '修 改';
  110. loadData(id)
  111. } else {
  112. state.dialog.title = '新增角色';
  113. state.dialog.submitTxt = '新 增';
  114. }
  115. }
  116. state.dialog.isShowDialog = true;
  117. };
  118. // 关闭弹窗
  119. const closeDialog = () => {
  120. state.dialog.isShowDialog = false;
  121. };
  122. // 取消
  123. const onCancel = () => {
  124. closeDialog();
  125. };
  126. const handleDelete=()=>{
  127. Msg.confirm(`确认删除该角色:${state.ruleForm.name}吗?该操作无法撤回`).then(()=>{
  128. $get(`/role/remove/${state.ruleForm.id}`).then(()=>{
  129. Msg.message("操作成功","success")
  130. })
  131. })
  132. }
  133. // 提交
  134. const onSubmit = (formEl: FormInstance | undefined) => {
  135. if (state.readonly) {
  136. closeDialog();
  137. return;
  138. }
  139. //console.log(state.ruleForm)
  140. if (!formEl) return
  141. formEl.validate((valid, fields) => {
  142. //console.log(fields)
  143. if (valid) {
  144. const url = state.ruleForm.id > 0 ? "role/modify" : "role/add"
  145. $body(url, state.ruleForm).then(() => {
  146. Msg.message("操作成功","success")
  147. //console.log('submit!')
  148. closeDialog();
  149. emit('refresh');
  150. })
  151. } else {
  152. //console.log('error submit!', fields)
  153. }
  154. })
  155. };
  156. // 获取菜单结构数据
  157. const loadData = (id: number) => {
  158. $get(`role/detail/${id}`).then((res: any) => {
  159. state.ruleForm = res;
  160. })
  161. }
  162. // 暴露变量
  163. defineExpose({
  164. openDialog,
  165. });
  166. </script>
  167. <style scoped lang="scss">
  168. .system-role-dialog-container {
  169. .menu-data-tree {
  170. width: 100%;
  171. border: 1px solid var(--el-border-color);
  172. border-radius: var(--el-input-border-radius, var(--el-border-radius-base));
  173. padding: 5px;
  174. }
  175. }
  176. </style>