auth.vue 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <div class="app-container">
  3. <div class="title-container">
  4. <breadcrumb id="breadcrumb-container" class="breadcrumb-container"/>
  5. </div>
  6. <!-- <y-detail-page-layout :save="handleCreate" :edit-status="true">-->
  7. <!-- -->
  8. <!-- </y-detail-page-layout>-->
  9. <div class="filter-main-div" style="margin-top: -20px;">
  10. <div class="set-menu">
  11. <div class="set-menu-box">
  12. <div class="left">
  13. <div style="margin-bottom: 10px;text-align: right;margin-top: 30px;">
  14. <!-- <span style="color: #666666;font-size: 10px;padding-left: 10px;">(点击设置权限)</span>-->
  15. </div>
  16. <!--default-expand-all-->
  17. <div style="height: calc(100vh - 200px); overflow-y: auto;">
  18. <el-tree
  19. v-loading="treeLoading"
  20. ref="tree"
  21. default-expand-all
  22. :expand-on-click-node="false"
  23. :data="treeData"
  24. node-key="id"
  25. @node-click="nodeClick"
  26. >
  27. <span slot-scope="{ node, data }" class="custom-tree-node">
  28. <el-checkbox
  29. v-model="data.checked"
  30. style="margin-right: 3px;"
  31. @change="(sta) => {checkboxChange(data, node, sta)}"
  32. />
  33. <el-tooltip class="item" effect="dark" :content="data.menuType" placement="top-start">
  34. <i v-if="data.menuType==='目录'" class="el-icon-folder-opened"/>
  35. <i v-if="data.menuType==='菜单'" class="el-icon-document"/>
  36. <i v-if="data.menuType==='按钮'" class="el-icon-thumb"/>
  37. </el-tooltip>
  38. <!-- <span class="tips">{{ node.level }}</span>-->
  39. <!-- <span>{{ data }}</span>-->
  40. <span style="margin-left: 5px;">{{ data.name }}</span>
  41. </span>
  42. </el-tree>
  43. </div>
  44. </div>
  45. <div class="right">
  46. <div class="btn-set">
  47. <el-button
  48. class="filter-item"
  49. style="position: absolute;right: 10px; z-index: 99;"
  50. type="primary"
  51. round
  52. @click="handleCreate"
  53. >
  54. 保存
  55. </el-button>
  56. <el-tabs type="card">
  57. <el-tab-pane :label=" '【'+postName + '】权限设置'">
  58. </el-tab-pane>
  59. </el-tabs>
  60. </div>
  61. </div>
  62. </div>
  63. </div>
  64. </div>
  65. </div>
  66. </template>
  67. <script>
  68. import Breadcrumb from '@/components/Breadcrumb'
  69. // import YDetailPageLayout from '@/components/YDetailPageLayout'
  70. export default {
  71. name: 'Auth',
  72. components: {
  73. Breadcrumb,
  74. // YDetailPageLayout,
  75. },
  76. data() {
  77. return {
  78. temp: {
  79. btn: [],
  80. level: '',
  81. dataPrivilege: null,
  82. },
  83. postName:this.$route.query.name,
  84. treeData: [],
  85. treeListData: [],
  86. defaultProps: {
  87. children: 'children',
  88. label: 'label'
  89. },
  90. menuItem: {},
  91. checkedIds: [],
  92. treeLoading: false,
  93. }
  94. },
  95. mounted() {
  96. this.getDetail()
  97. },
  98. methods: {
  99. initData() {
  100. this.temp = {
  101. btn: [],
  102. level: '',
  103. dataPrivilege: null,
  104. }
  105. },
  106. getDetail() {
  107. this.treeLoading = true
  108. this.$api.postAuth.tree({ postId: this.$route.query.id }).then(res => {
  109. this.treeData = res.data.filter((item) => { return item.id && item.name })
  110. const list = []
  111. const checkeds = []
  112. function treeMap(data) {
  113. data.forEach(item => {
  114. list.push(item)
  115. if (item.checked) {
  116. checkeds.push(item.id)
  117. }
  118. if (item.children && item.children.length > 0) {
  119. treeMap(item.children)
  120. }
  121. })
  122. }
  123. treeMap(this.treeData)
  124. this.treeListData = list
  125. this.checkedIds = checkeds
  126. this.$refs.tree.setCheckedKeys(this.checkedIds)
  127. this.treeLoading = false
  128. }).catch(()=>{
  129. this.treeLoading = false
  130. })
  131. },
  132. setParentCheck(data) { // 获取父节点id数组/修改父节点checked 为true
  133. const parentChecks = []
  134. function tree(data, list) {
  135. if (data.parentId) {
  136. list.forEach(item => {
  137. if (item.id === data.parentId) {
  138. parentChecks.push(item.id)
  139. item.checked = true
  140. if (item.parentId) {
  141. tree(item, list)
  142. }
  143. }
  144. })
  145. }
  146. }
  147. tree(data, this.treeListData)
  148. return parentChecks
  149. },
  150. setChildrenCheck(data, checkedStatus = true) { // 取消子节点选中效果
  151. const that = this
  152. const childrenIds = []
  153. function tree(list) {
  154. list.forEach(item => {
  155. // console.log('子菜单', item.name)
  156. item.checked = checkedStatus
  157. childrenIds.push(item.id)
  158. if (item.children) {
  159. tree(item.children)
  160. }
  161. })
  162. }
  163. data.children && tree(data.children)
  164. // console.log(data.children, '子树', childrenIds)
  165. return childrenIds
  166. },
  167. handleCheckChange(data, checked, indeterminate) {
  168. // console.log(data, checked, indeterminate, 'check node')
  169. // if (checked === true) {
  170. // const pChecks = this.setParentCheck(data)
  171. // this.checkedIds = this.checkedIds.concat(pChecks)
  172. // this.checkedIds = Array.from(new Set(this.checkedIds)) // 去重
  173. // this.$refs.tree.setCheckedKeys(this.checkedIds)
  174. // console.log(this.setChildrenCheck(data, true), 'id数据')
  175. // } else {
  176. // const childrenIds = this.setChildrenCheck(data)
  177. // this.checkedIds = Array.from(new Set(this.checkedIds)) // 去重
  178. // // childrenIds.forEach(cId => {
  179. // // const index = this.checkedIds.indexOf(cId)
  180. // // if (index > -1) {
  181. // // this.checkedIds.splice(index, 1)
  182. // // }
  183. // // })
  184. // // console.log(childrenIds, this.checkedIds, 'id数据')
  185. // // this.$refs.tree.setCheckedKeys(this.checkedIds)
  186. // }
  187. },
  188. nodeClick(data, node, it) {
  189. this.menuItem = data
  190. },
  191. checkboxChange(data, node, sta) {
  192. // console.log(this.$refs.tree.getCheckedKeys(), '当前被选中key')
  193. // this.menuItem = data
  194. if (sta) { // 设为选中,父子都选中
  195. // 父节点
  196. const pChecks = this.setParentCheck(data)
  197. // 字节点
  198. const childrenIds = this.setChildrenCheck(data, true)
  199. } else {
  200. // 字节点
  201. const childrenIds = this.setChildrenCheck(data, false)
  202. }
  203. },
  204. handleCreate() {
  205. // console.log(this.$refs.tree.getCheckedNodes())
  206. // console.log(this.$refs.tree.getCheckedKeys())
  207. // console.log(JSON.stringify({
  208. // postId: this.$route.query.id,
  209. // permissionTree: JSON.stringify(this.treeData)
  210. // }))
  211. this.$api.postAuth.edit({
  212. postId: this.$route.query.id,
  213. permissionTree: JSON.stringify(this.treeData)
  214. }).then(res => {
  215. if (res.code === 200) {
  216. this.$notify({
  217. title: '成功',
  218. message: '保存成功',
  219. type: 'success',
  220. duration: 2000
  221. })
  222. this.getDetail()
  223. }
  224. })
  225. }
  226. }
  227. }
  228. </script>
  229. <style scoped lang="scss">
  230. .btn-box {
  231. width: 1300px;
  232. margin: auto;
  233. margin-top: 30px;
  234. }
  235. .set-menu-box {
  236. display: flex;
  237. margin: 30px auto;
  238. /*border: 1px solid #f0f0f0;*/
  239. .left {
  240. width: 360px;
  241. padding: 10px;
  242. border-right: 1px solid #f0f0f0;
  243. }
  244. .right {
  245. flex: 1;
  246. min-height: 500px;
  247. .btn-set {
  248. padding: 10px 10px 30px 10px;
  249. position: relative;
  250. .title-line {
  251. border-bottom: 2px solid #2a8fe3;
  252. margin-bottom: 20px;
  253. margin-top: 10px;
  254. }
  255. .title {
  256. background-color: #2a8fe3;
  257. color: #ffffff;
  258. padding: 4px 5px;
  259. }
  260. }
  261. }
  262. }
  263. .custom-tree-node {
  264. display: flex;
  265. align-items: center;
  266. .tips {
  267. display: inline-block;
  268. width: 18px;
  269. height: 18px;
  270. text-align: center;
  271. background-color: #f67220;
  272. color: #ffffff;
  273. border-radius: 50%;
  274. }
  275. .btns {
  276. margin-left: 10px;
  277. }
  278. }
  279. </style>
  280. <style lang="scss" scoped>
  281. .set-menu {
  282. .el-tree-node__content {
  283. margin-bottom: 5px;
  284. }
  285. }
  286. </style>