Mr.Shi
2023-08-11 5a7fb4358eb902ce139096f20ef3c0625adcd7f4
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<template>
  <GlobalWindow
    :title="title"
    :visible.sync="visible"
    :confirm-working="isWorking"
    @confirm="confirm"
  >
    <el-form :model="form" ref="form" :rules="rules">
      <el-form-item label="权限模块" prop="module">
        <el-input v-model="form.module" placeholder="请输入权限模块" v-trim maxlength="500"/>
        <FormItemTip>多个模块可以通过"/"分割,例如"日志管理/操作日志"</FormItemTip>
      </el-form-item>
    </el-form>
  </GlobalWindow>
</template>
 
<script>
import BaseOpera from '@/components/base/BaseOpera'
import GlobalWindow from '@/components/common/GlobalWindow'
import FormItemTip from '@/components/common/FormItemTip'
import { updateByIdInBatch } from '@/api/system/permission'
 
export default {
  name: 'OperaModuleWindow',
  extends: BaseOpera,
  components: { FormItemTip, GlobalWindow },
  data () {
    return {
      // 行数据
      row: null,
      // 表单数据
      form: {
        module: ''
      },
      // 验证规则
      rules: {
        module: [
          { validator: this.__checkModule, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    /**
     * 打开窗口
     *
     * @param title 标题
     * @param target 行对象(仅编辑需该参数)
     */
    open (title, target) {
      this.title = title
      this.row = target
      this.visible = true
      // 编辑
      this.$nextTick(() => {
        this.form.module = target.name
      })
    },
    /**
     * 确认
     */
    confirm () {
      this.$refs.form.validate(valid => {
        if (!valid) {
          return
        }
        const pool = []
        this.__fillPool(pool, this.row.children, this.form.module)
        this.isWorking = true
        updateByIdInBatch(pool)
          .then(() => {
            this.visible = false
            this.$emit('success')
          })
          .catch(e => {
            this.$tip.apiFailed(e)
          })
          .finally(() => {
            this.isWorking = false
          })
      })
    },
    /**
     * 填充权限池,将所有权限的模块前缀调整为新权限模块后丢入pool中
     *
     * @param pool 权限池
     * @param list 权限列表
     * @param newModule 新权限模块
     */
    __fillPool (pool, list, newModule) {
      if (list == null) {
        return
      }
      for (const child of list) {
        if (child.type !== 'module') {
          const modulePaths = child.module.split('/')
          modulePaths[this.row.level] = newModule
          child.module = modulePaths.join('/')
          pool.push(child)
          continue
        }
        this.__fillPool(pool, child.children, newModule)
      }
    },
    /**
     * 验证模块
     *
     * @param rule 规则
     * @param value 值
     * @param callback 回调
     * @returns {*}
     * @private
     */
    __checkModule (rule, value, callback) {
      if (value == null || value === '') {
        return callback()
      }
      // 不允许包含空格
      if (value.indexOf(' ') !== -1) {
        return callback(new Error('不允许包含空格'))
      }
      // 不允许使用"/"开头或结尾
      if (value.startsWith('/') || value.endsWith('/')) {
        return callback(new Error('请勿使用"/"开头或结尾'))
      }
      return callback()
    }
  },
  created () {
    this.config({
      api: '/system/permission'
    })
  }
}
</script>