role.vue 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  1. <template>
  2. <div class="app-container">
  3. <div class="setting_title">权限管理</div>
  4. <el-divider></el-divider>
  5. <div class="mymain-container">
  6. <div class="btn-group clearfix">
  7. <div class="fl">
  8. <el-button size="small" type="primary" icon="el-icon-plus" @click="addOrEdit('add')" v-if="checkBtnRole('add')">新增角色</el-button>
  9. </div>
  10. <div class="fr">
  11. <ImportButton :imUrl="'stock/importToll'" @importSuccess="getList" />
  12. </div>
  13. </div>
  14. <div class="table">
  15. <el-table v-loading="listLoading" :data="dataList" element-loading-text="Loading" border fit highlight-current-row stripe>
  16. <el-table-column align="center" label="序号" type="index" width="50"></el-table-column>
  17. <el-table-column align="center" label="角色" prop="name"></el-table-column>
  18. <el-table-column align="center" label="操作" width="180">
  19. <template slot-scope="scope">
  20. <el-button type="text" @click="setMenuRole(scope.row.adminRoleId)" v-if="checkBtnRole('detail')">设置权限</el-button>
  21. <template v-if="checkBtnRole('detail')">
  22. <el-button type="text" @click="addOrEdit('edit', scope.row.adminRoleId)" v-if="scope.row.adminRoleId != 1">编辑</el-button>
  23. </template>
  24. <template v-if="checkBtnRole('del')">
  25. <el-popconfirm style="margin-left: 10px;" title="确定删除吗?" @onConfirm="handleDelete(scope.row.adminRoleId)" v-if="scope.row.adminRoleId != 1">
  26. <el-button slot="reference" type="text">删除</el-button>
  27. </el-popconfirm>
  28. </template>
  29. </template>
  30. </el-table-column>
  31. </el-table>
  32. </div>
  33. <div class="pagination clearfix">
  34. <div class="fr">
  35. <el-pagination
  36. @size-change="handleSizeChange"
  37. @current-change="handleCurrentChange"
  38. :current-page="currentPage"
  39. :page-sizes="[10, 20, 30, 50]"
  40. :page-size="10"
  41. layout="total, sizes, prev, pager, next, jumper"
  42. :total="listTotal">
  43. </el-pagination>
  44. </div>
  45. </div>
  46. </div>
  47. <!-- 新增编辑 -->
  48. <el-dialog :title="addFormType == 'add' ? '新增角色':'编辑角色'" :visible.sync="addFormVisible" :show-close="false" width="40%" :close-on-click-modal="false">
  49. <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="left" label-width="100px">
  50. <el-form-item label="角色名" prop="name">
  51. <el-input v-model="addForm.name" autocomplete="off" placeholder="请输入角色名"></el-input>
  52. </el-form-item>
  53. </el-form>
  54. <div slot="footer" class="dialog-footer">
  55. <el-button @click="cancelAddForm">取 消</el-button>
  56. <el-button type="primary" @click="submitAddForm">确 定</el-button>
  57. </div>
  58. </el-dialog>
  59. <!-- 设置权限 -->
  60. <el-dialog title="设置权限" :visible.sync="roleFormVisible" :show-close="false" width="40%" :close-on-click-modal="false" custom-class="tree-dialog" top="50px">
  61. <el-tree
  62. :data="menuRoleList"
  63. show-checkbox
  64. :check-strictly="false"
  65. :default-expand-all="true"
  66. node-key="moduleId"
  67. ref="tree"
  68. highlight-current
  69. :props="defaultProps">
  70. </el-tree>
  71. <div slot="footer" class="dialog-footer">
  72. <el-button @click="roleFormVisible = false">{{editId != 1 ? '取 消':'关 闭'}}</el-button>
  73. <el-button type="primary" @click="submitRoleForm" v-if="editId != 1">确 定</el-button>
  74. </div>
  75. </el-dialog>
  76. </div>
  77. </template>
  78. <script>
  79. import { mapGetters } from 'vuex'
  80. import { getRoleList, addRole, editRole, getRoleDetail, deleteRole, getMenuList, getMenuRoleIds, setMenuRole } from '@/api/setting'
  81. export default {
  82. data() {
  83. return {
  84. imageURL: this.$imageUrl,
  85. dataList: null, // 列表数据
  86. listLoading: true, // 列表加载loading
  87. currentPage: 1, // 当前页码
  88. pageSize: 10, // 每页数量
  89. listTotal: 0, // 列表总数
  90. editId: null,
  91. addFormType: 'add',
  92. addFormVisible: false,
  93. addForm: {
  94. name: '', // 角色名
  95. },
  96. addFormRules: {
  97. name: [
  98. { required: true, message: '请输入角色名', trigger: 'blur' }
  99. ],
  100. },
  101. roleFormVisible: false,
  102. menuRoleList: [],
  103. defaultProps: {
  104. children: 'children',
  105. label: 'moduleName'
  106. },
  107. }
  108. },
  109. computed: {
  110. ...mapGetters([
  111. 'userid',
  112. 'name'
  113. ])
  114. },
  115. created() {
  116. this.getList();
  117. },
  118. methods: {
  119. // 查询按钮权限
  120. checkBtnRole(value) {
  121. // let btnRole = this.$route.meta.roles;
  122. // if(!btnRole) {return true}
  123. // let index = btnRole.indexOf(value);
  124. // return index >= 0 ? true : false;
  125. return true;
  126. },
  127. getList() {
  128. this.listLoading = true;
  129. let params = {
  130. pageNum: this.currentPage,
  131. pageSize: this.pageSize
  132. };
  133. getRoleList(params).then(res => {
  134. this.dataList = res.data.records;
  135. this.listTotal = res.data.total;
  136. this.listLoading = false;
  137. })
  138. },
  139. // 更改每页数量
  140. handleSizeChange(val) {
  141. this.pageSize = val;
  142. this.currentPage = 1;
  143. this.getList();
  144. },
  145. // 更改当前页
  146. handleCurrentChange(val) {
  147. this.currentPage = val;
  148. this.getList();
  149. },
  150. // 操作 - 删除
  151. handleDelete(id) {
  152. deleteRole({adminRoleId: id}).then(res => {
  153. this.getList();
  154. this.$successMsg();
  155. })
  156. },
  157. // 新增编辑
  158. addOrEdit(type, id) {
  159. this.addFormType = type;
  160. this.addFormVisible = true;
  161. if(type == 'edit') {
  162. this.editId = id;
  163. getRoleDetail({adminRoleId: id}).then(res => {
  164. this.addForm = {
  165. name: res.data.name
  166. }
  167. })
  168. }
  169. },
  170. // 取消 新增编辑
  171. cancelAddForm(){
  172. this.addFormVisible = false;
  173. this.$refs.addForm.resetFields();
  174. },
  175. // 提交 新增编辑
  176. submitAddForm() {
  177. this.$refs.addForm.validate((valid) => {
  178. if (valid) {
  179. let params = {
  180. name: this.addForm.name
  181. }
  182. if(this.addFormType == 'edit') {
  183. params.adminRoleId = this.editId;
  184. editRole(params).then(res => {
  185. this.cancelAddForm();
  186. this.getList();
  187. this.$successMsg('编辑成功');
  188. })
  189. } else {
  190. addRole(params).then(res => {
  191. this.cancelAddForm();
  192. this.getList();
  193. this.$successMsg('新增成功');
  194. })
  195. }
  196. }
  197. })
  198. },
  199. // 设置权限 - 获取列表
  200. setMenuRole(id) {
  201. this.roleFormVisible = true;
  202. this.editId = id;
  203. getMenuList({adminUserId: this.userid}).then(res => {
  204. this.menuRoleList = res.data;
  205. })
  206. getMenuRoleIds({adminRoleId: id}).then(res => {
  207. this.$refs.tree.setCheckedKeys(res.data);
  208. })
  209. },
  210. // 设置权限 - 提交数据
  211. submitRoleForm() {
  212. // console.log('选中的:' + this.$refs.tree.getCheckedKeys());
  213. // console.log('半选的:' + this.$refs.tree.getHalfCheckedKeys());
  214. // return false
  215. let params = {
  216. adminModuleIds: this.$refs.tree.getCheckedKeys().join(','),
  217. adminRoleId: this.editId,
  218. }
  219. setMenuRole(params).then(res => {
  220. this.roleFormVisible = false;
  221. this.getList();
  222. this.$successMsg();
  223. })
  224. },
  225. }
  226. }
  227. </script>
  228. <style lang="scss">
  229. .tree-dialog {
  230. .el-dialog__body {
  231. padding: 20px;
  232. .el-tree {
  233. max-height: calc(100vh - 140px - 54px - 70px);
  234. overflow-y: scroll;
  235. padding: 0 30px;
  236. >.el-tree-node {
  237. padding: 15px 0;
  238. border: 1px dashed #ddd;
  239. margin-bottom: 15px;
  240. border-radius: 10px;
  241. }
  242. }
  243. }
  244. }
  245. </style>