index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. <template>
  2. <div class="p-4">
  3. <div class="role-header">
  4. <div class="handle-btns">
  5. <span class="label-item">角色名称:</span>
  6. <div class="select-item">
  7. <a-input v-model:value="searchValue" style="width: 100%;" placeholder="请输入名称"></a-input>
  8. </div>
  9. <a-button class="btn" type="primary" @click="searchTable">查询</a-button>
  10. </div>
  11. <div class="handle-btns">
  12. <a-button class="btn" type="primary" @click="handleAdd">新增</a-button>
  13. <a-button class="btn" type="primary" @click="handleSave">保存</a-button>
  14. </div>
  15. </div>
  16. <div class="role-body">
  17. <!-- 左侧表格区域 -->
  18. <div class="left-table">
  19. <BasicTable @register="registerTable" class="basic-table">
  20. <template #action="{ record }">
  21. <TableAction :actions="[
  22. {
  23. label: '修改',
  24. tooltip: '修改',
  25. onClick: handleEdit.bind(null, record),
  26. },
  27. {
  28. label: '删除',
  29. tooltip: '删除',
  30. color: 'error',
  31. onClick: handleDelete.bind(null, record),
  32. },
  33. ]" />
  34. </template>
  35. </BasicTable>
  36. </div>
  37. <!-- 右侧操作区域 -->
  38. <div class="right-opt">
  39. <div class="form-box">
  40. <a-form ref="formRef" :model="editForm" :rules="rules" :label-col="{ span: 3 }" :wrapper-col="{ span: 21 }">
  41. <a-form-item label="角色名称" name="groupName">
  42. <a-input v-model:value="editForm.groupName" style="width: 100%" placeholder="请输入名称" />
  43. </a-form-item>
  44. <a-form-item label="系统功能" name="checkedKeys">
  45. <div class="tree-box">
  46. <a-tree checkable :tree-data="menuTreeData" v-model:checkedKeys="editForm.checkedKeys" class="tree-list"
  47. :replaceFields="{ children: 'children', key: 'id', title: 'name' }"
  48. :scopedSlots="{ title: customTitle }">
  49. </a-tree>
  50. </div>
  51. </a-form-item>
  52. </a-form>
  53. </div>
  54. </div>
  55. </div>
  56. <RoleModal v-if="ifShowModal" @closeModal="ifShowModal = false" @onSubmit="onSubmit" />
  57. </div>
  58. </template>
  59. <script>
  60. import { defineComponent, nextTick, reactive, ref, toRefs, watch, onMounted, createVNode, computed } from 'vue';
  61. import { BasicTable, useTable, TableAction } from '/@/components/Table';
  62. import RoleModal from './RoleModal.vue';
  63. import { message, Modal } from 'ant-design-vue';
  64. import { ExclamationCircleOutlined } from '@ant-design/icons-vue';
  65. //角色列表api
  66. import { roleList } from '/@/api/sys/user';
  67. //菜单树api
  68. import { getMenuList } from '/@/api/sys/menu';
  69. //获取与保存角色绑定的菜单数据
  70. import { saveRoleMenu, getRoleMenus } from '/@/api/sys/user';
  71. //保存角色信息
  72. import { delRole, getRoleListByPage, setRoleStatus, saveOrUpdateRoleInfoWithMenu } from '/@/api/system/system';
  73. export default defineComponent({
  74. name: 'RoleManagement',
  75. components: { BasicTable, TableAction, ExclamationCircleOutlined, RoleModal },
  76. setup() {
  77. const formRef = ref(null)
  78. const ifShowModal = ref(false)
  79. const searchValue = ref('')
  80. const editForm = ref({
  81. groupName: '',
  82. sort: '1',
  83. groupid: '',
  84. checkedKeys: []
  85. })
  86. //表格筛选
  87. const filteredInfo = ref({
  88. groupName: null,
  89. });
  90. //按名称查询
  91. const searchTable = () => {
  92. formRef.value.resetFields();
  93. filteredInfo.value['groupName'] = [searchValue.value]
  94. }
  95. //新增
  96. const handleAdd = () => {
  97. ifShowModal.value = true
  98. }
  99. //修改
  100. const handleEdit = (record) => {
  101. editForm.value.groupName = record.groupName
  102. editForm.value.sort = record.sort
  103. editForm.value.groupid = record.groupid
  104. editForm.value.checkedKeys = []
  105. getRoleMenus({
  106. menusId: true,
  107. systemId: '1',
  108. roleIds: record.groupid || 'FB264E2A-BBEN-FB9T-9CEU-FF94AF30DB87',//values.groupid,
  109. }).then(res => {
  110. editForm.value.checkedKeys = res.map(i => i.id);
  111. })
  112. }
  113. //保存
  114. const handleSave = () => {
  115. if (!editForm.value.groupid) {
  116. message.error('没有选择角色')
  117. return;
  118. }
  119. // 首先保存用户信息,再保存菜单绑定数据
  120. formRef.value.validate().then(() => {
  121. let params = {
  122. groupName: editForm.value.groupName,
  123. sort: '1',
  124. groupid: editForm.value.groupid
  125. }
  126. saveOrUpdateRoleInfoWithMenu(params).then((res) => {
  127. if (res === 1) {
  128. let menusIds = [...new Set([].concat(editForm.value.checkedKeys))]
  129. saveRoleMenu({
  130. menusId: menusIds.join(","),
  131. systemId: "1",
  132. roleId: editForm.value.groupid
  133. }).then(r => {
  134. if (r) {
  135. message.success('修改成功')
  136. } else {
  137. message.error('权限修改失败')
  138. }
  139. reload();
  140. formRef.value.resetFields();
  141. })
  142. } else {
  143. message.error('角色修改失败')
  144. formRef.value.resetFields();
  145. }
  146. });
  147. }).catch((error) => {
  148. console.log('error', error);
  149. });
  150. }
  151. //删除
  152. const handleDelete = (record) => {
  153. Modal.confirm({
  154. title: '删除提示',
  155. icon: createVNode(ExclamationCircleOutlined),
  156. content: '确定删除该角色?',
  157. centered: true,
  158. okText: '确定',
  159. okType: 'danger',
  160. cancelText: '取消',
  161. onOk: (() => {
  162. delRole(record.groupid).then(res => {
  163. if (res.resp_code === 0) {
  164. message.success(res.resp_msg)
  165. reload();
  166. } else {
  167. message.error(res.resp_msg)
  168. }
  169. })
  170. formRef.value.resetFields();
  171. })
  172. });
  173. }
  174. //表格列
  175. const columns = computed(() => {
  176. const filtered = filteredInfo.value || {};
  177. return [
  178. {
  179. title: '角色名称',
  180. align: 'center',
  181. dataIndex: 'groupName',
  182. key: 'groupName',
  183. filteredValue: filtered.groupName || null,
  184. onFilter: (value, record) => record.groupName.includes(value)
  185. },
  186. ];
  187. });
  188. const [registerTable, { reload }] = useTable({
  189. title: '',
  190. api: roleList, //数据
  191. // dataSource: [],
  192. columns: columns, //表头配置
  193. bordered: false,
  194. striped: false,
  195. useSearchForm: false, //开启搜索区域
  196. pagination: {
  197. hideOnSinglePage: false
  198. },
  199. canResize: true,
  200. showTableSetting: false, // 显示表格设置
  201. showIndexColumn: true,
  202. actionColumn: {
  203. width: 120,
  204. title: '操作',
  205. dataIndex: 'action',
  206. slots: { customRender: 'action' },
  207. },
  208. });
  209. const rules = {
  210. groupName: [{
  211. required: true,
  212. message: '请输入角色名称',
  213. trigger: 'blur'
  214. }]
  215. };
  216. const menuTreeData = ref([])
  217. onMounted(() => {
  218. //此处请求菜单树
  219. getMenuList().then(res => {
  220. menuTreeData.value = res
  221. console.log("此处请求菜单树:", menuTreeData.value);
  222. })
  223. })
  224. const onSubmit = (e) => {
  225. ifShowModal.value = false
  226. e && reload()
  227. }
  228. function customTitle({ node }) {
  229. if (node.status === 1) {
  230. return null;
  231. }
  232. return node.name;
  233. }
  234. return {
  235. customTitle,
  236. ifShowModal,
  237. formRef,
  238. searchValue,
  239. filteredInfo,
  240. editForm,
  241. rules,
  242. menuTreeData,
  243. searchTable,
  244. handleAdd,
  245. handleEdit,
  246. handleSave,
  247. handleDelete,
  248. registerTable,
  249. onSubmit
  250. };
  251. },
  252. });
  253. </script>
  254. <style lang="less" scoped>
  255. .p-4 {
  256. height: 100%;
  257. .role-header {
  258. padding: 10px;
  259. width: 100%;
  260. height: 70px;
  261. background-color: #fff;
  262. display: flex;
  263. justify-content: space-between;
  264. align-items: center;
  265. border-radius: 6px;
  266. .handle-btns {
  267. margin-right: 10px;
  268. display: flex;
  269. align-items: center;
  270. &:first-child {
  271. margin-left: 10px;
  272. margin-right: 0;
  273. }
  274. .select-item {
  275. width: 200px;
  276. margin-right: 20px;
  277. }
  278. .btn {
  279. margin-right: 16px;
  280. &:last-child {
  281. margin-right: 0;
  282. }
  283. }
  284. }
  285. }
  286. .role-body {
  287. margin-top: 10px;
  288. height: calc(100% - 80px);
  289. display: flex;
  290. justify-content: space-between;
  291. .left-table {
  292. padding: 20px 20px 0;
  293. height: 100%;
  294. width: 860px;
  295. margin-right: 10px;
  296. background-color: #fff;
  297. border-radius: 6px;
  298. .basic-table {
  299. height: calc(100% - 20px);
  300. ::v-deep .ant-table-title {
  301. padding: 0 !important;
  302. display: none;
  303. }
  304. }
  305. }
  306. .right-opt {
  307. padding: 20px 20px 0;
  308. width: calc(100% - 870px);
  309. height: 100%;
  310. background-color: #fff;
  311. border-radius: 6px;
  312. .form-box {
  313. // max-height: 855px;
  314. // overflow: auto;
  315. ::v-deep .tree-box {
  316. width: 100%;
  317. height: 680px;
  318. overflow: auto;
  319. border-radius: 2px;
  320. box-sizing: border-box;
  321. border: 1px solid #D9D9D9;
  322. &::-webkit-scrollbar {
  323. width: 3px;
  324. }
  325. &::-webkit-scrollbar-thumb {
  326. border-radius: 2px;
  327. background: #ccd5df;
  328. }
  329. &::-webkit-scrollbar-track {
  330. display: none;
  331. }
  332. }
  333. }
  334. }
  335. }
  336. }
  337. </style>