menuModal.vue 12 KB

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