teachApply.js
4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import api from '~/api/api'
import {
TEACH_LIST_FIELDS,
isStrategyApiSuccess,
parseTeachingApplicationPageData,
} from '~/utils/teachApplyItem'
const PAGE_SIZE = 10
Page({
data: {
navBarHeight: 0,
keyword: '',
tabList: ['待审批(0)', '全部(0)'],
tabIndex: 0,
listFields: TEACH_LIST_FIELDS,
displayList: [],
pageNum: 1,
pageSize: PAGE_SIZE,
total: 0,
hasMore: true,
loading: false,
triggered: false,
nothing: false,
},
navbarReady(e) {
this.setData({ navBarHeight: e.detail.height })
},
onBack() {
wx.navigateBack()
},
onShow() {
this.refreshAll()
},
getKeyword() {
return String(this.data.keyword ?? '').trim()
},
onKeywordInput(e) {
this.setData({ keyword: e.detail.value })
},
clearKeyword() {
this.setData({ keyword: '' }, () => this.refreshAll())
},
search(e) {
const keyword = String(e?.detail?.value ?? this.data.keyword ?? '').trim()
this.setData({ keyword }, () => this.refreshAll())
},
onRefresh() {
this.setData({ triggered: true })
this.refreshAll().finally(() => {
this.setData({ triggered: false })
})
},
tabClick(e) {
const tabIndex = e.detail
if (tabIndex === this.data.tabIndex) return
this.setData({ tabIndex }, () => this.reloadList())
},
onReachBottom() {
const { hasMore, loading } = this.data
if (!hasMore || loading) return
this.loadList(false)
},
buildQueryParams({ pageNum, pageSize, tab } = {}) {
const { tabIndex, pageSize: defaultPageSize } = this.data
const keyWord = this.getKeyword()
const params = {
pageNum: pageNum ?? 1,
pageSize: pageSize ?? defaultPageSize,
keyWord,
keyword: keyWord,
}
const tabVal = tab !== undefined ? tab : tabIndex === 0 ? 0 : undefined
if (tabVal === 0) params.tab = 0
return params
},
buildListParams(pageNum) {
return this.buildQueryParams({ pageNum })
},
buildCountParams(tab) {
return this.buildQueryParams({ pageNum: 1, pageSize: 1, tab })
},
updateTabListLabels(pending, all) {
this.setData({
tabList: [`待审批(${pending})`, `全部(${all})`],
})
},
async fetchTabCounts() {
try {
const [pendingRes, allRes] = await Promise.all([
api.getTeachingApplicationPage(this.buildCountParams(0)),
api.getTeachingApplicationPage(this.buildCountParams()),
])
const pickTotal = (res) => parseTeachingApplicationPageData(res).total
this.updateTabListLabels(pickTotal(pendingRes), pickTotal(allRes))
} catch (e) {
// 计数失败不影响列表
}
},
async refreshAll() {
await Promise.all([this.fetchTabCounts(), this.reloadList()])
},
async reloadList() {
this.setData({ pageNum: 1, hasMore: true, displayList: [] })
await this.loadList(true)
},
async loadList(reset = true) {
if (this.data.loading) return
const pageNum = reset ? 1 : this.data.pageNum
this.setData({ loading: true })
try {
const res = await api.getTeachingApplicationPage(
this.buildListParams(pageNum)
)
const { list, total } = parseTeachingApplicationPageData(res)
if (!list.length && !isStrategyApiSuccess(res)) {
wx.showToast({
title: res?.message || '加载失败',
icon: 'none',
})
this.setData({ loading: false, hasMore: false, nothing: true })
return
}
const displayList = reset ? list : [...this.data.displayList, ...list]
const hasMore = displayList.length < total
this.setData({
displayList,
total,
pageNum: pageNum + 1,
hasMore,
nothing: !hasMore,
loading: false,
})
} catch (e) {
wx.showToast({ title: '加载失败', icon: 'none' })
this.setData({
loading: false,
hasMore: false,
nothing: true,
})
}
},
openDetail(e) {
const { item } = e.currentTarget.dataset
const applicationId = item?.applicationId || item?.id
if (!applicationId) return
wx.navigateTo({
url: `/pages/teachApplyDetail/teachApplyDetail?id=${applicationId}`,
})
},
goAddTeach() {
wx.navigateTo({
url: '/pages/teachApplyCreate/teachApplyCreate',
events: {
refreshList: () => this.refreshAll(),
},
})
},
})