home.vue 14 KB

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