quanx.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <template>
  2. <div>
  3. <!-- 表头 -->
  4. <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
  5. <el-form :inline="true" style="width: 87%">
  6. <el-form-item label="角色权限">
  7. </el-form-item>
  8. <el-form-item >
  9. <div>
  10. <el-input style="float:left;" v-model="keyword" class="input-with-select" placeholder="请输入关键字查询" clearable="true">
  11. <el-button slot="append" @click="mohu" @clear="mohus" icon="el-icon-search"></el-button>
  12. </el-input>
  13. </div>
  14. </el-form-item>
  15. <el-form-item style="float: right">
  16. <el-link type="primary" :underline="false" @click="handleAdd('添加角色', '立即添加')">添加角色</el-link>
  17. <el-link type="primary" :underline="false" @click="modify()" style="margin-left: 20px;">修改默认角色</el-link>
  18. </el-form-item>
  19. </el-form>
  20. </el-col>
  21. <!-- 表格 -->
  22. <el-table :data="tableData" style="width: 100%" height="615">
  23. <el-table-column prop="rolename" label="角色" align="center">
  24. <template slot-scope="scope">
  25. {{scope.row.rolename}} <span v-if="scope.row.isDefault == 1" style="color: #909399;"> - (默认角色)</span>
  26. </template>
  27. </el-table-column>
  28. <el-table-column prop="roleDescribe" label="描述"></el-table-column>
  29. <el-table-column prop="date" label="操作">
  30. <template slot-scope="scope">
  31. <div>
  32. <el-button size="small" @click="handleAdd('编辑角色', '立即修改', scope.row)" :style="scope.row.rolename != '超级管理员' ? '' : 'visibility:hidden;'">编辑角色</el-button>
  33. <el-button type="primary" size="small" @click="shisha(scope.row.id), roleName = scope.row.rolename" v-if="scope.row.rolename !='超级管理员' || (scope.row.rolename =='超级管理员' && user.roleName == '超级管理员')">分配权限</el-button>
  34. <el-button type="danger" size="small" @click="deteHand(scope.row)" v-if="scope.row.rolename != '超级管理员'">删除</el-button>
  35. <el-button size="small" @click="exportAuthority(scope.row)" v-if="scope.row.rolename != '超级管理员'" style="margin-left:30px">导出权限</el-button>
  36. </div>
  37. </template>
  38. </el-table-column>
  39. </el-table>
  40. <!--工具条-->
  41. <!-- <el-col :span="24" class="toolbar">
  42. <el-pagination
  43. @size-change="handleSizeChange"
  44. @current-change="handleCurrentChange"
  45. :page-sizes="[20 , 50 , 80 , 100]"
  46. :page-size="20"
  47. layout="total, sizes, prev, pager, next"
  48. :total="total"
  49. style="float:right;"
  50. ></el-pagination>
  51. </el-col> -->
  52. <!-- 分配权限弹窗 -->
  53. <el-dialog :title="'分配权限 - '+roleName" :visible.sync="dialogVisible" width="764px" :before-close="handleClose">
  54. <div class="quanxians">
  55. <!-- <el-input placeholder="输入关键字进行过滤" v-model="filterText"></el-input> -->
  56. <!-- <el-tree :data="data" show-checkbox node-key="id" :filter-node-method="filterNode" ref="tree" :default-expanded-keys="[2, 3]" :default-checked-keys="[5]" :props="defaultProps"> </el-tree> -->
  57. <el-form ref="form" :model="form" label-width="0px">
  58. <!-- 分模块展示功能项 -->
  59. <el-form-item>
  60. <ul>
  61. <!-- <li v-for="mainMenu in moduleList" :key="mainMenu.id" style="display: flex;"> -->
  62. <li v-for="mainMenu in moduleList" :key="mainMenu.id">
  63. <!-- <div style="width: 20px; display: inline-block;height: 10px"> -->
  64. <div style="display: inline-block; float: left;width: 20px;height: 10px">
  65. <span v-if="mainMenu.children.length > 0" @click="dianji(mainMenu)" style="cursor:pointer;"><i :class="mainMenu.useState ? 'el-icon-caret-bottom' : 'el-icon-caret-right'"></i></span>
  66. </div>
  67. <span style="display: flex;">
  68. <el-checkbox :label="mainMenu.name" name="type" v-model="mainMenu.checked" style="font-weight:bold;width: 160px" @change="mainChange(mainMenu)"></el-checkbox>
  69. <span v-if="mainMenu.functionList && mainMenu.functionList.length > 0" style="margin-right:10px;margin-left:10px;">
  70. <!-- <div></div>(功能: <el-checkbox v-for="fun in mainMenu.functionList" :key="fun.id" :label="fun.name" name="type" v-model="fun.checked" @change="subChanges(fun, mainMenu.id)"></el-checkbox>) -->
  71. <span class="gongn"><el-checkbox v-for="fun in mainMenu.functionList" :key="fun.id" :label="fun.name" name="type" v-model="fun.checked" style="width: 130px" @change="subChanges(fun, mainMenu.id)"></el-checkbox></span>
  72. </span>
  73. </span>
  74. <ul v-if="mainMenu.children && mainMenu.useState">
  75. <li v-for="subMenu in mainMenu.children" :key="subMenu.id">
  76. <el-checkbox :label="subMenu.name" name="type" v-model="subMenu.checked" style="font-weight:bold;" @change="subChange(subMenu)"></el-checkbox>
  77. <span v-if="subMenu.functionList && subMenu.functionList.length > 0" style="margin-right:10px;margin-left:10px;">(功能:
  78. <el-checkbox v-for="fun in subMenu.functionList" :key="fun.id" :label="fun.name" name="type" v-model="fun.checked" @change="funChange(fun)"></el-checkbox>)</span>
  79. </li>
  80. </ul>
  81. </li>
  82. </ul>
  83. </el-form-item>
  84. </el-form>
  85. </div>
  86. <span slot="footer" class="dialog-footer">
  87. <el-button type="primary" @click="saveList()">保存</el-button>
  88. </span>
  89. </el-dialog>
  90. <!-- 添加角色弹窗 -->
  91. <el-dialog :title="titles" :visible.sync="addDialogVisible" width="600px" :before-close="handleClose">
  92. <div>
  93. <el-form ref="form" :rules="rules" :model="form" label-width="80px">
  94. <el-form-item label="角色名称" prop="rolename">
  95. <el-input v-model="form.rolename" clearable></el-input>
  96. </el-form-item>
  97. <el-form-item label="描述">
  98. <el-input type="textarea" v-model="form.roleDescribe" maxlength="25" show-word-limit clearable></el-input>
  99. </el-form-item>
  100. <el-form-item>
  101. <el-button style="float: right" type="primary" @click="onSubmit('form')">确定</el-button>
  102. </el-form-item>
  103. </el-form>
  104. </div>
  105. </el-dialog>
  106. <!-- 修改默认角色弹窗 -->
  107. <el-dialog title="默认角色" :visible.sync="defaultRole" width="400px" :before-close="handleClose">
  108. <div>
  109. <el-form label-width="80px">
  110. <el-form-item label="默认角色">
  111. <el-select v-model="roleId" placeholder="请选择默认角色">
  112. <el-option v-for="item in auseList" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
  113. </el-select>
  114. </el-form-item>
  115. <div class="daoru"><span type="info" style="color:#999">*设置导入人员时的默认角色</span></div>
  116. </el-form>
  117. </div>
  118. <span slot="footer" class="dialog-footer">
  119. <el-button type="primary" @click="determine()">确 定</el-button>
  120. </span>
  121. </el-dialog>
  122. </div>
  123. </template>
  124. <script>
  125. export default {
  126. props: {},
  127. components: {},
  128. watch: {
  129. filterText(val) {
  130. this.$refs.tree.filter(val);
  131. }
  132. },
  133. data() {
  134. return {
  135. user: JSON.parse(sessionStorage.getItem("user")),
  136. tableData: [],
  137. total: 0,
  138. dialogVisible: false,
  139. filterText: '',
  140. addDialogVisible: false,
  141. form: {
  142. rolename: '',
  143. roleDescribe: '',
  144. id: ''
  145. },
  146. moduleList: [],
  147. roles: '',
  148. titles: '',
  149. addTil: '立即添加',
  150. keyword: '',
  151. auseList: [],
  152. defaultRole: false,
  153. roleId: '',
  154. roleName: '',
  155. rules: {
  156. rolename: [{
  157. required: true, message: '请输入角色名称', trigger: 'blur'
  158. }]
  159. }
  160. };
  161. },
  162. computed: {},
  163. watch: {
  164. filterText(val) {
  165. this.$refs.tree.filter(val);
  166. },
  167. keyword(val) {
  168. if(val == '') {
  169. this.mohu()
  170. }
  171. }
  172. },
  173. created() {},
  174. mounted() {
  175. this.getRole()
  176. // this.shisha()
  177. },
  178. methods: {
  179. // 默认角色点击确定
  180. determine() {
  181. this.http.post('/permission/setDefaultRole', {
  182. companyId: this.user.companyId,
  183. id: this.roleId
  184. },
  185. res => {
  186. if (res.code == "ok") {
  187. this.$message({
  188. message: '操作成功',
  189. type: "success"
  190. });
  191. this.defaultRole = false
  192. this.getRole()
  193. } else {
  194. this.$message({
  195. message: res.msg,
  196. type: "error"
  197. });
  198. }
  199. },
  200. error => {
  201. this.$message({
  202. message: error,
  203. type: "error"
  204. });
  205. });
  206. },
  207. // 默认角色
  208. modify() {
  209. for(var i in this.auseList) {
  210. if(this.auseList[i].isDefault == '1') {
  211. this.roleId = this.auseList[i].id
  212. }
  213. }
  214. this.defaultRole = true
  215. },
  216. // 模糊匹配
  217. mohu() {
  218. console.log(this.keyword)
  219. if(this.keyword == '') {
  220. this.tableData = this.auseList
  221. return
  222. }
  223. var len = this.auseList.length;
  224. var arr = []
  225. for(var i = 0; i < len; i++){
  226. if(this.auseList[i].rolename.indexOf(this.keyword)>=0){
  227. arr.push(this.auseList[i]);
  228. }
  229. }
  230. this.tableData = arr
  231. },
  232. // 还不知道是啥
  233. shisha(id) {
  234. this.roles = id
  235. this.http.post('/permission/getAuthority', {
  236. companyId: this.user.companyId,
  237. role: id
  238. },
  239. res => {
  240. if (res.code == "ok") {
  241. console.log(res.data, '数据来源(990)')
  242. this.moduleList = res.data
  243. this.dialogVisible = true
  244. } else {
  245. this.$message({
  246. message: res.msg,
  247. type: "error"
  248. });
  249. }
  250. },
  251. error => {
  252. this.$message({
  253. message: error,
  254. type: "error"
  255. });
  256. });
  257. },
  258. // 获取权限列表
  259. getRole() {
  260. this.http.post('/permission/getRoleList', {
  261. companyId: this.user.companyId
  262. },
  263. res => {
  264. if (res.code == "ok") {
  265. console.log(res.data, '数据来源')
  266. this.tableData = res.data
  267. this.auseList = res.data
  268. } else {
  269. this.$message({
  270. message: res.msg,
  271. type: "error"
  272. });
  273. }
  274. },
  275. error => {
  276. this.$message({
  277. message: error,
  278. type: "error"
  279. });
  280. });
  281. },
  282. handleSizeChange() {},
  283. handleCurrentChange() {},
  284. filterNode(value, data) {
  285. if (!value) return true;
  286. return data.label.indexOf(value) !== -1;
  287. },
  288. handleAdd(tex, tel, id) {
  289. if(id && id.rolename == '超级管理员'){
  290. this.$message('超级管理员为固定的默认角色无法编辑');
  291. return
  292. }
  293. this.titles = tex
  294. this.addTil = tel
  295. this.addDialogVisible = true
  296. // var ss = {rolename: '', roleDescribe: '',id: ''}
  297. var ss = {}
  298. id ? ss.id = id.id : ss.id = ''
  299. id ? ss.rolename = id.rolename : ss.rolename = ''
  300. id ? ss.roleDescribe = id.roleDescribe : ss.roleDescribe = ''
  301. this.form = ss
  302. },
  303. // 添加角色
  304. onSubmit(formName) {
  305. // var ss = this.form
  306. this.$refs[formName].validate((valid) => {
  307. if (valid) {
  308. this.http.post('/permission/editRole', {
  309. id: this.form.id,
  310. name: this.form.rolename,
  311. description: this.form.roleDescribe,
  312. companyId: this.user.companyId
  313. },
  314. res => {
  315. if (res.code == "ok") {
  316. console.log(res.data, '数据来源(123)')
  317. this.addDialogVisible = false
  318. this.getRole()
  319. } else {
  320. this.$message({
  321. message: res.msg,
  322. type: "error"
  323. });
  324. }
  325. },
  326. error => {
  327. this.$message({
  328. message: error,
  329. type: "error"
  330. });
  331. });
  332. } else {
  333. console.log('error submit!!');
  334. return false;
  335. }
  336. });
  337. },
  338. deteHand(item) {
  339. this.$confirm('此操作将删除('+item.rolename+')角色, 是否继续?', '提示', {
  340. confirmButtonText: '确定',
  341. cancelButtonText: '取消',
  342. type: 'warning'
  343. }).then(() => {
  344. this.http.post('/permission/deleteRole', {
  345. id: item.id,
  346. },
  347. res => {
  348. if (res.code == "ok") {
  349. this.$message({
  350. message: '删除成功',
  351. type: "success"
  352. });
  353. this.getRole()
  354. } else {
  355. this.$message({
  356. message: res.msg,
  357. type: "error"
  358. });
  359. }
  360. },
  361. error => {
  362. this.$message({
  363. message: error,
  364. type: "error"
  365. });
  366. });
  367. }).catch(() => {});
  368. },
  369. mainChange(mainMenu) {
  370. //一级选中,全部选中,一级取消全部取消
  371. console.log(mainMenu);
  372. mainMenu.children.forEach(c=>{
  373. c.checked = mainMenu.checked;
  374. c.functionList.forEach(f=>{
  375. f.checked = mainMenu.checked;
  376. });
  377. });
  378. if(mainMenu.functionList) {
  379. mainMenu.functionList.forEach(c=>{
  380. c.checked = mainMenu.checked;
  381. })
  382. }
  383. },
  384. subChange(subMenu) {
  385. //二级选中,自动勾选对应的一级
  386. console.log(subMenu);
  387. let mainMenuList = this.moduleList;
  388. mainMenuList.forEach(m=>{
  389. //检测子菜单
  390. if (m.id == subMenu.parentId) {
  391. //找到父菜单了
  392. var hasChecked = false;
  393. m.children.forEach(c=>{
  394. if (c.checked) {
  395. hasChecked = true;
  396. }
  397. })
  398. //当前点击的子菜单所有同级菜单是否有点中的,如果没有需要把父级取消勾选
  399. m.checked = hasChecked;
  400. }
  401. });
  402. },
  403. subChanges(subMenu, id) {
  404. let mainMenuList = this.moduleList
  405. mainMenuList.forEach(m=>{
  406. if(m.id == id) {
  407. //找到父菜单了
  408. var hasChecked = false;
  409. m.functionList.forEach(c=>{
  410. if (c.checked) {
  411. hasChecked = true;
  412. }
  413. })
  414. console.log(hasChecked, '事件')
  415. // m.checked = hasChecked;
  416. if(hasChecked) {
  417. console.log('执行')
  418. m.checked = hasChecked;
  419. }
  420. }
  421. });
  422. },
  423. dianji(item) {
  424. console.log(item)
  425. let mainMenuList = this.moduleList
  426. mainMenuList.forEach(m=>{
  427. if(m.id == item.id) {
  428. m.useState = !m.useState
  429. }
  430. });
  431. },
  432. saveList() {
  433. this.dialogVisible = false
  434. console.log(this.moduleList)
  435. this.http.post('/permission/savePermission', {
  436. role: this.roles,
  437. moduleList: JSON.stringify(this.moduleList),
  438. },
  439. res => {
  440. if (res.code == "ok") {
  441. this.$message({
  442. message: '操作成功',
  443. type: "success"
  444. });
  445. this.getRole()
  446. } else {
  447. this.$message({
  448. message: res.msg,
  449. type: "error"
  450. });
  451. }
  452. },
  453. error => {
  454. this.$message({
  455. message: error,
  456. type: "error"
  457. });
  458. });
  459. },
  460. exportAuthority(row){ // 导出权限
  461. console.log(row);
  462. return
  463. this.http.post('',{
  464. },res => {
  465. if(res.code == 'ok'){
  466. var a = document.createElement('a');
  467. a.download = "角色权限.xls";
  468. a.href = res.data;
  469. a.click();
  470. }else{
  471. this.$message({
  472. message: res.msg,
  473. type: 'error'
  474. })
  475. }
  476. },err => {
  477. this.$message({
  478. message: err,
  479. type: 'error'
  480. })
  481. })
  482. }
  483. },
  484. };
  485. </script>
  486. <style scoped>
  487. ul {
  488. padding: 0 0 0 30px;
  489. margin: 0;
  490. }
  491. li {
  492. list-style: none;
  493. }
  494. .quanxians {
  495. height: 400px;
  496. width: 100%;
  497. overflow: auto;
  498. }
  499. .gongn {
  500. display: inline-block;
  501. width: 500px;
  502. }
  503. .daoru {
  504. margin-left: 10px;
  505. }
  506. </style>