index copy 2.vue 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. <template>
  2. <div class="container">
  3. <Breadcrumb :items="['menu.taobao', 'menu.taobao.list']" />
  4. <a-card class="general-card" :title="$t('menu.taobao.list')">
  5. <a-row style="margin-bottom: 16px">
  6. <a-col :span="12">
  7. </a-col>
  8. <a-col :span="12" style="display: flex; align-items: center; justify-content: end">
  9. <a-tooltip :content="$t('searchTable.actions.refresh')">
  10. <div class="action-icon" @click="search"><icon-refresh size="18" /></div>
  11. </a-tooltip>
  12. <a-dropdown @select="handleSelectDensity">
  13. <a-tooltip :content="$t('searchTable.actions.density')">
  14. <div class="action-icon"><icon-line-height size="18" /></div>
  15. </a-tooltip>
  16. <template #content>
  17. <a-doption v-for="item in densityList" :key="item.value" :value="item.value"
  18. :class="{ active: item.value === size }">
  19. <span>{{ item.name }}</span>
  20. </a-doption>
  21. </template>
  22. </a-dropdown>
  23. <a-tooltip :content="$t('searchTable.actions.columnSetting')">
  24. <a-popover trigger="click" position="bl" @popup-visible-change="popupVisibleChange">
  25. <div class="action-icon"><icon-settings size="18" /></div>
  26. <template #content>
  27. <div id="tableSetting">
  28. <div v-for="(item, index) in showColumns" :key="item.dataIndex" class="setting">
  29. <div style="margin-right: 4px; cursor: move">
  30. <icon-drag-arrow />
  31. </div>
  32. <div>
  33. <a-checkbox v-model="item.checked" @change="
  34. handleChange($event, item as TableColumnData, index)
  35. ">
  36. </a-checkbox>
  37. </div>
  38. <div class="title">
  39. {{ item.title === '#' ? '序列号' : item.title }}
  40. </div>
  41. </div>
  42. </div>
  43. </template>
  44. </a-popover>
  45. </a-tooltip>
  46. </a-col>
  47. </a-row>
  48. <a-table row-key="id" :loading="loading" :pagination="pagination" :columns="(cloneColumns as TableColumnData[])"
  49. :data="renderData" :bordered="false" :size="size" @page-change="onPageChange" :load-more="loadMore">
  50. <template #index="{ rowIndex }">
  51. {{ rowIndex + 1 + (pagination.current - 1) * pagination.pageSize }}
  52. </template>
  53. <template #xAxis="{ record }">
  54. <template v-if="record.isLeaf">
  55. {{ record.accountName }}
  56. </template>
  57. <template v-else>
  58. {{ dayjs(record.log_date).format('YYYY-MM-DD') }}
  59. </template>
  60. </template>
  61. </a-table>
  62. </a-card>
  63. </div>
  64. </template>
  65. <script lang="ts" setup>
  66. import { useRouter } from 'vue-router';
  67. import { computed, ref, reactive, watch, nextTick } from 'vue';
  68. import { useI18n } from 'vue-i18n';
  69. import dayjs from 'dayjs';
  70. import useLoading from '@/hooks/loading';
  71. import { queryDailyLog, DailyLogRecord, DailyLogParams } from '@/api/taobao';
  72. import { Pagination } from '@/types/global';
  73. import type { SelectOptionData } from '@arco-design/web-vue/es/select/interface';
  74. import type { TableData, TableColumnData } from '@arco-design/web-vue/es/table/interface';
  75. import cloneDeep from 'lodash/cloneDeep';
  76. import Sortable from 'sortablejs';
  77. type SizeProps = 'mini' | 'small' | 'medium' | 'large';
  78. type Column = TableColumnData & { checked?: true };
  79. const generateFormModel = () => {
  80. return {
  81. accountName: 'all',
  82. current: 1, pageSize: 20, sort: 'log_date', order: 'descending'
  83. };
  84. };
  85. const router = useRouter();
  86. const { loading, setLoading } = useLoading(true);
  87. const { t } = useI18n();
  88. const renderData = ref<DailyLogRecord[]>([]);
  89. const formModel = ref(generateFormModel());
  90. const cloneColumns = ref<Column[]>([]);
  91. const showColumns = ref<Column[]>([]);
  92. const size = ref<SizeProps>('medium');
  93. const basePagination: Pagination = {
  94. current: 1,
  95. pageSize: 20,
  96. };
  97. const pagination = reactive({
  98. ...basePagination,
  99. });
  100. const densityList = computed(() => [
  101. {
  102. name: t('searchTable.size.mini'),
  103. value: 'mini',
  104. },
  105. {
  106. name: t('searchTable.size.small'),
  107. value: 'small',
  108. },
  109. {
  110. name: t('searchTable.size.medium'),
  111. value: 'medium',
  112. },
  113. {
  114. name: t('searchTable.size.large'),
  115. value: 'large',
  116. },
  117. ]);
  118. const columns = computed<TableColumnData[]>(() => [
  119. {
  120. title: '日期',
  121. slotName: 'xAxis',
  122. },
  123. {
  124. title: 'API调用数',
  125. dataIndex: 'total_count',
  126. },
  127. {
  128. title: '转链成功数',
  129. dataIndex: 'success_count',
  130. },
  131. {
  132. title: '转链成功率 %',
  133. dataIndex: 'success_percentage',
  134. slotName: 'success_percentage',
  135. },
  136. {
  137. title: '放弃转链数',
  138. dataIndex: 'abandon_count',
  139. slotName: 'abandon_count',
  140. },
  141. {
  142. title: '放弃转链 %',
  143. dataIndex: 'abandon_percentage',
  144. slotName: 'abandon_percentage',
  145. },
  146. {
  147. title: '最后更新',
  148. dataIndex: 'last_time',
  149. },
  150. ]);
  151. const fetchData = async (
  152. params: DailyLogParams = {
  153. // query_date: [row.report_date, row.report_date],
  154. accountName: 'all',
  155. current: 1, pageSize: 20, sort: 'log_date', order: 'descending'
  156. }
  157. ) => {
  158. setLoading(true);
  159. try {
  160. const { data } = await queryDailyLog(params);
  161. console.log(data);
  162. if (!data) return;
  163. // const { data } = await queryDailyLog(params);
  164. renderData.value = data.list;
  165. pagination.current = data.page;
  166. pagination.total = data.count;
  167. console.log(data)
  168. } catch (err) {
  169. console.log(err)
  170. // you can report use errorHandler or other
  171. } finally {
  172. setLoading(false);
  173. }
  174. };
  175. const loadMore = async (row: TableData, done: (data: TableData[]) => void) => {
  176. setLoading(true);
  177. try {
  178. const params: DailyLogParams = {
  179. query_date: [row.log_date, row.log_date],
  180. current: 1, pageSize: 999, sort: 'id', order: 'descending'
  181. };
  182. const { data } = await queryDailyLog(params);
  183. console.log(data);
  184. if (!data) return;
  185. const list: TableData[] = data.list.map((item: DailyLogRecord) => ({
  186. ...item,
  187. isLeaf: true
  188. }));
  189. done(list);
  190. } catch (err) {
  191. console.log(err)
  192. } finally {
  193. setLoading(false);
  194. }
  195. };
  196. const changeItem = async (
  197. data: DailyLogRecord
  198. ) => {
  199. router.push({ name: 'updateCookies' });
  200. };
  201. const search = () => {
  202. fetchData({
  203. ...basePagination,
  204. ...formModel.value,
  205. } as unknown as DailyLogParams);
  206. };
  207. const onPageChange = (current: number) => {
  208. fetchData({
  209. ...basePagination,
  210. ...formModel.value,
  211. current
  212. });
  213. };
  214. fetchData();
  215. const reset = () => {
  216. formModel.value = generateFormModel();
  217. };
  218. const handleSelectDensity = (
  219. val: string | number | Record<string, any> | undefined,
  220. e: Event
  221. ) => {
  222. size.value = val as SizeProps;
  223. };
  224. const handleChange = (
  225. checked: boolean | (string | boolean | number)[],
  226. column: Column,
  227. index: number
  228. ) => {
  229. if (!checked) {
  230. cloneColumns.value = showColumns.value.filter(
  231. (item) => item.dataIndex !== column.dataIndex
  232. );
  233. } else {
  234. cloneColumns.value.splice(index, 0, column);
  235. }
  236. };
  237. const exchangeArray = <T extends Array<any>>(
  238. array: T,
  239. beforeIdx: number,
  240. newIdx: number,
  241. isDeep = false
  242. ): T => {
  243. const newArray = isDeep ? cloneDeep(array) : array;
  244. if (beforeIdx > -1 && newIdx > -1) {
  245. // 先替换后面的,然后拿到替换的结果替换前面的
  246. newArray.splice(
  247. beforeIdx,
  248. 1,
  249. newArray.splice(newIdx, 1, newArray[beforeIdx]).pop()
  250. );
  251. }
  252. return newArray;
  253. };
  254. const popupVisibleChange = (val: boolean) => {
  255. if (val) {
  256. nextTick(() => {
  257. const el = document.getElementById('tableSetting') as HTMLElement;
  258. const sortable = new Sortable(el, {
  259. onEnd(e: any) {
  260. const { oldIndex, newIndex } = e;
  261. exchangeArray(cloneColumns.value, oldIndex, newIndex);
  262. exchangeArray(showColumns.value, oldIndex, newIndex);
  263. },
  264. });
  265. });
  266. }
  267. };
  268. watch(
  269. () => columns.value,
  270. (val) => {
  271. cloneColumns.value = cloneDeep(val);
  272. cloneColumns.value.forEach((item, index) => {
  273. item.checked = true;
  274. });
  275. showColumns.value = cloneDeep(cloneColumns.value);
  276. },
  277. { deep: true, immediate: true }
  278. );
  279. </script>
  280. <script lang="ts">
  281. export default {
  282. name: 'SearchTable',
  283. };
  284. </script>
  285. <style scoped lang="less">
  286. .container {
  287. padding: 0 20px 20px 20px;
  288. }
  289. :deep(.arco-table-th) {
  290. &:last-child {
  291. .arco-table-th-item-title {
  292. margin-left: 16px;
  293. }
  294. }
  295. }
  296. .action-icon {
  297. margin-left: 12px;
  298. cursor: pointer;
  299. }
  300. .active {
  301. color: #0960bd;
  302. background-color: #e3f4fc;
  303. }
  304. .setting {
  305. display: flex;
  306. align-items: center;
  307. width: 200px;
  308. .title {
  309. margin-left: 12px;
  310. cursor: pointer;
  311. }
  312. }
  313. </style>