home.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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" :style="item.bgImageUrl?{backgroundImage:`url(${item.bgImageUrl})`,backgroundSize:'100% 100%'}:{}">
  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-type">
  49. <scroll-view class="scroll-view_H" scroll-x="true" scroll-with-animation="true" :scroll-left="scrollLeft">
  50. <view class="scroll-view-item_H" :id="'svih_'+index" v-for="(item,index) in typeList" :key="index" @click="changeType(item,index)">
  51. <view class="cl_item" :class="{'active':tlIndex===index}">
  52. <text>{{item.name}}</text>
  53. </view>
  54. </view>
  55. </scroll-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, 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 typeList = ref([])
  125. const typeGrid = ref([])
  126. const tlIndex = ref(0)
  127. const scrollLeft = ref(0)
  128. const isOver = ref(false)
  129. const activityList = ref([])
  130. const queryParams = ref({
  131. page:1,
  132. limit:10,
  133. categoryId:'',
  134. userId:''
  135. })
  136. const toSearch = (keyword) => {
  137. if (!keyword) return
  138. uni.navigateTo({ url: '/pagesHome/allActivity?keyword=' + encodeURIComponent(keyword) })
  139. }
  140. const bannerClick = e => {
  141. let banner = bannarOrigin.value[e];
  142. if(banner&&banner?.redirect){
  143. if(banner?.redirectType==1){//微信公众号
  144. uni.navigateTo({
  145. url:'/pages/webView?src='+banner?.redirect
  146. })
  147. }else if(banner?.redirectType==2){//微信小程序
  148. uni.navigateToMiniProgram({
  149. appId:banner?.redirect
  150. })
  151. }
  152. }
  153. }
  154. const changeType = (item,index) => {
  155. queryParams.value.categoryId = item.id;
  156. // grid 与 tab 是同一个筛选行为,但两者的 index 不对齐,按 id 在 typeList 中重新查找 tab 索引
  157. const tabIdx = typeList.value.findIndex(t => t.id === item.id);
  158. tlIndex.value = tabIdx >= 0 ? tabIdx : 0;
  159. // if(typeList.value.length>4){
  160. // if(index<3) scrollLeft.value = 0
  161. // else{
  162. // scrollLeft.value = (index-2)*172/2;
  163. // }
  164. // }
  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. typeList.value = [{id:'',name:'全部'},...res.data.map(d=>({id:d.id,name:d.categoryName,iconUrl:d.iconUrl||'',bgImageUrl:d.bgImageUrl||''}))];
  190. // icon+圆形蒙层 grid:取前 8 个;至少有一个配过 icon 的才展示,避免空 grid
  191. 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||''}));
  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. activityList.value = [...activityList.value,...res.data.list];
  202. activityList.value.forEach(a=>{
  203. let cc = calculateCountdown(a?.signupEndTime);
  204. a.endTimeText = cc===-1?a?.signupEndTime:(`还有${cc.days}天${cc.hours}小时${cc.minutes}分钟`)
  205. a.signupEndTime = a.signupEndTime?new Date(a.signupEndTime).Format('yyyy.MM.dd hh:mm'):''
  206. })
  207. queryParams.value.page++;
  208. if(res.data.list.length===0) isOver.value = true;
  209. })
  210. }
  211. const calculateCountdown = (datetime) => {
  212. if(!datetime) return -1;
  213. const targetDate = new Date(datetime);
  214. const now = new Date();
  215. const difference = targetDate.getTime() - now.getTime();
  216. if (difference <= 0) return -1;
  217. const days = Math.floor(difference / (1000 * 60 * 60 * 24));
  218. const hours = Math.floor((difference % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
  219. const minutes = Math.floor((difference % (1000 * 60 * 60)) / (1000 * 60));
  220. return {
  221. days,
  222. hours,
  223. minutes
  224. };
  225. }
  226. watch(()=>userStore.token,(newVal,oldVal)=>{
  227. if(oldVal!=newVal&&newVal){
  228. setTimeout(()=>{
  229. uni.showToast({
  230. title: '登录成功',
  231. icon: 'success'
  232. });
  233. },200)
  234. nextTick(()=>{
  235. getActivityList()
  236. })
  237. }
  238. })
  239. onMounted(()=>{
  240. if(uni.getStorageSync('userInfo')){
  241. queryParams.value.userId = JSON.parse(uni.getStorageSync('userInfo'))?.id;
  242. }
  243. getSwiperList()
  244. getTypeList()
  245. getActivityList()
  246. })
  247. import { onShareAppMessage, onShareTimeline } from '@dcloudio/uni-app'
  248. onShareAppMessage(() => {
  249. return {
  250. title: '善行少年 - 公益活动平台',
  251. path: '/pages/home'
  252. }
  253. })
  254. onShareTimeline(() => {
  255. return {
  256. title: '善行少年 - 公益活动平台',
  257. path: '/pages/home'
  258. }
  259. })
  260. </script>
  261. <style scoped lang="scss">
  262. ::v-deep .indicator__dot{
  263. width: 48rpx;
  264. height: 4rpx;
  265. background: #E2E5E9;
  266. border-radius: 2rpx;
  267. margin: 0 8rpx;
  268. }
  269. ::v-deep .indicator__dot--active{
  270. width: 48rpx;
  271. height: 4rpx;
  272. background: #00AE57;
  273. border-radius: 2rpx;
  274. }
  275. ::v-deep .u-swiper__wrapper__item__wrapper__image{
  276. border-radius: 24rpx !important;
  277. }
  278. .scroll-view_H {
  279. white-space: nowrap;
  280. width: 100%;
  281. }
  282. .scroll-view-item_H {
  283. display: inline-block;
  284. // width: 152rpx;
  285. height: 100%;
  286. margin-left: 20rpx;
  287. &:first-child{
  288. margin-left: 0;
  289. }
  290. }
  291. .tab_page{
  292. .c-box{
  293. width: 100%;
  294. height: 100%;
  295. overflow-y: auto;
  296. position: relative;
  297. &-search{
  298. width: 100%;
  299. padding: 20rpx 30rpx 0;
  300. box-sizing: border-box;
  301. }
  302. &-lunbo{
  303. width: 100%;
  304. height: 326rpx;
  305. margin-top: 20rpx;
  306. }
  307. &-title{
  308. margin-top: 58rpx;
  309. &-left{
  310. width: 170rpx;
  311. height: 44rpx;
  312. padding-left: 2rpx;
  313. font-family: PingFang-SC, PingFang-SC;
  314. font-weight: 800;
  315. font-size: 36rpx;
  316. color: #151B29;
  317. line-height: 36rpx;
  318. background-image: url('https://oss.familydaf.cn/sxsnfile/20251218/6198fa26a7ff4fc1bce530fbdfaa20db.png');
  319. background-size: 170rpx 31rpx;
  320. background-position: 0 20rpx;
  321. background-repeat: no-repeat;
  322. }
  323. &-right{
  324. font-family: PingFangSC, PingFang SC;
  325. font-weight: 400;
  326. font-size: 28rpx;
  327. color: #676775;
  328. line-height: 33rpx;
  329. }
  330. }
  331. &-type{
  332. width: 100%;
  333. height: 68rpx;
  334. margin-top: 31rpx;
  335. .cl_item{
  336. // width: 152rpx;
  337. padding: 0 21rpx;
  338. height: 68rpx;
  339. background: #FFFFFF;
  340. border-radius: 34rpx;
  341. font-family: PingFangSC, PingFang SC;
  342. font-weight: 400;
  343. font-size: 28rpx;
  344. color: #676775;
  345. line-height: 68rpx;
  346. text-align: center;
  347. &.active{
  348. background: #B7F358;
  349. font-weight: bold;
  350. font-size: 30rpx;
  351. color: #151B29;
  352. }
  353. }
  354. }
  355. &-typegrid{
  356. width: 100%;
  357. display: flex;
  358. flex-wrap: wrap;
  359. margin-top: 36rpx;
  360. padding: 0 0;
  361. box-sizing: border-box;
  362. .typegrid-item{
  363. width: calc((100% - 60rpx) / 4);
  364. margin-bottom: 28rpx;
  365. align-items: center;
  366. .typegrid-circle{
  367. width: 112rpx;
  368. height: 112rpx;
  369. border-radius: 50%;
  370. background: #F4F8FF;
  371. background-repeat: no-repeat;
  372. background-position: center;
  373. display: flex;
  374. align-items: center;
  375. justify-content: center;
  376. image{
  377. width: 56rpx;
  378. height: 56rpx;
  379. }
  380. }
  381. .typegrid-name{
  382. margin-top: 12rpx;
  383. font-family: PingFangSC, PingFang SC;
  384. font-weight: 400;
  385. font-size: 24rpx;
  386. color: #151B29;
  387. line-height: 30rpx;
  388. text-align: center;
  389. }
  390. }
  391. }
  392. &-list{
  393. width: 100%;
  394. flex: 1;
  395. overflow-y: auto;
  396. margin-top: 20rpx;
  397. }
  398. }
  399. }
  400. </style>