quanx.vue 17 KB

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