quanx.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513
  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-link type="primary" :underline="false" @click="exportAuthority()" style="margin-left:20px">导出权限</el-link>
  19. </el-form-item>
  20. </el-form>
  21. </el-col>
  22. <!-- 表格 -->
  23. <el-table :data="tableData" style="width: 100%" height="615">
  24. <el-table-column prop="rolename" label="角色" align="center">
  25. <template slot-scope="scope">
  26. {{scope.row.rolename}} <span v-if="scope.row.isDefault == 1" style="color: #909399;"> - (默认角色)</span>
  27. </template>
  28. </el-table-column>
  29. <el-table-column prop="roleDescribe" label="描述"></el-table-column>
  30. <el-table-column prop="date" label="操作">
  31. <template slot-scope="scope">
  32. <div>
  33. <el-button size="small" @click="handleAdd('编辑角色', '立即修改', scope.row)" :style="scope.row.rolename != '超级管理员' ? '' : 'visibility:hidden;'">编辑角色</el-button>
  34. <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>
  35. <el-button type="danger" size="small" @click="deteHand(scope.row)" v-if="scope.row.rolename != '超级管理员'">删除</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. cgRoleParamer: null,
  155. roleName: '',
  156. rules: {
  157. rolename: [{
  158. required: true, message: '请输入角色名称', trigger: 'blur'
  159. }]
  160. }
  161. };
  162. },
  163. computed: {},
  164. watch: {
  165. filterText(val) {
  166. this.$refs.tree.filter(val);
  167. },
  168. keyword(val) {
  169. if(val == '') {
  170. this.mohu()
  171. }
  172. }
  173. },
  174. created() {},
  175. mounted() {
  176. this.getRole()
  177. // this.shisha()
  178. },
  179. methods: {
  180. // 默认角色点击确定
  181. determine() {
  182. this.http.post('/permission/setDefaultRole', {
  183. companyId: this.user.companyId,
  184. id: this.roleId
  185. },
  186. res => {
  187. if (res.code == "ok") {
  188. this.$message({
  189. message: '操作成功',
  190. type: "success"
  191. });
  192. this.defaultRole = false
  193. this.getRole()
  194. } else {
  195. this.$message({
  196. message: res.msg,
  197. type: "error"
  198. });
  199. }
  200. },
  201. error => {
  202. this.$message({
  203. message: error,
  204. type: "error"
  205. });
  206. });
  207. },
  208. // 默认角色
  209. modify() {
  210. for(var i in this.auseList) {
  211. if(this.auseList[i].isDefault == '1') {
  212. this.roleId = this.auseList[i].id
  213. }
  214. }
  215. this.defaultRole = true
  216. },
  217. // 模糊匹配
  218. mohu() {
  219. console.log(this.keyword)
  220. if(this.keyword == '') {
  221. this.tableData = this.auseList
  222. return
  223. }
  224. var len = this.auseList.length;
  225. var arr = []
  226. for(var i = 0; i < len; i++){
  227. if(this.auseList[i].rolename.indexOf(this.keyword)>=0){
  228. arr.push(this.auseList[i]);
  229. }
  230. }
  231. this.tableData = arr
  232. },
  233. // 还不知道是啥
  234. shisha(id) {
  235. this.roles = id
  236. this.http.post('/permission/getAuthority', {
  237. companyId: this.user.companyId,
  238. role: id
  239. },
  240. res => {
  241. if (res.code == "ok") {
  242. console.log(res.data, '数据来源(990)')
  243. this.moduleList = res.data
  244. this.dialogVisible = true
  245. } else {
  246. this.$message({
  247. message: res.msg,
  248. type: "error"
  249. });
  250. }
  251. },
  252. error => {
  253. this.$message({
  254. message: error,
  255. type: "error"
  256. });
  257. });
  258. },
  259. // 获取权限列表
  260. getRole() {
  261. this.http.post('/permission/getRoleList', {
  262. companyId: this.user.companyId
  263. },
  264. res => {
  265. if (res.code == "ok") {
  266. console.log(res.data, '数据来源')
  267. this.tableData = res.data
  268. this.auseList = res.data
  269. let item = this.tableData.filter(item => item.rolename == '超级管理员')
  270. this.cgRoleParamer = item[0].id
  271. } else {
  272. this.$message({
  273. message: res.msg,
  274. type: "error"
  275. });
  276. }
  277. },
  278. error => {
  279. this.$message({
  280. message: error,
  281. type: "error"
  282. });
  283. });
  284. },
  285. handleSizeChange() {},
  286. handleCurrentChange() {},
  287. filterNode(value, data) {
  288. if (!value) return true;
  289. return data.label.indexOf(value) !== -1;
  290. },
  291. handleAdd(tex, tel, id) {
  292. if(id && id.rolename == '超级管理员'){
  293. this.$message('超级管理员为固定的默认角色无法编辑');
  294. return
  295. }
  296. this.titles = tex
  297. this.addTil = tel
  298. this.addDialogVisible = true
  299. // var ss = {rolename: '', roleDescribe: '',id: ''}
  300. var ss = {}
  301. id ? ss.id = id.id : ss.id = ''
  302. id ? ss.rolename = id.rolename : ss.rolename = ''
  303. id ? ss.roleDescribe = id.roleDescribe : ss.roleDescribe = ''
  304. this.form = ss
  305. },
  306. // 添加角色
  307. onSubmit(formName) {
  308. // var ss = this.form
  309. this.$refs[formName].validate((valid) => {
  310. if (valid) {
  311. this.http.post('/permission/editRole', {
  312. id: this.form.id,
  313. name: this.form.rolename,
  314. description: this.form.roleDescribe,
  315. companyId: this.user.companyId
  316. },
  317. res => {
  318. if (res.code == "ok") {
  319. console.log(res.data, '数据来源(123)')
  320. this.addDialogVisible = false
  321. this.getRole()
  322. } else {
  323. this.$message({
  324. message: res.msg,
  325. type: "error"
  326. });
  327. }
  328. },
  329. error => {
  330. this.$message({
  331. message: error,
  332. type: "error"
  333. });
  334. });
  335. } else {
  336. console.log('error submit!!');
  337. return false;
  338. }
  339. });
  340. },
  341. deteHand(item) {
  342. this.$confirm('此操作将删除('+item.rolename+')角色, 是否继续?', '提示', {
  343. confirmButtonText: '确定',
  344. cancelButtonText: '取消',
  345. type: 'warning'
  346. }).then(() => {
  347. this.http.post('/permission/deleteRole', {
  348. id: item.id,
  349. },
  350. res => {
  351. if (res.code == "ok") {
  352. this.$message({
  353. message: '删除成功',
  354. type: "success"
  355. });
  356. this.getRole()
  357. } else {
  358. this.$message({
  359. message: res.msg,
  360. type: "error"
  361. });
  362. }
  363. },
  364. error => {
  365. this.$message({
  366. message: error,
  367. type: "error"
  368. });
  369. });
  370. }).catch(() => {});
  371. },
  372. mainChange(mainMenu) {
  373. //一级选中,全部选中,一级取消全部取消
  374. console.log(mainMenu);
  375. mainMenu.children.forEach(c=>{
  376. c.checked = mainMenu.checked;
  377. c.functionList.forEach(f=>{
  378. f.checked = mainMenu.checked;
  379. });
  380. });
  381. if(mainMenu.functionList) {
  382. mainMenu.functionList.forEach(c=>{
  383. c.checked = mainMenu.checked;
  384. })
  385. }
  386. },
  387. subChange(subMenu) {
  388. //二级选中,自动勾选对应的一级
  389. console.log(subMenu);
  390. let mainMenuList = this.moduleList;
  391. mainMenuList.forEach(m=>{
  392. //检测子菜单
  393. if (m.id == subMenu.parentId) {
  394. //找到父菜单了
  395. var hasChecked = false;
  396. m.children.forEach(c=>{
  397. if (c.checked) {
  398. hasChecked = true;
  399. }
  400. })
  401. //当前点击的子菜单所有同级菜单是否有点中的,如果没有需要把父级取消勾选
  402. m.checked = hasChecked;
  403. }
  404. });
  405. },
  406. subChanges(subMenu, id) {
  407. let mainMenuList = this.moduleList
  408. mainMenuList.forEach(m=>{
  409. if(m.id == id) {
  410. //找到父菜单了
  411. var hasChecked = false;
  412. m.functionList.forEach(c=>{
  413. if (c.checked) {
  414. hasChecked = true;
  415. }
  416. })
  417. console.log(hasChecked, '事件')
  418. // m.checked = hasChecked;
  419. if(hasChecked) {
  420. console.log('执行')
  421. m.checked = hasChecked;
  422. }
  423. }
  424. });
  425. },
  426. dianji(item) {
  427. console.log(item)
  428. let mainMenuList = this.moduleList
  429. mainMenuList.forEach(m=>{
  430. if(m.id == item.id) {
  431. m.useState = !m.useState
  432. }
  433. });
  434. },
  435. saveList() {
  436. this.dialogVisible = false
  437. console.log(this.moduleList)
  438. this.http.post('/permission/savePermission', {
  439. role: this.roles,
  440. moduleList: JSON.stringify(this.moduleList),
  441. },
  442. res => {
  443. if (res.code == "ok") {
  444. this.$message({
  445. message: '操作成功',
  446. type: "success"
  447. });
  448. this.getRole()
  449. } else {
  450. this.$message({
  451. message: res.msg,
  452. type: "error"
  453. });
  454. }
  455. },
  456. error => {
  457. this.$message({
  458. message: error,
  459. type: "error"
  460. });
  461. });
  462. },
  463. exportAuthority(){ // 导出权限
  464. this.http.post('/permission/exportData',{
  465. role: this.cgRoleParamer,
  466. companyId: this.user.companyId
  467. },res => {
  468. if(res.code == 'ok'){
  469. var a = document.createElement('a');
  470. a.download = "权限数据表.xls";
  471. a.href = res.data;
  472. a.click();
  473. }else{
  474. this.$message({
  475. message: res.msg,
  476. type: 'error'
  477. })
  478. }
  479. },err => {
  480. this.$message({
  481. message: err,
  482. type: 'error'
  483. })
  484. })
  485. }
  486. },
  487. };
  488. </script>
  489. <style scoped>
  490. ul {
  491. padding: 0 0 0 30px;
  492. margin: 0;
  493. }
  494. li {
  495. list-style: none;
  496. }
  497. .quanxians {
  498. height: 400px;
  499. width: 100%;
  500. overflow: auto;
  501. }
  502. .gongn {
  503. display: inline-block;
  504. width: 500px;
  505. }
  506. .daoru {
  507. margin-left: 10px;
  508. }
  509. </style>