role.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <div class="app-container calendar-list-container">
  3. <!-- 查询和其他操作 -->
  4. <div class="filter-container">
  5. <el-input clearable class="filter-item" style="width: 200px;" placeholder="请输入角色名称" v-model="listQuery.roleName">
  6. </el-input>
  7. <el-button class="filter-item" type="primary" v-waves icon="el-icon-search" @click="handleQuery">查找</el-button>
  8. <el-button class="filter-item" icon="el-icon-refresh" @click="resetQuery">重置</el-button>
  9. <el-button class="filter-item" type="primary" @click="handleCreate" icon="el-icon-plus">添加</el-button>
  10. </div>
  11. <!-- 查询结果 -->
  12. <el-table size="small" :data="list" v-loading="listLoading" element-loading-text="正在查询中。。。" border fit highlight-current-row>
  13. <!--<el-table-column align="center" width="100px" label="角色ID" prop="id" sortable>
  14. </el-table-column>-->
  15. <el-table-column type="index" label="序号" header-align="center" align="center">
  16. </el-table-column>
  17. <el-table-column align="center" min-width="100px" label="角色名称" prop="roleName">
  18. </el-table-column>
  19. <el-table-column align="center" label="操作" width="250" class-name="small-padding fixed-width">
  20. <template slot-scope="scope">
  21. <el-button type="primary" size="small" @click="handleUpdate(scope.row, false)">编辑</el-button>
  22. <el-button type="primary" size="small" @click="handleUpdate(scope.row, true)">权限</el-button>
  23. </template>
  24. </el-table-column>
  25. </el-table>
  26. <!-- 分页 -->
  27. <div class="pagination-container">
  28. <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="listQuery.page"
  29. :page-sizes="[10,20,30,50]" :page-size="listQuery.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
  30. </el-pagination>
  31. </div>
  32. <!-- 添加或修改对话框 -->
  33. <el-dialog :title="textMap[dialogStatus]" :visible.sync="dialogFormVisible">
  34. <el-form :rules="rules" ref="dataForm" :model="dataForm" status-icon label-position="left" label-width="100px" style='width: 400px; margin-left:50px;'>
  35. <template v-if="!dialogMain">
  36. <el-form-item label="角色名称" prop="roleName">
  37. <el-input v-model="dataForm.roleName"></el-input>
  38. </el-form-item>
  39. </template>
  40. <template v-else>
  41. <el-form-item label="角色权限" prop="password">
  42. <el-tree
  43. :data="router"
  44. show-checkbox
  45. node-key="name"
  46. ref="tree"
  47. :props="defaultProps">
  48. </el-tree>
  49. </el-form-item>
  50. </template>
  51. </el-form>
  52. <div slot="footer" class="dialog-footer">
  53. <el-button @click="dialogFormVisible = false">取消</el-button>
  54. <el-button v-if="dialogStatus=='create'" type="primary" @click="createData">确定</el-button>
  55. <template v-else>
  56. <el-button v-if="!dialogMain" type="primary" @click="updateRoleNames">确定</el-button>
  57. <el-button v-else type="primary" @click="updateRoleList">确定</el-button>
  58. </template>
  59. </div>
  60. </el-dialog>
  61. </div>
  62. </template>
  63. <script>
  64. import { asyncRouterMap } from '@/router'
  65. import { addRole, roleList, updateRoleName, getRoleMenu, updateRoleMenu } from '@/api/role'
  66. // import { listAdmin } from '@/api/admin'
  67. import waves from '@/directive/waves' // 水波纹指令
  68. export default {
  69. name: 'role',
  70. directives: {
  71. waves
  72. },
  73. data() {
  74. return {
  75. list: null,
  76. total: null,
  77. listLoading: true,
  78. dialogMain: false,
  79. router: asyncRouterMap,
  80. listQuery: {
  81. page: 1,
  82. limit: 20,
  83. roleName: ''
  84. },
  85. dataForm: {
  86. roleName: undefined
  87. },
  88. dialogFormVisible: false,
  89. dialogStatus: '',
  90. textMap: {
  91. update: '编辑角色',
  92. create: '添加角色'
  93. },
  94. rules: {
  95. roleName: [
  96. { required: true, message: '角色名称不能为空', trigger: 'blur' }
  97. ]
  98. },
  99. downloadLoading: false,
  100. data2: [
  101. {
  102. id: 1,
  103. label: '一级 1',
  104. children: [
  105. {
  106. id: 4,
  107. label: '二级 1-1',
  108. children: [
  109. {
  110. id: 9,
  111. label: '三级 1-1-1'
  112. },
  113. {
  114. id: 10,
  115. label: '三级 1-1-2'
  116. }
  117. ]
  118. }
  119. ]
  120. }
  121. ],
  122. defaultProps: {
  123. children: 'children',
  124. label: 'label'
  125. }
  126. }
  127. },
  128. created() {
  129. this.getList()
  130. this.router = this.initRouter(this.router)
  131. },
  132. methods: {
  133. getList() {
  134. this.listLoading = true
  135. roleList(this.listQuery)
  136. .then(response => {
  137. this.list = response.data.data.items
  138. this.total = response.data.data.total
  139. this.listLoading = false
  140. })
  141. .catch(() => {
  142. this.list = []
  143. this.total = 0
  144. this.listLoading = false
  145. })
  146. },
  147. handleQuery() {
  148. this.listQuery.page = 1
  149. this.getList()
  150. },
  151. /** 重置按钮操作 */
  152. resetQuery() {
  153. this.listQuery = {
  154. roleName: ''
  155. };
  156. this.handleQuery();
  157. },
  158. handleSizeChange(val) {
  159. this.listQuery.limit = val
  160. this.getList()
  161. },
  162. handleCurrentChange(val) {
  163. this.listQuery.page = val
  164. this.getList()
  165. },
  166. resetForm() {
  167. this.dataForm = {
  168. id: undefined,
  169. role: undefined
  170. }
  171. if (this.$refs.tree) {
  172. this.$refs.tree.setCheckedKeys([])
  173. }
  174. },
  175. handleCreate() {
  176. this.resetForm()
  177. this.dialogStatus = 'create'
  178. this.dialogMain = false
  179. this.dialogFormVisible = true
  180. this.$nextTick(() => {
  181. this.$refs['dataForm'].clearValidate()
  182. })
  183. },
  184. createData() {
  185. this.$refs['dataForm'].validate(valid => {
  186. if (valid) {
  187. addRole(this.dataForm).then(response => {
  188. this.getList()
  189. this.dialogFormVisible = false
  190. this.$notify({
  191. title: '成功',
  192. message: '创建成功',
  193. type: 'success',
  194. duration: 2000
  195. })
  196. })
  197. }
  198. })
  199. },
  200. handleUpdate(row, type) {
  201. this.dialogMain = type
  202. this.dataForm = Object.assign({}, { roleName: row.roleName, id: row.id })
  203. this.dialogStatus = 'update'
  204. this.dialogFormVisible = true
  205. if (type) {
  206. this.getRoleList()
  207. }
  208. this.$nextTick(() => {
  209. this.$refs['dataForm'].clearValidate()
  210. })
  211. },
  212. updateRoleNames() {
  213. this.$refs['dataForm'].validate(valid => {
  214. if (valid) {
  215. updateRoleName(this.dataForm).then(() => {
  216. this.dialogFormVisible = false
  217. this.getList()
  218. this.$notify({
  219. title: '成功',
  220. message: '更新成功',
  221. type: 'success',
  222. duration: 2000
  223. })
  224. })
  225. }
  226. })
  227. },
  228. updateRoleList() {
  229. var roleList = this.$refs.tree.getCheckedKeys().join(',')
  230. updateRoleMenu({
  231. roleId: this.dataForm.id,
  232. menuList: roleList
  233. }).then(() => {
  234. this.dialogFormVisible = false
  235. this.$notify({
  236. title: '成功',
  237. message: '更新成功',
  238. type: 'success',
  239. duration: 2000
  240. })
  241. })
  242. },
  243. initRouter(list) {
  244. var router = []
  245. list.map((obj) => {
  246. if (obj.meta) {
  247. obj.label = obj.meta.title
  248. if (obj.children) this.initRouter(obj.children)
  249. router.push(obj)
  250. }
  251. })
  252. return router
  253. },
  254. getRoleList() {
  255. getRoleMenu({ roleId: this.dataForm.id }).then(response => {
  256. this.$refs.tree.setCheckedKeys(response.data.data)
  257. })
  258. }
  259. }
  260. }
  261. </script>