shijiplus-web-plugin 0.1.25 → 0.1.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shijiplus-web-plugin",
3
- "version": "0.1.25",
3
+ "version": "0.1.27",
4
4
  "files": [
5
5
  "src"
6
6
  ],
@@ -1,3 +1,4 @@
1
+ /* eslint-disable no-undef */
1
2
  import PlusCircleProgressModal from './plus-circle-progress-modal.vue'
2
3
  export default {
3
4
  data() {
@@ -8,74 +9,162 @@ export default {
8
9
  }
9
10
  },
10
11
  methods: {
11
- invokeExportExcel(searchForm, apiFunction, options = {}) {
12
- const { exportDesc } = this.$merge({}, options)
13
- const checkExportDataTotal = (searchForm) => {
14
- return apiFunction({
12
+ // Helper function to generate a list of 4-hour intervals between startDate and endDate
13
+ getDateRange(startDate, endDate) {
14
+ const intervals = []
15
+ let currentDate = new Date(startDate)
16
+ const end = new Date(endDate)
17
+
18
+ while (currentDate <= end) {
19
+ const nextDate = new Date(currentDate)
20
+ nextDate.setHours(currentDate.getHours() + 4)
21
+ intervals.push({
22
+ start: new Date(currentDate),
23
+ end: nextDate > end ? new Date(end) : nextDate
24
+ })
25
+ currentDate = nextDate
26
+ }
27
+ return intervals
28
+ },
29
+
30
+ // Helper function to delay for a while
31
+ delay() {
32
+ return new Promise(resolve => setTimeout(resolve, 100))
33
+ },
34
+
35
+ // Format date to 'yyyy-MM-dd HH:mm:ss'
36
+ formatDate(date) {
37
+ const pad = (num) => (num < 10 ? '0' + num : num)
38
+ return `${date.getFullYear()}-${pad(date.getMonth() + 1)}-${pad(date.getDate())} ${pad(date.getHours())}:${pad(date.getMinutes())}:${pad(date.getSeconds())}`
39
+ },
40
+
41
+ // Function to fetch data for a specific 4-hour interval
42
+ async fetchDataForInterval(interval, searchForm, apiFunction) {
43
+ const intervalStart = this.formatDate(interval.start)
44
+ const intervalEnd = this.formatDate(interval.end)
45
+
46
+ // Query the total count for this interval
47
+ const intervalSearchForm = {
48
+ ...searchForm,
49
+ startDate: intervalStart,
50
+ endDate: intervalEnd,
51
+ pageNo: 1,
52
+ currentPage: 1,
53
+ pageSize: 1,
54
+ timestamp: new Date().getTime()
55
+ }
56
+ await this.delay()
57
+ const res = await apiFunction(intervalSearchForm)
58
+ const intervalTotalCount = res.meta ? res.meta.totalCount : (res.data || []).length
59
+
60
+ let intervalData = []
61
+ if (intervalTotalCount <= this.orderExportPageSize) {
62
+ // Fetch all data for the interval in one request
63
+ await this.delay()
64
+ const fetchRes = await apiFunction({
15
65
  ...searchForm,
66
+ startDate: intervalStart,
67
+ endDate: intervalEnd,
16
68
  pageNo: 1,
17
69
  currentPage: 1,
18
- pageSize: 1
70
+ pageSize: intervalTotalCount
19
71
  })
20
- .then((res) => {
21
- if (res && res.meta) {
22
- this.totalCount = res.meta.totalCount
23
- } else {
24
- this.totalCount = (res.data || []).length
25
- }
72
+ intervalData = fetchRes.data || []
73
+ } else {
74
+ // Fetch data in batches (pagination)
75
+ const totalPages = Math.ceil(intervalTotalCount / this.orderExportPageSize)
76
+ for (let page = 1; page <= totalPages; page++) {
77
+ await this.delay()
78
+ const fetchRes = await apiFunction({
79
+ ...searchForm,
80
+ startDate: intervalStart,
81
+ endDate: intervalEnd,
82
+ pageNo: page,
83
+ currentPage: page,
84
+ pageSize: this.orderExportPageSize
26
85
  })
86
+ intervalData.push(...(fetchRes.data || []))
87
+ }
27
88
  }
28
- let exportData = []
29
- const excelExportFun = (pageNo, pageSize, processUpdate) => {
30
- return apiFunction({
31
- ...searchForm,
32
- currentPage: pageNo,
33
- pageNo: pageNo,
34
- pageSize: pageSize
35
- }).then((res) => {
36
- if (res && res.data) {
37
- exportData.push(...res.data)
38
- } else {
39
- throw Error('导出失败')
40
- }
41
- this.orderExportModalPercent = (exportData.length / (this.totalCount || 1) * 100).toFixed(2)
42
- if (processUpdate) {
43
- processUpdate(this.orderExportModalPercent)
44
- }
45
- if (exportData.length < this.totalCount) {
46
- return excelExportFun(pageNo + 1, pageSize, processUpdate)
47
- }
48
- return exportData
49
- }).catch((error) => {
50
- console.log('--------error---------', error)
51
- throw Error('导出失败')
52
- })
53
- }
54
- return checkExportDataTotal(searchForm).then((res) => {
55
- if (this.totalCount === 0) {
56
- // 宏任务最后执行
89
+ return { intervalData, intervalTotalCount }
90
+ },
91
+
92
+ // Main export function
93
+ async invokeExportExcel(searchForm, apiFunction) {
94
+ // 3. Initialize the progress modal
95
+ const instance = this.$getComponentInstance(PlusCircleProgressModal)
96
+ instance.$on('on-close', () => {
97
+ setTimeout(() => {
98
+ this.$removeVComp(instance)
99
+ }, 500)
100
+ })
101
+ try {
102
+ // 0. Validate the date range (limit to 1 month)
103
+ const startDate = new Date(searchForm.startDate || searchForm.startTime || "")
104
+ const endDate = new Date(searchForm.endDate || searchForm.startTime || "")
105
+ const oneMonthInMs = 31 * 24 * 60 * 60 * 1000 // 1 个月的毫秒数
106
+
107
+ if (endDate - startDate > oneMonthInMs) {
57
108
  setTimeout(() => {
58
- this.$Message.error('暂无数据,请搜索后重试', 3)
109
+ this.$Message.error('导出时间范围不能超过1个月,请调整后重试', 3)
59
110
  }, 0)
60
111
  return
61
112
  }
62
- const processInstance = this.$getComponentInstance(PlusCircleProgressModal)
63
- processInstance.$on('on-close', (e) => {
64
- setTimeout(() => {
65
- this.$removeVComp(processInstance)
66
- }, 500)
67
- })
68
- processInstance.show({ exportDesc })
69
- return excelExportFun(1, this.orderExportPageSize, (process) => {
70
- processInstance.updatePercent(process)
71
- }).catch(() => {
72
- // 宏任务最后执行
113
+
114
+ // 1. Query the total count for the entire task
115
+ const checkExportDataTotal = async (searchForm) => {
116
+ await this.delay()
117
+ const res = await apiFunction({
118
+ ...searchForm,
119
+ pageNo: 1,
120
+ currentPage: 1,
121
+ pageSize: 1
122
+ })
123
+ this.totalCount = res.meta ? res.meta.totalCount : (res.data || []).length
124
+ }
125
+ await checkExportDataTotal(searchForm)
126
+
127
+ if (this.totalCount === 0) {
73
128
  setTimeout(() => {
74
- this.$Message.error('导出失败', 3)
75
- this.$removeVComp(processInstance)
129
+ this.$Message.error('暂无数据,请搜索后重试', 3)
76
130
  }, 0)
77
- })
78
- })
131
+ return
132
+ }
133
+
134
+ // 2. Get the 4-hour intervals from startDate to endDate
135
+ const intervals = this.getDateRange(startDate, endDate)
136
+
137
+ instance.show()
138
+
139
+ // 4. Fetch data interval by interval and update progress
140
+ let exportedCount = 0
141
+ let exportData = []
142
+ for (const interval of intervals) {
143
+ const { intervalData, intervalTotalCount } = await this.fetchDataForInterval(interval, searchForm, apiFunction)
144
+ exportData.push(...intervalData)
145
+ exportedCount += intervalData.length
146
+
147
+ // Update progress based on total count
148
+ this.orderExportModalPercent = (exportedCount / this.totalCount * 100).toFixed(2)
149
+ instance.updatePercent(this.orderExportModalPercent)
150
+ }
151
+
152
+ // 5. Export the data (logic omitted here, add your export implementation)
153
+ // console.log('Export data:', exportData)
154
+
155
+ return exportData
156
+
157
+ // 6. Close the progress modal
158
+ // instance.close()
159
+ } catch (error) {
160
+ console.error('导出失败:', error)
161
+ setTimeout(() => {
162
+ this.$Message.error('导出失败', 3)
163
+ if (instance) {
164
+ this.$removeVComp(instance)
165
+ }
166
+ }, 0)
167
+ }
79
168
  },
80
169
  exportAllData(searchForm, options = {}) {
81
170
  let exportData = []