role.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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-item label="上传文件" prop="fileUrl">
  54. <fileUpload :fileList="fileList" />
  55. </el-form-item>
  56. </el-form>
  57. <div slot="footer" class="dialog-footer">
  58. <el-button @click="cancelAddForm">取 消</el-button>
  59. <el-button type="primary" @click="submitAddForm">确 定</el-button>
  60. </div>
  61. </el-dialog>
  62. <!-- 设置权限 -->
  63. <el-dialog title="设置权限" :visible.sync="roleFormVisible" :show-close="false" width="40%" :close-on-click-modal="false" custom-class="tree-dialog" top="50px">
  64. <el-tree
  65. :data="menuRoleList"
  66. show-checkbox
  67. :check-strictly="true"
  68. :default-expand-all="true"
  69. node-key="moduleId"
  70. ref="tree"
  71. highlight-current
  72. :props="defaultProps">
  73. </el-tree>
  74. <div slot="footer" class="dialog-footer">
  75. <el-button @click="roleFormVisible = false">{{editId != 1 ? '取 消':'关 闭'}}</el-button>
  76. <el-button type="primary" @click="submitRoleForm" v-if="editId != 1">确 定</el-button>
  77. </div>
  78. </el-dialog>
  79. </div>
  80. </template>
  81. <script>
  82. import { mapGetters } from 'vuex'
  83. import { getRoleList, addRole, editRole, getRoleDetail, deleteRole, getMenuList, getMenuRoleIds, setMenuRole } from '@/api/setting'
  84. import fileUpload from '@/components/Common/file-upload.vue'
  85. export default {
  86. components: {
  87. fileUpload
  88. },
  89. data() {
  90. return {
  91. imageURL: this.$imageUrl,
  92. dataList: null, // 列表数据
  93. listLoading: true, // 列表加载loading
  94. currentPage: 1, // 当前页码
  95. pageSize: 10, // 每页数量
  96. listTotal: 0, // 列表总数
  97. editId: null,
  98. addFormType: 'add',
  99. addFormVisible: false,
  100. addForm: {
  101. name: '', // 角色名
  102. },
  103. addFormRules: {
  104. name: [
  105. { required: true, message: '请输入角色名', trigger: 'blur' }
  106. ],
  107. },
  108. roleFormVisible: false,
  109. menuRoleList: [],
  110. defaultProps: {
  111. children: 'children',
  112. label: 'moduleName'
  113. },
  114. fileList: [],
  115. }
  116. },
  117. computed: {
  118. ...mapGetters([
  119. 'userid',
  120. 'name'
  121. ])
  122. },
  123. created() {
  124. this.getList();
  125. },
  126. methods: {
  127. // 查询按钮权限
  128. checkBtnRole(value) {
  129. // let btnRole = this.$route.meta.roles;
  130. // if(!btnRole) {return true}
  131. // let index = btnRole.indexOf(value);
  132. // return index >= 0 ? true : false;
  133. return true;
  134. },
  135. getList() {
  136. this.listLoading = true;
  137. let params = {
  138. pageNum: this.currentPage,
  139. pageSize: this.pageSize
  140. };
  141. getRoleList(params).then(res => {
  142. this.dataList = res.data.records;
  143. this.listTotal = res.data.total;
  144. this.listLoading = false;
  145. })
  146. },
  147. // 更改每页数量
  148. handleSizeChange(val) {
  149. this.pageSize = val;
  150. this.currentPage = 1;
  151. this.getList();
  152. },
  153. // 更改当前页
  154. handleCurrentChange(val) {
  155. this.currentPage = val;
  156. this.getList();
  157. },
  158. // 操作 - 删除
  159. handleDelete(id) {
  160. deleteRole({adminRoleId: id}).then(res => {
  161. this.getList();
  162. this.$successMsg();
  163. })
  164. },
  165. // 新增编辑
  166. addOrEdit(type, id) {
  167. this.addFormType = type;
  168. this.addFormVisible = true;
  169. if(type == 'edit') {
  170. this.editId = id;
  171. getRoleDetail({adminRoleId: id}).then(res => {
  172. this.addForm = {
  173. name: res.data.name
  174. }
  175. })
  176. }
  177. },
  178. // 取消 新增编辑
  179. cancelAddForm(){
  180. this.addFormVisible = false;
  181. this.$refs.addForm.resetFields();
  182. },
  183. // 提交 新增编辑
  184. submitAddForm() {
  185. this.$refs.addForm.validate((valid) => {
  186. if (valid) {
  187. let params = {
  188. name: this.addForm.name
  189. }
  190. if(this.addFormType == 'edit') {
  191. params.adminRoleId = this.editId;
  192. editRole(params).then(res => {
  193. this.cancelAddForm();
  194. this.getList();
  195. this.$successMsg('编辑成功');
  196. })
  197. } else {
  198. addRole(params).then(res => {
  199. this.cancelAddForm();
  200. this.getList();
  201. this.$successMsg('新增成功');
  202. })
  203. }
  204. }
  205. })
  206. },
  207. // 设置权限 - 获取列表
  208. setMenuRole(id) {
  209. this.roleFormVisible = true;
  210. this.editId = id;
  211. getMenuList({adminUserId: this.userid}).then(res => {
  212. this.menuRoleList = res.data;
  213. })
  214. getMenuRoleIds({adminRoleId: id}).then(res => {
  215. this.$refs.tree.setCheckedKeys(res.data);
  216. })
  217. },
  218. // 设置权限 - 提交数据
  219. submitRoleForm() {
  220. console.log(this.$refs.tree.getCheckedKeys());
  221. let params = {
  222. adminModuleIds: this.$refs.tree.getCheckedKeys().join(','),
  223. adminRoleId: this.editId,
  224. }
  225. setMenuRole(params).then(res => {
  226. this.roleFormVisible = false;
  227. this.getList();
  228. this.$successMsg();
  229. })
  230. },
  231. }
  232. }
  233. </script>
  234. <style lang="scss">
  235. .tree-dialog {
  236. .el-dialog__body {
  237. padding: 20px;
  238. .el-tree {
  239. max-height: calc(100vh - 140px - 54px - 70px);
  240. overflow-y: scroll;
  241. padding: 0 30px;
  242. >.el-tree-node {
  243. padding: 15px 0;
  244. border: 1px dashed #ddd;
  245. margin-bottom: 15px;
  246. border-radius: 10px;
  247. }
  248. }
  249. }
  250. }
  251. </style>