index.vue 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <template>
  2. <div>
  3. <div class="search__example--wrap">
  4. <com-search
  5. :data="searchData"
  6. @search-submit="searchSubmit"
  7. @reset-submit="resetSubmit"
  8. />
  9. </div>
  10. <div class="button__example--wrap">
  11. <el-button type="primary" icon="el-icon-circle-plus-outline" @click="open(false)">新增</el-button>
  12. <el-button
  13. type="danger"
  14. icon="el-icon-delete"
  15. @click="dels"
  16. >删除</el-button>
  17. </div>
  18. <com-table
  19. v-loading="loading"
  20. selection
  21. :columns="columns"
  22. :data="tableData"
  23. :pagination="{
  24. currentPage: defalutParams.pageIndex,
  25. total: total,
  26. onSizeChange: handleSizeChange,
  27. onCurrentChange: handleCurrentChange
  28. }"
  29. @selection-change="handleSelectionChange"
  30. >
  31. <template #importance="scope">
  32. <el-tag
  33. :type="scope.row.importance === 3
  34. ? 'success'
  35. : (scope.row.importance === 2
  36. ? 'warning'
  37. : 'danger')"
  38. >{{ scope.row.importance === 3
  39. ? '重要'
  40. : (scope.row.importance === 2
  41. ? '良好'
  42. : '一般') }}
  43. </el-tag>
  44. </template>
  45. <template #action="scope">
  46. <el-button type="primary" size="mini" @click="open(scope.row)">编辑</el-button>
  47. <el-button type="danger" size="mini" @click="dels(scope.row)">删除</el-button>
  48. </template>
  49. </com-table>
  50. </div>
  51. </template>
  52. <script lang="ts">
  53. import { defineComponent, onMounted, onUnmounted } from 'vue'
  54. import { useRouter } from 'vue-router'
  55. import vueBus from '@/vue-bus'
  56. import { useExample } from '@/hooks/useExample'
  57. import { Message } from '_c/Message'
  58. import { getExampleListApi, delsExampApi } from './api'
  59. const searchData = [
  60. {
  61. label: '标题',
  62. value: '',
  63. itemType: 'input',
  64. field: 'title',
  65. placeholder: '请输入标题',
  66. clearable: true
  67. }
  68. ]
  69. const columns = [
  70. {
  71. key: 'title',
  72. label: '标题',
  73. showOverflowTooltip: true
  74. },
  75. {
  76. key: 'author',
  77. label: '作者'
  78. },
  79. {
  80. key: 'display_time',
  81. label: '创建时间'
  82. },
  83. {
  84. key: 'importance',
  85. label: '重要性',
  86. slots: {
  87. default: 'importance'
  88. }
  89. },
  90. {
  91. key: 'pageviews',
  92. label: '阅读数'
  93. },
  94. {
  95. key: 'action',
  96. label: '操作',
  97. width: '150px',
  98. slots: {
  99. default: 'action'
  100. }
  101. }
  102. ]
  103. export default defineComponent({
  104. name: 'ExamplePage',
  105. setup() {
  106. const { push } = useRouter()
  107. const {
  108. defalutParams,
  109. tableData,
  110. loading,
  111. total,
  112. currentChange,
  113. sizeChange,
  114. handleSelectionChange,
  115. selectionData,
  116. delData
  117. } = useExample()
  118. // 请求数据
  119. async function getExampleList(data?: any): Promise<void> {
  120. try {
  121. const res = await getExampleListApi({
  122. params: Object.assign(defalutParams, data || {})
  123. })
  124. if (res.code === '0000') {
  125. total.value = res.data.total
  126. tableData.value = res.data.list
  127. }
  128. } finally {
  129. loading.value = false
  130. }
  131. }
  132. // 查询
  133. function searchSubmit(data: any) {
  134. // 该方法重置了一些默认参数
  135. currentChange(1)
  136. getExampleList(data)
  137. }
  138. // 重置
  139. function resetSubmit(data: any) {
  140. // 该方法重置了一些默认参数
  141. currentChange(1)
  142. getExampleList(data)
  143. }
  144. // 展示多少条
  145. function handleSizeChange(val: number) {
  146. // 该方法重置了一些默认参数
  147. sizeChange(val)
  148. getExampleList()
  149. }
  150. // 展示第几页
  151. function handleCurrentChange(val: number) {
  152. // 该方法重置了一些默认参数
  153. currentChange(val)
  154. getExampleList()
  155. }
  156. // 删除多选
  157. function dels(item?: any) {
  158. delData(async() => {
  159. let ids: string[]
  160. if (item.id) {
  161. ids = [item.id]
  162. } else {
  163. ids = selectionData.value.map((v: any) => {
  164. return v.id
  165. })
  166. }
  167. const res = await delsExampApi({
  168. data: { ids }
  169. })
  170. if (res.code === '0000') {
  171. Message.success('删除成功!')
  172. getExampleList()
  173. }
  174. }, { hiddenVerify: item.id })
  175. }
  176. // 打开新页面
  177. function open(row: any) {
  178. push(row ? `/example-demo/example-edit?id=${row.id}` : `/example-demo/example-add`)
  179. }
  180. getExampleList()
  181. onMounted(() => {
  182. vueBus.$on('success', (type: string) => {
  183. if (type === 'add') {
  184. currentChange(1)
  185. }
  186. getExampleList()
  187. })
  188. })
  189. onUnmounted(() => {
  190. vueBus.$off('success')
  191. })
  192. return {
  193. open,
  194. searchData, searchSubmit, resetSubmit,
  195. columns,
  196. defalutParams,
  197. loading,
  198. tableData,
  199. total,
  200. handleSizeChange,
  201. handleCurrentChange,
  202. handleSelectionChange,
  203. dels
  204. }
  205. }
  206. })
  207. </script>
  208. <style>
  209. </style>