dialog.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <style scoped lang="scss">
  2. </style>
  3. <template>
  4. <div class="system-dialog-container">
  5. <el-drawer
  6. :title="state.dialog.title"
  7. v-model="state.dialog.isShowDialog"
  8. size="620px"
  9. append-to-body
  10. destroy-on-close
  11. :close-on-click-modal="false"
  12. >
  13. <el-form
  14. :model="state.ruleForm"
  15. :rules="state.rules"
  16. label-position="top"
  17. ref="formRef"
  18. size="default"
  19. label-width="100px"
  20. class="mt5 pd10">
  21. <el-form-item label="头像">
  22. <ext-upload v-model="state.ruleForm.avatar" @on-change="handleAvatarChange"></ext-upload>
  23. </el-form-item>
  24. <el-form-item label="用户名" prop="username">
  25. <el-input
  26. v-model="state.ruleForm.username"
  27. placeholder="用户名"
  28. clearable
  29. class="w100">
  30. </el-input>
  31. </el-form-item>
  32. <el-form-item label="昵称" prop="nickname">
  33. <el-input
  34. v-model="state.ruleForm.nickname"
  35. placeholder="昵称"
  36. clearable
  37. class="w100">
  38. </el-input>
  39. </el-form-item>
  40. <el-form-item label="密码" prop="password" v-if="!state.ruleForm.id">
  41. <el-input
  42. v-model="state.ruleForm.password"
  43. placeholder="密码"
  44. clearable
  45. class="w100">
  46. </el-input>
  47. </el-form-item>
  48. <el-form-item label="手机号" prop="mobilePhone">
  49. <el-input
  50. v-model="state.ruleForm.mobilePhone"
  51. placeholder="手机号"
  52. clearable
  53. class="w100">
  54. </el-input>
  55. </el-form-item>
  56. <el-form-item label="状态" prop="status">
  57. <ext-d-select
  58. type="AdminUser.status"
  59. v-model="state.ruleForm.status"
  60. placeholder="状态"
  61. clearable
  62. class="w100">
  63. </ext-d-select>
  64. </el-form-item>
  65. <el-form-item label="最后登录时间" v-if="state.ruleForm.id>0">
  66. <el-input
  67. v-model="state.ruleForm.lastLoginTime"
  68. placeholder="最后登录时间"
  69. clearable
  70. readonly
  71. class="w100">
  72. </el-input>
  73. </el-form-item>
  74. <el-card header="角色">
  75. <el-checkbox-group
  76. v-model="state.checkRoleIdList"
  77. @change="handleCheckedRoleChange"
  78. >
  79. <el-checkbox v-for="role in state.roleList" :key="role.id" :label="role.id">{{ role.roleName }}</el-checkbox>
  80. </el-checkbox-group>
  81. </el-card>
  82. </el-form>
  83. <template #footer>
  84. <div class="dialog-footer">
  85. <el-button @click="onCancel" size="default">取 消</el-button>
  86. <el-button :loading="state.btnLoading" type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
  87. </div>
  88. </template>
  89. </el-drawer>
  90. </div>
  91. </template>
  92. <script setup lang="ts" name="AdminUserDialog">
  93. import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
  94. import {Msg} from "/@/utils/message";
  95. import {$body, $get} from "/@/utils/request";
  96. import u from '/@/utils/u'
  97. import type {FormInstance, FormRules} from 'element-plus';
  98. import ExtDSelect from "/@/components/form/ExtDSelect.vue";
  99. import ExtUpload from "/@/components/form/ExtUpload.vue";
  100. // 引入组件
  101. // const ExtDetailForm = defineAsyncComponent(() => import('/@/components/form/ExtDetailForm.vue'));
  102. // 定义子组件向父组件传值/事件
  103. const emit = defineEmits(['refresh']);
  104. const formRef = ref();
  105. //定义初始变量,重置使用
  106. const initState = () => ({
  107. ruleForm: {
  108. id: 0
  109. },
  110. btnLoading: false,
  111. dialog: {
  112. isShowDialog: false,
  113. type: '',
  114. title: '',
  115. submitTxt: '',
  116. },
  117. rules: {
  118. username: [u.validator.required],
  119. mobilePhone: [u.validator.required],
  120. nickname: [u.validator.required],
  121. status: [u.validator.required],
  122. password: [u.validator.required],
  123. },
  124. roleList: [],
  125. checkRoleIdList: []
  126. })
  127. // 定义变量内容
  128. const state = reactive(initState());
  129. // 打开弹窗
  130. const open = (action: string = 'add', row: any) => {
  131. loadRole();
  132. state.dialog.title = u.dialog.actions[action].title + "『运营用户』"
  133. state.dialog.submitTxt = u.dialog.actions[action].btn + "『运营用户』"
  134. state.dialog.isShowDialog = true;
  135. if (action !== 'add') {
  136. loadData(row.id);
  137. }
  138. };
  139. const handleCheckedRoleChange = () => {
  140. }
  141. const handleAvatarChange = (val: string) => {
  142. console.log("avatar:", val)
  143. }
  144. // 关闭弹窗
  145. const onClose = () => {
  146. state.dialog.isShowDialog = false;
  147. Object.assign(state, initState())
  148. };
  149. // 取消
  150. const onCancel = () => {
  151. onClose();
  152. };
  153. // 提交
  154. const onSubmit = () => {
  155. formRef.value.validate((valid, fields) => {
  156. // console.log('basic checkForm!', valid,fields)
  157. if (valid) {
  158. state.btnLoading = true;
  159. const url = state.ruleForm.id > 0 ? "admin-user/modify" : "admin-user/add"
  160. $body(url, state.ruleForm).then(() => {
  161. state.btnLoading = false;
  162. Msg.message('操作成功');
  163. console.log('submit!')
  164. if (state.ruleForm.id > 0) {
  165. $body(`admin-user/updateRole`, {userId: state.ruleForm.id, roleIdList: state.checkRoleIdList}).then(() => {
  166. onClose();
  167. emit('refresh');
  168. })
  169. } else {
  170. onClose();
  171. emit('refresh');
  172. }
  173. }).catch(()=>{
  174. state.btnLoading = false;
  175. })
  176. } else {
  177. state.btnLoading = false;
  178. Msg.message('表单校验失败', 'error');
  179. }
  180. })
  181. // formRef.value.checkForm().then(() => {
  182. //
  183. // }).catch(() => {
  184. // state.btnLoading = false;
  185. // Msg.message('表单校验失败', 'error');
  186. // })
  187. };
  188. const handleFormChange = (formData: any) => {
  189. console.log(formData)
  190. }
  191. const loadRole = () => {
  192. $get(`role/list`).then((res: any) => {
  193. state.roleList = res;
  194. })
  195. }
  196. // 初始化表格数据
  197. const loadData = (id: number) => {
  198. $get(`admin-user/detail/${id}`).then((res: any) => {
  199. let {adminUser, roles} = res;
  200. state.ruleForm = adminUser;
  201. state.checkRoleIdList = roles.map(k => k.roleId)
  202. })
  203. }
  204. // 暴露变量
  205. defineExpose({
  206. open
  207. });
  208. </script>