index.vue 10 KB

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