home.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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' } : 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 isTypeGridSticky = ref(false)
  129. const typeGridOffsetTop = ref(0)
  130. const typeGridHeight = ref(0)
  131. // 进入页面默认查全部(categoryId 为空),点图标网格才切到具体分类
  132. const queryParams = ref({
  133. page:1,
  134. limit:10,
  135. categoryId:'',
  136. userId:''
  137. })
  138. // 图标网格分页:每页 8 个(4 列 × 2 行),超过则分多页
  139. const TYPEGRID_PAGE_SIZE = 8
  140. const typeGridPages = computed(() => {
  141. const pages = []
  142. const list = typeGrid.value || []
  143. for (let i = 0; i < list.length; i += TYPEGRID_PAGE_SIZE) {
  144. pages.push(list.slice(i, i + TYPEGRID_PAGE_SIZE))
  145. }
  146. return pages
  147. })
  148. const toSearch = (keyword) => {
  149. if (!keyword) return
  150. uni.navigateTo({ url: '/pagesHome/allActivity?keyword=' + encodeURIComponent(keyword) })
  151. }
  152. const bannerClick = e => {
  153. let banner = bannarOrigin.value[e];
  154. if(banner&&banner?.redirect){
  155. if(banner?.redirectType==1){//微信公众号
  156. uni.navigateTo({
  157. url:'/pages/webView?src='+banner?.redirect
  158. })
  159. }else if(banner?.redirectType==2){//微信小程序
  160. uni.navigateToMiniProgram({
  161. appId:banner?.redirect
  162. })
  163. }
  164. }
  165. }
  166. const changeType = (item,index) => {
  167. queryParams.value.categoryId = item.id;
  168. initList();
  169. getActivityList();
  170. }
  171. const initList = () => {
  172. queryParams.value.page = 1;
  173. isOver.value = false;
  174. activityList.value = [];
  175. }
  176. const toTurnPage = (url,needLogin) => {
  177. if(!needLogin){
  178. uni.navigateTo({ url })
  179. }
  180. }
  181. const getSwiperList = () => {
  182. proxy.$api.get('/core/advertisement/manage/page',{page:1,limit:-1}).then(({data:res})=>{
  183. if(res.code!==0) return proxy.$showToast(res.msg)
  184. const list = res.data.list.filter(l=>l.enable==1);
  185. bannarOrigin.value = list;
  186. bannarList.value = list.map(l=>l.fileUrl);
  187. })
  188. }
  189. const getTypeList = () => {
  190. proxy.$api.get('/core/activity/category/list').then(({data:res})=>{
  191. if(res.code!==0) return proxy.$showToast(res.msg)
  192. // icon 网格:取所有配过 icon 的分类;超过 8 个会自动分页
  193. typeGrid.value = res.data.filter(d=>d.iconUrl).map(d=>({id:d.id,name:d.categoryName,iconUrl:d.iconUrl}));
  194. gridIndex.value = 0
  195. measureTypeGrid()
  196. })
  197. }
  198. const measureTypeGrid = () => {
  199. nextTick(() => {
  200. const query = uni.createSelectorQuery().in(proxy)
  201. query.select('.c-box').boundingClientRect()
  202. query.select('.c-box-typegrid-anchor').boundingClientRect()
  203. query.exec((rects) => {
  204. const boxRect = rects?.[0]
  205. const gridRect = rects?.[1]
  206. if (!boxRect || !gridRect) return
  207. typeGridOffsetTop.value = gridRect.top - boxRect.top
  208. typeGridHeight.value = gridRect.height
  209. })
  210. })
  211. }
  212. const handleScroll = (event) => {
  213. if (!typeGridOffsetTop.value) return
  214. const scrollTop = event?.detail?.scrollTop || 0
  215. isTypeGridSticky.value = scrollTop >= typeGridOffsetTop.value
  216. }
  217. const scrolltolower = () => {
  218. if(isOver.value) return
  219. getActivityList()
  220. }
  221. const getActivityList = () => {
  222. proxy.$api.get('/core/activity/page',queryParams.value).then(({data:res})=>{
  223. if(res.code!==0) return proxy.$showToast(res.msg)
  224. // 只对本次新增项做格式化,避免 loadMore 时旧项(已格式化成 "yyyy.MM.dd hh:mm")
  225. // 被再次 new Date() 解析为 Invalid Date 导致显示 NaN
  226. const newItems = res.data.list.map(a=>{
  227. let cc = calculateCountdown(a?.signupEndTime);
  228. a.endTimeText = cc===-1?a?.signupEndTime:(`还有${cc.days}天${cc.hours}小时${cc.minutes}分钟`)
  229. a.signupEndTime = a.signupEndTime?new Date(a.signupEndTime).Format('yyyy.MM.dd hh:mm'):''
  230. return a
  231. })
  232. activityList.value = [...activityList.value, ...newItems];
  233. queryParams.value.page++;
  234. if(res.data.list.length===0) isOver.value = true;
  235. })
  236. }
  237. const calculateCountdown = (datetime) => {
  238. if(!datetime) return -1;
  239. const targetDate = new Date(datetime);
  240. const now = new Date();
  241. const difference = targetDate.getTime() - now.getTime();
  242. if (difference <= 0) return -1;
  243. const days = Math.floor(difference / (1000 * 60 * 60 * 24));
  244. const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  245. const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
  246. return {
  247. days,
  248. hours,
  249. minutes
  250. };
  251. }
  252. watch(()=>userStore.token,(newVal,oldVal)=>{
  253. if(oldVal!=newVal&&newVal){
  254. setTimeout(()=>{
  255. uni.showToast({
  256. title: '登录成功',
  257. icon: 'success'
  258. });
  259. },200)
  260. nextTick(()=>{
  261. getActivityList()
  262. })
  263. }
  264. })
  265. onMounted(()=>{
  266. if(uni.getStorageSync('userInfo')){
  267. queryParams.value.userId = JSON.parse(uni.getStorageSync('userInfo'))?.id;
  268. }
  269. getSwiperList()
  270. getTypeList()
  271. getActivityList()
  272. })
  273. import { onShareAppMessage, onShareTimeline, onPullDownRefresh } from '@dcloudio/uni-app'
  274. onShareAppMessage(() => {
  275. return {
  276. title: '善行少年 - 公益活动平台',
  277. path: '/pages/home'
  278. }
  279. })
  280. onShareTimeline(() => {
  281. return {
  282. title: '善行少年 - 公益活动平台',
  283. path: '/pages/home'
  284. }
  285. })
  286. // 下拉刷新:若当前处于某个分类,回到全部列表;否则重新加载当前页
  287. onPullDownRefresh(() => {
  288. queryParams.value.categoryId = ''
  289. initList()
  290. getActivityList()
  291. uni.stopPullDownRefresh()
  292. })
  293. </script>
  294. <style scoped lang="scss">
  295. ::v-deep .indicator__dot{
  296. width: 48rpx;
  297. height: 4rpx;
  298. background: #E2E5E9;
  299. border-radius: 2rpx;
  300. margin: 0 8rpx;
  301. }
  302. ::v-deep .indicator__dot--active{
  303. width: 48rpx;
  304. height: 4rpx;
  305. background: #00AE57;
  306. border-radius: 2rpx;
  307. }
  308. ::v-deep .u-swiper__wrapper__item__wrapper__image{
  309. border-radius: 24rpx !important;
  310. }
  311. .tab_page{
  312. .c-box{
  313. width: 100%;
  314. height: 100%;
  315. overflow: hidden;
  316. position: relative;
  317. &-search{
  318. width: 100%;
  319. padding: 20rpx 30rpx 0;
  320. box-sizing: border-box;
  321. }
  322. &-lunbo{
  323. width: 100%;
  324. height: 326rpx;
  325. margin-top: 20rpx;
  326. }
  327. &-title{
  328. margin-top: 58rpx;
  329. &-left{
  330. width: 170rpx;
  331. height: 44rpx;
  332. padding-left: 2rpx;
  333. font-family: PingFang-SC, PingFang-SC;
  334. font-weight: 800;
  335. font-size: 36rpx;
  336. color: #151B29;
  337. line-height: 36rpx;
  338. background-image: url('https://oss.familydaf.cn/sxsnfile/20251218/6198fa26a7ff4fc1bce530fbdfaa20db.png');
  339. background-size: 170rpx 31rpx;
  340. background-position: 0 20rpx;
  341. background-repeat: no-repeat;
  342. }
  343. &-right{
  344. font-family: PingFangSC, PingFang SC;
  345. font-weight: 400;
  346. font-size: 28rpx;
  347. color: #676775;
  348. line-height: 33rpx;
  349. }
  350. }
  351. &-typegrid{
  352. width: 100%;
  353. padding: 8rpx 0;
  354. box-sizing: border-box;
  355. z-index: 10;
  356. background: #FFFFFF;
  357. &.is-typegrid-sticky{
  358. position: fixed;
  359. left: 0;
  360. right: 0;
  361. }
  362. .typegrid-swiper{
  363. width: 100%;
  364. height: 360rpx;
  365. }
  366. .typegrid-page{
  367. width: 100%;
  368. height: 100%;
  369. display: flex;
  370. flex-wrap: wrap;
  371. }
  372. .typegrid-item{
  373. width: 25%;
  374. margin-bottom: 28rpx;
  375. align-items: center;
  376. .typegrid-circle{
  377. width: 112rpx;
  378. height: 112rpx;
  379. display: flex;
  380. align-items: center;
  381. justify-content: center;
  382. image{
  383. width: 96rpx;
  384. height: 96rpx;
  385. }
  386. }
  387. .typegrid-name{
  388. margin-top: 12rpx;
  389. font-family: PingFangSC, PingFang SC;
  390. font-weight: 400;
  391. font-size: 24rpx;
  392. color: #151B29;
  393. line-height: 30rpx;
  394. text-align: center;
  395. }
  396. }
  397. .typegrid-dots{
  398. width: 100%;
  399. justify-content: center;
  400. margin-top: 4rpx;
  401. .typegrid-dot{
  402. width: 12rpx;
  403. height: 12rpx;
  404. border-radius: 50%;
  405. background: #E2E5E9;
  406. margin: 0 6rpx;
  407. &.active{
  408. background: #00AE57;
  409. }
  410. }
  411. }
  412. }
  413. &-list{
  414. width: 100%;
  415. margin-top: 20rpx;
  416. }
  417. &-typegrid-anchor{
  418. width: 100%;
  419. margin-top: 36rpx;
  420. }
  421. .activity-list-item{
  422. width: 100%;
  423. }
  424. }
  425. }
  426. </style>