menuModal.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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 type="primary" id="roleMenuEdit-save" @click="handleSubmit()" :style="{ marginRight: '8px' }">保存</a-button>
  43. <a-button id="roleMenuEdit-cancel" :style="{ marginLeft: '8px' }" @click="isshow=false">取消</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. form: {},
  93. rules: {},
  94. authType: '1',
  95. id: '' // 角色id
  96. }
  97. },
  98. methods: {
  99. onExpand (expandedKeys) {
  100. this.expandedKeys = expandedKeys
  101. // this.autoExpandParent = false
  102. },
  103. onCheck (checkedKeys, info) {
  104. // console.log(checkedKeys, info)
  105. this.halfCheckedKeys = info.halfCheckedKeys
  106. this.checkedKeys = checkedKeys
  107. // 判断某分类是否全选
  108. this.checkLeafNode = []
  109. info.checkedNodes.map(item => {
  110. const node = item.data.props
  111. const a = node.code.split('_')
  112. this.checkLeafNode.push({pr:a[a.length-1],id:node.id})
  113. })
  114. this.hasAllSelect()
  115. },
  116. // 判断某分类是否全选
  117. hasAllSelect(){
  118. // console.log(this.leafNode)
  119. // console.log(this.checkLeafNode)
  120. this.authAllPrice = []
  121. this.priceOptions.map(item => {
  122. const a = this.leafNode.filter(n => n.pr == item.value)
  123. const b = this.checkLeafNode.filter(n => n.pr == item.value)
  124. if(a&&b&&a.length == b.length && !this.authAllPrice.find(d => d==item.value)){
  125. this.authAllPrice.push(item.value)
  126. }
  127. })
  128. },
  129. // 全选某分类价格
  130. allSelect(data,authPrice,flag){
  131. const _this = this
  132. for (let i = 0; i < data.length; i++) {
  133. const node = data[i]
  134. const a = node.code.split('_')
  135. if(a[a.length-1] == authPrice){
  136. // 全选
  137. if(flag){
  138. _this.checkedKeys.push(node.id)
  139. }else{
  140. // 取消
  141. _this.checkedKeys = _this.checkedKeys.filter(item => item != node.id)
  142. }
  143. }
  144. if (node.children && node.children.length) {
  145. _this.allSelect(node.children, authPrice, flag)
  146. }
  147. }
  148. },
  149. onPriceChange(val){
  150. // 全选
  151. if(this.authAllPrice.length < val.length){
  152. let tempVal = val.filter(item=>!this.authAllPrice.includes(item))
  153. console.log(tempVal)
  154. this.allSelect(this.nowData.allMenuList, tempVal[0], true)
  155. }else{
  156. // 取消
  157. let tempVal = this.authAllPrice.filter(item=>!val.includes(item))
  158. this.allSelect(this.nowData.allMenuList, tempVal[0], false)
  159. }
  160. this.authAllPrice = val
  161. },
  162. // 保存提交
  163. handleSubmit () {
  164. const _this = this
  165. this.$refs.ruleForm.validate(valid => {
  166. if (valid) {
  167. const arr = [...this.checkedKeys, ...this.halfCheckedKeys, ...this.checkedData].filter((x, index,self)=>self.indexOf(x)===index)
  168. console.log(this.checkedData)
  169. _this.spinning = true
  170. addMenuPower({ id: this.id, menuIds: arr.join(',') }).then(res => {
  171. if (res.status == 200) {
  172. _this.$message.success(res.message)
  173. _this.$emit('refresh')
  174. setTimeout(function () {
  175. _this.isshow = false
  176. _this.spinning = false
  177. }, 300)
  178. } else {
  179. _this.spinning = false
  180. }
  181. })
  182. } else {
  183. console.log('error submit!!')
  184. return false
  185. }
  186. })
  187. },
  188. clear () {
  189. this.form.resetFields()
  190. this.titleText = ''
  191. this.id = ''
  192. this.checkedKeys = []
  193. this.expandedKeys = []
  194. },
  195. isPriceNode(node){
  196. const a = node.code.split('_')
  197. const c = this.priceOptions.find(item => item.value == a[a.length-1])
  198. return c
  199. },
  200. // 查找叶子节点
  201. findLeaf (data, arr) {
  202. const _this = this
  203. for (let i = 0; i < data.length; i++) {
  204. const node = data[i]
  205. if (node.children && node.children.length) {
  206. this.findLeaf(node.children, arr)
  207. } else {
  208. if(_this.isPriceNode(node)){
  209. data.splice(i,1)
  210. }else{
  211. node.class = 'isLeaf'
  212. const hasNode = arr.find(item => item == node.id)
  213. const hasCheck = _this.checkedKeys.find(item => item == node.id)
  214. if (hasNode && !hasCheck) {
  215. this.expandedKeys.push(node.id)
  216. this.checkedKeys.push(node.id)
  217. }
  218. }
  219. }
  220. }
  221. },
  222. // 查找有价格权限的菜单
  223. findPrice(data, arr){
  224. const _this = this
  225. for (let i = 0; i < data.length; i++) {
  226. const node = data[i]
  227. const a = node.code.split('_')
  228. // 已选节点勾选
  229. const hasNode = arr.find(item => item == node.id)
  230. if(!_this.isPriceNode(node)){
  231. // 删除此节点
  232. node.checkable = false
  233. node.title = node.title.replace('(必选)','')
  234. if(!node.children||node.children.length==0){
  235. data.splice(i,1)
  236. }
  237. }else{
  238. node.class = 'leafNode'
  239. _this.expandedKeys.push(node.id)
  240. // 默认回显已选节点
  241. const hasCheck = _this.checkedKeys.find(item => item == node.id)
  242. if (hasNode && !hasCheck) {
  243. _this.checkedKeys.push(node.id)
  244. _this.checkLeafNode.push({pr:a[a.length-1],id:node.id})
  245. }
  246. // 统计价格节点
  247. const hasLeaf = _this.leafNode.find(item => item.id == node.id)
  248. if(!hasLeaf){
  249. _this.leafNode.push({pr:a[a.length-1],id:node.id})
  250. }
  251. }
  252. if (node.children && node.children.length) {
  253. _this.findPrice(node.children, arr)
  254. }
  255. }
  256. },
  257. pageInit(){
  258. this.id = this.nowData.role.id
  259. this.titleText = (this.nowData.type == 1 ? '功能权限分配': '价格权限分配') + '(' + this.nowData.role.name + ')'
  260. // 已选节点
  261. const arr = this.nowData.role.menuIds
  262. // 权限类型, 1功能权限设置,0 价格权限设置
  263. this.authType = this.nowData.type
  264. this.checkedData = arr ? arr.split(',') : []
  265. // 找出叶子节点
  266. const treeData = this.nowData.allMenuList
  267. this.expandedKeys.push(treeData.id)
  268. if(this.authType == 1){
  269. this.findLeaf(treeData, this.checkedData)
  270. }
  271. if(this.authType == 0){
  272. this.findPrice(treeData, this.checkedData)
  273. this.hasAllSelect()
  274. }
  275. this.checkedData = this.checkedData.filter(item => !this.checkedKeys.includes(item))
  276. // console.log(this.checkedData)
  277. // console.log(this.checkedKeys)
  278. this.treeData = treeData
  279. if(this.authType == 0){
  280. let leafNodes = []
  281. setTimeout(()=>{
  282. if(leafNodes.length == 0){
  283. leafNodes = document.querySelectorAll('ul > li.leafNode:first-child')
  284. for(let i=0;i<leafNodes.length;i++){
  285. const cn = leafNodes[i].parentNode.className
  286. leafNodes[i].parentNode.className += cn.indexOf('leafParent')<0 ? ' leafParent' : ''
  287. leafNodes[i].parentNode.parentNode.style.padding = '6px 0'
  288. }
  289. }
  290. },100)
  291. }
  292. }
  293. },
  294. watch: {
  295. visible (newValue, oldValue) {
  296. this.isshow = newValue
  297. },
  298. isshow (newValue, oldValue) {
  299. if (!newValue) {
  300. this.$emit('close')
  301. this.$refs.ruleForm.resetFields()
  302. this.id = ''
  303. this.authAllPrice = []
  304. this.checkLeafNode = []
  305. this.leafNode = []
  306. this.checkedKeys = []
  307. this.expandedKeys = []
  308. this.titleText = '新增角色'
  309. }else{
  310. this.priceOptions = this.$store.state.app.priceAuthOptions
  311. }
  312. },
  313. nowData: {
  314. handler (newValue, oldValue) {
  315. if (this.isshow && newValue) {
  316. this.pageInit()
  317. }
  318. },
  319. deep: true
  320. }
  321. }
  322. }
  323. </script>
  324. <style lang="less">
  325. .roleMenuEdit-modal{
  326. .ant-modal-title{
  327. padding-right: 30px;
  328. }
  329. .ant-modal-footer {
  330. display: none;
  331. }
  332. .form-box {
  333. max-height: 600px !important;
  334. overflow: auto;
  335. }
  336. .form-box::-webkit-scrollbar{
  337. width: 6px;
  338. height: 6px ;
  339. }
  340. .form-box::-webkit-scrollbar-thumb{
  341. width: 6px;
  342. height: 6px ;
  343. border-radius: 4px;
  344. -webkit-box-shadow: inset 0 0 2px #d1d1d1;
  345. background: #e4e4e4;
  346. }
  347. .ant-tree li ul{
  348. white-space: pre-wrap;
  349. }
  350. .leafParent{
  351. display: inline-block;
  352. width: 80%;
  353. float: right;
  354. }
  355. .isLeaf{
  356. display: inline-block;
  357. width: 25%;
  358. }
  359. .leafNode{
  360. display: inline-block;
  361. padding: 0;
  362. &:first-child{
  363. padding: 0;
  364. }
  365. .ant-tree-node-content-wrapper{
  366. color: rgb(209, 4, 4);
  367. }
  368. }
  369. }
  370. </style>