index.vue 9.9 KB

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