index.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  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. store.dispatch('handlerAddress',data.address)
  83. settingAddress();
  84. console.log('address', address)
  85. }
  86. const loadmoreInfo = ref({
  87. status: 'loadmore',
  88. loadingText: '努力加载中...',
  89. loadmoreText: '点击加载更多~',
  90. nomoreText: '已经到底啦~',
  91. })
  92. // 加载更多
  93. async function handleLoadmore(e) {
  94. if (
  95. pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)
  96. ) {
  97. pages.value.current += 1
  98. loadmoreInfo.value.status = 'loading'
  99. await getList()
  100. } else {
  101. loadmoreInfo.value.status = 'nomore'
  102. }
  103. }
  104. const getList = async () => {
  105. try {
  106. loadmoreInfo.value.status = 'loading'
  107. const { address } = data
  108. const { cityCode, latitude, longitude } = address
  109. const params = {
  110. pageNum: pages.value.current,
  111. pageSize: pages.value.pageSize,
  112. serviceCategory: pages.value.serviceCategory || '',
  113. businessManagementId: 0,
  114. provinceName: cityCode.data[0], // 省
  115. provinceCode: cityCode.code[0],
  116. cityName: cityCode.data[1], // 市
  117. cityCode: cityCode.code[1],
  118. districtName: cityCode.data[2],
  119. districtCode: cityCode.code[2],
  120. latitude,
  121. longitude,
  122. }
  123. console.log('volunteerinfolist ==>',params);
  124. const res = await volunteerinfolist(params)
  125. if (!res || !res.rows) {
  126. return
  127. }
  128. console.log(
  129. '判断是否已经到了最后一页',
  130. pages.value.current >= Math.ceil(res.total / pages.value.pageSize)
  131. )
  132. // 如果是第一页,先清空
  133. if (pages.value.current === 1) {
  134. leftList.value = [{ type: 'slideshow' }]
  135. rightList.value = []
  136. }
  137. // 每次都追加新数据
  138. res.rows.forEach((item, index) => {
  139. index % 2 !== 0 ? rightList.value.push(item) : leftList.value.push(item)
  140. })
  141. console.log('home', leftList.value, rightList.value);
  142. pages.value.total = res.total
  143. if (pages.value.current >= Math.ceil(res.total / pages.value.pageSize)) {
  144. loadmoreInfo.value.status = 'nomore'
  145. } else {
  146. loadmoreInfo.value.status = 'loadmore'
  147. }
  148. } catch (error) {
  149. leftList.value = []
  150. rightList.value = []
  151. loadmoreInfo.value.status = 'loadmore'
  152. pages.value.total = 0
  153. console.error('Error fetching data:', error)
  154. }
  155. }
  156. onReachBottom(() => {
  157. if (
  158. pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)
  159. ) {
  160. pages.value.current = pages.value.current + 1
  161. loadmoreInfo.value.status = 'nomore'
  162. getList()
  163. }
  164. })
  165. const getBanners = async () => {
  166. try {
  167. const res = await slideshow(21)
  168. if (res.code === 200 && res.data.picture) {
  169. list3.value = res.data.picture.split(',')
  170. }
  171. const value_res = await slideshow(20)
  172. if (value_res.code === 200 && value_res.data.picture) {
  173. ValueZone.value = value_res.data.picture.split(',')
  174. }
  175. const hot_res = await slideshow(11)
  176. if (hot_res.code === 200 && hot_res.data.picture) {
  177. hotList.value = hot_res.data.picture.split(',')
  178. }
  179. const hot_node = await slideshow(22)
  180. if (hot_node.code === 200 && hot_node.data.picture) {
  181. ValueZoneSwiper.value = hot_node.data.picture.split(',')
  182. }
  183. } catch (error) {
  184. console.log('error', error)
  185. }
  186. }
  187. const settingAddress = async () => {
  188. try {
  189. console.log('data', data)
  190. const { address } = data
  191. const { cityCode, latitude, longitude } = address
  192. const parmas = {
  193. provinceName: cityCode.data[0], // 省
  194. provinceCode: cityCode.code[0],
  195. provinceInd: cityCode.index[0],
  196. cityName: cityCode.data[1], // 市
  197. cityCode: cityCode.code[1],
  198. cityInd: cityCode.index[1],
  199. districtName: cityCode.data[2],
  200. districtCode: cityCode.code[2],
  201. districtInd: cityCode.index[2],
  202. address: address.name,
  203. latitude,
  204. longitude,
  205. }
  206. console.log("TCL: settingAddress -> parmas", parmas);
  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. const token = getToken();
  236. // console.log("TCL: token", token || 'null')
  237. if (token) {
  238. const res_dara = await regionAddresstree()
  239. addresstree.value = res_dara.data
  240. locateTheCurrentAddress(res_dara.data)
  241. .then((res) => {
  242. console.log('locateTheCurrentAddress', res)
  243. data.address = res
  244. settingAddress()
  245. })
  246. .catch((error) => {
  247. console.log('获取地址失败!', error)
  248. getList()
  249. })
  250. } else {
  251. //设置默认角色
  252. uni.setStorageSync('userType', 1);
  253. getList();
  254. }
  255. })
  256. onUnload(() => { })
  257. </script>
  258. <style scoped lang="scss">
  259. // 首页顶部区域-轮播图
  260. .home-banner {
  261. position: fixed;
  262. z-index: 130;
  263. // top: v-bind('globalData.statusBarHeight + 10 + "px"');
  264. left: 0;
  265. height: 90px;
  266. width: 100%;
  267. padding: 10rpx 0;
  268. transition: background-color 0.3s ease;
  269. display: flex;
  270. flex-direction: column;
  271. justify-content: center;
  272. &.home-banner-with-bg {
  273. // background-color: rgba(255, 255, 255, 0.9);
  274. // box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  275. background: #FAFBF0;
  276. height: 90px;
  277. display: flex;
  278. flex-direction: column;
  279. justify-content: center;
  280. }
  281. .home-banner-left {
  282. display: flex;
  283. align-items: center;
  284. padding-left: 10rpx;
  285. box-sizing: border-box;
  286. margin-top: 80rpx;
  287. .map-pin-fill {
  288. width: 34rpx;
  289. height: 34rpx;
  290. }
  291. .address-text {
  292. width: 178rpx;
  293. height: 42rpx;
  294. font-family: PingFang SC;
  295. font-size: 28rpx;
  296. font-weight: normal;
  297. line-height: 40rpx;
  298. letter-spacing: normal;
  299. color: #000000;
  300. max-width: 120rpx;
  301. white-space: nowrap;
  302. overflow: hidden;
  303. text-overflow: ellipsis;
  304. }
  305. }
  306. }
  307. // 分类下轮播图
  308. .home-swiper {
  309. margin-top: 40rpx;
  310. margin-right: 23rpx;
  311. margin-left: 23rpx;
  312. width: 704rpx;
  313. height: 180rpx;
  314. border-radius: 16rpx;
  315. background: linear-gradient(180deg, #FEF9ED 0%, #FFF9F3 100%);
  316. }
  317. // 瀑布流
  318. .home-ranking {
  319. margin-top: 93rpx;
  320. margin-left: 11rpx;
  321. margin-right: 17rpx;
  322. margin-bottom: 120rpx;
  323. &.no-margin {
  324. margin-top: 0;
  325. }
  326. }
  327. // 轮播图下的圆角样式
  328. .home-Wrapper{
  329. position: absolute;
  330. top: 542rpx;
  331. width: 100%;
  332. border-radius: 20rpx 20rpx 0rpx 0rpx;
  333. overflow: hidden;
  334. background: #FFF9F3;
  335. }
  336. /* 底部安全区域 */
  337. .safe-area-bottom {
  338. height: 150rpx;
  339. width: 100%;
  340. }
  341. </style>