index.vue 11 KB

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