quanx.vue 13 KB

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