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" @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="false" 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. })
  76. // 搜索
  77. const searchClick = () => {
  78. uni.navigateTo({
  79. url: '/pages_home/pages/search/index'
  80. })
  81. }
  82. const cityClick = async () => {
  83. const { latitude, longitude } = data.address
  84. const address = await chooseLocationInit(
  85. { latitude, longitude },
  86. addresstree.value
  87. )
  88. data.address = { ...data.address, ...address }
  89. console.log("TCL: settingAddress1 -> data", data)
  90. store.dispatch('handlerAddress', data.address)
  91. const token = getToken();
  92. token ? settingAddress() : getList();
  93. }
  94. const loadmoreInfo = ref({
  95. status: 'loadmore',
  96. loadingText: '努力加载中...',
  97. loadmoreText: '点击加载更多~',
  98. nomoreText: '已经到底啦~',
  99. })
  100. // 加载更多
  101. async function handleLoadmore(e) {
  102. if (
  103. pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)
  104. ) {
  105. pages.value.current += 1
  106. loadmoreInfo.value.status = 'loading'
  107. await getList()
  108. } else {
  109. loadmoreInfo.value.status = 'nomore'
  110. }
  111. }
  112. // 加载滚动文字
  113. const getNotice = async () => {
  114. const res = await systemConfig()
  115. text1.value = res.data
  116. }
  117. const getList = async () => {
  118. try {
  119. if (userType !== 1) {
  120. console.log('志愿者端');
  121. return
  122. }
  123. console.log('用户端');
  124. loadmoreInfo.value.status = 'loading'
  125. const { address } = data
  126. const { cityCode, latitude, longitude } = address
  127. const params = {
  128. pageNum: pages.value.current,
  129. pageSize: pages.value.pageSize,
  130. serviceCategory: pages.value.serviceCategory || '',
  131. businessManagementId: 0,
  132. provinceName: cityCode.data[0], // 省
  133. provinceCode: cityCode.code[0],
  134. cityName: cityCode.data[1], // 市
  135. cityCode: cityCode.code[1],
  136. districtName: cityCode.data[2],
  137. districtCode: cityCode.code[2],
  138. latitude,
  139. longitude,
  140. }
  141. const res = await volunteerinfolist(params)
  142. if (!res || !res.rows) {
  143. return
  144. }
  145. // 如果是第一页,先清空
  146. if (pages.value.current === 1) {
  147. leftList.value = [{ type: 'slideshow' }]
  148. rightList.value = []
  149. }
  150. // 每次都追加新数据
  151. res.rows.forEach((item, index) => {
  152. index % 2 !== 0 ? rightList.value.push(item) : leftList.value.push(item)
  153. })
  154. pages.value.total = res.total
  155. if (pages.value.current >= Math.ceil(res.total / pages.value.pageSize)) {
  156. loadmoreInfo.value.status = 'nomore'
  157. } else {
  158. loadmoreInfo.value.status = 'loadmore'
  159. }
  160. } catch (error) {
  161. leftList.value = []
  162. rightList.value = []
  163. loadmoreInfo.value.status = 'loadmore'
  164. pages.value.total = 0
  165. console.error('Error fetching data:', error)
  166. }
  167. }
  168. onReachBottom(() => {
  169. if (
  170. pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)
  171. ) {
  172. pages.value.current = pages.value.current + 1
  173. loadmoreInfo.value.status = 'nomore'
  174. getList()
  175. }
  176. })
  177. const getBanners = async () => {
  178. try {
  179. const res = await slideshow(21)
  180. if (res.code === 200 && res.data.picture) {
  181. list3.value = res.data.picture.split(',')
  182. }
  183. const value_res = await slideshow(111)
  184. if (value_res.code === 200 && value_res.data.picture) {
  185. ValueZone.value = value_res.data.picture.split(',')
  186. }
  187. const hot_res = await slideshow(11)
  188. if (hot_res.code === 200 && hot_res.data.picture) {
  189. hotList.value = hot_res.data.picture.split(',')
  190. }
  191. const hot_node = await slideshow(22)
  192. if (hot_node.code === 200 && hot_node.data.picture) {
  193. ValueZoneSwiper.value = hot_node.data.picture.split(',')
  194. }
  195. } catch (error) {
  196. console.log('error', error)
  197. }
  198. }
  199. const settingAddress = async () => {
  200. try {
  201. const { address } = data
  202. const { cityCode, latitude, longitude } = address
  203. const parmas = {
  204. provinceName: cityCode.data[0], // 省
  205. provinceCode: cityCode.code[0],
  206. provinceInd: cityCode.index[0],
  207. cityName: cityCode.data[1], // 市
  208. cityCode: cityCode.code[1],
  209. cityInd: cityCode.index[1],
  210. districtName: cityCode.data[2],
  211. districtCode: cityCode.code[2],
  212. districtInd: cityCode.index[2],
  213. address: address.name,
  214. latitude,
  215. longitude,
  216. }
  217. const res = await settingHomeAddress(parmas)
  218. leftList.value = [];
  219. rightList.value = [];
  220. userType === 1 && getList()
  221. } catch (error) {
  222. console.log('设置地址失败', error)
  223. }
  224. }
  225. onShow(() => {
  226. getBanners()
  227. // 在 App.vue 中初始化
  228. uni.getSystemInfo({
  229. success: (res) => {
  230. const statusBarHeight = res.statusBarHeight
  231. const navBarHeight =
  232. res.platform === 'android' ? 48 : 44 + res.statusBarHeight
  233. globalData.value = { statusBarHeight, navBarHeight }
  234. },
  235. })
  236. })
  237. const addresstree = ref([])
  238. const showAddressBg = ref(false)
  239. // Handle page scroll event
  240. onPageScroll(e => {
  241. // Show background when scrolled past threshold (middle of carousel, around 200px)
  242. showAddressBg.value = e.scrollTop > 180
  243. })
  244. onMounted(async () => {
  245. try {
  246. console.log("TCL: uni.$u",)
  247. getNotice()
  248. const token = getToken();
  249. const res_dara = await regionAddresstree()
  250. addresstree.value = res_dara.data
  251. if (token) {
  252. locateTheCurrentAddress(res_dara.data)
  253. .then((res) => {
  254. data.address = res
  255. console.log("TCL: settingAddress0 -> data", data)
  256. settingAddress()
  257. })
  258. .catch((error) => {
  259. console.log('获取地址失败!', error)
  260. getList()
  261. })
  262. } else {
  263. getList();
  264. }
  265. } catch (error) {
  266. getList();
  267. }
  268. })
  269. </script>
  270. <style scoped lang="scss">
  271. // 首页顶部区域-轮播图
  272. .home-banner {
  273. position: fixed;
  274. z-index: 130;
  275. // top: v-bind('globalData.statusBarHeight + 10 + "px"');
  276. left: 0;
  277. height: 90px;
  278. width: 100%;
  279. padding: 10rpx 0;
  280. transition: background-color 0.3s ease;
  281. display: flex;
  282. flex-direction: column;
  283. justify-content: center;
  284. &.home-banner-with-bg {
  285. background: #FAFBF0;
  286. // background: red;
  287. height: 90px;
  288. display: flex;
  289. flex-direction: column;
  290. justify-content: center;
  291. }
  292. .home-banner-left {
  293. display: flex;
  294. align-items: center;
  295. box-sizing: border-box;
  296. margin-top: 80rpx;
  297. width: 516rpx;
  298. height: 64rpx;
  299. border-radius: 36rpx;
  300. background: rgba(255, 255, 255, 0.56);
  301. margin-left: 16rpx;
  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>