menuModal.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. <template>
  2. <div>
  3. <a-modal
  4. centered
  5. class="roleMenuEdit-modal"
  6. :footer="null"
  7. :maskClosable="false"
  8. :title="titleText"
  9. v-model="isshow"
  10. @cancel="isshow=false"
  11. :width="800">
  12. <a-spin :spinning="spinning" tip="Loading...">
  13. <a-form-model
  14. id="roleMenuEdit-form"
  15. ref="ruleForm"
  16. :model="form"
  17. :rules="rules"
  18. :label-col="formItemLayout.labelCol"
  19. :wrapper-col="formItemLayout.wrapperCol"
  20. >
  21. <div style="display:flex;justify-content: flex-end;align-items: center;" v-if="authType==0">
  22. <div style="width:270px;text-align: right;flex:1;">快捷全选:</div>
  23. <div style="padding:0 10px;">
  24. <a-checkbox-group id="roleMenuEdit-chooseAll" :value="authAllPrice" :options="priceOptions" @change="onPriceChange" />
  25. </div>
  26. </div>
  27. <div style="height: 500px;overflow-y: auto;">
  28. <a-tree
  29. checkable
  30. id="roleMenuEdit-tree"
  31. :selectable="false"
  32. @check="onCheck"
  33. @expand="onExpand"
  34. :expandedKeys="expandedKeys"
  35. :defaultExpandAll="true"
  36. :autoExpandParent="autoExpandParent"
  37. v-model="checkedKeys"
  38. :treeData="treeData"
  39. >
  40. <!-- <a-icon v-if="authType == 0" slot="switcherIcon" type="file" /> -->
  41. </a-tree>
  42. </div>
  43. <a-form-model-item :label-col="{span: 0}" :wrapper-col="{span: 24}" style="text-align: center;margin: 18px 0 0;">
  44. <a-button id="roleMenuEdit-cancel" :style="{ marginRight: '10px' }" @click="isshow=false">取消</a-button>
  45. <a-button type="primary" id="roleMenuEdit-save" @click="handleSubmit()">保存</a-button>
  46. </a-form-model-item>
  47. </a-form-model>
  48. </a-spin>
  49. </a-modal>
  50. </div>
  51. </template>
  52. <script>
  53. import { commonMixin } from '@/utils/mixin'
  54. // 组件
  55. import { STable, VSelect } from '@/components'
  56. // 接口
  57. import { addMenuPower } from '@/api/power-role.js'
  58. export default {
  59. name: 'RoleModal',
  60. mixins: [commonMixin],
  61. components: { STable, VSelect },
  62. props: {
  63. visible: {
  64. type: Boolean,
  65. default: false
  66. },
  67. nowData: {
  68. type: Object,
  69. default: function () {
  70. return {}
  71. }
  72. }
  73. },
  74. data () {
  75. return {
  76. spinning: false,
  77. treeData: [], // 功能权限列表数据
  78. priceOptions: [], // 价格权限列表数据
  79. titleText: '', // 弹窗标题
  80. expandedKeys: [], // 展开指定的树节点
  81. autoExpandParent: true, // 是否自动展开父节点
  82. checkedKeys: [], // 选中节点数据
  83. halfCheckedKeys: [], // 半选节点值
  84. checkedData: [], // 所有已选节点值
  85. authPriceData: [], // 价格权限值
  86. isshow: this.visible, // 显示弹窗
  87. // form 表单label布局
  88. formItemLayout: {
  89. labelCol: { span: 6 },
  90. wrapperCol: { span: 16 }
  91. },
  92. authAllPrice: [], // 所有已选价格权限值
  93. leafNode: [], // 子节点集合
  94. checkLeafNode: [], // 选中节点集合
  95. priceCheckNode: [], // 已选价格集合
  96. form: {}, // 提交参数
  97. rules: {}, // 表单验证规则
  98. authType: '1', // 权限类型 1功能权限设置 0 价格权限设置
  99. id: '' // 角色id
  100. }
  101. },
  102. methods: {
  103. // 展开/收起节点
  104. onExpand (expandedKeys) {
  105. this.expandedKeys = expandedKeys
  106. },
  107. // 点击复选框
  108. onCheck (checkedKeys, info) {
  109. // console.log(checkedKeys, info)
  110. this.halfCheckedKeys = info.halfCheckedKeys
  111. this.checkedKeys = checkedKeys
  112. // 判断某分类是否全选
  113. this.checkLeafNode = []
  114. info.checkedNodes.map(item => {
  115. const node = item.data.props
  116. const a = node.code.split('_')
  117. this.checkLeafNode.push({ pr: a[a.length - 1], id: node.id })
  118. })
  119. this.hasAllSelect()
  120. },
  121. // 判断某分类是否全选
  122. hasAllSelect () {
  123. // console.log(this.leafNode)
  124. // console.log(this.checkLeafNode)
  125. this.authAllPrice = []
  126. this.priceOptions.map(item => {
  127. const a = this.leafNode.filter(n => n.pr == item.value)
  128. const b = this.checkLeafNode.filter(n => n.pr == item.value)
  129. if (a && b && a.length == b.length && !this.authAllPrice.find(d => d == item.value)) {
  130. this.authAllPrice.push(item.value)
  131. }
  132. })
  133. },
  134. // 全选某分类价格
  135. allSelect (data, authPrice, flag) {
  136. const _this = this
  137. for (let i = 0; i < data.length; i++) {
  138. const node = data[i]
  139. const a = node.code.split('_')
  140. if (a[a.length - 1] == authPrice) {
  141. // 全选
  142. if (flag) {
  143. _this.checkedKeys.push(node.id)
  144. } else {
  145. // 取消
  146. _this.checkedKeys = _this.checkedKeys.filter(item => item != node.id)
  147. }
  148. }
  149. if (node.children && node.children.length) {
  150. _this.allSelect(node.children, authPrice, flag)
  151. }
  152. }
  153. },
  154. // 选择价格权限 change
  155. onPriceChange (val) {
  156. // 全选
  157. if (this.authAllPrice.length < val.length) {
  158. const tempVal = val.filter(item => !this.authAllPrice.includes(item))
  159. console.log(tempVal)
  160. this.allSelect(this.nowData.allMenuList, tempVal[0], true)
  161. } else {
  162. // 取消
  163. const tempVal = this.authAllPrice.filter(item => !val.includes(item))
  164. this.allSelect(this.nowData.allMenuList, tempVal[0], false)
  165. }
  166. this.authAllPrice = val
  167. },
  168. // 保存提交
  169. handleSubmit () {
  170. const _this = this
  171. this.$refs.ruleForm.validate(valid => {
  172. if (valid) {
  173. // 功能权限时的已选价格菜单或价格权限时的已选功能菜单
  174. const checkData = this.authType == 1 ? this.priceCheckNode : this.checkedData.filter(item => !this.priceCheckNode.includes(item))
  175. // 功能权限时,首次半选节点拿不到值(this.halfCheckedKeys为空时),
  176. // 此时通过从所有已选checkedData数据中过滤当前选择的功能菜单和已选的价格菜单,剩下的就是半选节点
  177. let halfCheckedKeys = this.halfCheckedKeys.length ? this.halfCheckedKeys : this.checkedData.filter(item => ![...this.checkedKeys, ...checkData].includes(item))
  178. // 功能权限没有选择任何菜单或价格权限时,halfCheckedKeys 半选节点为空
  179. if (this.checkedKeys.length == 0 || this.authType == 0) {
  180. halfCheckedKeys = []
  181. }
  182. // 最终保存的节点,包括当前已选的和半选的节点
  183. const arr = [...this.checkedKeys, ...halfCheckedKeys, ...checkData].filter((x, index, self) => self.indexOf(x) === index)
  184. console.log(arr, '--444--')
  185. _this.spinning = true
  186. addMenuPower({ id: this.id, menuIds: arr.join(',') }).then(res => {
  187. if (res.status == 200) {
  188. _this.$message.success(res.message)
  189. _this.$emit('refresh')
  190. setTimeout(function () {
  191. _this.isshow = false
  192. _this.spinning = false
  193. }, 300)
  194. } else {
  195. _this.spinning = false
  196. }
  197. })
  198. } else {
  199. console.log('error submit!!')
  200. return false
  201. }
  202. })
  203. },
  204. // 重置
  205. clear () {
  206. this.form.resetFields()
  207. this.titleText = ''
  208. this.id = ''
  209. this.checkedKeys = []
  210. this.expandedKeys = []
  211. },
  212. // 是否是价格权限节点
  213. isPriceNode (node) {
  214. const a = node.code.split('_')
  215. const c = this.priceOptions.find(item => item.value == a[a.length - 1])
  216. return c
  217. },
  218. // 查找叶子节点
  219. findLeaf (data, arr) {
  220. const _this = this
  221. for (let i = 0; i < data.length; i++) {
  222. const node = data[i]
  223. if (node.children && node.children.length) {
  224. this.findLeaf(node.children, arr)
  225. } else {
  226. if (_this.isPriceNode(node)) {
  227. data.splice(i, 1)
  228. } else {
  229. node.class = 'isLeaf'
  230. const hasNode = arr.find(item => item == node.id)
  231. const hasCheck = _this.checkedKeys.find(item => item == node.id)
  232. if (hasNode && !hasCheck) {
  233. this.expandedKeys.push(node.id)
  234. this.checkedKeys.push(node.id)
  235. }
  236. }
  237. }
  238. }
  239. },
  240. // 查找有价格权限的菜单
  241. findPrice (data, arr) {
  242. const _this = this
  243. for (let i = 0; i < data.length; i++) {
  244. const node = data[i]
  245. const a = node.code.split('_')
  246. // 已选节点勾选
  247. const hasNode = arr.find(item => item == node.id)
  248. if (!_this.isPriceNode(node)) {
  249. // 删除此节点
  250. node.checkable = false
  251. node.title = node.title.replace('(必选)', '')
  252. if (!node.children || node.children.length == 0) {
  253. data.splice(i, 1)
  254. }
  255. } else {
  256. node.class = 'leafNode'
  257. _this.expandedKeys.push(node.id)
  258. // 默认回显已选节点
  259. const hasCheck = _this.checkedKeys.find(item => item == node.id)
  260. if (hasNode && !hasCheck) {
  261. _this.checkedKeys.push(node.id)
  262. _this.checkLeafNode.push({ pr: a[a.length - 1], id: node.id })
  263. }
  264. // 统计价格节点
  265. const hasLeaf = _this.leafNode.find(item => item.id == node.id)
  266. if (!hasLeaf) {
  267. _this.leafNode.push({ pr: a[a.length - 1], id: node.id })
  268. }
  269. }
  270. if (node.children && node.children.length) {
  271. _this.findPrice(node.children, arr)
  272. }
  273. }
  274. },
  275. // 根据id查找是否价格菜单
  276. getPriceTreeNode (data, arr) {
  277. const _this = this
  278. for (let i = 0; i < data.length; i++) {
  279. const node = data[i]
  280. if (_this.isPriceNode(node)) {
  281. // 默认回显已选节点
  282. const hasCheck = _this.priceCheckNode.find(item => item == node.id)
  283. // 已选节点勾选
  284. const hasNode = arr.find(item => item == node.id)
  285. if (hasNode && !hasCheck) {
  286. _this.priceCheckNode.push(node.id)
  287. }
  288. }
  289. if (node.children && node.children.length) {
  290. _this.getPriceTreeNode(node.children, arr)
  291. }
  292. }
  293. },
  294. // 初始化
  295. pageInit () {
  296. this.id = this.nowData.role.id
  297. this.titleText = (this.nowData.type == 1 ? '功能权限分配' : '价格权限分配') + '(' + this.nowData.role.name + ')'
  298. // 已选节点
  299. const arr = this.nowData.role.menuIds
  300. // 权限类型, 1功能权限设置,0 价格权限设置
  301. this.authType = this.nowData.type
  302. this.checkedData = arr ? arr.split(',') : []
  303. // 找出叶子节点
  304. const treeData = this.nowData.allMenuList
  305. this.expandedKeys.push(treeData.id)
  306. // 获取已选的价格菜单
  307. this.getPriceTreeNode(treeData, this.checkedData)
  308. // 功能权限设置
  309. if (this.authType == 1) {
  310. this.findLeaf(treeData, this.checkedData)
  311. }
  312. // 价格权限设置
  313. if (this.authType == 0) {
  314. this.findPrice(treeData, this.checkedData)
  315. this.hasAllSelect()
  316. }
  317. console.log(this.checkedKeys)
  318. console.log(this.checkedData)
  319. this.treeData = treeData
  320. // if (this.authType == 0) {
  321. // let leafNodes = []
  322. // setTimeout(() => {
  323. // if (leafNodes.length == 0) {
  324. // leafNodes = document.querySelectorAll('ul > li.leafNode:first-child')
  325. // for (let i = 0; i < leafNodes.length; i++) {
  326. // const cn = leafNodes[i].parentNode.className
  327. // leafNodes[i].parentNode.className += cn.indexOf('leafParent') < 0 ? ' leafParent' : ''
  328. // leafNodes[i].parentNode.parentNode.style.padding = '6px 0'
  329. // }
  330. // }
  331. // }, 100)
  332. // }
  333. }
  334. },
  335. watch: {
  336. visible (newValue, oldValue) {
  337. this.isshow = newValue
  338. },
  339. isshow (newValue, oldValue) {
  340. if (!newValue) {
  341. this.$emit('close')
  342. this.$refs.ruleForm.resetFields()
  343. this.id = ''
  344. this.authAllPrice = []
  345. this.checkLeafNode = []
  346. this.priceCheckNode = []
  347. this.leafNode = []
  348. this.checkedKeys = []
  349. this.expandedKeys = []
  350. this.titleText = '新增角色'
  351. } else {
  352. this.priceOptions = this.$store.state.app.priceAuthOptions
  353. }
  354. }
  355. }
  356. }
  357. </script>
  358. <style lang="less">
  359. .roleMenuEdit-modal{
  360. .ant-modal-title{
  361. padding-right: 30px;
  362. }
  363. .ant-modal-footer {
  364. display: none;
  365. }
  366. .form-box {
  367. max-height: 600px !important;
  368. overflow: auto;
  369. }
  370. .form-box::-webkit-scrollbar{
  371. width: 6px;
  372. height: 6px ;
  373. }
  374. .form-box::-webkit-scrollbar-thumb{
  375. width: 6px;
  376. height: 6px ;
  377. border-radius: 4px;
  378. -webkit-box-shadow: inset 0 0 2px #d1d1d1;
  379. background: #e4e4e4;
  380. }
  381. .ant-tree li ul{
  382. white-space: pre-wrap;
  383. }
  384. .ant-tree li{
  385. border-left:1px solid #eee;
  386. }
  387. .leafParent{
  388. display: inline-block;
  389. width: 80%;
  390. float: right;
  391. }
  392. .ant-tree li .ant-tree-node-content-wrapper{
  393. font-weight: bold;
  394. }
  395. .ant-tree li.isLeaf .ant-tree-node-content-wrapper{
  396. font-weight: normal;
  397. }
  398. .ant-tree li.isLeaf{
  399. display: inline-block;
  400. width: 25%;
  401. border:0;
  402. }
  403. .ant-tree li.leafNode{
  404. display: inline-block;
  405. padding: 0;
  406. border:0;
  407. &:first-child{
  408. padding: 0;
  409. }
  410. .ant-tree-node-content-wrapper{
  411. color: rgb(209, 4, 4);
  412. font-weight: normal;
  413. }
  414. }
  415. }
  416. </style>