index.tsx 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. import ColHeader from '@/components/col-header'
  2. import {
  3. Badge,
  4. Dialog,
  5. Divider,
  6. Icon,
  7. Image,
  8. PullRefresh,
  9. Sticky,
  10. Swipe,
  11. SwipeItem,
  12. Tab,
  13. Tabs
  14. } from 'vant'
  15. import { defineComponent } from 'vue'
  16. import HotGoods from './components/hot-goods'
  17. import MenuList from './components/menu-list'
  18. import TabList from './components/tab-list'
  19. import styles from './index.module.less'
  20. import iconShopCart from './images/icon-shop-cart.png'
  21. import request from '@/helpers/request'
  22. import {
  23. listenerMessage,
  24. postMessage,
  25. removeListenerMessage
  26. } from '@/helpers/native-message'
  27. import ColSearch from '@/components/col-search'
  28. import { browser, setAuth } from '@/helpers/utils'
  29. import { cartCount, getCartCount } from './shop-mall'
  30. import TheHomeHeader from './components/TheHomeHeader'
  31. export default defineComponent({
  32. name: 'shop-mall',
  33. data() {
  34. return {
  35. loading: false,
  36. height: 'auto' as any,
  37. count: 0, // 购买车数量
  38. advertiseList: [], // 广告列表
  39. productList: [], // 商品分类
  40. hotProductList: [], // 热门商品列表
  41. productCategoryList: [] // 商品分类列表
  42. }
  43. },
  44. mounted() {
  45. this.init()
  46. // if (browser().ios) {
  47. document.addEventListener('visibilitychange', event => {
  48. postMessage({ api: 'setBarStatus', content: { status: 1 } })
  49. if (!document.hidden) {
  50. getCartCount()
  51. const tabFirst = this.$refs.tabFirst as any
  52. tabFirst?.onSearch()
  53. }
  54. })
  55. // }
  56. // else {
  57. // listenerMessage('UpdateToken', result => {
  58. // if (result?.content.token) {
  59. // setAuth(result?.content.token)
  60. // }
  61. // getCartCount()
  62. // const tabFirst = this.$refs.tabFirst as any
  63. // tabFirst?.onSearch()
  64. // })
  65. // }
  66. },
  67. unmounted() {
  68. // removeListenerMessage('UpdateToken', () => {})
  69. },
  70. methods: {
  71. onRefresh() {
  72. this.productCategoryList = []
  73. this.$nextTick(() => {
  74. this.init()
  75. })
  76. const tabFirst = this.$refs.tabFirst as any
  77. tabFirst?.onSearch()
  78. },
  79. async init() {
  80. try {
  81. const res = await request.get('/api-mall-portal/home/content')
  82. const result = res.data || {}
  83. this.count = result.count
  84. cartCount.value = result.count
  85. this.advertiseList = result.advertiseList
  86. const category = result.productCategoryList || []
  87. const categoryResult: any = []
  88. while (category.length > 0) {
  89. const chunk = category.splice(0, 5)
  90. categoryResult.push(chunk)
  91. }
  92. this.productList = categoryResult
  93. this.hotProductList = result.hotProductList || []
  94. this.productCategoryList = result.productAttributeCategoryList || []
  95. } catch {}
  96. setTimeout(() => {
  97. this.loading = false
  98. }, 500)
  99. },
  100. openWebView(url: string): void {
  101. try {
  102. this.$router.push(url)
  103. } catch (error) {}
  104. }
  105. },
  106. render() {
  107. return (
  108. <div class={styles.shopMall}>
  109. <TheHomeHeader
  110. onCart={() => this.openWebView('/cart')}
  111. onSearch={() => this.openWebView('/goodsList?input=focus')}
  112. onMore={action => {
  113. if (!action) return
  114. this.openWebView(action.url)
  115. }}
  116. />
  117. <PullRefresh
  118. v-model={this.loading}
  119. loading-text="正在刷新..."
  120. success-text="刷新完成"
  121. onRefresh={() => this.onRefresh()}
  122. >
  123. <div class={styles.hotContent}>
  124. <Swipe class={styles.swipe} autoplay={3000} showIndicators={false}>
  125. {this.advertiseList.map((item: any) => (
  126. <SwipeItem onClick={() => this.openWebView(item.url)}>
  127. <Image
  128. class={styles.swipeItemImg}
  129. src={item.pic}
  130. fit="fill"
  131. />
  132. </SwipeItem>
  133. ))}
  134. </Swipe>
  135. <MenuList
  136. productList={this.productList}
  137. onOpenWebView={this.openWebView}
  138. />
  139. </div>
  140. {this.hotProductList.length === 3 && (
  141. <HotGoods
  142. hotProductList={this.hotProductList}
  143. onOpenWebView={this.openWebView}
  144. />
  145. )}
  146. <Tabs
  147. shrink
  148. lineWidth={25}
  149. background={'#f6f8f9'}
  150. color="var(--van-primary)"
  151. class={styles.tabs}
  152. sticky
  153. offsetTop={this.height}
  154. lazyRender
  155. >
  156. <Tab title="全部" name={0}>
  157. <TabList
  158. ref="tabFirst"
  159. isTab={true}
  160. showAdd={false}
  161. productAttributeCategoryId={0}
  162. onOpenWebView={this.openWebView}
  163. />
  164. </Tab>
  165. {this.productCategoryList.map((item: any) => (
  166. <Tab title={item.name} name={item.id}>
  167. <TabList
  168. isTab={true}
  169. showAdd={false}
  170. productAttributeCategoryId={item.id}
  171. onOpenWebView={this.openWebView}
  172. />
  173. </Tab>
  174. ))}
  175. </Tabs>
  176. </PullRefresh>
  177. </div>
  178. )
  179. }
  180. })