Item.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <div class="d-flex mt-3 mb-4">
  3. <div class="res-title flex-grow-0 flex-shrink-0">{{ resource.label }}</div>
  4. <div class="flex-fill">
  5. <!-- 全选 -->
  6. <a-checkbox
  7. class="mr-2"
  8. :checked="resource.checkAll"
  9. @change="handleCheckAllChange"
  10. :indeterminate="resource.isIndeterminate"
  11. :disabled="resource.disabled">{{$t('system.text_320')}}</a-checkbox>
  12. <a-checkbox-group
  13. v-model="normalChecked"
  14. @change="handleCheckedActionsChange"
  15. :options="normalActions" />
  16. <!-- 执行操作 -->
  17. <a-checkbox
  18. class="ml-2"
  19. :checked="performActionShowChecked"
  20. @change="performActionCheckedChange"
  21. :indeterminate="isPerformActionIndeterminate"
  22. :disabled="resource.disabled">{{performAction.label}}</a-checkbox>
  23. <template v-if="extraActions.length">
  24. <a-button type="link" @click="showExtraActionConfig">{{extraActionName}}</a-button>
  25. </template>
  26. </div>
  27. </div>
  28. </template>
  29. <script>
  30. import * as R from 'ramda'
  31. import WindowsMixin from '@/mixins/windows'
  32. import { DEFAULT_ACTIONS_KEY } from '../../../constants'
  33. const EXPECT_PERFORM_DEFAULT_ACTION_KEY = DEFAULT_ACTIONS_KEY.slice(0, DEFAULT_ACTIONS_KEY.length - 1)
  34. export default {
  35. name: 'PolicyRuleCheckboxItem',
  36. mixins: [WindowsMixin],
  37. props: {
  38. resource: Object,
  39. permissions: Object,
  40. itemPolicy: Object,
  41. },
  42. data () {
  43. return {
  44. normalChecked: [],
  45. }
  46. },
  47. computed: {
  48. // normalChecked () {
  49. // return this.resource.checked.filter(action => EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(action))
  50. // },
  51. normalActions () {
  52. return this.resource.actions.filter(action => EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(action.action))
  53. },
  54. extraActions () {
  55. return this.resource.actions.filter(action => !DEFAULT_ACTIONS_KEY.includes(action.action))
  56. },
  57. performAction () {
  58. return this.resource.actions.filter(action => action.action === 'perform')[0]
  59. },
  60. extraActionName () {
  61. return '所有'
  62. },
  63. performActionShowChecked () {
  64. if (!this.extraActions.length) {
  65. return this.resource.checked.includes('perform')
  66. }
  67. let allChecked = true
  68. this.extraActions.map(item => {
  69. if (!this.resource.checked.includes(item.action)) {
  70. allChecked = false
  71. }
  72. })
  73. return allChecked
  74. },
  75. // 是否展示执行操作为 选中一部分
  76. isPerformActionIndeterminate () {
  77. if (!this.extraActions.length) {
  78. return false
  79. }
  80. let extraCheckedCount = 0
  81. this.extraActions.map(action => {
  82. if (this.resource.checked.includes(action.action)) {
  83. extraCheckedCount += 1
  84. }
  85. })
  86. return extraCheckedCount > 0 && extraCheckedCount < this.extraActions.length
  87. },
  88. },
  89. watch: {
  90. permissions: {
  91. handler (val) {
  92. if (!R.isEmpty(val) && !R.isNil(val)) {
  93. const checked = [...this.resource.checked]
  94. this.resource.actions.forEach(action => {
  95. const permission = val[`${this.resource.resource}_${action.action}`]
  96. if (
  97. permission[permission.length - 2] === 'allow' &&
  98. permission[permission.length - 1] === 'allow' &&
  99. !action.disabled
  100. ) {
  101. // const checked = [...this.resource.checked]
  102. if (!checked.includes(action.action)) {
  103. checked.push(action.action)
  104. // this.handleCheckedActionsChange(checked)
  105. } else {
  106. // this.handleCheckedActionsChange([action.action])
  107. }
  108. }
  109. })
  110. this.initChecked(checked)
  111. }
  112. },
  113. immediate: true,
  114. },
  115. // itemPolicy: {
  116. // handler (val) {
  117. // if (!R.isEmpty(val) && !R.isNil(val)) {
  118. // const checked = [...this.resource.checked]
  119. // this.resource.actions.forEach(action => {
  120. // if (val[action.action]['*'] === 'allow' && !action.disabled) {
  121. // if (!checked.includes(action.action)) {
  122. // checked.push(action.action)
  123. // // this.handleCheckedActionsChange(checked)
  124. // } else {
  125. // // this.handleCheckedActionsChange([action.action])
  126. // }
  127. // }
  128. // })
  129. // this.handleCheckedActionsChange(checked)
  130. // }
  131. // },
  132. // immediate: true,
  133. // },
  134. 'resource.checked': {
  135. handler (val) {
  136. this.normalChecked = val.filter(action => EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(action))
  137. },
  138. immediate: true,
  139. },
  140. },
  141. methods: {
  142. initChecked (checked) {
  143. this.normalChecked = checked.filter(action => EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(action))
  144. const isIndeterminate = checked.length > 0 && checked.length < this.resource.actions.length
  145. const checkAll = checked.length === this.resource.actions.length
  146. this.$set(this.resource, 'checked', checked)
  147. this.$set(this.resource, 'isIndeterminate', isIndeterminate)
  148. this.$set(this.resource, 'checkAll', checkAll)
  149. this.$emit('resourceCheckChange')
  150. },
  151. // 全选
  152. handleCheckAllChange (e) {
  153. const val = e.target.checked
  154. const unDisabledActions = this.resource.actions.filter(item => !item.disabled)
  155. const checked = val ? unDisabledActions.map(action => action.action) : []
  156. const isIndeterminate = checked.length > 0 && checked.length < this.resource.actions.length
  157. const checkAll = checked.length === this.resource.actions.length
  158. this.$set(this.resource, 'checked', checked)
  159. this.$set(this.resource, 'isIndeterminate', isIndeterminate)
  160. this.$set(this.resource, 'checkAll', checkAll)
  161. this.$emit('resourceCheckChange')
  162. },
  163. // [增删改查详情]选择
  164. handleCheckedActionsChange (val) {
  165. this.normalChecked = val
  166. // 操作详情的选择
  167. let extraAndPerformChecked = this.resource.checked.filter(action => !EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(action))
  168. // 联动 [执行操作]
  169. this.extraActions.map(action => {
  170. const actionTypes = action.action.split('_')
  171. if (actionTypes[1] && actionTypes[1] === '*' && EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(actionTypes[0])) {
  172. if (val.includes(actionTypes[0]) && !extraAndPerformChecked.includes(action.action)) {
  173. extraAndPerformChecked.push(action.action)
  174. } else if (!val.includes(actionTypes[0]) && extraAndPerformChecked.includes(action.action)) {
  175. extraAndPerformChecked = extraAndPerformChecked.filter(key => key !== action.action)
  176. }
  177. }
  178. })
  179. const checked = [...val, ...extraAndPerformChecked]
  180. const isIndeterminate = checked.length > 0 && checked.length < this.resource.actions.length
  181. const checkAll = checked.length === this.resource.actions.length
  182. this.$set(this.resource, 'checked', checked)
  183. this.$set(this.resource, 'isIndeterminate', isIndeterminate)
  184. this.$set(this.resource, 'checkAll', checkAll)
  185. this.$emit('resourceCheckChange')
  186. },
  187. // 执行操作选择
  188. performActionCheckedChange (e) {
  189. const val = e.target.checked
  190. // [增删改查详情]的选择
  191. let normalChecked = this.resource.checked.filter(action => {
  192. return EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(action)
  193. })
  194. // 操作详情的选择
  195. let extraChecked = this.extraActions.map(action => {
  196. // 清空或选中操作详情 联动 [增删改查详情]
  197. const actionTypes = action.action.split('_')
  198. if (actionTypes[1] && actionTypes[1] === '*' && EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(actionTypes[0])) {
  199. if (!val && normalChecked.includes(actionTypes[0])) {
  200. normalChecked = normalChecked.filter(key => key !== actionTypes[0])
  201. } else if (val && !normalChecked.includes(actionTypes[0])) {
  202. normalChecked.push(actionTypes[0])
  203. }
  204. }
  205. return action.action
  206. })
  207. if (!val) {
  208. extraChecked = []
  209. }
  210. const checked = [...normalChecked, ...extraChecked]
  211. if (extraChecked.length === this.extraActions.length) {
  212. checked.push('perform')
  213. }
  214. const isIndeterminate = checked.length > 0 && checked.length < this.resource.actions.length
  215. const checkAll = checked.length === this.resource.actions.length
  216. this.$set(this.resource, 'checked', checked)
  217. this.$set(this.resource, 'isIndeterminate', isIndeterminate)
  218. this.$set(this.resource, 'checkAll', checkAll)
  219. this.$emit('resourceCheckChange')
  220. },
  221. // 执行操作详情选择
  222. extraActionsCheckedChange (extraChecked) {
  223. const normalChecked = this.resource.checked.filter(action => {
  224. return EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(action)
  225. })
  226. const checked = [...normalChecked, ...extraChecked]
  227. if (extraChecked.length) {
  228. checked.push('perform')
  229. }
  230. const isIndeterminate = checked.length > 0 && checked.length < this.resource.actions.length
  231. const checkAll = checked.length === this.resource.actions.length
  232. this.$set(this.resource, 'checked', checked)
  233. this.$set(this.resource, 'isIndeterminate', isIndeterminate)
  234. this.$set(this.resource, 'checkAll', checkAll)
  235. this.$emit('resourceCheckChange')
  236. },
  237. extraChangeNormal (action, val) {
  238. let normalChecked = this.resource.checked.filter(action => EXPECT_PERFORM_DEFAULT_ACTION_KEY.includes(action))
  239. if (val && !normalChecked.includes(action)) {
  240. normalChecked.push(action)
  241. } else if (!val && normalChecked.includes(action)) {
  242. normalChecked = normalChecked.filter(key => key !== action)
  243. }
  244. this.handleCheckedActionsChange(normalChecked)
  245. },
  246. showExtraActionConfig () {
  247. this.createDialog('PolicyExtraActionConfig', {
  248. resource: this.resource,
  249. actions: this.extraActions,
  250. checked: this.resource.checked,
  251. checkedChange: this.extraActionsCheckedChange,
  252. extraChangeNormal: this.extraChangeNormal,
  253. })
  254. },
  255. },
  256. }
  257. </script>
  258. <style lang="less" scoped>
  259. .res-title {
  260. width: 160px;
  261. }
  262. .extra-checkboxs {
  263. visibility: none;
  264. }
  265. </style>