home.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  1. <template>
  2. <view class="tab_page adffc" :style="{'height':h+'px', 'padding-top':mt+'px'}">
  3. <up-navbar title=" " bgColor="transparent">
  4. <template #left>
  5. <view class="u-nav-slot" slot="left" style="display: flex;background-color: transparent;">
  6. <image src="https://oss.familydaf.cn/sxsnfile/20260105/087c26c68a18424c83763a768a2fd81d.png" style="width: 370rpx;height: 40rpx;"></image>
  7. </view>
  8. </template>
  9. </up-navbar>
  10. <image src="https://oss.familydaf.cn/sxsnfile/20251218/ee40161fef4647f0b736d118e554dc2f.png" class="top_bg_img" mode="widthFix"></image>
  11. <scroll-view class="c-box adffc" scroll-y="true" @scroll="handleScroll" @scrolltolower="scrolltolower">
  12. <view class="c-box-search">
  13. <cus-search @handleSearch="toSearch"></cus-search>
  14. </view>
  15. <view class="c-box-lunbo">
  16. <up-swiper
  17. :list="bannarList"
  18. @change="e => current = e.current"
  19. @click="bannerClick"
  20. :autoplay="true"
  21. :interval="2000"
  22. :duration="200"
  23. :circular="true"
  24. imgMode="scaleToFill"
  25. height="326rpx"
  26. >
  27. <template #indicator>
  28. <view class="indicator adf">
  29. <view class="indicator__dot" v-for="(item, index) in bannarList" :key="index"
  30. :class="[index === current && 'indicator__dot--active']">
  31. </view>
  32. </view>
  33. </template>
  34. </up-swiper>
  35. </view>
  36. <view class="c-box-title adfacjb">
  37. <view class="c-box-title-left">公益活动</view>
  38. <view class="c-box-title-right adfac" @click="toTurnPage('/pagesHome/allActivity',false)">更多 <up-icon name="arrow-right" size="30rpx" style="margin-left: 10rpx;"></up-icon></view>
  39. </view>
  40. <view class="c-box-typegrid-anchor" v-if="typeGrid.length" :style="isTypeGridSticky ? { height: typeGridHeight + 'px' } : null">
  41. <view class="c-box-typegrid" :class="{ 'is-typegrid-sticky': isTypeGridSticky }" :style="isTypeGridSticky ? { top: mt + 'px', left: typeGridLeft + 'px', width: typeGridWidth + 'px' } : null">
  42. <swiper class="typegrid-swiper" :current="gridIndex" @change="e => gridIndex = e.detail.current" :duration="200" :circular="false" :disable-touch="false">
  43. <swiper-item v-for="(page, pi) in typeGridPages" :key="pi">
  44. <view class="typegrid-page">
  45. <view class="typegrid-item adffc" v-for="(item, idx) in page" :key="item.id" @click="changeType(item)">
  46. <view class="typegrid-circle">
  47. <image v-if="item.iconUrl" :src="item.iconUrl" mode="aspectFit"></image>
  48. </view>
  49. <view class="typegrid-name">{{item.name}}</view>
  50. </view>
  51. </view>
  52. </swiper-item>
  53. </swiper>
  54. <view class="typegrid-dots adf" v-if="typeGridPages.length > 1">
  55. <view class="typegrid-dot" v-for="(_, i) in typeGridPages" :key="i" :class="{'active': i === gridIndex}"></view>
  56. </view>
  57. </view>
  58. </view>
  59. <view class="c-box-list" v-if="activityList.length">
  60. <view class="activity-list-item" v-for="(item, index) in activityList" :key="item.id">
  61. <NonprofitActivety :item="item" :index="index"></NonprofitActivety>
  62. </view>
  63. </view>
  64. <view class="dataEmpty" v-else>
  65. <page-empty></page-empty>
  66. </view>
  67. </scroll-view>
  68. <login-register></login-register>
  69. <CusTabbar :tabbarIndex="0"></CusTabbar>
  70. </view>
  71. </template>
  72. <script setup name="">
  73. import CusTabbar from '@/components/CusTabbar/index.vue'
  74. import CusSearch from '@/components/CusSearch/index.vue'
  75. import NonprofitActivety from '@/components/pages/nonprofitActivety/index.vue'
  76. import PageEmpty from '@/components/pageEmpty/index.vue'
  77. import { ref, computed, getCurrentInstance, onMounted, nextTick, watch } from 'vue'
  78. const { proxy } = getCurrentInstance()
  79. import { useUserStore } from '@/common/stores/user';
  80. const userStore = useUserStore();
  81. import { onLoad } from '@dcloudio/uni-app'
  82. const parseUrlParams = (url) => {
  83. const result = {}
  84. const queryStr = url.indexOf('?') !== -1 ? url.split('?')[1] : (url.indexOf('#') !== -1 ? url.split('#')[1] : '')
  85. if (!queryStr) return result
  86. queryStr.split('&').forEach(pair => {
  87. const [key, val] = pair.split('=')
  88. if (key) result[decodeURIComponent(key)] = decodeURIComponent(val || '')
  89. })
  90. return result
  91. }
  92. onLoad((options) => {
  93. console.log('[onLoad] options:', JSON.stringify(options))
  94. console.log('[onLoad] options.q:', options.q, '| typeof:', typeof options.q)
  95. console.log('[onLoad] 条件判断 options && options.q:', !!(options && options.q))
  96. if (options && options.q) {
  97. console.log('[onLoad] 进入 if 分支')
  98. const url = decodeURIComponent(options.q)
  99. console.log('[onLoad] 解码后 url:', url)
  100. const params = parseUrlParams(url)
  101. console.log('[onLoad] 解析参数:', JSON.stringify(params))
  102. const channelId = params.channel
  103. console.log('[onLoad] channelId:', channelId)
  104. if (!channelId) {
  105. console.log('[onLoad] channelId 为空,退出')
  106. return
  107. }
  108. const token = uni.getStorageSync('token')
  109. console.log('[onLoad] token:', token ? '有值' : '无值(未登录)')
  110. if (!token) return
  111. proxy.$api.post(`/channel/bind?channelId=${channelId}`, {}).then(({ data: res }) => {
  112. console.log('[onLoad] bind 结果:', JSON.stringify(res))
  113. if (res.code === 0) {
  114. uni.showToast({ title: '渠道加入成功', icon: 'success', duration: 2000 })
  115. } else {
  116. console.error('渠道绑定失败:', res.msg)
  117. }
  118. })
  119. }
  120. })
  121. const bannarList = ref([])
  122. const bannarOrigin = ref([])
  123. const current = ref(0)
  124. const typeGrid = ref([])
  125. const gridIndex = ref(0)
  126. const isOver = ref(false)
  127. const activityList = ref([])
  128. const listRequestVersion = ref(0)
  129. const isTypeGridSticky = ref(false)
  130. const typeGridOffsetTop = ref(0)
  131. const typeGridHeight = ref(0)
  132. const typeGridLeft = ref(0)
  133. const typeGridWidth = ref(0)
  134. // 进入页面默认查全部(categoryId 为空),点图标网格才切到具体分类
  135. const queryParams = ref({
  136. page:1,
  137. limit:10,
  138. categoryId:'',
  139. userId:''
  140. })
  141. // 图标网格分页:每页 8 个(4 列 × 2 行),超过则分多页
  142. const TYPEGRID_PAGE_SIZE = 8
  143. const typeGridPages = computed(() => {
  144. const pages = []
  145. const list = typeGrid.value || []
  146. for (let i = 0; i < list.length; i += TYPEGRID_PAGE_SIZE) {
  147. pages.push(list.slice(i, i + TYPEGRID_PAGE_SIZE))
  148. }
  149. return pages
  150. })
  151. const toSearch = (keyword) => {
  152. if (!keyword) return
  153. uni.navigateTo({ url: '/pagesHome/allActivity?keyword=' + encodeURIComponent(keyword) })
  154. }
  155. const bannerClick = e => {
  156. let banner = bannarOrigin.value[e];
  157. if(banner&&banner?.redirect){
  158. if(banner?.redirectType==1){//微信公众号
  159. uni.navigateTo({
  160. url:'/pages/webView?src='+banner?.redirect
  161. })
  162. }else if(banner?.redirectType==2){//微信小程序
  163. uni.navigateToMiniProgram({
  164. appId:banner?.redirect
  165. })
  166. }
  167. }
  168. }
  169. const changeType = (item,index) => {
  170. queryParams.value.categoryId = item.id;
  171. initList();
  172. getActivityList();
  173. }
  174. const initList = () => {
  175. listRequestVersion.value++;
  176. queryParams.value.page = 1;
  177. isOver.value = false;
  178. activityList.value = [];
  179. }
  180. const toTurnPage = (url,needLogin) => {
  181. if(!needLogin){
  182. uni.navigateTo({ url })
  183. }
  184. }
  185. const getSwiperList = () => {
  186. proxy.$api.get('/core/advertisement/manage/page',{page:1,limit:-1}).then(({data:res})=>{
  187. if(res.code!==0) return proxy.$showToast(res.msg)
  188. const list = res.data.list.filter(l=>l.enable==1);
  189. bannarOrigin.value = list;
  190. bannarList.value = list.map(l=>l.fileUrl);
  191. })
  192. }
  193. const getTypeList = () => {
  194. proxy.$api.get('/core/activity/category/list').then(({data:res})=>{
  195. if(res.code!==0) return proxy.$showToast(res.msg)
  196. // icon 网格:取所有配过 icon 的分类;超过 8 个会自动分页
  197. typeGrid.value = res.data.filter(d=>d.iconUrl).map(d=>({id:d.id,name:d.categoryName,iconUrl:d.iconUrl}));
  198. gridIndex.value = 0
  199. measureTypeGrid()
  200. })
  201. }
  202. const measureTypeGrid = () => {
  203. nextTick(() => {
  204. const query = uni.createSelectorQuery().in(proxy)
  205. query.select('.c-box').boundingClientRect()
  206. query.select('.c-box-typegrid-anchor').boundingClientRect()
  207. query.exec((rects) => {
  208. const boxRect = rects?.[0]
  209. const gridRect = rects?.[1]
  210. if (!boxRect || !gridRect) return
  211. typeGridOffsetTop.value = gridRect.top - boxRect.top
  212. typeGridHeight.value = gridRect.height
  213. typeGridLeft.value = gridRect.left
  214. typeGridWidth.value = gridRect.width
  215. })
  216. })
  217. }
  218. const handleScroll = (event) => {
  219. if (!typeGridOffsetTop.value) return
  220. const scrollTop = event?.detail?.scrollTop || 0
  221. isTypeGridSticky.value = scrollTop >= typeGridOffsetTop.value
  222. }
  223. const scrolltolower = () => {
  224. if(isOver.value) return
  225. getActivityList()
  226. }
  227. const getActivityList = () => {
  228. const requestVersion = listRequestVersion.value
  229. const requestParams = { ...queryParams.value }
  230. proxy.$api.get('/core/activity/page',requestParams).then(({data:res})=>{
  231. if(requestVersion!==listRequestVersion.value) return
  232. if(res.code!==0) return proxy.$showToast(res.msg)
  233. // 只对本次新增项做格式化,避免 loadMore 时旧项(已格式化成 "yyyy.MM.dd hh:mm")
  234. // 被再次 new Date() 解析为 Invalid Date 导致显示 NaN
  235. const newItems = res.data.list.map(a=>{
  236. let cc = calculateCountdown(a?.signupEndTime);
  237. a.endTimeText = cc===-1?a?.signupEndTime:(`还有${cc.days}天${cc.hours}小时${cc.minutes}分钟`)
  238. a.signupEndTime = a.signupEndTime?new Date(a.signupEndTime).Format('yyyy.MM.dd hh:mm'):''
  239. return a
  240. })
  241. activityList.value = [...activityList.value, ...newItems];
  242. queryParams.value.page++;
  243. if(res.data.list.length===0) isOver.value = true;
  244. })
  245. }
  246. const calculateCountdown = (datetime) => {
  247. if(!datetime) return -1;
  248. const targetDate = new Date(datetime);
  249. const now = new Date();
  250. const difference = targetDate.getTime() - now.getTime();
  251. if (difference <= 0) return -1;
  252. const days = Math.floor(difference / (1000 * 60 * 60 * 24));
  253. const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  254. const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
  255. return {
  256. days,
  257. hours,
  258. minutes
  259. };
  260. }
  261. watch(()=>userStore.token,(newVal,oldVal)=>{
  262. if(oldVal!=newVal&&newVal){
  263. setTimeout(()=>{
  264. uni.showToast({
  265. title: '登录成功',
  266. icon: 'success'
  267. });
  268. },200)
  269. nextTick(()=>{
  270. getActivityList()
  271. })
  272. }
  273. })
  274. onMounted(()=>{
  275. if(uni.getStorageSync('userInfo')){
  276. queryParams.value.userId = JSON.parse(uni.getStorageSync('userInfo'))?.id;
  277. }
  278. getSwiperList()
  279. getTypeList()
  280. getActivityList()
  281. })
  282. import { onShareAppMessage, onShareTimeline, onPullDownRefresh } from '@dcloudio/uni-app'
  283. onShareAppMessage(() => {
  284. return {
  285. title: '善行少年 - 公益活动平台',
  286. path: '/pages/home'
  287. }
  288. })
  289. onShareTimeline(() => {
  290. return {
  291. title: '善行少年 - 公益活动平台',
  292. path: '/pages/home'
  293. }
  294. })
  295. // 下拉刷新:若当前处于某个分类,回到全部列表;否则重新加载当前页
  296. onPullDownRefresh(() => {
  297. queryParams.value.categoryId = ''
  298. initList()
  299. getActivityList()
  300. uni.stopPullDownRefresh()
  301. })
  302. </script>
  303. <style scoped lang="scss">
  304. ::v-deep .indicator__dot{
  305. width: 48rpx;
  306. height: 4rpx;
  307. background: #E2E5E9;
  308. border-radius: 2rpx;
  309. margin: 0 8rpx;
  310. }
  311. ::v-deep .indicator__dot--active{
  312. width: 48rpx;
  313. height: 4rpx;
  314. background: #00AE57;
  315. border-radius: 2rpx;
  316. }
  317. ::v-deep .u-swiper__wrapper__item__wrapper__image{
  318. border-radius: 24rpx !important;
  319. }
  320. .tab_page{
  321. .c-box{
  322. width: 100%;
  323. height: 100%;
  324. overflow: hidden;
  325. position: relative;
  326. &-search{
  327. width: 100%;
  328. padding: 20rpx 30rpx 0;
  329. box-sizing: border-box;
  330. }
  331. &-lunbo{
  332. width: 100%;
  333. height: 326rpx;
  334. margin-top: 20rpx;
  335. }
  336. &-title{
  337. margin-top: 58rpx;
  338. &-left{
  339. width: 170rpx;
  340. height: 44rpx;
  341. padding-left: 2rpx;
  342. font-family: PingFang-SC, PingFang-SC;
  343. font-weight: 800;
  344. font-size: 36rpx;
  345. color: #151B29;
  346. line-height: 36rpx;
  347. background-image: url('https://oss.familydaf.cn/sxsnfile/20251218/6198fa26a7ff4fc1bce530fbdfaa20db.png');
  348. background-size: 170rpx 31rpx;
  349. background-position: 0 20rpx;
  350. background-repeat: no-repeat;
  351. }
  352. &-right{
  353. font-family: PingFangSC, PingFang SC;
  354. font-weight: 400;
  355. font-size: 28rpx;
  356. color: #676775;
  357. line-height: 33rpx;
  358. }
  359. }
  360. &-typegrid{
  361. width: 100%;
  362. padding: 8rpx 0;
  363. box-sizing: border-box;
  364. z-index: 10;
  365. background: #FFFFFF;
  366. &.is-typegrid-sticky{
  367. position: fixed;
  368. }
  369. .typegrid-swiper{
  370. width: 100%;
  371. height: 360rpx;
  372. }
  373. .typegrid-page{
  374. width: 100%;
  375. height: 100%;
  376. display: flex;
  377. flex-wrap: wrap;
  378. }
  379. .typegrid-item{
  380. width: 25%;
  381. margin-bottom: 28rpx;
  382. align-items: center;
  383. .typegrid-circle{
  384. width: 112rpx;
  385. height: 112rpx;
  386. display: flex;
  387. align-items: center;
  388. justify-content: center;
  389. image{
  390. width: 96rpx;
  391. height: 96rpx;
  392. }
  393. }
  394. .typegrid-name{
  395. margin-top: 12rpx;
  396. font-family: PingFangSC, PingFang SC;
  397. font-weight: 400;
  398. font-size: 24rpx;
  399. color: #151B29;
  400. line-height: 30rpx;
  401. text-align: center;
  402. }
  403. }
  404. .typegrid-dots{
  405. width: 100%;
  406. justify-content: center;
  407. margin-top: 4rpx;
  408. .typegrid-dot{
  409. width: 12rpx;
  410. height: 12rpx;
  411. border-radius: 50%;
  412. background: #E2E5E9;
  413. margin: 0 6rpx;
  414. &.active{
  415. background: #00AE57;
  416. }
  417. }
  418. }
  419. }
  420. &-list{
  421. width: 100%;
  422. margin-top: 20rpx;
  423. }
  424. &-typegrid-anchor{
  425. width: 100%;
  426. margin-top: 36rpx;
  427. }
  428. .activity-list-item{
  429. width: 100%;
  430. }
  431. }
  432. }
  433. </style>