index.vue 9.8 KB

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