【前端】智能库房综合管理系统前端项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

381 lines
13 KiB

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