index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 rowClick = (record, index) => {
  189. return {
  190. onClick: () => {
  191. handleEdit(record)
  192. }
  193. }
  194. }
  195. const [registerTable, { reload }] = useTable({
  196. title: '',
  197. api: roleList, //数据
  198. // dataSource: [],
  199. columns: columns, //表头配置
  200. bordered: false,
  201. striped: false,
  202. useSearchForm: false, //开启搜索区域
  203. pagination: {
  204. hideOnSinglePage: false
  205. },
  206. customRow: rowClick,
  207. canResize: true,
  208. showTableSetting: false, // 显示表格设置
  209. showIndexColumn: true,
  210. actionColumn: {
  211. width: 120,
  212. title: '操作',
  213. dataIndex: 'action',
  214. slots: { customRender: 'action' },
  215. },
  216. });
  217. const rules = {
  218. groupName: [{
  219. required: true,
  220. message: '请输入角色名称',
  221. trigger: 'blur'
  222. }]
  223. };
  224. const menuTreeData = ref([])
  225. onMounted(() => {
  226. //此处请求菜单树
  227. getMenuList().then(res => {
  228. menuTreeData.value = res
  229. console.log("此处请求菜单树:", menuTreeData.value);
  230. })
  231. })
  232. const onSubmit = (e) => {
  233. ifShowModal.value = false
  234. e && reload()
  235. }
  236. function customTitle({ node }) {
  237. if (node.status === 1) {
  238. return null;
  239. }
  240. return node.name;
  241. }
  242. return {
  243. rowClick,
  244. customTitle,
  245. ifShowModal,
  246. formRef,
  247. searchValue,
  248. filteredInfo,
  249. editForm,
  250. rules,
  251. menuTreeData,
  252. searchTable,
  253. handleAdd,
  254. handleEdit,
  255. handleSave,
  256. handleDelete,
  257. registerTable,
  258. onSubmit
  259. };
  260. },
  261. });
  262. </script>
  263. <style lang="less" scoped>
  264. .p-4 {
  265. height: 100%;
  266. .role-header {
  267. padding: 10px;
  268. width: 100%;
  269. height: 70px;
  270. background-color: #fff;
  271. display: flex;
  272. justify-content: space-between;
  273. align-items: center;
  274. border-radius: 6px;
  275. .handle-btns {
  276. margin-right: 10px;
  277. display: flex;
  278. align-items: center;
  279. &:first-child {
  280. margin-left: 10px;
  281. margin-right: 0;
  282. }
  283. .select-item {
  284. width: 200px;
  285. margin-right: 20px;
  286. }
  287. .btn {
  288. margin-right: 16px;
  289. &:last-child {
  290. margin-right: 0;
  291. }
  292. }
  293. }
  294. }
  295. .role-body {
  296. margin-top: 10px;
  297. height: calc(100% - 80px);
  298. display: flex;
  299. justify-content: space-between;
  300. .left-table {
  301. padding: 20px 20px 0;
  302. height: 100%;
  303. width: 860px;
  304. margin-right: 10px;
  305. background-color: #fff;
  306. border-radius: 6px;
  307. .basic-table {
  308. height: calc(100% - 20px);
  309. ::v-deep .ant-table-title {
  310. padding: 0 !important;
  311. display: none;
  312. }
  313. }
  314. }
  315. .right-opt {
  316. padding: 20px 20px 0;
  317. width: calc(100% - 870px);
  318. height: 100%;
  319. background-color: #fff;
  320. border-radius: 6px;
  321. .form-box {
  322. // max-height: 855px;
  323. // overflow: auto;
  324. ::v-deep .tree-box {
  325. width: 100%;
  326. height: 680px;
  327. overflow: auto;
  328. border-radius: 2px;
  329. box-sizing: border-box;
  330. border: 1px solid #D9D9D9;
  331. &::-webkit-scrollbar {
  332. width: 3px;
  333. }
  334. &::-webkit-scrollbar-thumb {
  335. border-radius: 2px;
  336. background: #ccd5df;
  337. }
  338. &::-webkit-scrollbar-track {
  339. display: none;
  340. }
  341. }
  342. }
  343. }
  344. }
  345. }
  346. </style>