dialog.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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. class="pd10"
  12. :close-on-click-modal="false"
  13. @close="onClose"
  14. >
  15. <el-form
  16. :model="state.ruleForm"
  17. :rules="state.rules"
  18. label-position="top"
  19. ref="formRef"
  20. size="default"
  21. label-width="100px"
  22. class="mt5 pd10">
  23. <el-form-item label="头像">
  24. <ext-upload v-model="state.ruleForm.avatar" @on-change="handleAvatarChange"></ext-upload>
  25. </el-form-item>
  26. <el-form-item label="用户名" prop="username">
  27. <el-input
  28. v-model="state.ruleForm.username"
  29. placeholder="用户名"
  30. clearable
  31. class="w100">
  32. </el-input>
  33. </el-form-item>
  34. <el-form-item label="昵称" prop="nickname">
  35. <el-input
  36. v-model="state.ruleForm.nickname"
  37. placeholder="昵称"
  38. clearable
  39. class="w100">
  40. </el-input>
  41. </el-form-item>
  42. <el-form-item label="密码" prop="password" v-if="!state.ruleForm.id">
  43. <el-input
  44. v-model="state.ruleForm.password"
  45. placeholder="密码"
  46. clearable
  47. show-password
  48. type="password"
  49. class="w100">
  50. </el-input>
  51. </el-form-item>
  52. <el-form-item label="手机号" prop="mobilePhone">
  53. <el-input
  54. v-model="state.ruleForm.mobilePhone"
  55. placeholder="手机号"
  56. clearable
  57. class="w100">
  58. </el-input>
  59. </el-form-item>
  60. <el-form-item label="状态" prop="status">
  61. <ext-d-select
  62. type="AdminUser.status"
  63. v-model="state.ruleForm.status"
  64. placeholder="状态"
  65. clearable
  66. class="w100">
  67. </ext-d-select>
  68. </el-form-item>
  69. <el-form-item label="最后登录时间" v-if="state.ruleForm.id>0">
  70. <el-input
  71. v-model="state.ruleForm.lastLoginTime"
  72. placeholder="最后登录时间"
  73. clearable
  74. readonly
  75. class="w100">
  76. </el-input>
  77. </el-form-item>
  78. <el-card header="角色">
  79. <el-checkbox-group
  80. v-model="state.checkRoleIdList"
  81. @change="handleCheckedRoleChange"
  82. >
  83. <el-checkbox v-for="role in state.roleList" :key="role.id" :label="role.id">{{ role.roleName }}</el-checkbox>
  84. </el-checkbox-group>
  85. </el-card>
  86. <el-card header="管理站点" style="margin-top: 20px;">
  87. <ext-select
  88. v-model="state.adminUserStationIdList"
  89. multiple
  90. placeholder="关联站点"
  91. url="station/list"
  92. url-method="get"
  93. label-key="stationName"
  94. value-key="stationId"
  95. data-key=""
  96. clearable
  97. class="w100 mt5">
  98. </ext-select>
  99. </el-card>
  100. </el-form>
  101. <template #footer>
  102. <div class="dialog-footer">
  103. <el-button @click="onCancel" size="default">取 消</el-button>
  104. <el-button :loading="state.btnLoading" type="primary" @click="onSubmit" size="default">{{ state.dialog.submitTxt }}</el-button>
  105. </div>
  106. </template>
  107. </el-drawer>
  108. </div>
  109. </template>
  110. <script setup lang="ts" name="AdminUserDialog">
  111. import {defineAsyncComponent, reactive, onMounted, ref} from 'vue';
  112. import {Msg} from "/@/utils/message";
  113. import {$body, $get} from "/@/utils/request";
  114. import u from '/@/utils/u'
  115. import type {FormInstance, FormRules} from 'element-plus';
  116. import ExtDSelect from "/@/components/form/ExtDSelect.vue";
  117. import ExtUpload from "/@/components/form/ExtUpload.vue";
  118. import ExtSelect from "/@/components/form/ExtSelect.vue";
  119. import {Session} from "/@/utils/storage";
  120. // 引入组件
  121. // const ExtDetailForm = defineAsyncComponent(() => import('/@/components/form/ExtDetailForm.vue'));
  122. // 定义子组件向父组件传值/事件
  123. const emit = defineEmits(['refresh']);
  124. const formRef = ref();
  125. //定义初始变量,重置使用
  126. const initState = () => ({
  127. ruleForm: {
  128. id: 0
  129. },
  130. btnLoading: false,
  131. dialog: {
  132. isShowDialog: false,
  133. type: '',
  134. title: '',
  135. submitTxt: '',
  136. },
  137. rules: {
  138. username: [u.validator.required],
  139. mobilePhone: [u.validator.required, u.validator.mobile],
  140. nickname: [u.validator.required],
  141. status: [u.validator.required],
  142. password: [u.validator.required],
  143. },
  144. roleList: [],
  145. checkRoleIdList: [],
  146. adminUserStationList: [],
  147. adminUserStationIdList: []
  148. })
  149. // 定义变量内容
  150. const state = reactive(initState());
  151. // 打开弹窗
  152. const open = (action: string = 'add', row: any) => {
  153. console.log(state.ruleForm)
  154. loadRole(row);
  155. state.dialog.title = u.dialog.actions[action].title + "『运营用户』"
  156. state.dialog.submitTxt = u.dialog.actions[action].btn + "『运营用户』"
  157. state.dialog.isShowDialog = true;
  158. if (action !== 'add') {
  159. loadData(row.id);
  160. }
  161. };
  162. const handleCheckedRoleChange = () => {
  163. }
  164. const handleAvatarChange = (val: string) => {
  165. console.log("avatar:", val)
  166. }
  167. // 关闭弹窗
  168. const onClose = () => {
  169. state.dialog.isShowDialog = false;
  170. Object.assign(state, initState())
  171. console.log(state.ruleForm)
  172. };
  173. // 取消
  174. const onCancel = () => {
  175. onClose();
  176. };
  177. // 提交
  178. const onSubmit = () => {
  179. formRef.value.validate((valid, fields) => {
  180. if (valid) {
  181. state.btnLoading = true;
  182. const url = state.ruleForm.id > 0 ? "admin-user/modify" : "admin-user/add"
  183. $body(url, state.ruleForm).then((id) => {
  184. if (id) {
  185. state.ruleForm.id = id;
  186. }
  187. Msg.message('操作成功');
  188. if (state.ruleForm.id > 0) {
  189. $body(`admin-user/updateRole`, {userId: state.ruleForm.id, roleIdList: state.checkRoleIdList}).then(() => {
  190. emit('refresh');
  191. state.btnLoading = false;
  192. if(state.adminUserStationIdList.length ===0){
  193. onClose()
  194. }
  195. })
  196. } else {
  197. emit('refresh');
  198. }
  199. if (state.adminUserStationIdList.length > 0) {
  200. let adminUserStationList = state.adminUserStationIdList.map(k => {
  201. return {
  202. adminUserId: state.ruleForm.id,
  203. stationId: k
  204. }
  205. })
  206. $body(`admin-user-station/save`, adminUserStationList).then(() => {
  207. emit('refresh');
  208. state.btnLoading = false;
  209. onClose()
  210. })
  211. }
  212. }).catch(() => {
  213. state.btnLoading = false;
  214. })
  215. } else {
  216. state.btnLoading = false;
  217. Msg.message('表单校验失败', 'error');
  218. }
  219. })
  220. // formRef.value.checkForm().then(() => {
  221. //
  222. // }).catch(() => {
  223. // state.btnLoading = false;
  224. // Msg.message('表单校验失败', 'error');
  225. // })
  226. };
  227. const handleFormChange = (formData: any) => {
  228. console.log(formData)
  229. }
  230. const loadRole = (row: any) => {
  231. $get(`role/list`).then((res: any) => {
  232. state.roleList = res;
  233. if (!row || !row.id) {
  234. if (res && !u.isEmptyOrNull(res)) {
  235. state.checkRoleIdList = [res[0].id]
  236. }
  237. }
  238. })
  239. }
  240. // 初始化表格数据
  241. const loadData = (id: number) => {
  242. $get(`admin-user/detail/${id}`).then((res: any) => {
  243. let {adminUser, roles} = res;
  244. state.ruleForm = adminUser;
  245. state.checkRoleIdList = roles.map(k => k.roleId)
  246. loadAdminUserStationList()
  247. })
  248. }
  249. const loadAdminUserStationList = () => {
  250. if(state.ruleForm.id<=0){
  251. return;
  252. }
  253. let param = {
  254. userId: state.ruleForm.id,
  255. pageNum: 1,
  256. pageSize: 1024
  257. }
  258. $get(`admin-user-station/list`, param).then((res: any) => {
  259. let {list} = res;
  260. state.adminUserStationIdList = list.map(k => k.stationId)
  261. state.adminUserStationList = res.list;
  262. })
  263. }
  264. // 暴露变量
  265. defineExpose({
  266. open
  267. });
  268. </script>