index.tsx 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. import OHeader from '@/components/o-header'
  2. import OSticky from '@/components/o-sticky'
  3. import request from '@/helpers/request'
  4. import { ActionSheet, Cell, Icon, List, Popover, Tag } from 'vant'
  5. import { defineComponent, onMounted, onUnmounted, reactive } from 'vue'
  6. import { useRouter } from 'vue-router'
  7. import styles from './index.module.less'
  8. import { postMessage } from '@/helpers/native-message'
  9. import { state } from '@/state'
  10. import OEmpty from '@/components/o-empty'
  11. import { orchestraType } from '@/constant'
  12. import OFullRefresh from '@/components/o-full-refresh'
  13. import OActionSheet from '@/components/o-action-sheet'
  14. export default defineComponent({
  15. name: 'my-orchestra',
  16. setup() {
  17. const router = useRouter()
  18. const form = reactive({
  19. showPopover: false,
  20. actions: [
  21. { name: '全部乐团', selected: true, value: 'ALL' },
  22. { name: '交付团', value: 'DELIVERY' },
  23. { name: '晋升团', value: 'PROMOTION' }
  24. ],
  25. isClick: false,
  26. list: [] as any,
  27. listState: {
  28. dataShow: true, // 判断是否有数据
  29. loading: false,
  30. finished: false,
  31. refreshing: false,
  32. height: 0 // 页面头部高度,为了处理下拉刷新用的
  33. },
  34. params: {
  35. type: null,
  36. status: 'DONE',
  37. page: 1,
  38. rows: 20
  39. }
  40. })
  41. const formatType = (type: any) => {
  42. const template = {
  43. DELIVERY: '交付团',
  44. PROMOTION: '晋升团'
  45. }
  46. return template[type] || '全部乐团'
  47. }
  48. const onSelect = (val: any) => {
  49. form.actions.forEach((child: any) => {
  50. child.selected = false
  51. })
  52. val.selected = true
  53. form.params.type = val.value === 'ALL' ? null : val.value
  54. form.showPopover = false
  55. form.params.page = 1
  56. form.list = []
  57. form.listState.dataShow = true // 判断是否有数据
  58. form.listState.loading = false
  59. form.listState.finished = false
  60. getList()
  61. }
  62. const getList = async () => {
  63. try {
  64. if (form.isClick) return
  65. form.isClick = true
  66. const res = await request.post('/api-school/orchestra/page', {
  67. data: {
  68. ...form.params,
  69. schoolId: state.user.data.school.id
  70. }
  71. })
  72. form.listState.loading = false
  73. form.listState.refreshing = false
  74. const result = res.data || {}
  75. // 处理重复请求数据
  76. if (form.list.length > 0 && result.current === 1) {
  77. return
  78. }
  79. form.list = form.list.concat(result.rows || [])
  80. form.listState.finished = result.current >= result.pages
  81. form.params.page = result.current + 1
  82. form.listState.dataShow = form.list.length > 0
  83. form.isClick = false
  84. } catch {
  85. form.listState.dataShow = false
  86. form.listState.finished = true
  87. form.listState.refreshing = false
  88. form.isClick = false
  89. }
  90. }
  91. const onDetail = (item: any) => {
  92. console.log(item)
  93. router.push({
  94. path: '/orchestra-detail',
  95. query: {
  96. id: item.id
  97. }
  98. })
  99. }
  100. const onRefresh = () => {
  101. form.params.page = 1
  102. form.list = []
  103. form.listState.dataShow = true // 判断是否有数据
  104. form.listState.loading = false
  105. form.listState.finished = false
  106. getList()
  107. }
  108. onMounted(() => {
  109. getList()
  110. // 处理返回上一页的问题
  111. window.history.pushState(null, '', document.URL)
  112. window.addEventListener('popstate', onBack, false)
  113. })
  114. const onBack = () => {
  115. postMessage({ api: 'back' })
  116. }
  117. onUnmounted(() => {
  118. window.removeEventListener('popstate', onBack)
  119. })
  120. return () => (
  121. <div class={!form.listState.dataShow && 'emptyRootContainer'}>
  122. <OSticky
  123. position="top"
  124. onGetHeight={(height: any) => {
  125. form.listState.height = height
  126. }}
  127. >
  128. <OHeader>
  129. {{
  130. right: () => (
  131. <Icon
  132. name="plus"
  133. size={19}
  134. color="#333"
  135. onClick={() => {
  136. router.push('/create-orchestra')
  137. }}
  138. />
  139. )
  140. }}
  141. </OHeader>
  142. <div class="searchGroup-single">
  143. <span
  144. onClick={() => (form.showPopover = true)}
  145. class={['searchItem', form.showPopover ? 'searchItem-active' : '']}
  146. >
  147. {formatType(form.params.type)}
  148. </span>
  149. </div>
  150. </OSticky>
  151. {form.listState.dataShow ? (
  152. <OFullRefresh
  153. v-model:modelValue={form.listState.refreshing}
  154. onRefresh={onRefresh}
  155. style={{
  156. minHeight: `calc(100vh - ${form.listState.height}px)`
  157. }}
  158. >
  159. <List
  160. // v-model:loading={form.listState.loading}
  161. finished={form.listState.finished}
  162. finishedText=" "
  163. class={[styles.liveList]}
  164. onLoad={getList}
  165. immediateCheck={false}
  166. >
  167. {form.list.map((item: any) => (
  168. <Cell isLink center class={styles.oCell} onClick={() => onDetail(item)}>
  169. {{
  170. title: () => (
  171. <div class={styles.oTitle}>
  172. <div>{item.name}</div>
  173. <Tag
  174. style={{ marginLeft: '6px', flexShrink: 0 }}
  175. color={item.type === 'DELIVERY' ? '#FF8057' : '#64A9FF'}
  176. >
  177. {orchestraType[item.type]}
  178. </Tag>
  179. </div>
  180. ),
  181. label: () => <p>学员人数:{item.currentStudentNum || 0}人</p>
  182. }}
  183. </Cell>
  184. ))}
  185. </List>
  186. </OFullRefresh>
  187. ) : (
  188. <OEmpty btnStatus={false} tips="暂无乐团" />
  189. )}
  190. <OActionSheet v-model:show={form.showPopover} actions={form.actions} onSelect={onSelect} />
  191. </div>
  192. )
  193. }
  194. })