home.vue 12 KB

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