index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  1. <template>
  2. <zj-tab-page ref="tabPage" :defaultActives="[{ key: 'list', label: $route.meta.title + '-列表', essential: true }]">
  3. <template slot-scope="{ activeKey, data }">
  4. <div
  5. :style="{
  6. width: '100%',
  7. height: activeKey == 'list' ? '100%' : '0px',
  8. overflow: 'hidden'
  9. }"
  10. >
  11. <div class="app-container">
  12. <div class="setting_title">权限管理</div>
  13. <el-divider></el-divider>
  14. <div class="mymain-container">
  15. <div class="btn-group clearfix">
  16. <div class="fl">
  17. <el-button size="small" type="primary" icon="el-icon-plus" @click="addOrEdit('add')"
  18. >新增角色</el-button
  19. >
  20. </div>
  21. <div class="fr">
  22. <el-input
  23. placeholder="商户名称"
  24. v-model="screenForm.companyWechatName"
  25. size="small"
  26. style="width: 300px"
  27. >
  28. <el-button slot="append" icon="el-icon-search" size="small" @click="getListByScreen"></el-button>
  29. </el-input>
  30. <el-button
  31. size="small"
  32. @click="
  33. screenForm.companyWechatName = ''
  34. getListByScreen()
  35. "
  36. >清空</el-button
  37. >
  38. </div>
  39. </div>
  40. <div class="table">
  41. <el-table
  42. v-loading="listLoading"
  43. :data="dataList"
  44. element-loading-text="Loading"
  45. border
  46. fit
  47. highlight-current-row
  48. stripe
  49. >
  50. <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
  51. <el-table-column align="center" label="商户" prop="companyName"></el-table-column>
  52. <el-table-column align="center" label="父级" prop="parentRoleName"></el-table-column>
  53. <el-table-column align="center" label="角色" prop="name"></el-table-column>
  54. <el-table-column align="center" label="适用机构" prop="type">
  55. <template slot-scope="scope">
  56. {{ ['网点', '商户', '平台'][scope.row.type] }}
  57. </template>
  58. </el-table-column>
  59. <el-table-column align="center" label="级别" prop="level">
  60. <template slot-scope="scope">
  61. {{ { 0: '无', 1: '一级网点', 2: '二级网点', 100: '通用' }[scope.row.level] }}
  62. </template>
  63. </el-table-column>
  64. <el-table-column align="center" label="操作" width="180">
  65. <template slot-scope="scope">
  66. <el-button type="text" @click="setMenuRole(scope.row)">设置权限</el-button>
  67. <el-button
  68. type="text"
  69. @click="addOrEdit('edit', scope.row.adminRoleId)"
  70. v-if="scope.row.adminRoleId != 1"
  71. >编辑</el-button
  72. >
  73. <el-popconfirm
  74. style="margin-left: 10px"
  75. title="确定删除吗?"
  76. @confirm="handleDelete(scope.row.adminRoleId)"
  77. v-if="scope.row.adminRoleId != 1"
  78. >
  79. <el-button slot="reference" type="text">删除</el-button>
  80. </el-popconfirm>
  81. </template>
  82. </el-table-column>
  83. </el-table>
  84. </div>
  85. <div class="pagination clearfix">
  86. <div class="fr">
  87. <el-pagination
  88. @size-change="handleSizeChange"
  89. @current-change="handleCurrentChange"
  90. :current-page="currentPage"
  91. :page-sizes="[10, 20, 30, 50]"
  92. :page-size="10"
  93. layout="total, sizes, prev, pager, next, jumper"
  94. :total="listTotal"
  95. >
  96. </el-pagination>
  97. </div>
  98. </div>
  99. </div>
  100. </div>
  101. </div>
  102. <div v-if="~['add', 'edit'].indexOf(activeKey)" style="box-sizing: border-box; padding: 16px">
  103. <el-form
  104. ref="addForm"
  105. :model="addForm"
  106. :inline="true"
  107. :rules="addFormRules"
  108. label-position="left"
  109. label-width="100px"
  110. >
  111. <el-form-item label="父级" prop="parentRoleId">
  112. <el-select
  113. v-model="addForm.parentRoleId"
  114. placeholder="请选择"
  115. @change="
  116. v => {
  117. if (v && dataList22.find(item => item.adminRoleId == v)) {
  118. addForm.parentRoleName = dataList22.find(item => item.adminRoleId == v).name
  119. } else {
  120. addForm.parentRoleName = ''
  121. }
  122. }
  123. "
  124. >
  125. <el-option
  126. v-for="item in (dataList22 || []).filter(a => a.adminRoleId != addForm.adminRoleId)"
  127. :key="item.adminRoleId"
  128. :label="item.name"
  129. :value="item.adminRoleId"
  130. >
  131. </el-option>
  132. </el-select>
  133. </el-form-item>
  134. <el-form-item label="角色名" prop="name">
  135. <el-input v-model="addForm.name" autocomplete="off" placeholder="请输入角色名"></el-input>
  136. </el-form-item>
  137. <el-form-item label="适用机构" prop="type">
  138. <el-select
  139. v-model="addForm.type"
  140. placeholder="请选择"
  141. @change="
  142. v => {
  143. addForm.level = v == 1 ? 0 : ''
  144. }
  145. "
  146. >
  147. <el-option v-for="item in adminRoleGetRoleType" :key="item.code" :label="item.name" :value="item.code">
  148. </el-option>
  149. </el-select>
  150. </el-form-item>
  151. <el-form-item label="级别" prop="level" v-if="[0, 1].includes(addForm.type)">
  152. <el-select v-model="addForm.level" placeholder="请选择">
  153. <el-option label="无" :value="0" v-if="[1].includes(addForm.type)" />
  154. <el-option label="一级网点" :value="1" />
  155. <el-option label="二级网点" :value="2" />
  156. <el-option label="通用" :value="100" v-if="[0].includes(addForm.type)" />
  157. </el-select>
  158. </el-form-item>
  159. </el-form>
  160. <div slot="footer" class="dialog-footer" style="text-align: right">
  161. <el-button size="mini" @click="data.removeTab()">取 消</el-button>
  162. <el-button size="mini" type="primary" @click="submitAddForm(data.removeTab)">确 定</el-button>
  163. </div>
  164. </div>
  165. <div v-if="~['detail'].indexOf(activeKey)" style="box-sizing: border-box; padding: 16px">
  166. <div>
  167. <span class="custom-tree-node">
  168. <span>菜单</span>
  169. <span>
  170. <el-button type="text" size="mini" @click.stop="quanbugouxuan(null, { children: menuRoleList })"
  171. >全部勾选</el-button
  172. >
  173. <el-button type="text" size="mini" @click.stop="quxiaogouxuan(null, { children: menuRoleList })"
  174. >取消勾选</el-button
  175. >
  176. </span>
  177. </span>
  178. </div>
  179. <zj-tree
  180. :data="menuRoleList"
  181. show-checkbox
  182. :default-expand-all="true"
  183. node-key="moduleId"
  184. ref="tree"
  185. highlight-current
  186. :props="defaultProps"
  187. :check-strictly="true"
  188. :expand-on-click-node="true"
  189. >
  190. <span class="custom-tree-node" slot-scope="{ node, data }">
  191. <span>{{ node.label }}</span>
  192. <span v-if="[1, 2].includes(data.type)">
  193. <el-button type="text" size="mini" @click.stop="quanbugouxuan(node, data)">全部勾选</el-button>
  194. <el-button type="text" size="mini" @click.stop="quxiaogouxuan(node, data)">取消勾选</el-button>
  195. </span>
  196. </span>
  197. </zj-tree>
  198. <div slot="footer" class="dialog-footer" style="text-align: right">
  199. <el-button size="mini" @click="data.removeTab()">{{ editId != 1 ? '取 消' : '关 闭' }}</el-button>
  200. <el-button size="mini" type="primary" @click="submitRoleForm(data.removeTab)" v-if="editId != 1"
  201. >确 定</el-button
  202. >
  203. </div>
  204. </div>
  205. </template>
  206. </zj-tab-page>
  207. </template>
  208. <script>
  209. import { mapGetters } from 'vuex'
  210. import {
  211. getRoleList,
  212. addRole,
  213. editRole,
  214. getRoleDetail,
  215. deleteRole,
  216. getMenuList,
  217. getMenuRoleIds,
  218. setMenuRole,
  219. adminRoleGetRoleType
  220. } from '@/api/setting'
  221. export default {
  222. data() {
  223. return {
  224. baseURL: process.env.VUE_APP_BASE_API,
  225. dataList: null, // 列表数据
  226. listLoading: true, // 列表加载loading
  227. currentPage: 1, // 当前页码
  228. pageSize: 10, // 每页数量
  229. listTotal: 0, // 列表总数
  230. editId: null,
  231. addFormType: 'add',
  232. screenForm: {
  233. companyWechatName: ''
  234. },
  235. addForm: {
  236. name: '', // 角色名
  237. type: '',
  238. level: '',
  239. parentRoleId: '',
  240. parentRoleName: ''
  241. },
  242. addFormRules: {
  243. name: [{ required: true, message: '请输入角色名', trigger: 'blur' }],
  244. type: [{ required: true, message: '请输入角色名', trigger: 'blur' }],
  245. level: [{ required: true, message: '请输入角色名', trigger: 'blur' }]
  246. },
  247. menuRoleList: [],
  248. defaultProps: {
  249. children: 'children',
  250. label: 'moduleName'
  251. },
  252. adminRoleGetRoleType: [],
  253. dataList22: []
  254. }
  255. },
  256. computed: {
  257. ...mapGetters(['userid', 'name'])
  258. },
  259. created() {
  260. this.getList()
  261. getRoleList({
  262. pageNum: 1,
  263. pageSize: -1
  264. }).then(res => {
  265. this.dataList22 = res.data.records
  266. })
  267. adminRoleGetRoleType().then(res => {
  268. this.adminRoleGetRoleType = res.data
  269. })
  270. },
  271. methods: {
  272. diguichazhaoid(arr, ids) {
  273. for (var item of arr) {
  274. ids.push(item.moduleId)
  275. if (item.children && item.children.length) {
  276. this.diguichazhaoid(item.children, ids)
  277. }
  278. }
  279. },
  280. zhaofuji(data, arr) {
  281. if (data.parent) {
  282. arr.push(data.data.moduleId)
  283. this.zhaofuji(data.parent, arr)
  284. }
  285. },
  286. quanbugouxuan(node, data) {
  287. let nowChecked = this.$refs.tree.getCheckedKeys()
  288. let thisId = data.moduleId
  289. let childId = []
  290. if (data.children.length) {
  291. childId = this.familyTree(data.children)
  292. }
  293. var parentId = []
  294. this.zhaofuji(node, parentId)
  295. let setChecked = nowChecked.concat([thisId]).concat(childId).concat(parentId)
  296. this.$refs.tree.setCheckedKeys(setChecked)
  297. // var ids = []
  298. // this.diguichazhaoid(data.children, ids)
  299. // this.$refs.tree.setCheckedKeys([data?.moduleId || '', ...ids, ...this.$refs.tree.getCheckedKeys()])
  300. },
  301. quxiaogouxuan(node, data) {
  302. let nowChecked = this.$refs.tree.getCheckedKeys()
  303. let thisId = data.moduleId
  304. let childId = []
  305. if (data.children.length) {
  306. childId = this.familyTree(data.children)
  307. }
  308. let setChecked = nowChecked
  309. if (setChecked.indexOf(thisId) >= 0) {
  310. setChecked.splice(setChecked.indexOf(thisId), 1)
  311. }
  312. if (childId.length) {
  313. for (var i = 0; i < childId.length; i++) {
  314. if (setChecked.indexOf(childId[i]) >= 0) {
  315. setChecked.splice(setChecked.indexOf(childId[i]), 1)
  316. }
  317. }
  318. }
  319. this.$refs.tree.setCheckedKeys(setChecked)
  320. // var ids = []
  321. // this.diguichazhaoid(data.children, ids)
  322. // var keyIds = [data?.moduleId || '', ...ids]
  323. // var keys = this.$refs.tree.getCheckedKeys().filter(k => !keyIds.includes(k))
  324. // this.$refs.tree.setCheckedKeys(keys)
  325. },
  326. // 递归子id
  327. familyTree(arr) {
  328. var temp = []
  329. var forFn = function (list) {
  330. for (var i = 0; i < list.length; i++) {
  331. var item = list[i]
  332. if (item.children) {
  333. temp.push(item.moduleId)
  334. forFn(item.children)
  335. }
  336. }
  337. }
  338. forFn(arr)
  339. return temp
  340. },
  341. getList() {
  342. this.listLoading = true
  343. let params = {
  344. pageNum: this.currentPage,
  345. pageSize: this.pageSize,
  346. companyWechatName: this.screenForm.companyWechatName
  347. }
  348. getRoleList(params).then(res => {
  349. this.dataList = res.data.records
  350. this.listTotal = res.data.total
  351. this.listLoading = false
  352. })
  353. },
  354. // 筛选后重新获取列表
  355. getListByScreen() {
  356. this.currentPage = 1
  357. this.getList()
  358. },
  359. // 更改每页数量
  360. handleSizeChange(val) {
  361. this.pageSize = val
  362. this.currentPage = 1
  363. this.getList()
  364. },
  365. // 更改当前页
  366. handleCurrentChange(val) {
  367. this.currentPage = val
  368. this.getList()
  369. },
  370. // 操作 - 删除
  371. handleDelete(id) {
  372. deleteRole({ adminRoleId: id }).then(res => {
  373. this.getList()
  374. this.$successMsg()
  375. })
  376. },
  377. // 新增编辑
  378. addOrEdit(type, id) {
  379. this.$refs.tabPage.addTab({
  380. activeKey: type,
  381. key: type,
  382. label: { add: '新增', edit: '编辑' }[type],
  383. triggerEvent: () => {
  384. this.$refs?.addForm?.resetFields()
  385. this.$data.addForm = this.$options.data().addForm
  386. this.addFormType = type
  387. if (type == 'edit') {
  388. this.editId = id
  389. getRoleDetail({ adminRoleId: id }).then(res => {
  390. this.addForm = {
  391. ...res.data
  392. }
  393. })
  394. }
  395. },
  396. closeEvent: () => {}
  397. })
  398. },
  399. // 提交 新增编辑
  400. submitAddForm(removeTab) {
  401. this.$refs.addForm.validate(valid => {
  402. if (valid) {
  403. let params = {
  404. ...this.addForm
  405. }
  406. if (this.addFormType == 'edit') {
  407. params.adminRoleId = this.editId
  408. editRole(params).then(res => {
  409. removeTab('list')
  410. this.getList()
  411. this.$successMsg('编辑成功')
  412. getRoleList({
  413. pageNum: 1,
  414. pageSize: -1
  415. }).then(res => {
  416. this.dataList22 = res.data.records
  417. })
  418. })
  419. } else {
  420. addRole(params).then(res => {
  421. removeTab('list')
  422. this.getList()
  423. this.$successMsg('新增成功')
  424. getRoleList({
  425. pageNum: 1,
  426. pageSize: -1
  427. }).then(res => {
  428. this.dataList22 = res.data.records
  429. })
  430. })
  431. }
  432. }
  433. })
  434. },
  435. // 设置权限 - 获取列表
  436. setMenuRole(row) {
  437. console.log(row)
  438. this.$refs.tabPage.addTab({
  439. activeKey: 'detail',
  440. key: 'detail',
  441. label: '设置权限',
  442. triggerEvent: () => {
  443. this.$nextTick(() => {
  444. this.editId = row.adminRoleId
  445. function aaa(list) {
  446. for (var item of list) {
  447. if (item.type == 3) {
  448. item.tiling = true
  449. item.labelWidthNum = 115
  450. }
  451. if (item.children && item.children.length) {
  452. aaa(item.children)
  453. }
  454. }
  455. }
  456. getMenuList({
  457. adminUserId: this.userid,
  458. parentAdminRoleId: row.parentRoleId
  459. }).then(res => {
  460. aaa(res.data)
  461. this.menuRoleList = res.data
  462. })
  463. getMenuRoleIds({ adminRoleId: row.adminRoleId }).then(res => {
  464. this.$refs.tree.setCheckedKeys(res.data)
  465. })
  466. })
  467. },
  468. closeEvent: () => {}
  469. })
  470. },
  471. // 设置权限 - 提交数据
  472. submitRoleForm(removeTab) {
  473. let params = {
  474. adminModuleIds: this.$refs.tree.getCheckedKeys(),
  475. adminModuleIds2: this.$refs.tree.getHalfCheckedKeys(),
  476. adminRoleId: this.editId
  477. }
  478. setMenuRole(params).then(res => {
  479. removeTab('list')
  480. this.getList()
  481. this.$successMsg()
  482. })
  483. }
  484. }
  485. }
  486. </script>
  487. <style lang="scss">
  488. .tree-dialog {
  489. .el-dialog__body {
  490. padding: 20px;
  491. .el-tree {
  492. max-height: calc(100vh - 140px - 54px - 70px);
  493. overflow-y: scroll;
  494. padding: 0 30px;
  495. > .el-tree-node {
  496. padding: 15px 0;
  497. border: 1px dashed #ddd;
  498. margin-bottom: 15px;
  499. border-radius: 10px;
  500. }
  501. }
  502. }
  503. }
  504. .custom-tree-node {
  505. flex: 1;
  506. display: flex;
  507. align-items: center;
  508. justify-content: space-between;
  509. font-size: 14px;
  510. padding-right: 8px;
  511. }
  512. </style>