home.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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. <view class="typegrid-item adffc" v-for="(item,index) in typeGrid" :key="item.id" :style="{'margin-right':(index+1)%4===0?'0':'20rpx'}" @click="changeType(item,index)">
  42. <view class="typegrid-circle">
  43. <image v-if="item.iconUrl" :src="item.iconUrl" mode="aspectFit"></image>
  44. </view>
  45. <view class="typegrid-name">{{item.name}}</view>
  46. </view>
  47. </view>
  48. <view class="c-box-list" v-if="activityList.length">
  49. <up-list @scrolltolower="scrolltolower" style="height: 100%;">
  50. <up-list-item v-for="(item, index) in activityList" :key="item.id">
  51. <NonprofitActivety :item="item" :index="index"></NonprofitActivety>
  52. </up-list-item>
  53. </up-list>
  54. </view>
  55. <view class="dataEmpty" v-else>
  56. <page-empty></page-empty>
  57. </view>
  58. </view>
  59. <login-register></login-register>
  60. <CusTabbar :tabbarIndex="0"></CusTabbar>
  61. </view>
  62. </template>
  63. <script setup name="">
  64. import CusTabbar from '@/components/CusTabbar/index.vue'
  65. import CusSearch from '@/components/CusSearch/index.vue'
  66. import NonprofitActivety from '@/components/pages/nonprofitActivety/index.vue'
  67. import PageEmpty from '@/components/pageEmpty/index.vue'
  68. import { ref, getCurrentInstance, onMounted, nextTick, watch } from 'vue'
  69. const { proxy } = getCurrentInstance()
  70. import { useUserStore } from '@/common/stores/user';
  71. const userStore = useUserStore();
  72. import { onLoad } from '@dcloudio/uni-app'
  73. const parseUrlParams = (url) => {
  74. const result = {}
  75. const queryStr = url.indexOf('?') !== -1 ? url.split('?')[1] : (url.indexOf('#') !== -1 ? url.split('#')[1] : '')
  76. if (!queryStr) return result
  77. queryStr.split('&').forEach(pair => {
  78. const [key, val] = pair.split('=')
  79. if (key) result[decodeURIComponent(key)] = decodeURIComponent(val || '')
  80. })
  81. return result
  82. }
  83. onLoad((options) => {
  84. console.log('[onLoad] options:', JSON.stringify(options))
  85. console.log('[onLoad] options.q:', options.q, '| typeof:', typeof options.q)
  86. console.log('[onLoad] 条件判断 options && options.q:', !!(options && options.q))
  87. if (options && options.q) {
  88. console.log('[onLoad] 进入 if 分支')
  89. const url = decodeURIComponent(options.q)
  90. console.log('[onLoad] 解码后 url:', url)
  91. const params = parseUrlParams(url)
  92. console.log('[onLoad] 解析参数:', JSON.stringify(params))
  93. const channelId = params.channel
  94. console.log('[onLoad] channelId:', channelId)
  95. if (!channelId) {
  96. console.log('[onLoad] channelId 为空,退出')
  97. return
  98. }
  99. const token = uni.getStorageSync('token')
  100. console.log('[onLoad] token:', token ? '有值' : '无值(未登录)')
  101. if (!token) return
  102. proxy.$api.post(`/channel/bind?channelId=${channelId}`, {}).then(({ data: res }) => {
  103. console.log('[onLoad] bind 结果:', JSON.stringify(res))
  104. if (res.code === 0) {
  105. uni.showToast({ title: '渠道加入成功', icon: 'success', duration: 2000 })
  106. } else {
  107. console.error('渠道绑定失败:', res.msg)
  108. }
  109. })
  110. }
  111. })
  112. const bannarList = ref([])
  113. const bannarOrigin = ref([])
  114. const current = ref(0)
  115. const typeGrid = ref([])
  116. const isOver = ref(false)
  117. const activityList = ref([])
  118. // 进入页面默认查全部(categoryId 为空),点图标网格才切到具体分类
  119. const queryParams = ref({
  120. page:1,
  121. limit:10,
  122. categoryId:'',
  123. userId:''
  124. })
  125. const toSearch = (keyword) => {
  126. if (!keyword) return
  127. uni.navigateTo({ url: '/pagesHome/allActivity?keyword=' + encodeURIComponent(keyword) })
  128. }
  129. const bannerClick = e => {
  130. let banner = bannarOrigin.value[e];
  131. if(banner&&banner?.redirect){
  132. if(banner?.redirectType==1){//微信公众号
  133. uni.navigateTo({
  134. url:'/pages/webView?src='+banner?.redirect
  135. })
  136. }else if(banner?.redirectType==2){//微信小程序
  137. uni.navigateToMiniProgram({
  138. appId:banner?.redirect
  139. })
  140. }
  141. }
  142. }
  143. const changeType = (item,index) => {
  144. queryParams.value.categoryId = item.id;
  145. initList();
  146. getActivityList();
  147. }
  148. const initList = () => {
  149. queryParams.value.page = 1;
  150. isOver.value = false;
  151. activityList.value = [];
  152. }
  153. const toTurnPage = (url,needLogin) => {
  154. if(!needLogin){
  155. uni.navigateTo({ url })
  156. }
  157. }
  158. const getSwiperList = () => {
  159. proxy.$api.get('/core/advertisement/manage/page',{page:1,limit:-1}).then(({data:res})=>{
  160. if(res.code!==0) return proxy.$showToast(res.msg)
  161. const list = res.data.list.filter(l=>l.enable==1);
  162. bannarOrigin.value = list;
  163. bannarList.value = list.map(l=>l.fileUrl);
  164. })
  165. }
  166. const getTypeList = () => {
  167. proxy.$api.get('/core/activity/category/list').then(({data:res})=>{
  168. if(res.code!==0) return proxy.$showToast(res.msg)
  169. // icon 网格:取配过 icon 的前 8 个分类;空 grid 时不展示
  170. typeGrid.value = res.data.filter(d=>d.iconUrl).slice(0,8).map(d=>({id:d.id,name:d.categoryName,iconUrl:d.iconUrl,bgImageUrl:d.bgImageUrl||''}));
  171. })
  172. }
  173. const scrolltolower = () => {
  174. if(isOver.value) return
  175. getActivityList()
  176. }
  177. const getActivityList = () => {
  178. proxy.$api.get('/core/activity/page',queryParams.value).then(({data:res})=>{
  179. if(res.code!==0) return proxy.$showToast(res.msg)
  180. // 只对本次新增项做格式化,避免 loadMore 时旧项(已格式化成 "yyyy.MM.dd hh:mm")
  181. // 被再次 new Date() 解析为 Invalid Date 导致显示 NaN
  182. const newItems = res.data.list.map(a=>{
  183. let cc = calculateCountdown(a?.signupEndTime);
  184. a.endTimeText = cc===-1?a?.signupEndTime:(`还有${cc.days}天${cc.hours}小时${cc.minutes}分钟`)
  185. a.signupEndTime = a.signupEndTime?new Date(a.signupEndTime).Format('yyyy.MM.dd hh:mm'):''
  186. return a
  187. })
  188. activityList.value = [...activityList.value, ...newItems];
  189. queryParams.value.page++;
  190. if(res.data.list.length===0) isOver.value = true;
  191. })
  192. }
  193. const calculateCountdown = (datetime) => {
  194. if(!datetime) return -1;
  195. const targetDate = new Date(datetime);
  196. const now = new Date();
  197. const difference = targetDate.getTime() - now.getTime();
  198. if (difference <= 0) return -1;
  199. const days = Math.floor(difference / (1000 * 60 * 60 * 24));
  200. const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  201. const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
  202. return {
  203. days,
  204. hours,
  205. minutes
  206. };
  207. }
  208. watch(()=>userStore.token,(newVal,oldVal)=>{
  209. if(oldVal!=newVal&&newVal){
  210. setTimeout(()=>{
  211. uni.showToast({
  212. title: '登录成功',
  213. icon: 'success'
  214. });
  215. },200)
  216. nextTick(()=>{
  217. getActivityList()
  218. })
  219. }
  220. })
  221. onMounted(()=>{
  222. if(uni.getStorageSync('userInfo')){
  223. queryParams.value.userId = JSON.parse(uni.getStorageSync('userInfo'))?.id;
  224. }
  225. getSwiperList()
  226. getTypeList()
  227. getActivityList()
  228. })
  229. import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
  230. onShareAppMessage(() => {
  231. return {
  232. title: '善行少年 - 公益活动平台',
  233. path: '/pages/home'
  234. }
  235. })
  236. onShareTimeline(() => {
  237. return {
  238. title: '善行少年 - 公益活动平台',
  239. path: '/pages/home'
  240. }
  241. })
  242. </script>
  243. <style scoped lang="scss">
  244. ::v-deep .indicator__dot{
  245. width: 48rpx;
  246. height: 4rpx;
  247. background: #E2E5E9;
  248. border-radius: 2rpx;
  249. margin: 0 8rpx;
  250. }
  251. ::v-deep .indicator__dot--active{
  252. width: 48rpx;
  253. height: 4rpx;
  254. background: #00AE57;
  255. border-radius: 2rpx;
  256. }
  257. ::v-deep .u-swiper__wrapper__item__wrapper__image{
  258. border-radius: 24rpx !important;
  259. }
  260. .tab_page{
  261. .c-box{
  262. width: 100%;
  263. height: 100%;
  264. overflow-y: auto;
  265. position: relative;
  266. &-search{
  267. width: 100%;
  268. padding: 20rpx 30rpx 0;
  269. box-sizing: border-box;
  270. }
  271. &-lunbo{
  272. width: 100%;
  273. height: 326rpx;
  274. margin-top: 20rpx;
  275. }
  276. &-title{
  277. margin-top: 58rpx;
  278. &-left{
  279. width: 170rpx;
  280. height: 44rpx;
  281. padding-left: 2rpx;
  282. font-family: PingFang-SC, PingFang-SC;
  283. font-weight: 800;
  284. font-size: 36rpx;
  285. color: #151B29;
  286. line-height: 36rpx;
  287. background-image: url('https://oss.familydaf.cn/sxsnfile/20251218/6198fa26a7ff4fc1bce530fbdfaa20db.png');
  288. background-size: 170rpx 31rpx;
  289. background-position: 0 20rpx;
  290. background-repeat: no-repeat;
  291. }
  292. &-right{
  293. font-family: PingFangSC, PingFang SC;
  294. font-weight: 400;
  295. font-size: 28rpx;
  296. color: #676775;
  297. line-height: 33rpx;
  298. }
  299. }
  300. &-typegrid{
  301. width: 100%;
  302. display: flex;
  303. flex-wrap: wrap;
  304. margin-top: 36rpx;
  305. padding: 0 0;
  306. box-sizing: border-box;
  307. .typegrid-item{
  308. width: calc((100% - 60rpx) / 4);
  309. margin-bottom: 28rpx;
  310. align-items: center;
  311. .typegrid-circle{
  312. width: 112rpx;
  313. height: 112rpx;
  314. display: flex;
  315. align-items: center;
  316. justify-content: center;
  317. image{
  318. width: 96rpx;
  319. height: 96rpx;
  320. }
  321. }
  322. .typegrid-name{
  323. margin-top: 12rpx;
  324. font-family: PingFangSC, PingFang SC;
  325. font-weight: 400;
  326. font-size: 24rpx;
  327. color: #151B29;
  328. line-height: 30rpx;
  329. text-align: center;
  330. }
  331. }
  332. }
  333. &-list{
  334. width: 100%;
  335. flex: 1;
  336. overflow-y: auto;
  337. margin-top: 20rpx;
  338. }
  339. }
  340. }
  341. </style>