Rap 原分销系统代码Web
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

361 行
12KB

  1. <template>
  2. <div class="app-container">
  3. <!--工具栏-->
  4. <div class="head-container">
  5. <div v-if="crud.props.searchToggle">
  6. <!-- 搜索 -->
  7. <el-input v-model="query.blurry" size="small" clearable placeholder="输入名称或者描述搜索" style="width: 200px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
  8. <date-range-picker v-model="query.createTime" class="date-item" />
  9. <rrOperation />
  10. </div>
  11. <crudOperation :permission="permission" />
  12. </div>
  13. <!-- 表单渲染 -->
  14. <el-dialog append-to-body :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="520px">
  15. <el-form ref="form" :inline="true" :model="form" :rules="rules" size="small" label-width="80px">
  16. <el-form-item label="角色名称" prop="name">
  17. <el-input v-model="form.name" style="width: 380px;" />
  18. </el-form-item>
  19. <el-form-item label="角色级别" prop="level">
  20. <el-input-number v-model.number="form.level" :min="1" controls-position="right" style="width: 145px;" />
  21. </el-form-item>
  22. <el-form-item label="数据范围" prop="dataScope">
  23. <el-select v-model="form.dataScope" style="width: 140px" placeholder="请选择数据范围" @change="changeScope">
  24. <el-option
  25. v-for="item in dateScopes"
  26. :key="item"
  27. :label="item"
  28. :value="item"
  29. />
  30. </el-select>
  31. </el-form-item>
  32. <el-form-item v-if="form.dataScope === '自定义'" label="数据权限" prop="depts">
  33. <treeselect
  34. v-model="deptDatas"
  35. :load-options="loadDepts"
  36. :options="depts"
  37. multiple
  38. style="width: 380px"
  39. placeholder="请选择"
  40. />
  41. </el-form-item>
  42. <el-form-item label="描述信息" prop="description">
  43. <el-input v-model="form.description" style="width: 380px;" rows="5" type="textarea" />
  44. </el-form-item>
  45. </el-form>
  46. <div slot="footer" class="dialog-footer">
  47. <el-button type="text" @click="crud.cancelCU">取消</el-button>
  48. <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
  49. </div>
  50. </el-dialog>
  51. <el-row :gutter="15">
  52. <!--角色管理-->
  53. <el-col :xs="24" :sm="24" :md="16" :lg="16" :xl="17" style="margin-bottom: 10px">
  54. <el-card class="box-card" shadow="never">
  55. <div slot="header" class="clearfix">
  56. <span class="role-span">角色列表</span>
  57. </div>
  58. <el-table ref="table" v-loading="crud.loading" highlight-current-row style="width: 100%;" :data="crud.data" @selection-change="crud.selectionChangeHandler" @current-change="handleCurrentChange">
  59. <el-table-column :selectable="checkboxT" type="selection" width="55" />
  60. <el-table-column prop="name" label="名称" />
  61. <el-table-column prop="dataScope" label="数据权限" />
  62. <el-table-column prop="level" label="角色级别" />
  63. <el-table-column :show-overflow-tooltip="true" prop="description" label="描述" />
  64. <el-table-column :show-overflow-tooltip="true" width="135px" prop="createTime" label="创建日期" />
  65. <el-table-column v-if="checkPer(['admin','roles:edit','roles:del'])" label="操作" width="130px" align="center" fixed="right">
  66. <template slot-scope="scope">
  67. <udOperation
  68. v-if="scope.row.level >= level"
  69. :data="scope.row"
  70. :permission="permission"
  71. />
  72. </template>
  73. </el-table-column>
  74. </el-table>
  75. <!--分页组件-->
  76. <pagination />
  77. </el-card>
  78. </el-col>
  79. <!-- 菜单授权 -->
  80. <el-col :xs="24" :sm="24" :md="8" :lg="8" :xl="7">
  81. <el-card class="box-card" shadow="never">
  82. <div slot="header" class="clearfix">
  83. <el-tooltip class="item" effect="dark" content="选择指定角色分配菜单" placement="top">
  84. <span class="role-span">菜单分配</span>
  85. </el-tooltip>
  86. <el-button
  87. v-permission="['admin','roles:edit']"
  88. :disabled="!showButton"
  89. :loading="menuLoading"
  90. icon="el-icon-check"
  91. size="mini"
  92. style="float: right; padding: 6px 9px"
  93. type="primary"
  94. @click="saveMenu"
  95. >保存</el-button>
  96. </div>
  97. <el-tree
  98. ref="menu"
  99. lazy
  100. :data="menus"
  101. :default-checked-keys="menuIds"
  102. :load="getMenuDatas"
  103. :props="defaultProps"
  104. check-strictly
  105. accordion
  106. show-checkbox
  107. node-key="id"
  108. @check="menuChange"
  109. />
  110. </el-card>
  111. </el-col>
  112. </el-row>
  113. </div>
  114. </template>
  115. <script>
  116. import crudRoles from '@/api/system/role'
  117. import { getDepts, getDeptSuperior } from '@/api/system/dept'
  118. import { getMenusTree, getChild } from '@/api/system/menu'
  119. import CRUD, { presenter, header, form, crud } from '@crud/crud'
  120. import rrOperation from '@crud/RR.operation'
  121. import crudOperation from '@crud/CRUD.operation'
  122. import udOperation from '@crud/UD.operation'
  123. import pagination from '@crud/Pagination'
  124. import Treeselect from '@riophae/vue-treeselect'
  125. import '@riophae/vue-treeselect/dist/vue-treeselect.css'
  126. import { LOAD_CHILDREN_OPTIONS } from '@riophae/vue-treeselect'
  127. import DateRangePicker from '@/components/DateRangePicker'
  128. const defaultForm = { id: null, name: null, depts: [], description: null, dataScope: '全部', level: 3 }
  129. export default {
  130. name: 'Role',
  131. components: { Treeselect, pagination, crudOperation, rrOperation, udOperation, DateRangePicker },
  132. cruds() {
  133. return CRUD({ title: '角色', url: 'api/roles', sort: 'level,asc', crudMethod: { ...crudRoles }})
  134. },
  135. mixins: [presenter(), header(), form(defaultForm), crud()],
  136. data() {
  137. return {
  138. defaultProps: { children: 'children', label: 'label', isLeaf: 'leaf' },
  139. dateScopes: ['全部', '本级', '自定义'], level: 3,
  140. currentId: 0, menuLoading: false, showButton: false,
  141. menus: [], menuIds: [], depts: [], deptDatas: [], // 多选时使用
  142. permission: {
  143. add: ['admin', 'roles:add'],
  144. edit: ['admin', 'roles:edit'],
  145. del: ['admin', 'roles:del']
  146. },
  147. rules: {
  148. name: [
  149. { required: true, message: '请输入名称', trigger: 'blur' }
  150. ],
  151. permission: [
  152. { required: true, message: '请输入权限', trigger: 'blur' }
  153. ]
  154. }
  155. }
  156. },
  157. created() {
  158. crudRoles.getLevel().then(data => {
  159. this.level = data.level
  160. })
  161. },
  162. methods: {
  163. getMenuDatas(node, resolve) {
  164. setTimeout(() => {
  165. getMenusTree(node.data.id ? node.data.id : 0).then(res => {
  166. resolve(res)
  167. })
  168. }, 100)
  169. },
  170. [CRUD.HOOK.afterRefresh]() {
  171. this.$refs.menu.setCheckedKeys([])
  172. },
  173. // 新增前初始化部门信息
  174. [CRUD.HOOK.beforeToAdd](crud, form) {
  175. this.deptDatas = []
  176. form.menus = null
  177. },
  178. // 编辑前初始化自定义数据权限的部门信息
  179. [CRUD.HOOK.beforeToEdit](crud, form) {
  180. this.deptDatas = []
  181. if (form.dataScope === '自定义') {
  182. this.getSupDepts(form.depts)
  183. }
  184. const _this = this
  185. form.depts.forEach(function(dept) {
  186. _this.deptDatas.push(dept.id)
  187. })
  188. // 将角色的菜单清空,避免日志入库数据过长
  189. form.menus = null
  190. },
  191. // 提交前做的操作
  192. [CRUD.HOOK.afterValidateCU](crud) {
  193. if (crud.form.dataScope === '自定义' && this.deptDatas.length === 0) {
  194. this.$message({
  195. message: '自定义数据权限不能为空',
  196. type: 'warning'
  197. })
  198. return false
  199. } else if (crud.form.dataScope === '自定义') {
  200. const depts = []
  201. this.deptDatas.forEach(function(data) {
  202. const dept = { id: data }
  203. depts.push(dept)
  204. })
  205. crud.form.depts = depts
  206. } else {
  207. crud.form.depts = []
  208. }
  209. return true
  210. },
  211. // 触发单选
  212. handleCurrentChange(val) {
  213. if (val) {
  214. const _this = this
  215. // 清空菜单的选中
  216. this.$refs.menu.setCheckedKeys([])
  217. // 保存当前的角色id
  218. this.currentId = val.id
  219. // 初始化默认选中的key
  220. this.menuIds = []
  221. val.menus.forEach(function(data) {
  222. _this.menuIds.push(data.id)
  223. })
  224. this.showButton = true
  225. }
  226. },
  227. menuChange(menu) {
  228. // 获取该节点的所有子节点,id 包含自身
  229. getChild(menu.id).then(childIds => {
  230. // 判断是否在 menuIds 中,如果存在则删除,否则添加
  231. if (this.menuIds.indexOf(menu.id) !== -1) {
  232. for (let i = 0; i < childIds.length; i++) {
  233. const index = this.menuIds.indexOf(childIds[i])
  234. if (index !== -1) {
  235. this.menuIds.splice(index, 1)
  236. }
  237. }
  238. } else {
  239. for (let i = 0; i < childIds.length; i++) {
  240. const index = this.menuIds.indexOf(childIds[i])
  241. if (index === -1) {
  242. this.menuIds.push(childIds[i])
  243. }
  244. }
  245. }
  246. this.$refs.menu.setCheckedKeys(this.menuIds)
  247. })
  248. },
  249. // 保存菜单
  250. saveMenu() {
  251. this.menuLoading = true
  252. const role = { id: this.currentId, menus: [] }
  253. // 得到已选中的 key 值
  254. this.menuIds.forEach(function(id) {
  255. const menu = { id: id }
  256. role.menus.push(menu)
  257. })
  258. crudRoles.editMenu(role).then(() => {
  259. this.crud.notify('保存成功', CRUD.NOTIFICATION_TYPE.SUCCESS)
  260. this.menuLoading = false
  261. this.update()
  262. }).catch(err => {
  263. this.menuLoading = false
  264. console.log(err.response.data.message)
  265. })
  266. },
  267. // 改变数据
  268. update() {
  269. // 无刷新更新 表格数据
  270. crudRoles.get(this.currentId).then(res => {
  271. for (let i = 0; i < this.crud.data.length; i++) {
  272. if (res.id === this.crud.data[i].id) {
  273. this.crud.data[i] = res
  274. break
  275. }
  276. }
  277. })
  278. },
  279. // 获取部门数据
  280. getDepts() {
  281. getDepts({ enabled: true }).then(res => {
  282. this.depts = res.content.map(function(obj) {
  283. if (obj.hasChildren) {
  284. obj.children = null
  285. }
  286. return obj
  287. })
  288. })
  289. },
  290. getSupDepts(depts) {
  291. const ids = []
  292. depts.forEach(dept => {
  293. ids.push(dept.id)
  294. })
  295. getDeptSuperior(ids).then(res => {
  296. const date = res.content
  297. this.buildDepts(date)
  298. this.depts = date
  299. })
  300. },
  301. buildDepts(depts) {
  302. depts.forEach(data => {
  303. if (data.children) {
  304. this.buildDepts(data.children)
  305. }
  306. if (data.hasChildren && !data.children) {
  307. data.children = null
  308. }
  309. })
  310. },
  311. // 获取弹窗内部门数据
  312. loadDepts({ action, parentNode, callback }) {
  313. if (action === LOAD_CHILDREN_OPTIONS) {
  314. getDepts({ enabled: true, pid: parentNode.id }).then(res => {
  315. parentNode.children = res.content.map(function(obj) {
  316. if (obj.hasChildren) {
  317. obj.children = null
  318. }
  319. return obj
  320. })
  321. setTimeout(() => {
  322. callback()
  323. }, 200)
  324. })
  325. }
  326. },
  327. // 如果数据权限为自定义则获取部门数据
  328. changeScope() {
  329. if (this.form.dataScope === '自定义') {
  330. this.getDepts()
  331. }
  332. },
  333. checkboxT(row) {
  334. return row.level >= this.level
  335. }
  336. }
  337. }
  338. </script>
  339. <style rel="stylesheet/scss" lang="scss">
  340. .role-span {
  341. font-weight: bold;color: #303133;
  342. font-size: 15px;
  343. }
  344. </style>
  345. <style rel="stylesheet/scss" lang="scss" scoped>
  346. ::v-deep .el-input-number .el-input__inner {
  347. text-align: left;
  348. }
  349. ::v-deep .vue-treeselect__multi-value{
  350. margin-bottom: 0;
  351. }
  352. ::v-deep .vue-treeselect__multi-value-item{
  353. border: 0;
  354. padding: 0;
  355. }
  356. </style>