Role.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. morenxz.push(item)
  139. })
  140. console.log('sssssa', morenxz)
  141. tianjia().then(async () => {
  142. const formData = await getFormData()
  143. formData.roleName = res.data.roleName
  144. formData.enName = res.data.enName
  145. console.log('sssssa131', 22)
  146. })
  147. }
  148. //删除
  149. const handerdeleteRole = async (data) => {
  150. ElMessageBox.confirm('确定删除该数据?')
  151. .then(async () => {
  152. let arr = {
  153. roleIds: [data.row.roleId]
  154. }
  155. const res = await deleteRole(arr)
  156. if (res.code == 200) {
  157. ElMessage.success('删除成功')
  158. }
  159. refresh()
  160. })
  161. .catch(() => {
  162. // catch error
  163. })
  164. }
  165. const roleName = ref('')
  166. const isshow = ref(true)
  167. interface Tree {
  168. label: string
  169. children?: Tree[]
  170. }
  171. let data: Tree[] = []
  172. //搜索
  173. const handersearch = function () {
  174. tableState.currentPage.value = 1
  175. refresh()
  176. }
  177. const tianjiabox = () => {
  178. isedit.value = '添加'
  179. query.authorityList = []
  180. query2.authorityList = []
  181. morenxz = []
  182. tianjia()
  183. }
  184. const tianjia = async () => {
  185. let res = await getAuthTree()
  186. const transformedData: any = []
  187. res.data.map((item, index) => {
  188. if (item.children.length == 1) {
  189. let a1 = {
  190. label: item.name,
  191. id: item.id,
  192. value: item.id
  193. }
  194. transformedData.push(a1)
  195. } else {
  196. let a1 = {
  197. label: item.name,
  198. id: item.id,
  199. value: item.id,
  200. children: item.children.map((item2, index) => {
  201. let a2 = {
  202. label: item2.name,
  203. id: item2.id,
  204. value: item2.id
  205. }
  206. return a2
  207. })
  208. }
  209. transformedData.push(a1)
  210. }
  211. })
  212. data = transformedData
  213. console.log('sadas22d', data)
  214. isshow.value = false
  215. }
  216. const handleNodeClick = (data: Tree) => {
  217. console.log(data)
  218. }
  219. const defaultProps = {
  220. children: 'children',
  221. label: 'label'
  222. }
  223. const tijiao = async () => {
  224. const formRef = await getElFormExpose()
  225. if ([...new Set(query2.authorityList.flat(Infinity))].length !== 0) {
  226. await formRef?.validate(async (isValid) => {
  227. if (isValid) {
  228. const formData = await getFormData()
  229. console.log('sdaasdasdas', isedit)
  230. if (isedit.value == '编辑') {
  231. console.log('sadasd1111', newroleId.value)
  232. query2.roleId = newroleId.value
  233. console.log('asdasdiddiididid', query2.roleId)
  234. query2.roleName = formData.roleName
  235. query2.enName = formData.enName
  236. query2.authorityList = [...new Set(query2.authorityList.flat(Infinity))]
  237. let res1 = await saveRole(query2)
  238. console.log('kankanwwwww222', res1)
  239. if (res1.code == 200) {
  240. isshow.value = true
  241. ElMessage.success('编辑成功')
  242. refresh()
  243. }
  244. } else {
  245. query.roleName = formData.roleName
  246. query.enName = formData.enName
  247. query.authorityList = [...new Set(query.authorityList.flat(Infinity))]
  248. let res1 = await saveRole(query)
  249. console.log('kankanwwwww', res1)
  250. if (res1.code == 200) {
  251. isshow.value = true
  252. ElMessage.success('添加成功')
  253. refresh()
  254. }
  255. }
  256. }
  257. })
  258. } else {
  259. ElMessage.warning('请选择功能权限')
  260. }
  261. }
  262. const rules = reactive<{
  263. [key: string]: FormItemRule[]
  264. }>({
  265. enName: [
  266. required(),
  267. { validator: (rule, value, callback) => onlyEnglish(value, callback, '请输入英文') }
  268. ]
  269. })
  270. const schema = reactive<FormSchema[]>([
  271. {
  272. field: 'roleName',
  273. label: '角色名称',
  274. component: 'Input',
  275. colProps: {
  276. span: 24
  277. },
  278. componentProps: {
  279. style: {
  280. width: '100%'
  281. },
  282. placeholder: '请输入角色名称'
  283. },
  284. formItemProps: {
  285. rules: [required()]
  286. }
  287. },
  288. {
  289. field: 'enName',
  290. label: '英文名称',
  291. component: 'Input',
  292. colProps: {
  293. span: 24
  294. },
  295. componentProps: {
  296. style: {
  297. width: '100%'
  298. },
  299. placeholder: '请输入英文名称'
  300. }
  301. }
  302. ])
  303. const send = (a, b) => {
  304. //每次点击节点树会触发的事件,默认有两个参数
  305. console.log('a,b', a, b)
  306. query2.authorityList = []
  307. query.authorityList = []
  308. b.checkedNodes.map((item) => {
  309. query.authorityList.push(item.value)
  310. query2.authorityList.push(item.value)
  311. })
  312. }
  313. //多选删除
  314. const getSelections = async () => {
  315. const elTableRef = await getElTableExpose()
  316. const selections = elTableRef?.getSelectionRows()
  317. let arr = {
  318. roleIds: []
  319. }
  320. if (selections) {
  321. arr.roleIds = selections.map((item) => {
  322. return item.roleId
  323. })
  324. }
  325. if (arr.roleIds.length !== 0) {
  326. ElMessageBox.confirm('确定删除所选数据?')
  327. .then(async () => {
  328. const res = await deleteRole(arr)
  329. if (res.code == 200) {
  330. ElMessage.success('删除成功')
  331. }
  332. refresh()
  333. })
  334. .catch(() => {
  335. // catch error
  336. })
  337. } else {
  338. ElMessage.warning('请选择数据')
  339. }
  340. }
  341. </script>
  342. <template>
  343. <ContentWrap v-if="isshow">
  344. <div style="margin-bottom: 10px">
  345. <span style="color: #606266"> 角色名称:</span>
  346. <el-input
  347. v-model="roleName"
  348. style="width: 200px; height: 43px; margin: 7px"
  349. placeholder="请输入角色名称"
  350. />
  351. <ElButton type="primary" size="large" @click="handersearch" :icon="Search">搜索</ElButton>
  352. <ElButton type="success" size="large" @click="tianjiabox" :icon="Edit">添加</ElButton>
  353. <ElButton type="danger" size="large" @click="getSelections" :icon="Delete">删除</ElButton>
  354. </div>
  355. <Table
  356. v-model:pageSize="pageSize"
  357. v-model:currentPage="currentPage"
  358. :columns="columns"
  359. :data="dataList"
  360. row-key="id"
  361. headerAlign="center"
  362. align="center"
  363. :loading="loading"
  364. sortable
  365. :pagination="{
  366. total: total
  367. }"
  368. @register="tableRegister"
  369. />
  370. </ContentWrap>
  371. <ContentWrap
  372. v-if="!isshow"
  373. style="display: flex; position: relative; justify-content: space-between"
  374. >
  375. <Form :schema="schema" :rules="rules" @register="formRegister" />
  376. </ContentWrap>
  377. <br />
  378. <ContentWrap
  379. v-if="!isshow"
  380. style="display: flex; position: relative; justify-content: space-between"
  381. >
  382. <div style="padding-bottom: 10px">
  383. <img style="margin-top: -20px" src="../../assets/imgs/diandian.png" alt="" srcset="" />
  384. 选择功能权限:</div
  385. >
  386. <el-tree
  387. style="max-width: 600px"
  388. show-checkbox
  389. :data="data"
  390. node-key="id"
  391. :default-checked-keys="morenxz"
  392. :props="defaultProps"
  393. @check="send"
  394. />
  395. <BaseButton
  396. @click="tijiao"
  397. style="font-size: 1rem; height: 2rem; position: absolute; top: 20px; right: 10px"
  398. type="primary"
  399. class="w-[75px]"
  400. >
  401. 提交
  402. </BaseButton>
  403. </ContentWrap>
  404. </template>