index.vue 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  1. <template>
  2. <view>
  3. <!-- 首页顶部定位区域 -->
  4. <view class="home-banner" :class="{ 'home-banner-with-bg': showAddressBg }">
  5. <view class="home-banner-left" @click="cityClick">
  6. <img src="@/static/uview/common/map-pin-fill@1x.png" alt="" class="map-pin-fill">
  7. <text class="address-text">{{ data.address.name }}</text>
  8. </view>
  9. </view>
  10. <view class="home-main">
  11. <view class="custom-swiper" v-if="list3.length > 0">
  12. <up-swiper :list="list3" indicator indicatorMode="line" circular :height="'554rpx'"></up-swiper>
  13. </view>
  14. </view>
  15. <view class="home-Wrapper">
  16. <view class="home-grid">
  17. <Client />
  18. </view>
  19. <view class="home-swiper" v-if="userType == 1">
  20. <view v-if="ValueZone.length > 0">
  21. <up-swiper :list="ValueZone" :indicator="false" indicatorMode="line" circular></up-swiper>
  22. </view>
  23. <view>
  24. <up-notice-bar :text="text1" class="notice-bar"></up-notice-bar>
  25. </view>
  26. </view>
  27. <view class="home-ranking" :class="{ 'no-margin': userType === 2 }">
  28. <ServIces :leftList="leftList" :rightList="rightList" :ValueZoneSwiper="ValueZoneSwiper" v-if="userType == 1">
  29. </ServIces>
  30. <RankingList v-if="userType === 2" />
  31. </view>
  32. <up-loadmore style="margin-top: 40rpx" :status="loadmoreInfo.status" :loadmoreText="loadmoreInfo.loadingText"
  33. :loadingText="loadmoreInfo.loadmoreText" :nomoreText="loadmoreInfo.nomoreText" @loadmore="handleLoadmore"
  34. v-if="userType == 1" />
  35. </view>
  36. <custom-tab-bar page="home" class="customTabBar" />
  37. <view class="safe-area-bottom"></view>
  38. </view>
  39. </template>
  40. <script setup>
  41. import { ref, reactive, onMounted } from 'vue'
  42. import { onShow, onReachBottom, onUnload, onPageScroll } from '@dcloudio/uni-app'
  43. import RankingList from '@/pages/common/rankingList/index.vue'
  44. import ServIces from '@/components/Services/services.vue'
  45. import { Client } from '@/components/Client/new_file.vue'
  46. import { volunteerinfolist, systemConfig } from '@/api/volunteerDetailsApi/details.js'
  47. import { slideshow, settingHomeAddress } from '@/api/home.js'
  48. import { chooseLocationInit, locateTheCurrentAddress } from '@/utils/adress'
  49. import CustomTabBar from '@/components/CustomTabBar/index.vue'
  50. import { regionAddresstree } from '@/api/home.js'
  51. import { getToken } from '@/utils/auth'
  52. import store from '@/store'
  53. const rightList = ref([])
  54. const leftList = ref([])
  55. const list3 = ref(['']) // Initialize with at least one empty item
  56. const ValueZone = ref(['']) // Initialize with at least one empty item
  57. const hotList = ref([])
  58. const ValueZoneSwiper = ref([''])
  59. const userType = uni.getStorageSync('userType') || 1; //读取本地存储
  60. const text1 = ref('');
  61. const globalData = ref({
  62. statusBarHeight: 47,
  63. navBarHeight: 91,
  64. })
  65. const data = reactive({
  66. address: store.state.user.addressInfo,
  67. queryValue: '',
  68. })
  69. // 分页
  70. const pages = ref({
  71. current: 1,
  72. pageSize: 10,
  73. total: 0,
  74. serviceCategory: '',
  75. // appStatus:"2",
  76. })
  77. const cityClick = async () => {
  78. const { latitude, longitude } = data.address
  79. const address = await chooseLocationInit(
  80. { latitude, longitude },
  81. addresstree.value
  82. )
  83. data.address = { ...data.address, ...address }
  84. console.log("TCL: settingAddress1 -> data", data)
  85. store.dispatch('handlerAddress', data.address)
  86. const token = getToken();
  87. token ? settingAddress():getList();
  88. }
  89. const loadmoreInfo = ref({
  90. status: 'loadmore',
  91. loadingText: '努力加载中...',
  92. loadmoreText: '点击加载更多~',
  93. nomoreText: '已经到底啦~',
  94. })
  95. // 加载更多
  96. async function handleLoadmore(e) {
  97. if (
  98. pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)
  99. ) {
  100. pages.value.current += 1
  101. loadmoreInfo.value.status = 'loading'
  102. await getList()
  103. } else {
  104. loadmoreInfo.value.status = 'nomore'
  105. }
  106. }
  107. // 加载滚动文字
  108. const getNotice = async () => {
  109. const res = await systemConfig()
  110. text1.value = res.data
  111. }
  112. const getList = async () => {
  113. try {
  114. loadmoreInfo.value.status = 'loading'
  115. const { address } = data
  116. const { cityCode, latitude, longitude } = address
  117. const params = {
  118. pageNum: pages.value.current,
  119. pageSize: pages.value.pageSize,
  120. serviceCategory: pages.value.serviceCategory || '',
  121. businessManagementId: 0,
  122. provinceName: cityCode.data[0], // 省
  123. provinceCode: cityCode.code[0],
  124. cityName: cityCode.data[1], // 市
  125. cityCode: cityCode.code[1],
  126. districtName: cityCode.data[2],
  127. districtCode: cityCode.code[2],
  128. latitude,
  129. longitude,
  130. }
  131. const res = await volunteerinfolist(params)
  132. if (!res || !res.rows) {
  133. return
  134. }
  135. // 如果是第一页,先清空
  136. if (pages.value.current === 1) {
  137. leftList.value = [{ type: 'slideshow' }]
  138. rightList.value = []
  139. }
  140. // 每次都追加新数据
  141. res.rows.forEach((item, index) => {
  142. index % 2 !== 0 ? rightList.value.push(item) : leftList.value.push(item)
  143. })
  144. pages.value.total = res.total
  145. if (pages.value.current >= Math.ceil(res.total / pages.value.pageSize)) {
  146. loadmoreInfo.value.status = 'nomore'
  147. } else {
  148. loadmoreInfo.value.status = 'loadmore'
  149. }
  150. } catch (error) {
  151. leftList.value = []
  152. rightList.value = []
  153. loadmoreInfo.value.status = 'loadmore'
  154. pages.value.total = 0
  155. console.error('Error fetching data:', error)
  156. }
  157. }
  158. onReachBottom(() => {
  159. if (
  160. pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)
  161. ) {
  162. pages.value.current = pages.value.current + 1
  163. loadmoreInfo.value.status = 'nomore'
  164. getList()
  165. }
  166. })
  167. const getBanners = async () => {
  168. try {
  169. const res = await slideshow(21)
  170. if (res.code === 200 && res.data.picture) {
  171. list3.value = res.data.picture.split(',')
  172. }
  173. const value_res = await slideshow(20)
  174. if (value_res.code === 200 && value_res.data.picture) {
  175. ValueZone.value = value_res.data.picture.split(',')
  176. }
  177. const hot_res = await slideshow(11)
  178. if (hot_res.code === 200 && hot_res.data.picture) {
  179. hotList.value = hot_res.data.picture.split(',')
  180. }
  181. const hot_node = await slideshow(22)
  182. if (hot_node.code === 200 && hot_node.data.picture) {
  183. ValueZoneSwiper.value = hot_node.data.picture.split(',')
  184. }
  185. } catch (error) {
  186. console.log('error', error)
  187. }
  188. }
  189. const settingAddress = async () => {
  190. try {
  191. const { address } = data
  192. const { cityCode, latitude, longitude } = address
  193. const parmas = {
  194. provinceName: cityCode.data[0], // 省
  195. provinceCode: cityCode.code[0],
  196. provinceInd: cityCode.index[0],
  197. cityName: cityCode.data[1], // 市
  198. cityCode: cityCode.code[1],
  199. cityInd: cityCode.index[1],
  200. districtName: cityCode.data[2],
  201. districtCode: cityCode.code[2],
  202. districtInd: cityCode.index[2],
  203. address: address.name,
  204. latitude,
  205. longitude,
  206. }
  207. const res = await settingHomeAddress(parmas)
  208. leftList.value = [];
  209. rightList.value = [];
  210. userType === 1 && getList()
  211. } catch (error) {
  212. console.log('设置地址失败', error)
  213. }
  214. }
  215. onShow(() => {
  216. getBanners()
  217. // 在 App.vue 中初始化
  218. uni.getSystemInfo({
  219. success: (res) => {
  220. const statusBarHeight = res.statusBarHeight
  221. const navBarHeight =
  222. res.platform === 'android' ? 48 : 44 + res.statusBarHeight
  223. globalData.value = { statusBarHeight, navBarHeight }
  224. },
  225. })
  226. })
  227. const addresstree = ref([])
  228. const showAddressBg = ref(false)
  229. // Handle page scroll event
  230. onPageScroll(e => {
  231. // Show background when scrolled past threshold (middle of carousel, around 200px)
  232. showAddressBg.value = e.scrollTop > 180
  233. })
  234. onMounted(async () => {
  235. getNotice()
  236. const token = getToken();
  237. const res_dara = await regionAddresstree()
  238. addresstree.value = res_dara.data
  239. if (token) {
  240. locateTheCurrentAddress(res_dara.data)
  241. .then((res) => {
  242. data.address = res
  243. console.log("TCL: settingAddress0 -> data", data)
  244. settingAddress()
  245. })
  246. .catch((error) => {
  247. console.log('获取地址失败!', error)
  248. getList()
  249. })
  250. } else {
  251. getList();
  252. }
  253. })
  254. onUnload(() => { })
  255. </script>
  256. <style scoped lang="scss">
  257. // 首页顶部区域-轮播图
  258. .home-banner {
  259. position: fixed;
  260. z-index: 130;
  261. // top: v-bind('globalData.statusBarHeight + 10 + "px"');
  262. left: 0;
  263. height: 90px;
  264. width: 100%;
  265. padding: 10rpx 0;
  266. transition: background-color 0.3s ease;
  267. display: flex;
  268. flex-direction: column;
  269. justify-content: center;
  270. &.home-banner-with-bg {
  271. // background-color: rgba(255, 255, 255, 0.9);
  272. // box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  273. background: #FAFBF0;
  274. height: 90px;
  275. display: flex;
  276. flex-direction: column;
  277. justify-content: center;
  278. }
  279. .home-banner-left {
  280. display: flex;
  281. align-items: center;
  282. padding-left: 10rpx;
  283. box-sizing: border-box;
  284. margin-top: 80rpx;
  285. .map-pin-fill {
  286. width: 34rpx;
  287. height: 34rpx;
  288. }
  289. .address-text {
  290. width: 178rpx;
  291. height: 42rpx;
  292. font-family: PingFang SC;
  293. font-size: 28rpx;
  294. font-weight: normal;
  295. line-height: 40rpx;
  296. letter-spacing: normal;
  297. color: #000000;
  298. max-width: 120rpx;
  299. white-space: nowrap;
  300. overflow: hidden;
  301. text-overflow: ellipsis;
  302. }
  303. }
  304. }
  305. // 分类下轮播图
  306. .home-swiper {
  307. margin-top: 40rpx;
  308. margin-right: 23rpx;
  309. margin-left: 23rpx;
  310. width: 704rpx;
  311. height: 180rpx;
  312. border-radius: 16rpx;
  313. background: linear-gradient(180deg, #FEF9ED 0%, #FFF9F3 100%);
  314. .notice-bar {
  315. margin-top: 10rpx;
  316. }
  317. }
  318. // 瀑布流
  319. .home-ranking {
  320. margin-top: 160rpx;
  321. margin-left: 11rpx;
  322. margin-right: 17rpx;
  323. margin-bottom: 120rpx;
  324. &.no-margin {
  325. margin-top: 0;
  326. }
  327. }
  328. // 轮播图下的圆角样式
  329. .home-Wrapper {
  330. position: absolute;
  331. top: 542rpx;
  332. width: 100%;
  333. border-radius: 20rpx 20rpx 0rpx 0rpx;
  334. overflow: hidden;
  335. background: #FFFFFF;
  336. }
  337. /* 底部安全区域 */
  338. .safe-area-bottom {
  339. height: 150rpx;
  340. width: 100%;
  341. }
  342. </style>