index.vue 10 KB

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