index.vue 11 KB

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