role.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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="允许访问系统" prop="visitSysStatus" width="120">
  19. <template slot-scope="scope">
  20. <el-switch
  21. v-model="scope.row.visitSysStatus"
  22. :disabled="scope.row.type === 1 || scope.row.type === 2"
  23. @change="changeStatus(scope.row.adminRoleId, scope.row.visitSysStatus)">
  24. </el-switch>
  25. </template>
  26. </el-table-column>
  27. <el-table-column align="center" label="操作" width="180">
  28. <template slot-scope="scope">
  29. <el-button type="text" @click="setMenuRole(scope.row.adminRoleId)" v-if="checkBtnRole('detail')">设置权限</el-button>
  30. <template v-if="checkBtnRole('detail')">
  31. <el-button type="text" @click="addOrEdit('edit', scope.row.adminRoleId)" v-if="scope.row.adminRoleId != 1">编辑</el-button>
  32. </template>
  33. <template v-if="checkBtnRole('del')">
  34. <el-popconfirm style="margin-left: 10px;" title="确定删除吗?" @onConfirm="handleDelete(scope.row.adminRoleId)" v-if="scope.row.adminRoleId != 1">
  35. <el-button slot="reference" type="text">删除</el-button>
  36. </el-popconfirm>
  37. </template>
  38. </template>
  39. </el-table-column>
  40. </el-table>
  41. </div>
  42. <div class="pagination clearfix">
  43. <div class="fr">
  44. <el-pagination
  45. @size-change="handleSizeChange"
  46. @current-change="handleCurrentChange"
  47. :current-page="currentPage"
  48. :page-sizes="[10, 20, 30, 50]"
  49. :page-size="10"
  50. layout="total, sizes, prev, pager, next, jumper"
  51. :total="listTotal">
  52. </el-pagination>
  53. </div>
  54. </div>
  55. </div>
  56. <!-- 新增编辑 -->
  57. <el-dialog :title="addFormType == 'add' ? '新增角色':'编辑角色'" :visible.sync="addFormVisible" :show-close="false" width="40%" :close-on-click-modal="false">
  58. <el-form ref="addForm" :model="addForm" :rules="addFormRules" label-position="left" label-width="100px">
  59. <el-form-item label="角色名" prop="name">
  60. <el-input v-model="addForm.name" autocomplete="off" placeholder="请输入角色名"></el-input>
  61. </el-form-item>
  62. </el-form>
  63. <div slot="footer" class="dialog-footer">
  64. <el-button @click="cancelAddForm">取 消</el-button>
  65. <el-button type="primary" @click="submitAddForm">确 定</el-button>
  66. </div>
  67. </el-dialog>
  68. <!-- 设置权限 -->
  69. <el-dialog title="设置权限" :visible.sync="roleFormVisible" :show-close="false" width="40%" :close-on-click-modal="false" custom-class="tree-dialog" top="50px">
  70. <el-tree
  71. :data="menuRoleList"
  72. show-checkbox
  73. :check-strictly="true"
  74. :default-expand-all="true"
  75. :expand-on-click-node="false"
  76. node-key="moduleId"
  77. ref="tree"
  78. highlight-current
  79. :props="defaultProps">
  80. <span class="custom-tree-node" slot-scope="{ node, data }">
  81. <span>{{ node.label }}</span>
  82. <span v-if="data.type < 3">
  83. <el-button type="text" size="mini" @click="() => quickSelection(data)">一键全选</el-button>
  84. <el-button type="text" size="mini" @click="() => quickCancel(data)" style="color: #f56c6c">一键取消</el-button>
  85. </span>
  86. </span>
  87. </el-tree>
  88. <div slot="footer" class="dialog-footer">
  89. <el-button @click="roleFormVisible = false">{{editId != 1 ? '取 消':'关 闭'}}</el-button>
  90. <el-button type="primary" @click="submitRoleForm" v-if="editId != 1">确 定</el-button>
  91. </div>
  92. </el-dialog>
  93. </div>
  94. </template>
  95. <script>
  96. import { mapGetters } from 'vuex'
  97. import { getRoleList, addRole, editRole, getRoleDetail, deleteRole, getMenuList, getMenuRoleIds, setMenuRole } from '@/api/setting'
  98. export default {
  99. data() {
  100. return {
  101. imageURL: this.$imageUrl,
  102. dataList: null, // 列表数据
  103. listLoading: true, // 列表加载loading
  104. currentPage: 1, // 当前页码
  105. pageSize: 10, // 每页数量
  106. listTotal: 0, // 列表总数
  107. editId: null,
  108. addFormType: 'add',
  109. addFormVisible: false,
  110. addForm: {
  111. name: '', // 角色名
  112. },
  113. addFormRules: {
  114. name: [
  115. { required: true, message: '请输入角色名', trigger: 'blur' }
  116. ],
  117. },
  118. roleFormVisible: false,
  119. menuRoleList: [],
  120. defaultProps: {
  121. children: 'children',
  122. label: 'moduleName'
  123. },
  124. }
  125. },
  126. computed: {
  127. ...mapGetters([
  128. 'userid',
  129. 'name'
  130. ])
  131. },
  132. created() {
  133. this.getList();
  134. },
  135. methods: {
  136. // 查询按钮权限
  137. checkBtnRole(value) {
  138. // let btnRole = this.$route.meta.roles;
  139. // if(!btnRole) {return true}
  140. // let index = btnRole.indexOf(value);
  141. // return index >= 0 ? true : false;
  142. return true;
  143. },
  144. getList() {
  145. this.listLoading = true;
  146. let params = {
  147. pageNum: this.currentPage,
  148. pageSize: this.pageSize
  149. };
  150. getRoleList(params).then(res => {
  151. this.dataList = res.data.records;
  152. this.listTotal = res.data.total;
  153. this.listLoading = false;
  154. })
  155. },
  156. // 更改每页数量
  157. handleSizeChange(val) {
  158. this.pageSize = val;
  159. this.currentPage = 1;
  160. this.getList();
  161. },
  162. // 更改当前页
  163. handleCurrentChange(val) {
  164. this.currentPage = val;
  165. this.getList();
  166. },
  167. // 操作 - 删除
  168. handleDelete(id) {
  169. deleteRole({adminRoleId: id}).then(res => {
  170. this.getList();
  171. this.$successMsg();
  172. })
  173. },
  174. // 新增编辑
  175. addOrEdit(type, id) {
  176. this.addFormType = type;
  177. this.addFormVisible = true;
  178. if(type == 'edit') {
  179. this.editId = id;
  180. getRoleDetail({adminRoleId: id}).then(res => {
  181. this.addForm = {
  182. name: res.data.name
  183. }
  184. })
  185. }
  186. },
  187. // 取消 新增编辑
  188. cancelAddForm(){
  189. this.addFormVisible = false;
  190. this.$refs.addForm.resetFields();
  191. },
  192. // 提交 新增编辑
  193. submitAddForm() {
  194. this.$refs.addForm.validate((valid) => {
  195. if (valid) {
  196. let params = {
  197. name: this.addForm.name
  198. }
  199. if(this.addFormType == 'edit') {
  200. params.adminRoleId = this.editId;
  201. editRole(params).then(res => {
  202. this.cancelAddForm();
  203. this.getList();
  204. this.$successMsg('编辑成功');
  205. })
  206. } else {
  207. addRole(params).then(res => {
  208. this.cancelAddForm();
  209. this.getList();
  210. this.$successMsg('新增成功');
  211. })
  212. }
  213. }
  214. })
  215. },
  216. // 设置权限 - 获取列表
  217. setMenuRole(id) {
  218. this.roleFormVisible = true;
  219. this.editId = id;
  220. getMenuList({adminUserId: this.userid}).then(res => {
  221. this.menuRoleList = res.data;
  222. })
  223. getMenuRoleIds({adminRoleId: id}).then(res => {
  224. this.$refs.tree.setCheckedKeys(res.data);
  225. })
  226. },
  227. // 设置权限 - 提交数据
  228. submitRoleForm() {
  229. let params = {
  230. adminModuleIds: this.$refs.tree.getCheckedKeys().join(','),
  231. adminRoleId: this.editId,
  232. }
  233. setMenuRole(params).then(res => {
  234. this.roleFormVisible = false;
  235. this.getList();
  236. this.$successMsg();
  237. })
  238. },
  239. // 一键全选
  240. quickSelection(data) {
  241. let nowChecked = this.$refs.tree.getCheckedKeys();
  242. let thisId = data.moduleId;
  243. let childId = [];
  244. if(data.children.length) {
  245. childId = this.familyTree(data.children);
  246. }
  247. let setChecked = nowChecked.concat([thisId]).concat(childId);
  248. this.$refs.tree.setCheckedKeys(setChecked);
  249. },
  250. // 一键取消
  251. quickCancel(data) {
  252. let nowChecked = this.$refs.tree.getCheckedKeys();
  253. let thisId = data.moduleId;
  254. let childId = [];
  255. if(data.children.length) {
  256. childId = this.familyTree(data.children);
  257. }
  258. let setChecked = nowChecked;
  259. if(setChecked.indexOf(thisId) >= 0) {
  260. setChecked.splice(setChecked.indexOf(thisId), 1);
  261. }
  262. if(childId.length) {
  263. for (var i = 0; i < childId.length; i++) {
  264. if(setChecked.indexOf(childId[i]) >= 0) {
  265. setChecked.splice(setChecked.indexOf(childId[i]), 1);
  266. }
  267. }
  268. }
  269. this.$refs.tree.setCheckedKeys(setChecked);
  270. },
  271. // 递归子id
  272. familyTree(arr) {
  273. var temp = [];
  274. var forFn = function (list) {
  275. for (var i = 0; i < list.length; i++) {
  276. var item = list[i];
  277. if (item.children) {
  278. temp.push(item.moduleId);
  279. forFn(item.children);
  280. }
  281. }
  282. };
  283. forFn(arr);
  284. return temp;
  285. },
  286. changeStatus(id, status) {
  287. editRole({
  288. adminRoleId: id,
  289. visitSysStatus: status
  290. }).then(res => {
  291. this.getList();
  292. this.$successMsg('编辑成功');
  293. })
  294. }
  295. }
  296. }
  297. </script>
  298. <style lang="scss" scoped>
  299. ::v-deep .tree-dialog {
  300. .el-dialog__body {
  301. padding: 20px;
  302. .el-tree {
  303. max-height: calc(100vh - 140px - 54px - 70px);
  304. overflow-y: scroll;
  305. padding: 0 30px;
  306. >.el-tree-node {
  307. padding: 15px 0;
  308. border: 1px dashed #ddd;
  309. margin-bottom: 15px;
  310. border-radius: 10px;
  311. }
  312. }
  313. }
  314. }
  315. ::v-deep .custom-tree-node {
  316. flex: 1;
  317. display: flex;
  318. align-items: center;
  319. justify-content: space-between;
  320. font-size: 14px;
  321. padding-right: 8px;
  322. }
  323. </style>