index.vue 9.5 KB

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