index.vue 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. <template>
  2. <view>
  3. <!-- 首页顶部定位区域 -->
  4. <view class="home-banner"
  5. :class="{ 'home-banner-with-bg': showAddressBg }"
  6. >
  7. <view class="home-banner-left" @click="cityClick">
  8. <img src="@/static/uview/common/map-pin-fill@1x.png" alt="" class="map-pin-fill">
  9. <text class="address-text">{{ data.address.name }}</text>
  10. </view>
  11. </view>
  12. <view class="home-main">
  13. <view class="custom-swiper" v-if="list3.length > 0">
  14. <up-swiper :list="list3" indicator indicatorMode="line" circular :height="'554rpx'"></up-swiper>
  15. </view>
  16. </view>
  17. <view class="Wrapper-Top">
  18. <view class="home-grid">
  19. <Client />
  20. </view>
  21. </view>
  22. <view class="home-swiper">
  23. <view v-if="ValueZone.length > 0">
  24. <up-swiper :list="ValueZone" :indicator="false" indicatorMode="line" circular></up-swiper>
  25. </view>
  26. </view>
  27. <view class="home-ranking">
  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 class="Wrapper">
  36. <view class="home-g-bgc">
  37. <Client />
  38. </view>
  39. <view class="hot-swiper" v-if="ValueZone.length > 0">
  40. <up-swiper :list="ValueZone" :indicator="false" indicatorMode="line" circular></up-swiper>
  41. </view>
  42. <view class="home-ranking">
  43. <ServIces :leftList="leftList" :rightList="rightList" v-if="userType == 1"></ServIces>
  44. <RankingList v-if="userType === 2" />
  45. </view>
  46. <custom-tab-bar page="home" class="customTabBar" />
  47. <view class="safe-area-bottom"></view>
  48. </view>
  49. </view>
  50. </template>
  51. <script setup>
  52. import { ref, reactive, onMounted } from 'vue'
  53. import { provide } from 'vue'
  54. import { onLoad, onShow, onReachBottom, onUnload, onPageScroll } from '@dcloudio/uni-app'
  55. import RankingList from '@/pages/common/rankingList/index.vue'
  56. import ServIces from '@/components/Services/services.vue'
  57. import { Client } from '@/components/Client/new_file.vue'
  58. import { volunteerinfolist } from '@/api/volunteerDetailsApi/details.js'
  59. import { slideshow, settingHomeAddress } from '@/api/home.js'
  60. import { useDict } from '@/utils/dict.js'
  61. import { chooseLocationInit, locateTheCurrentAddress } from '@/utils/adress'
  62. import CustomTabBar from '@/components/CustomTabBar/index.vue'
  63. import { regionAddresstree } from '@/api/home.js'
  64. import { getToken } from '@/utils/auth'
  65. const rightList = ref([])
  66. const leftList = ref([])
  67. const list3 = ref(['']) // Initialize with at least one empty item
  68. const ValueZone = ref(['']) // Initialize with at least one empty item
  69. const hotList = ref([])
  70. const ValueZoneSwiper = ref([''])
  71. const userType = uni.getStorageSync('userType'); //读取本地存储
  72. const globalData = ref({
  73. statusBarHeight: 47,
  74. navBarHeight: 91,
  75. })
  76. const data = reactive({
  77. address: {
  78. name: '重庆市',
  79. latitude: 29.333000000000002,
  80. longitude: 105.94909000000001,
  81. cityCode: {
  82. code: [500000, 500100, 500118],
  83. data: ['重庆市', '重庆市', '永川区'],
  84. index: [21, 0, 17],
  85. },
  86. },
  87. queryValue: '',
  88. })
  89. // 分页
  90. const pages = ref({
  91. current: 1,
  92. pageSize: 10,
  93. total: 0,
  94. serviceCategory: '',
  95. // appStatus:"2",
  96. })
  97. const cityClick = async () => {
  98. const { latitude, longitude } = data.address
  99. const address = await chooseLocationInit(
  100. { latitude, longitude },
  101. addresstree.value
  102. )
  103. console.log('TCL: cityClick -> address', address)
  104. data.address = { ...data.address, ...address }
  105. settingAddress()
  106. console.log('address', address)
  107. }
  108. const loadmoreInfo = ref({
  109. status: 'loadmore',
  110. loadingText: '努力加载中...',
  111. loadmoreText: '点击加载更多~',
  112. nomoreText: '已经到底啦~',
  113. })
  114. // 加载更多
  115. async function handleLoadmore(e) {
  116. if (
  117. pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)
  118. ) {
  119. pages.value.current += 1
  120. loadmoreInfo.value.status = 'loading'
  121. await getList()
  122. } else {
  123. loadmoreInfo.value.status = 'nomore'
  124. }
  125. }
  126. const getList = async () => {
  127. try {
  128. loadmoreInfo.value.status = 'loading'
  129. const params = {
  130. pageNum: pages.value.current,
  131. pageSize: pages.value.pageSize,
  132. serviceCategory: pages.value.serviceCategory || '',
  133. businessManagementId: 0,
  134. // appStatus:pages.value.appStatus,
  135. }
  136. const res = await volunteerinfolist(params)
  137. if (!res || !res.rows) {
  138. return
  139. }
  140. console.log(
  141. '判断是否已经到了最后一页',
  142. pages.value.current >= Math.ceil(res.total / pages.value.pageSize)
  143. )
  144. // 如果是第一页,先清空
  145. if (pages.value.current === 1) {
  146. leftList.value = [{ type: 'slideshow' }]
  147. rightList.value = []
  148. }
  149. // 每次都追加新数据
  150. res.rows.forEach((item, index) => {
  151. index % 2 !== 0 ? rightList.value.push(item) : leftList.value.push(item)
  152. })
  153. console.log('home', leftList.value, rightList.value);
  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(20)
  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. console.log('data', data)
  202. const { address } = data
  203. const { cityCode, latitude, longitude } = address
  204. const parmas = {
  205. provinceName: cityCode.data[0], // 省
  206. provinceCode: cityCode.code[0],
  207. provinceInd: cityCode.index[0],
  208. cityName: cityCode.data[1], // 市
  209. cityCode: cityCode.code[1],
  210. cityInd: cityCode.index[1],
  211. districtName: cityCode.data[2],
  212. districtCode: cityCode.code[2],
  213. districtInd: cityCode.index[2],
  214. address: address.name,
  215. latitude,
  216. longitude,
  217. }
  218. console.log("TCL: settingAddress -> parmas", parmas);
  219. const res = await settingHomeAddress(parmas)
  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. const token = getToken();
  246. console.log("TCL: token", token || 'null')
  247. if (token) {
  248. const res_dara = await regionAddresstree()
  249. addresstree.value = res_dara.data
  250. locateTheCurrentAddress(res_dara.data)
  251. .then((res) => {
  252. console.log('locateTheCurrentAddress', res)
  253. data.address = res
  254. settingAddress()
  255. })
  256. .catch((error) => {
  257. console.log('获取地址失败!', error)
  258. getList()
  259. })
  260. } else {
  261. //设置默认角色
  262. uni.setStorageSync('userType', 1);
  263. getList();
  264. }
  265. })
  266. onUnload(() => { })
  267. </script>
  268. <style scoped lang="scss">
  269. // 首页顶部区域-轮播图
  270. .home-banner {
  271. position: fixed;
  272. z-index: 130;
  273. // top: v-bind('globalData.statusBarHeight + 10 + "px"');
  274. left: 0;
  275. height: 90px;
  276. width: 100%;
  277. padding: 10rpx 0;
  278. transition: background-color 0.3s ease;
  279. display: flex;
  280. flex-direction: column;
  281. justify-content: center;
  282. &.home-banner-with-bg {
  283. // background-color: rgba(255, 255, 255, 0.9);
  284. // box-shadow: 0 2rpx 10rpx rgba(0, 0, 0, 0.1);
  285. background: #FAFBF0;
  286. height: 90px;
  287. display: flex;
  288. flex-direction: column;
  289. justify-content: center;
  290. }
  291. .home-banner-left {
  292. display: flex;
  293. align-items: center;
  294. padding-left: 10rpx;
  295. box-sizing: border-box;
  296. margin-top: 80rpx;
  297. .map-pin-fill {
  298. width: 34rpx;
  299. height: 34rpx;
  300. }
  301. .address-text {
  302. width: 178rpx;
  303. height: 42rpx;
  304. font-family: PingFang SC;
  305. font-size: 28rpx;
  306. font-weight: normal;
  307. line-height: 40rpx;
  308. letter-spacing: normal;
  309. color: #000000;
  310. max-width: 120rpx;
  311. white-space: nowrap;
  312. overflow: hidden;
  313. text-overflow: ellipsis;
  314. }
  315. }
  316. }
  317. // 分类下轮播图
  318. .home-swiper {
  319. margin-top: 40rpx;
  320. margin-right: 23rpx;
  321. margin-left: 23rpx;
  322. width: 704rpx;
  323. height: 180rpx;
  324. border-radius: 16rpx;
  325. background: linear-gradient(180deg, #FEF9ED 0%, #FFF9F3 100%);
  326. }
  327. // 瀑布流
  328. .home-ranking {
  329. margin-top: 80rpx;
  330. margin-left: 25rpx;
  331. margin-right: 25rpx;
  332. margin-bottom: 120rpx;
  333. }
  334. /* 底部安全区域 */
  335. .safe-area-bottom {
  336. height: 150rpx;
  337. width: 100%;
  338. }
  339. </style>