accountEdit.vue 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. <!--
  2. * @Author: Badguy
  3. * @Date: 2022-02-15 11:37:42
  4. * @LastEditTime: 2022-02-23 19:24:51
  5. * @LastEditors: your name
  6. * @Description: 编辑账号
  7. * have a nice day!
  8. -->
  9. <template>
  10. <div class="account-edit">
  11. <div class="account-edit-header">
  12. <div class="title">
  13. <div class="title-left">{{ pageTitle }}</div>
  14. <!-- <div class="account-status">
  15. 是否启用
  16. <el-radio
  17. v-model="accountForm.status"
  18. v-if="accountForm.status == 1 || accountForm.status == 3"
  19. :label="1"
  20. >是</el-radio
  21. >
  22. <el-radio v-model="accountForm.status" v-if="accountForm.status == 2" :label="2"
  23. >是</el-radio
  24. >
  25. <el-radio v-model="accountForm.status" :label="3">否</el-radio>
  26. </div> -->
  27. <div>
  28. <!-- <button type="submmit" class="btn-delete" @click="delate">删除</button> -->
  29. <button
  30. type="submmit"
  31. class="btn-save"
  32. @click="handleClickSave('accountForm')"
  33. >保存</button>
  34. </div>
  35. </div>
  36. <div class="title-content">
  37. <el-form
  38. ref="accountForm"
  39. label-width="80px"
  40. :inline="true"
  41. :model="accountForm"
  42. :rules="formRules"
  43. >
  44. <el-row :gutter="40">
  45. <el-col :span="5">
  46. <el-form-item
  47. label="账号名称"
  48. prop="name"
  49. >
  50. <el-input
  51. v-model="accountForm.name"
  52. maxlength="32"
  53. placeholder="请输入名称"
  54. size="small"
  55. />
  56. </el-form-item>
  57. </el-col>
  58. <el-col :span="5">
  59. <el-form-item
  60. label="登录密码"
  61. prop="pwd"
  62. >
  63. <el-input
  64. v-model="accountForm.pwd"
  65. placeholder="*******"
  66. size="small"
  67. disabled
  68. />
  69. </el-form-item>
  70. </el-col>
  71. <el-col :span="2">
  72. <button
  73. class="btn-reset-pwd"
  74. @click="resetPwd"
  75. >重置密码</button>
  76. </el-col>
  77. <el-col :span="8">
  78. <el-form-item
  79. label="描述"
  80. prop="desc"
  81. >
  82. <el-input
  83. v-model="accountForm.desc"
  84. maxlength="128"
  85. placeholder="描述内容···"
  86. size="small"
  87. />
  88. </el-form-item>
  89. </el-col>
  90. </el-row>
  91. </el-form>
  92. </div>
  93. </div>
  94. <div class="content">
  95. <!-- 用户组和角色未开启 -->
  96. <el-row
  97. v-if="!openGroup && !openRole"
  98. :gutter="24"
  99. >
  100. <el-col :span="8">
  101. <div class="content-card content-card-lg">
  102. <PermissionTree
  103. title="权限树"
  104. :query-type="queryType"
  105. :checked-keys="permissionTreeChckedKeys"
  106. @getTreeData="getPermissionTreeChecked"
  107. />
  108. </div>
  109. </el-col>
  110. <el-col :span="8">
  111. <div class="content-card content-card-lg">
  112. <RulesOfCompetency
  113. title="权限规则"
  114. margin-b="20px"
  115. @getData="getRulesOfCompetency"
  116. />
  117. </div>
  118. </el-col>
  119. <el-col :span="8">
  120. <div class="content-card content-card-lg">
  121. <LoginPolicy
  122. :checked-list="loginPolicyCheckedList"
  123. @getCheckedList="getLoginPolicyChecked"
  124. />
  125. </div>
  126. </el-col>
  127. </el-row>
  128. <!-- 角色和用户组都开启 -->
  129. <el-row
  130. v-if="openGroup && openRole"
  131. :gutter="24"
  132. >
  133. <el-col :span="8">
  134. <div class="content-card content-card-xl">
  135. <AccountGroupTree
  136. title="所属账号组"
  137. nodekey="GroupId"
  138. :default-props="accountGroupTreeProps"
  139. :checked-keys="accountGroupTreeCheckedList"
  140. @getTreeData="getAccountGroupChecked"
  141. />
  142. </div>
  143. </el-col>
  144. <el-col :span="8">
  145. <div class="content-card content-card-xl">
  146. <RoleList
  147. title="角色列表"
  148. :role-type="roleType"
  149. :group-ids="groupIds"
  150. :check-box-list="roleListCheckedList"
  151. :number="8"
  152. :active="true"
  153. @checkChange="getRoleListChecked"
  154. @checkClick="selectRole"
  155. @roleListChange="roleListCheckedChange"
  156. />
  157. </div>
  158. </el-col>
  159. <el-col :span="8">
  160. <div class="content-card content-card-sm">
  161. <PermissionList
  162. title="权限列表"
  163. class="permission-list"
  164. :role-list="checkedRoles"
  165. :check="true"
  166. @Competen="Competen"
  167. />
  168. </div>
  169. <div class="content-card content-card-xs">
  170. <LoginPolicy
  171. :checked-list="loginPolicyCheckedList"
  172. @getCheckedList="getLoginPolicyChecked"
  173. />
  174. </div>
  175. </el-col>
  176. </el-row>
  177. <!-- 只开启用户组 -->
  178. <el-row
  179. v-if="openGroup && !openRole"
  180. :gutter="24"
  181. >
  182. <el-col :span="8">
  183. <div class="content-card content-card-xxl">
  184. <AccountGroupTree
  185. title="所属账号组"
  186. nodekey="GroupId"
  187. :default-props="accountGroupTreeProps"
  188. :checked-keys="accountGroupTreeCheckedList"
  189. @getTreeData="getAccountGroupChecked"
  190. />
  191. </div>
  192. </el-col>
  193. <el-col :span="8">
  194. <div class="content-card content-card-xxl">
  195. <PermissionTree
  196. title="权限树"
  197. :query-type="queryType"
  198. :query-ids="queryIds"
  199. :checked-keys="permissionTreeChckedKeys"
  200. @getTreeData="getPermissionTreeChecked"
  201. />
  202. </div>
  203. </el-col>
  204. <el-col :span="8">
  205. <div class="content-card content-card-md">
  206. <RulesOfCompetency
  207. title="权限规则"
  208. margin-b="20px"
  209. @getData="getRulesOfCompetency"
  210. />
  211. </div>
  212. <div class="content-card content-card-xs">
  213. <LoginPolicy
  214. :checked-list="loginPolicyCheckedList"
  215. @getCheckedList="getLoginPolicyChecked"
  216. />
  217. </div>
  218. </el-col>
  219. </el-row>
  220. <!-- 只开启角色 -->
  221. <el-row
  222. v-if="!openGroup && openRole"
  223. :gutter="24"
  224. >
  225. <el-col :span="8">
  226. <div class="content-card content-card-lg">
  227. <RoleList
  228. title="角色列表"
  229. :role-type="roleType"
  230. :check-box-list="roleListCheckedList"
  231. :number="8"
  232. :active="true"
  233. @checkChange="getRoleListChecked"
  234. @checkClick="selectRole"
  235. />
  236. </div>
  237. </el-col>
  238. <el-col :span="8">
  239. <div class="content-card content-card-lg">
  240. <PermissionList
  241. title="权限列表"
  242. :role-list="checkedRoles"
  243. :check="true"
  244. class="permission-list"
  245. @Competen="Competen"
  246. />
  247. </div>
  248. </el-col>
  249. <el-col :span="8">
  250. <div class="content-card content-card-lg">
  251. <LoginPolicy
  252. :checked-list="loginPolicyCheckedList"
  253. @getCheckedList="getLoginPolicyChecked"
  254. />
  255. </div>
  256. </el-col>
  257. </el-row>
  258. </div>
  259. <!-- <Dialog :flag="dialogVisible">
  260. <div class="closeDialog">
  261. <div class="title">删除账号</div>
  262. <div class="content">是否确定要删除该账号?</div>
  263. <div class="foot right t30">
  264. <el-button
  265. size="medium"
  266. type="danger"
  267. class="r24"
  268. >删除</el-button>
  269. <el-button size="medium">取消</el-button>
  270. </div>
  271. </div>
  272. </Dialog> -->
  273. </div>
  274. </template>
  275. <script>
  276. import AccountGroupTree from '@/components/usergrouptree/index.vue'
  277. import PermissionTree from '@/components/permissiontree/index.vue'
  278. import RulesOfCompetency from '@/components/rulesofcompetency/index.vue'
  279. import LoginPolicy from '@/components/loginpolicy/index.vue'
  280. import RoleList from '@/components/rolelist/index.vue'
  281. import PermissionList from '@/components/permissionlist/index.vue'
  282. // import Dialog from '@/layout/components/Dialog'
  283. import { RoleAuths } from '@/api/apiAuthority'
  284. import { getAccountDetails, editAccount, addAccount } from '@/api/Account.js'
  285. import { pwdProduce } from '@/utils/validate'
  286. import { mapGetters } from 'vuex'
  287. export default {
  288. components: {
  289. AccountGroupTree,
  290. PermissionTree,
  291. RulesOfCompetency,
  292. LoginPolicy,
  293. RoleList,
  294. PermissionList
  295. // Dialog
  296. },
  297. data() {
  298. return {
  299. doesAccountExist: this.$route.meta.doesAccountExist, // 控制账号新增/编辑
  300. userId: '',
  301. openRole: 0,
  302. openGroup: 0,
  303. pwdStruc: '',
  304. pwdLengthBegin: 0,
  305. pwdLengthEnd: 0,
  306. permissionTreeChckedKeys: [], // 权限树初始勾选项
  307. permissionTreeChckedTemp: [], // 权限树当前勾选项
  308. rulesOfCompetency: null, // 当前编辑的权限规则
  309. loginPolicyCheckedList: [], // 登录策略初始勾选项
  310. loginPolicyCheckedTemp: [], // 登录策略当前勾选项
  311. accountGroupTreeCheckedList: [], // 账号组树初始勾选项
  312. accountGroupTreeCheckedTemp: [-1], // 账号组树当前勾选项
  313. roleListCheckedList: [], // 角色列表初始勾选项
  314. roleListCheckedTemp: [], // 角色列表当前勾选项
  315. roleType: '',
  316. queryType: '',
  317. checkedRoles: [],
  318. currentSelectedRoleId: 0,
  319. groupIds: [],
  320. queryIds: [],
  321. // dialogVisible: false,
  322. formRules: {
  323. name: [
  324. { required: true, message: '请输入账号名称', trigger: 'blur' },
  325. { min: 1, max: 32, message: '长度在 1到 32 个字符', trigger: 'blur' }
  326. ],
  327. desc: [{ min: 1, max: 128, message: '长度最多128个字符', trigger: 'blur' }]
  328. },
  329. accountForm: {
  330. name: null,
  331. pwd: null,
  332. desc: null,
  333. status: 0
  334. },
  335. accountGroupTreeProps: {
  336. children: 'children',
  337. label: 'GroupName'
  338. }
  339. }
  340. },
  341. computed: {
  342. ...mapGetters(['systemSet']),
  343. pageTitle() {
  344. return this.doesAccountExist ? '编辑账号' : '新增账号'
  345. }
  346. },
  347. created() {
  348. // 获取系统配置
  349. let obj
  350. if (typeof this.systemSet === 'string') {
  351. obj = JSON.parse(this.systemSet)
  352. } else {
  353. obj = this.systemSet
  354. }
  355. // console.log(obj)
  356. const { OpenRole, OpenGroup, PwdStruc, PwdLengthBegin, PwdLengthEnd } = obj
  357. this.openRole = OpenRole
  358. this.openGroup = OpenGroup
  359. this.pwdStruc = PwdStruc
  360. this.pwdLengthBegin = PwdLengthBegin
  361. this.pwdLengthEnd = PwdLengthEnd
  362. },
  363. mounted() {
  364. if (this.doesAccountExist) {
  365. this.userId = this.$route.query.userId
  366. this.getAccountInfo()
  367. } else {
  368. this.resetPwd()
  369. this.roleType = 'onlyRole'
  370. this.queryType = 'all'
  371. }
  372. },
  373. methods: {
  374. // 获取当前账号信息
  375. async getAccountInfo() {
  376. try {
  377. const res = await getAccountDetails({
  378. UserId: this.userId
  379. })
  380. if (res.code === 0) {
  381. // console.log(res.returnData)
  382. const { UserDesc, UserName, GroupList, TacList, AuthList, RoleList, Status } = res.returnData
  383. this.accountForm.name = UserName
  384. this.accountForm.desc = UserDesc
  385. this.accountForm.status = Status
  386. AuthList &&
  387. AuthList.length &&
  388. AuthList.forEach(auth => {
  389. this.permissionTreeChckedKeys.push(auth.AuthId)
  390. this.permissionTreeChckedTemp.push(auth)
  391. })
  392. this.accountGroupTreeCheckedTemp = []
  393. GroupList &&
  394. GroupList.length &&
  395. GroupList.forEach(group => {
  396. this.accountGroupTreeCheckedList.push(group.GroupId)
  397. this.accountGroupTreeCheckedTemp.push(group.GroupId)
  398. this.groupIds.push(group.GroupId)
  399. this.queryIds.push(group.GroupId)
  400. })
  401. RoleList &&
  402. RoleList.length &&
  403. RoleList.forEach(role => {
  404. role.IsSelected && this.roleListCheckedTemp.push(role)
  405. })
  406. TacList &&
  407. TacList &&
  408. TacList.forEach(tac => {
  409. tac.IsSelected && this.loginPolicyCheckedList.push(tac)
  410. tac.IsSelected && this.loginPolicyCheckedTemp.push(tac)
  411. })
  412. this.roleType = this.openGroup ? 'roleByUpId' : 'onlyRole'
  413. this.queryType = this.openGroup ? 'group' : 'all'
  414. } else {
  415. this.$message.error(res.message)
  416. }
  417. } catch (error) {
  418. console.log('出错了', error)
  419. }
  420. },
  421. // 随机生成密码
  422. resetPwd() {
  423. this.accountForm.pwd = pwdProduce(this.pwdLengthBegin, this.pwdLengthEnd, this.pwdStruc)
  424. },
  425. // 获取当前权限树勾选项
  426. getPermissionTreeChecked(arr) {
  427. this.permissionTreeChckedTemp = arr.map(auth => auth.AuthList)
  428. },
  429. // 获取当前编辑的权限规则
  430. getRulesOfCompetency(obj) {
  431. this.rulesOfCompetency = obj
  432. },
  433. // 获取当前勾选的登录策略
  434. getLoginPolicyChecked(arr) {
  435. this.loginPolicyCheckedTemp = arr
  436. },
  437. // 获取当前勾选的账号组
  438. getAccountGroupChecked(arr) {
  439. if (arr && arr.length && arr[0] !== -1) {
  440. const GroupIds = arr.map(item => item.GroupId)
  441. this.accountGroupTreeCheckedTemp = GroupIds
  442. this.roleType = 'roleByUpId'
  443. this.groupIds = GroupIds
  444. this.queryType = 'group'
  445. this.queryIds = GroupIds
  446. } else {
  447. this.accountGroupTreeCheckedTemp = [-1]
  448. this.roleType = 'onlyRole'
  449. this.queryType = 'all'
  450. }
  451. },
  452. // 获取当前勾选的角色列表
  453. getRoleListChecked(arr) {
  454. this.roleListCheckedTemp = arr
  455. },
  456. roleListCheckedChange(arr) {
  457. if (arr && arr.length) {
  458. this.roleListCheckedTemp = this.roleListCheckedTemp.filter(role =>
  459. arr.some(data => data.RoleId === role.RoleId)
  460. )
  461. } else {
  462. this.roleListCheckedTemp = []
  463. }
  464. this.roleListCheckedList = this.roleListCheckedTemp.map(role => role.RoleId)
  465. },
  466. // 点击角色后显示对应权限列表
  467. async selectRole(data) {
  468. if (this.currentSelectedRoleId === data.RoleId) return
  469. try {
  470. const params = {
  471. RoleId: data.RoleId
  472. }
  473. const result = await RoleAuths(params)
  474. if (result.code === 0) {
  475. this.checkedRoles = result.returnData
  476. this.currentSelectedRoleId = data.RoleId
  477. } else {
  478. this.$message.error(result.message)
  479. }
  480. } catch (error) {
  481. console.log('出错了', error)
  482. }
  483. },
  484. // 修改权限规则
  485. Competen(data) {
  486. console.log(data)
  487. },
  488. // 账号编辑保存
  489. handleClickSave(formName) {
  490. this.$refs[formName].validate(valid => {
  491. if (valid) {
  492. this.handleSaveEdit()
  493. } else {
  494. console.log('error submit!!')
  495. return false
  496. }
  497. })
  498. },
  499. handleSaveEdit() {
  500. const params = {
  501. AuthList: this.permissionTreeChckedTemp,
  502. GroupList: this.accountGroupTreeCheckedTemp.map(groupId => ({ GroupId: groupId })),
  503. RoleList: this.roleListCheckedTemp,
  504. TacList: this.loginPolicyCheckedTemp,
  505. UserDesc: this.accountForm.desc,
  506. UserName: this.accountForm.name,
  507. UserPwd: this.accountForm.pwd
  508. }
  509. if (this.doesAccountExist) {
  510. this.saveEditAccount({
  511. ...params,
  512. UserId: this.userId,
  513. Status: this.accountForm.status || 0
  514. })
  515. } else {
  516. this.saveAddAccount(params)
  517. }
  518. },
  519. // 编辑账号
  520. async saveEditAccount(params) {
  521. try {
  522. const res = await editAccount(params)
  523. if (res.code === 0) {
  524. this.$message.success(res.message)
  525. setTimeout(() => {
  526. this.$store.dispatch('tagsView/delView', this.$route)
  527. this.$router.push('/account')
  528. }, 1000)
  529. } else {
  530. this.$message.error(res.message)
  531. }
  532. } catch (error) {
  533. console.log('出错了', error)
  534. }
  535. },
  536. // 新增账号
  537. async saveAddAccount(params) {
  538. try {
  539. const res = await addAccount(params)
  540. if (res.code === 0) {
  541. this.$message.success(res.message)
  542. setTimeout(() => {
  543. this.$store.dispatch('tagsView/delView', this.$route)
  544. this.$router.push('/account')
  545. }, 1000)
  546. } else {
  547. this.$message.error(res.message)
  548. }
  549. } catch (error) {
  550. console.log('出错了', error)
  551. }
  552. }
  553. }
  554. }
  555. </script>
  556. <style lang="scss" scoped>
  557. .account-edit {
  558. width: 100%;
  559. height: calc(100% - 178px);
  560. padding: 0 146px;
  561. .account-edit-header {
  562. box-sizing: border-box;
  563. width: 100%;
  564. height: 184px;
  565. margin: 32px 0 24px;
  566. padding: 32px;
  567. background: #ffffff;
  568. box-shadow: 0px 6px 7px 0px rgba(0, 0, 0, 0.06);
  569. border-radius: 16px;
  570. .title {
  571. display: flex;
  572. justify-content: space-between;
  573. flex-direction: row;
  574. position: relative;
  575. .account-status {
  576. position: absolute;
  577. left: 194px;
  578. top: 6px;
  579. }
  580. .title-left {
  581. font-size: 24px;
  582. font-weight: bold;
  583. color: #303133;
  584. }
  585. .btn-save {
  586. width: 80px;
  587. height: 40px;
  588. background: linear-gradient(0deg, #777dba, #6983be);
  589. border-radius: 6px;
  590. color: #fff;
  591. border: none;
  592. cursor: pointer;
  593. }
  594. .btn-delete {
  595. width: 80px;
  596. height: 40px;
  597. background: #f56c6c;
  598. border-radius: 6px;
  599. color: #fff;
  600. border: none;
  601. margin-right: 24px;
  602. }
  603. }
  604. .title-content {
  605. width: 100%;
  606. padding-top: 35px;
  607. box-sizing: border-box;
  608. .btn-reset-pwd {
  609. width: 72px;
  610. height: 32px;
  611. background: #6e81bc;
  612. border: none;
  613. border-radius: 6px;
  614. color: #fff;
  615. font-size: 14px;
  616. cursor: pointer;
  617. margin-top: 4px;
  618. margin-left: -25px;
  619. }
  620. }
  621. }
  622. .content {
  623. width: 100%;
  624. height: calc(100% - 274px);
  625. box-sizing: border-box;
  626. ::v-deep .el-empty {
  627. padding: 0;
  628. }
  629. .content-card {
  630. width: 100%;
  631. background: #ffffff;
  632. box-shadow: 0px 6px 7px 0px rgba(0, 0, 0, 0.06);
  633. border-radius: 16px;
  634. &.content-card-xxl {
  635. height: 856px;
  636. }
  637. &.content-card-xl {
  638. height: 720px;
  639. }
  640. &.content-card-lg {
  641. height: 704px;
  642. }
  643. &.content-card-md {
  644. height: 512px;
  645. }
  646. &.content-card-sm {
  647. height: 376px;
  648. }
  649. &.content-card-xs {
  650. height: 320px;
  651. margin-top: 24px;
  652. overflow: hidden;
  653. }
  654. }
  655. .img-onlyRead {
  656. pointer-events: none;
  657. }
  658. .last {
  659. display: flex;
  660. flex-direction: column;
  661. justify-content: space-between;
  662. }
  663. }
  664. }
  665. </style>