quanx.vue 19 KB

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