index.vue 10 KB

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