index.vue 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. <custom-tab-bar page="home" class="customTabBar" />
  36. <view class="safe-area-bottom"></view>
  37. </view>
  38. </template>
  39. <script setup>
  40. import { ref, reactive, onMounted } from 'vue'
  41. import { provide } from 'vue'
  42. import { onLoad, 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 } from '@/api/volunteerDetailsApi/details.js'
  47. import { slideshow, settingHomeAddress } from '@/api/home.js'
  48. import { useDict } from '@/utils/dict.js'
  49. import { chooseLocationInit, locateTheCurrentAddress } from '@/utils/adress'
  50. import CustomTabBar from '@/components/CustomTabBar/index.vue'
  51. import { regionAddresstree } from '@/api/home.js'
  52. import { getToken } from '@/utils/auth'
  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'); //读取本地存储
  60. const globalData = ref({
  61. statusBarHeight: 47,
  62. navBarHeight: 91,
  63. })
  64. const data = reactive({
  65. address: {
  66. name: '重庆市',
  67. latitude: 29.333000000000002,
  68. longitude: 105.94909000000001,
  69. cityCode: {
  70. code: [500000, 500100, 500118],
  71. data: ['重庆市', '重庆市', '永川区'],
  72. index: [21, 0, 17],
  73. },
  74. },
  75. queryValue: '',
  76. })
  77. // 分页
  78. const pages = ref({
  79. current: 1,
  80. pageSize: 10,
  81. total: 0,
  82. serviceCategory: '',
  83. // appStatus:"2",
  84. })
  85. const cityClick = async () => {
  86. const { latitude, longitude } = data.address
  87. const address = await chooseLocationInit(
  88. { latitude, longitude },
  89. addresstree.value
  90. )
  91. console.log('TCL: cityClick -> address', address)
  92. data.address = { ...data.address, ...address }
  93. settingAddress()
  94. console.log('address', address)
  95. }
  96. const loadmoreInfo = ref({
  97. status: 'loadmore',
  98. loadingText: '努力加载中...',
  99. loadmoreText: '点击加载更多~',
  100. nomoreText: '已经到底啦~',
  101. })
  102. // 加载更多
  103. async function handleLoadmore(e) {
  104. if (
  105. pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)
  106. ) {
  107. pages.value.current += 1
  108. loadmoreInfo.value.status = 'loading'
  109. await getList()
  110. } else {
  111. loadmoreInfo.value.status = 'nomore'
  112. }
  113. }
  114. const getList = async () => {
  115. try {
  116. loadmoreInfo.value.status = 'loading'
  117. const params = {
  118. pageNum: pages.value.current,
  119. pageSize: pages.value.pageSize,
  120. serviceCategory: pages.value.serviceCategory || '',
  121. businessManagementId: 0,
  122. provinceCode: data.address.cityCode?.code[0],
  123. cityCode: data.address.cityCode?.code[1],
  124. districtCode: data.address.cityCode?.code[2],
  125. }
  126. const res = await volunteerinfolist(params)
  127. if (!res || !res.rows) {
  128. return
  129. }
  130. console.log(
  131. '判断是否已经到了最后一页',
  132. pages.value.current >= Math.ceil(res.total / pages.value.pageSize)
  133. )
  134. // 如果是第一页,先清空
  135. if (pages.value.current === 1) {
  136. leftList.value = [{ type: 'slideshow' }]
  137. rightList.value = []
  138. }
  139. // 每次都追加新数据
  140. res.rows.forEach((item, index) => {
  141. index % 2 !== 0 ? rightList.value.push(item) : leftList.value.push(item)
  142. })
  143. console.log('home', leftList.value, rightList.value);
  144. pages.value.total = res.total
  145. if (pages.value.current >= Math.ceil(res.total / pages.value.pageSize)) {
  146. loadmoreInfo.value.status = 'nomore'
  147. } else {
  148. loadmoreInfo.value.status = 'loadmore'
  149. }
  150. } catch (error) {
  151. leftList.value = []
  152. rightList.value = []
  153. loadmoreInfo.value.status = 'loadmore'
  154. pages.value.total = 0
  155. console.error('Error fetching data:', error)
  156. }
  157. }
  158. onReachBottom(() => {
  159. if (
  160. pages.value.current < Math.ceil(pages.value.total / pages.value.pageSize)
  161. ) {
  162. pages.value.current = pages.value.current + 1
  163. loadmoreInfo.value.status = 'nomore'
  164. getList()
  165. }
  166. })
  167. const getBanners = async () => {
  168. try {
  169. const res = await slideshow(21)
  170. if (res.code === 200 && res.data.picture) {
  171. list3.value = res.data.picture.split(',')
  172. }
  173. const value_res = await slideshow(20)
  174. if (value_res.code === 200 && value_res.data.picture) {
  175. ValueZone.value = value_res.data.picture.split(',')
  176. }
  177. const hot_res = await slideshow(11)
  178. if (hot_res.code === 200 && hot_res.data.picture) {
  179. hotList.value = hot_res.data.picture.split(',')
  180. }
  181. const hot_node = await slideshow(22)
  182. if (hot_node.code === 200 && hot_node.data.picture) {
  183. ValueZoneSwiper.value = hot_node.data.picture.split(',')
  184. }
  185. } catch (error) {
  186. console.log('error', error)
  187. }
  188. }
  189. const settingAddress = async () => {
  190. try {
  191. console.log('data', data)
  192. const { address } = data
  193. const { cityCode, latitude, longitude } = address
  194. const parmas = {
  195. provinceName: cityCode.data[0], // 省
  196. provinceCode: cityCode.code[0],
  197. provinceInd: cityCode.index[0],
  198. cityName: cityCode.data[1], // 市
  199. cityCode: cityCode.code[1],
  200. cityInd: cityCode.index[1],
  201. districtName: cityCode.data[2],
  202. districtCode: cityCode.code[2],
  203. districtInd: cityCode.index[2],
  204. address: address.name,
  205. latitude,
  206. longitude,
  207. }
  208. console.log("TCL: settingAddress -> parmas", parmas);
  209. const res = await settingHomeAddress(parmas)
  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: 80rpx;
  320. margin-left: 25rpx;
  321. margin-right: 25rpx;
  322. margin-bottom: 120rpx;
  323. }
  324. /* 底部安全区域 */
  325. .safe-area-bottom {
  326. height: 150rpx;
  327. width: 100%;
  328. }
  329. </style>