quanx.vue 19 KB

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