Role.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467
  1. <!--
  2. * @Author: 半生瓜 1515706227@qq.com
  3. * @Date: 2024-05-06 19:31:40
  4. * @LastEditors: 半生瓜 1515706227@qq.com
  5. * @LastEditTime: 2024-05-22 14:51:25
  6. * @FilePath: \vue-element-plus-admin-mini\src\views\zy\Menu12.vue
  7. * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
  8. -->
  9. <script setup lang="tsx">
  10. import {
  11. ElInput,
  12. ElButton,
  13. ElRadio,
  14. ElRadioButton,
  15. ElTree,
  16. ElMessageBox,
  17. ElMessage,
  18. FormItemRule
  19. } from 'element-plus'
  20. import { onBeforeRouteUpdate, useRoute } from 'vue-router'
  21. import { ref, watch } from 'vue'
  22. import { ContentWrap } from '@/components/ContentWrap'
  23. import { useI18n } from '@/hooks/web/useI18n'
  24. import { Table, TableColumn, TableSlotDefault } from '@/components/Table'
  25. import { getRoleAll, getRole, getAuthTree, saveRole, deleteRole, getRoleDetail } from '@/api/role'
  26. import { reactive, unref, onMounted } from 'vue'
  27. import { useTable } from '@/hooks/web/useTable'
  28. import { Dialog } from '@/components/Dialog'
  29. import { Form, FormSchema, RadioOption } from '@/components/Form'
  30. import { useForm } from '@/hooks/web/useForm'
  31. import { useValidator } from '@/hooks/web/useValidator'
  32. import { BaseButton } from '@/components/Button'
  33. import { Search, Edit, Delete } from '@element-plus/icons-vue'
  34. import { any } from 'vue-types'
  35. import router from '@/router'
  36. const { required, onlyEnglish } = useValidator()
  37. const route = useRoute()
  38. watch(route, (newValue, oldValue) => {
  39. console.log(`Message changed from "${oldValue}" to "${newValue}"`)
  40. })
  41. defineOptions({
  42. name: 'TemplateManage'
  43. })
  44. //提交角色参数
  45. let query = {
  46. roleName: '',
  47. enName: '',
  48. authorityList: [] as any
  49. }
  50. //编辑角色参数
  51. let query2 = {
  52. roleName: '',
  53. enName: '',
  54. roleId: '',
  55. authorityList: [] as any
  56. }
  57. let morenxz = reactive([] as any)
  58. //角色列表
  59. const rolelists = reactive([] as { label: any; value: any }[])
  60. onMounted(async () => {
  61. const res = await getRoleAll()
  62. res.data.map((item) => {
  63. let a1 = {
  64. label: item.roleName,
  65. value: String(item.roleId)
  66. }
  67. rolelists.push(a1)
  68. })
  69. console.log('okokokokoko', rolelists)
  70. })
  71. const { tableRegister, tableState, tableMethods } = useTable({
  72. fetchDataApi: async () => {
  73. const { currentPage, pageSize } = tableState
  74. const res = await getRole({
  75. page: unref(currentPage),
  76. pageSize: unref(pageSize),
  77. roleName: unref(roleName)
  78. })
  79. return {
  80. list: res.data.content,
  81. total: res.data.totalElements
  82. }
  83. }
  84. })
  85. let isedit = ref('编辑')
  86. const { loading, dataList, total, currentPage, pageSize } = tableState
  87. const { refresh, getElTableExpose } = tableMethods
  88. const { t } = useI18n()
  89. const { formRegister, formMethods } = useForm()
  90. const { getElFormExpose, getFormData } = formMethods
  91. const columns = reactive<TableColumn[]>([
  92. {
  93. field: 'selection',
  94. type: 'selection'
  95. },
  96. {
  97. field: 'roleName',
  98. label: '角色名称'
  99. },
  100. {
  101. field: 'auths',
  102. label: '功能权限'
  103. },
  104. {
  105. field: 'updateTime',
  106. label: '更新时间'
  107. },
  108. {
  109. field: 'action',
  110. label: t('tableDemo.action'),
  111. slots: {
  112. default: (data) => {
  113. return (
  114. <>
  115. <ElButton type="primary" onClick={() => editRole(data)}>
  116. {t('tableDemo.edit')}
  117. </ElButton>
  118. <ElButton type="danger" onClick={() => handerdeleteRole(data)}>
  119. {t('tableDemo.delete')}
  120. </ElButton>
  121. </>
  122. )
  123. }
  124. }
  125. }
  126. ])
  127. let newroleId = ref('')
  128. //编辑
  129. const editRole = async (data) => {
  130. isedit.value = '编辑'
  131. morenxz = []
  132. console.log('sadasd11', isedit)
  133. newroleId.value = data.row.roleId
  134. const res = await getRoleDetail({ roleId: data.row.roleId })
  135. console.log('kankna===', res)
  136. query2.authorityList = res.data.authorityList
  137. res.data.authorityList.map((item) => {
  138. if (item == 1) {
  139. morenxz.push(1)
  140. }
  141. if (item == 3) {
  142. morenxz.push(2)
  143. }
  144. if (item == 5) {
  145. morenxz.push(3)
  146. }
  147. if (item == 10) {
  148. morenxz.push(7)
  149. }
  150. if (item == 10) {
  151. morenxz.push(7)
  152. }
  153. if (item == 8) {
  154. morenxz.push(5)
  155. }
  156. if (item == 9) {
  157. morenxz.push(6)
  158. }
  159. })
  160. tianjia().then(async () => {
  161. const formData = await getFormData()
  162. formData.roleName = res.data.roleName
  163. formData.enName = res.data.enName
  164. console.log('sssssa131', 22)
  165. })
  166. }
  167. //删除
  168. const handerdeleteRole = async (data) => {
  169. ElMessageBox.confirm('确定删除该数据?')
  170. .then(async () => {
  171. let arr = {
  172. roleIds: [data.row.roleId]
  173. }
  174. const res = await deleteRole(arr)
  175. if (res.code == 200) {
  176. ElMessage.success('删除成功')
  177. }
  178. refresh()
  179. })
  180. .catch(() => {
  181. // catch error
  182. })
  183. }
  184. const roleName = ref('')
  185. const isshow = ref(true)
  186. interface Tree {
  187. label: string
  188. children?: Tree[]
  189. }
  190. let data: Tree[] = []
  191. //搜索
  192. const handersearch = function () {
  193. tableState.currentPage.value = 1
  194. refresh()
  195. }
  196. const tianjiabox = () => {
  197. isedit.value = '添加'
  198. query.authorityList = []
  199. query2.authorityList = []
  200. morenxz = []
  201. tianjia()
  202. }
  203. const tianjia = async () => {
  204. let res = await getAuthTree()
  205. console.log('kanjiiiiiiii', res)
  206. //处理树状结构
  207. const transformedData = [
  208. {
  209. label: '区域管理',
  210. id: 1,
  211. value: [2, 1]
  212. },
  213. {
  214. label: '资源管理',
  215. id: 2,
  216. value: [4, 3]
  217. },
  218. {
  219. label: '审核管理',
  220. id: 3,
  221. value: [6, 5]
  222. },
  223. {
  224. label: '权限管理',
  225. id: 4,
  226. value: [7],
  227. children: [
  228. {
  229. label: '用户管理',
  230. id: 5,
  231. value: 8
  232. },
  233. {
  234. label: '角色管理',
  235. id: 6,
  236. value: 9
  237. }
  238. ]
  239. },
  240. {
  241. label: '系统设置',
  242. id: 7,
  243. value: [11, 10]
  244. }
  245. ]
  246. data = transformedData
  247. isshow.value = false
  248. }
  249. const handleNodeClick = (data: Tree) => {
  250. console.log(data)
  251. }
  252. const defaultProps = {
  253. children: 'children',
  254. label: 'label'
  255. }
  256. const tijiao = async () => {
  257. const formRef = await getElFormExpose()
  258. if ([...new Set(query2.authorityList.flat(Infinity))].length !== 0) {
  259. await formRef?.validate(async (isValid) => {
  260. if (isValid) {
  261. const formData = await getFormData()
  262. console.log('sdaasdasdas', isedit)
  263. if (isedit.value == '编辑') {
  264. console.log('sadasd1111', newroleId.value)
  265. query2.roleId = newroleId.value
  266. console.log('asdasdiddiididid', query2.roleId)
  267. query2.roleName = formData.roleName
  268. query2.enName = formData.enName
  269. query2.authorityList = [...new Set(query2.authorityList.flat(Infinity))]
  270. let res1 = await saveRole(query2)
  271. console.log('kankanwwwww222', res1)
  272. if (res1.code == 200) {
  273. isshow.value = true
  274. ElMessage.success('编辑成功')
  275. refresh()
  276. }
  277. } else {
  278. query.roleName = formData.roleName
  279. query.enName = formData.enName
  280. query.authorityList = [...new Set(query.authorityList.flat(Infinity))]
  281. let res1 = await saveRole(query)
  282. console.log('kankanwwwww', res1)
  283. if (res1.code == 200) {
  284. isshow.value = true
  285. ElMessage.success('添加成功')
  286. refresh()
  287. }
  288. }
  289. }
  290. })
  291. } else {
  292. ElMessage.warning('请选择功能权限')
  293. }
  294. }
  295. const rules = reactive<{
  296. [key: string]: FormItemRule[]
  297. }>({
  298. enName: [
  299. required(),
  300. { validator: (rule, value, callback) => onlyEnglish(value, callback, '请输入英文') }
  301. ]
  302. })
  303. const schema = reactive<FormSchema[]>([
  304. {
  305. field: 'roleName',
  306. label: '角色名称',
  307. component: 'Input',
  308. colProps: {
  309. span: 24
  310. },
  311. componentProps: {
  312. style: {
  313. width: '100%'
  314. },
  315. placeholder: '请输入角色名称'
  316. },
  317. formItemProps: {
  318. rules: [required()]
  319. }
  320. },
  321. {
  322. field: 'enName',
  323. label: '英文名称',
  324. component: 'Input',
  325. colProps: {
  326. span: 24
  327. },
  328. componentProps: {
  329. style: {
  330. width: '100%'
  331. },
  332. placeholder: '请输入英文名称'
  333. }
  334. }
  335. ])
  336. const send = (a, b) => {
  337. //每次点击节点树会触发的事件,默认有两个参数
  338. console.log('a,b', a, b)
  339. query2.authorityList = []
  340. query.authorityList = []
  341. b.checkedNodes.map((item) => {
  342. query.authorityList.push(item.value)
  343. query2.authorityList.push(item.value)
  344. })
  345. }
  346. //多选删除
  347. const getSelections = async () => {
  348. const elTableRef = await getElTableExpose()
  349. const selections = elTableRef?.getSelectionRows()
  350. let arr = {
  351. roleIds: []
  352. }
  353. if (selections) {
  354. arr.roleIds = selections.map((item) => {
  355. return item.roleId
  356. })
  357. }
  358. if (arr.roleIds.length !== 0) {
  359. ElMessageBox.confirm('确定删除所选数据?')
  360. .then(async () => {
  361. const res = await deleteRole(arr)
  362. if (res.code == 200) {
  363. ElMessage.success('删除成功')
  364. }
  365. refresh()
  366. })
  367. .catch(() => {
  368. // catch error
  369. })
  370. } else {
  371. ElMessage.warning('请选择数据')
  372. }
  373. }
  374. </script>
  375. <template>
  376. <ContentWrap v-if="isshow">
  377. <div style="margin-bottom: 10px">
  378. <span style="color: #606266"> 角色名称:</span>
  379. <el-input
  380. v-model="roleName"
  381. style="width: 200px; height: 43px; margin: 7px"
  382. placeholder="请输入角色名称"
  383. />
  384. <ElButton type="primary" size="large" @click="handersearch" :icon="Search">搜索</ElButton>
  385. <ElButton type="success" size="large" @click="tianjiabox" :icon="Edit">添加</ElButton>
  386. <ElButton type="danger" size="large" @click="getSelections" :icon="Delete">删除</ElButton>
  387. </div>
  388. <Table
  389. v-model:pageSize="pageSize"
  390. v-model:currentPage="currentPage"
  391. :columns="columns"
  392. :data="dataList"
  393. row-key="id"
  394. headerAlign="center"
  395. align="center"
  396. :loading="loading"
  397. sortable
  398. :pagination="{
  399. total: total
  400. }"
  401. @register="tableRegister"
  402. />
  403. </ContentWrap>
  404. <ContentWrap
  405. v-if="!isshow"
  406. style="display: flex; position: relative; justify-content: space-between"
  407. >
  408. <Form :schema="schema" :rules="rules" @register="formRegister" />
  409. </ContentWrap>
  410. <br />
  411. <ContentWrap
  412. v-if="!isshow"
  413. style="display: flex; position: relative; justify-content: space-between"
  414. >
  415. <div style="padding-bottom: 10px">
  416. <img style="margin-top: -20px" src="../../assets/imgs/diandian.png" alt="" srcset="" />
  417. 选择功能权限:</div
  418. >
  419. <el-tree
  420. style="max-width: 600px"
  421. show-checkbox
  422. :data="data"
  423. node-key="id"
  424. :default-checked-keys="morenxz"
  425. :props="defaultProps"
  426. @check="send"
  427. />
  428. <BaseButton
  429. @click="tijiao"
  430. style="font-size: 1rem; height: 2rem; position: absolute; top: 20px; right: 10px"
  431. type="primary"
  432. class="w-[75px]"
  433. >
  434. 提交
  435. </BaseButton>
  436. </ContentWrap>
  437. </template>