index.vue 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <template>
  2. <div class="pageWrap bgc">
  3. <div class="content-warp flex-column-page-wrap">
  4. <!-- 公用搜索组件 -->
  5. <LeSearchForm ref="searchForm" v-model:searchData="searchData" :forms="forms" :loading="tableOpts.options.loading"></LeSearchForm>
  6. <!-- LeTable 组件使用 -->
  7. <LeTable
  8. ref="tableRef"
  9. v-model:searchParams="tableOpts.searchParams"
  10. v-bind="tableOpts"
  11. v-model:curRow="tableOpts.curRow"
  12. v-model:checked-options="checkedColumns"
  13. :columns="activeColumns"
  14. >
  15. <template #toolLeft>
  16. <el-button type="primary" @click="addHandler">
  17. <el-icon class="btn-icon">
  18. <Plus />
  19. </el-icon>
  20. </el-button>
  21. <el-button type="danger" :disabled="curSelectionRows.length === 0" @click="batch_del">
  22. <el-icon class="btn-icon">
  23. <Delete />
  24. </el-icon>
  25. </el-button>
  26. <el-button type="primary" plain :disabled="!curSelectionRows.length || curSelectionRows.length !== 1" @click="visiblePermission = true"
  27. >权限设置
  28. </el-button>
  29. </template>
  30. <template #statusSlot="scope">
  31. <status-indicator v-if="scope.row.status === 1" pulse type="success"></status-indicator>
  32. <status-indicator v-else pulse type="danger"></status-indicator>
  33. </template>
  34. <template #actionSlot="scope">
  35. <el-tooltip content="编辑" placement="bottom" effect="light">
  36. <el-icon class="ibt0" @click="table_edit(scope.row)">
  37. <Edit />
  38. </el-icon>
  39. </el-tooltip>
  40. <el-divider direction="vertical"></el-divider>
  41. <el-popconfirm title="确定删除吗?" @confirm="table_del(scope.row)">
  42. <template #reference>
  43. <el-icon class="ibt0">
  44. <Delete />
  45. </el-icon>
  46. </template>
  47. </el-popconfirm>
  48. </template>
  49. </LeTable>
  50. </div>
  51. <LeFormConfigDialog
  52. v-if="visible"
  53. ref="dialogUserRef"
  54. v-model="visible"
  55. :title="`${isCreate ? '新增' : '编辑'}角色`"
  56. width="600px"
  57. :form-data="activeData"
  58. :form-options="formOptions"
  59. @submit="submitHandler"
  60. />
  61. <!-- 权限设置 -->
  62. <Permission
  63. v-if="visiblePermission"
  64. v-model="visiblePermission"
  65. :role-id="curSelectionRows.map(item => item.id).join(',')"
  66. @closed="visiblePermission = false"
  67. />
  68. </div>
  69. </template>
  70. <script lang="tsx" setup>
  71. import role from '@/api/system/role'
  72. import { computed, nextTick, ref, watch } from 'vue'
  73. import { ElMessage, ElMessageBox, ElTree } from 'element-plus'
  74. import { useTablePage } from '@/hooks/useTablePage'
  75. import { Plus, Delete } from '@element-plus/icons-vue'
  76. import StatusIndicator from '@/components/StatusIndicator'
  77. import Permission from './permission.vue'
  78. const visible = ref(false) // 弹窗显示隐藏
  79. const isCreate = ref(true)
  80. const visiblePermission = ref(false) // 权限设置弹窗显示隐藏
  81. const activeData = ref({})
  82. const formsDialog = [
  83. {
  84. prop: 'name',
  85. label: '角色名称',
  86. itemType: 'input',
  87. rules: [{ required: true, message: '请输入角色名称', trigger: 'blur' }]
  88. },
  89. {
  90. prop: 'alias',
  91. label: '角色别名',
  92. itemType: 'input',
  93. rules: [{ required: true, message: '请输入角色别名', trigger: 'blur' }]
  94. },
  95. {
  96. prop: 'sort',
  97. label: '排序',
  98. itemType: 'inputNumber',
  99. rules: [{ required: true, message: '请输入排序', trigger: 'blur' }]
  100. },
  101. {
  102. prop: 'status',
  103. label: '状态',
  104. itemType: 'switch',
  105. activeText: '是',
  106. inactiveText: '否'
  107. },
  108. {
  109. prop: 'remark',
  110. label: '备注',
  111. itemType: 'input'
  112. }
  113. ]
  114. // 新增的表单 和 编辑的表单
  115. const formOptions = computed(() => {
  116. return {
  117. forms: formsDialog,
  118. labelWidth: 120,
  119. span: 30,
  120. showResetBtn: true,
  121. formConfig: {
  122. showCancelBtn: true,
  123. submitLoading: false
  124. }
  125. }
  126. })
  127. const groupFilterText = ref('')
  128. const treeRef = ref<InstanceType<typeof ElTree>>()
  129. // 表格搜索条件
  130. const forms = ref([
  131. {
  132. prop: 'keyword',
  133. label: '角色名称:',
  134. itemType: 'input',
  135. placeholder: '请输入角色名称'
  136. },
  137. {
  138. prop: 'status',
  139. label: '状态:',
  140. itemType: 'select',
  141. placeholder: '请选择状态',
  142. options: [
  143. {
  144. label: '禁用',
  145. value: 0
  146. },
  147. {
  148. label: '正常',
  149. value: 1
  150. }
  151. ]
  152. }
  153. ])
  154. // table列表数据请求
  155. const queryList = async () => {
  156. const { options, searchParams } = tableOpts
  157. options.loading = true
  158. try {
  159. const { records: list, total } = await role.rolePageApi(searchParams)
  160. tableOpts.total = total
  161. tableOpts.list = list
  162. } catch {
  163. console.log('获取列表数据失败')
  164. tableOpts.total = 0
  165. tableOpts.list = []
  166. options.loading = false // 更改加载中的 loading值
  167. } finally {
  168. options.loading = false
  169. }
  170. }
  171. // table 参数
  172. const columns = [
  173. {
  174. prop: 'name',
  175. label: '角色名称',
  176. minWidth: 80
  177. },
  178. {
  179. prop: 'status',
  180. label: '状态',
  181. minWidth: 50,
  182. slots: {
  183. default: 'statusSlot'
  184. }
  185. },
  186. {
  187. prop: 'alias',
  188. label: '别名',
  189. minWidth: 100
  190. },
  191. {
  192. prop: 'remark',
  193. label: '备注',
  194. minWidth: 100
  195. },
  196. {
  197. prop: 'sort',
  198. label: '排序',
  199. minWidth: 80
  200. },
  201. {
  202. prop: 'updateBy',
  203. label: '修改人',
  204. minWidth: 100
  205. },
  206. {
  207. prop: 'updateTime',
  208. label: '修改时间',
  209. minWidth: 126
  210. },
  211. {
  212. prop: 'createBy',
  213. label: '创建人',
  214. minWidth: 100
  215. },
  216. {
  217. prop: 'createTime',
  218. label: '创建时间',
  219. minWidth: 126
  220. },
  221. {
  222. prop: 'action',
  223. label: '操作',
  224. width: 100,
  225. fixed: 'right',
  226. slots: {
  227. default: 'actionSlot'
  228. }
  229. }
  230. ]
  231. const { searchData, tableOpts, checkedColumns, activeColumns, curSelectionRows, updateParams } = useTablePage(
  232. {
  233. options: {
  234. showIndex: false
  235. },
  236. // 需要展示的列
  237. columns,
  238. // 控制列配置
  239. columnsConfig: {
  240. columns
  241. }
  242. },
  243. {
  244. queryList,
  245. fetchImmediate: false
  246. }
  247. )
  248. // 删除
  249. const deleteItem = async ids => {
  250. // try {
  251. await role.roleDeleteApi(ids)
  252. ElMessage.success(`删除成功~`)
  253. updateParams()
  254. // } catch (e) {
  255. // console.log('删除失败')
  256. // ElMessage.error(`删除失败~`)
  257. // }
  258. }
  259. // 单个删除
  260. const table_del = row => {
  261. deleteItem([row.id])
  262. }
  263. //批量删除
  264. const batch_del = () => {
  265. const ids = curSelectionRows.value.map(item => item.id) // 多选数据
  266. ElMessageBox.confirm('是否删除选中数据?', '提示', {
  267. confirmButtonText: '确认',
  268. cancelButtonText: '取消',
  269. type: 'error',
  270. buttonSize: 'default'
  271. }).then(() => {
  272. deleteItem(ids)
  273. })
  274. }
  275. const table_edit = async row => {
  276. isCreate.value = false
  277. activeData.value = { ...row, status: row.status ? true : false }
  278. visible.value = true
  279. }
  280. // 弹窗事件
  281. const submitHandler = async params => {
  282. formOptions.value.formConfig.submitLoading = true
  283. try {
  284. params.status = params.status ? 1 : 0
  285. params.id = activeData.value.id ? activeData.value.id : null
  286. await role.roleSaveApi(params)
  287. ElMessage.success(`${isCreate.value ? '新增' : '修改'}成功~`)
  288. visible.value = false
  289. updateParams()
  290. formOptions.value.formConfig.submitLoading = false
  291. } catch (e) {
  292. console.log(e)
  293. formOptions.value.formConfig.submitLoading = false
  294. }
  295. }
  296. const addHandler = () => {
  297. isCreate.value = true
  298. activeData.value = {}
  299. visible.value = true
  300. }
  301. nextTick(() => {
  302. queryList()
  303. })
  304. watch(groupFilterText, val => {
  305. treeRef.value!.filter(val)
  306. })
  307. </script>