index.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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 { useRect } from '@vant/use'
  23. import {
  24. listenerMessage,
  25. postMessage,
  26. removeListenerMessage
  27. } from '@/helpers/native-message'
  28. import ColSearch from '@/components/col-search'
  29. import { browser, setAuth } from '@/helpers/utils'
  30. import { cartCount, getCartCount } from './shop-mall'
  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. if (!document.hidden) {
  49. getCartCount()
  50. }
  51. })
  52. } else {
  53. listenerMessage('UpdateToken', result => {
  54. if (result?.content.token) {
  55. setAuth(result?.content.token)
  56. }
  57. getCartCount()
  58. })
  59. }
  60. },
  61. unmounted() {
  62. removeListenerMessage('UpdateToken', () => {})
  63. },
  64. methods: {
  65. onSearch() {},
  66. async init() {
  67. try {
  68. const res = await request.get('/api-mall-portal/home/content')
  69. console.log(res)
  70. const result = res.data || {}
  71. this.count = result.count
  72. cartCount.value = result.count
  73. this.advertiseList = result.advertiseList
  74. const category = result.productCategoryList || []
  75. const categoryResult: any = []
  76. while (category.length > 0) {
  77. const chunk = category.splice(0, 5)
  78. categoryResult.push(chunk)
  79. }
  80. this.productList = categoryResult
  81. this.hotProductList = result.hotProductList || []
  82. this.productCategoryList = result.productAttributeCategoryList || []
  83. } catch {}
  84. setTimeout(() => {
  85. this.loading = false
  86. }, 500)
  87. },
  88. openWebView(url: string): void {
  89. // console.log(url)
  90. let dev = false
  91. let origin = location.origin + location.pathname + '#'
  92. if (!url) return
  93. if (url.indexOf('http') < 0) {
  94. url = origin + url
  95. }
  96. console.log('跳转url:', url)
  97. if (!browser().isApp) {
  98. location.href = url
  99. return
  100. }
  101. postMessage({
  102. api: 'openWebView',
  103. content: {
  104. url: url,
  105. orientation: 1,
  106. isHideTitle: false
  107. }
  108. })
  109. }
  110. },
  111. render() {
  112. return (
  113. <div>
  114. <div ref="headers">
  115. <ColHeader
  116. border={false}
  117. background="linear-gradient(#59e5d5, 30%, #f6f8f9)"
  118. onClickRight={() => this.openWebView('/cart')}
  119. v-slots={{
  120. right: () => (
  121. <div class={styles['icon-shop-cart']}>
  122. <Badge
  123. class={styles.iconBadge}
  124. showZero={false}
  125. offset={[-8, 8]}
  126. color="rgba(236,92,50,1)"
  127. content={cartCount.value}
  128. >
  129. <Icon name={iconShopCart} size={30} />
  130. </Badge>
  131. </div>
  132. )
  133. }}
  134. onHeaderBack={() => {
  135. this.$nextTick(() => {
  136. const { height } = useRect((this as any).$refs.headers)
  137. this.height = height - 1
  138. })
  139. }}
  140. />
  141. </div>
  142. <PullRefresh
  143. v-model={this.loading}
  144. loading-text="正在刷新..."
  145. success-text="刷新完成"
  146. onRefresh={() => this.init()}
  147. >
  148. <div
  149. onClick={() => {
  150. this.openWebView('/goodsList?input=focus')
  151. }}
  152. >
  153. <ColSearch
  154. disabled
  155. class={styles.searchBox}
  156. background="transparent"
  157. />
  158. </div>
  159. <Swipe
  160. class={styles.swipe}
  161. autoplay={3000}
  162. showIndicators={false}
  163. lazyRender
  164. >
  165. {this.advertiseList.map((item: any) => (
  166. <SwipeItem onClick={() => this.openWebView(item.url)}>
  167. <Image class={styles.swipeItemImg} src={item.pic} fit="fill" />
  168. </SwipeItem>
  169. ))}
  170. </Swipe>
  171. <MenuList
  172. productList={this.productList}
  173. onOpenWebView={this.openWebView}
  174. />
  175. {this.hotProductList.length === 3 && (
  176. <HotGoods
  177. hotProductList={this.hotProductList}
  178. onOpenWebView={this.openWebView}
  179. />
  180. )}
  181. <Tabs
  182. shrink
  183. lineWidth={25}
  184. background={'#f6f8f9'}
  185. color="var(--van-primary)"
  186. class={styles.tabs}
  187. sticky
  188. offsetTop={this.height}
  189. lazyRender
  190. >
  191. {this.productCategoryList.length > 0 && (
  192. <Tab title="全部" name={0}>
  193. <TabList
  194. isTab={true}
  195. productAttributeCategoryId={0}
  196. onOpenWebView={this.openWebView}
  197. />
  198. </Tab>
  199. )}
  200. {this.productCategoryList.map((item: any) => (
  201. <Tab title={item.name} name={item.id}>
  202. <TabList
  203. isTab={true}
  204. productAttributeCategoryId={item.id}
  205. onOpenWebView={this.openWebView}
  206. />
  207. </Tab>
  208. ))}
  209. </Tabs>
  210. </PullRefresh>
  211. </div>
  212. )
  213. }
  214. })