sign-in.vue 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613
  1. <template>
  2. <view class="sign-in-page">
  3. <!-- 顶部导航栏 -->
  4. <view class="header">
  5. <view class="back-btn" @click="goBack">
  6. <text class="back-icon">‹</text>
  7. </view>
  8. <text class="header-title">签到</text>
  9. <view class="placeholder"></view>
  10. </view>
  11. <scroll-view scroll-y class="content">
  12. <!-- 签到日历标题 -->
  13. <view class="calendar-header">
  14. <text class="calendar-title">{{ currentMonthText }}签到日历</text>
  15. <text class="calendar-subtitle">红线相系,良缘成就一生欢喜。</text>
  16. </view>
  17. <!-- 月份选择 -->
  18. <view class="month-selector">
  19. <text class="month-text">{{ currentMonthText }}</text>
  20. <view class="streak-badge">
  21. <text class="streak-text">红线季良缘</text>
  22. </view>
  23. <view class="calendar-icon">📅</view>
  24. </view>
  25. <!-- 日历 -->
  26. <view class="calendar">
  27. <view class="weekdays">
  28. <text class="weekday">日</text>
  29. <text class="weekday">一</text>
  30. <text class="weekday">二</text>
  31. <text class="weekday">三</text>
  32. <text class="weekday">四</text>
  33. <text class="weekday">五</text>
  34. <text class="weekday">六</text>
  35. </view>
  36. <view class="days">
  37. <view
  38. v-for="(day, index) in calendarDays"
  39. :key="index"
  40. class="day-item"
  41. :class="{
  42. 'other-month': day.isOtherMonth,
  43. 'signed': day.isSigned,
  44. 'today': day.isToday
  45. }"
  46. >
  47. <text class="day-number">{{ day.day }}</text>
  48. </view>
  49. </view>
  50. </view>
  51. <!-- 签到统计 -->
  52. <view class="sign-stats">
  53. <view class="stat-item">
  54. <text class="stat-icon">📊</text>
  55. <text class="stat-text">累计签到: {{ totalSignDays }}天</text>
  56. </view>
  57. <view class="stat-item">
  58. <text class="stat-icon">💗</text>
  59. <text class="stat-text">可用积分: {{ availablePoints }}</text>
  60. </view>
  61. <view class="gift-icon">🎁</view>
  62. </view>
  63. <!-- 连续签到进度 -->
  64. <view class="streak-progress">
  65. <text class="progress-label">连续签到奖励</text>
  66. <text class="progress-days">连续{{ consecutiveDays }}天</text>
  67. </view>
  68. <view class="progress-bar">
  69. <view class="progress-fill" :style="{ width: progressWidth }"></view>
  70. </view>
  71. <!-- 签到按钮 -->
  72. <view class="sign-button" :class="{ disabled: isSigned }" @click="handleSignIn">
  73. <text class="button-text">{{ isSigned ? '今日已签到' : '今日签到 +10积分' }}</text>
  74. </view>
  75. <!-- 提示文字 -->
  76. <view class="tips">
  77. <text class="tips-text">坚持签到,缘分更近一步</text>
  78. <text class="tips-sub">品牌出品 | 青鸟之恋1跟平台</text>
  79. </view>
  80. </scroll-view>
  81. </view>
  82. </template>
  83. <script>
  84. import api from '@/utils/api.js'
  85. export default {
  86. data() {
  87. return {
  88. currentYear: new Date().getFullYear(),
  89. currentMonth: new Date().getMonth() + 1,
  90. totalSignDays: 0,
  91. availablePoints: 0,
  92. consecutiveDays: 0,
  93. makerId: null,
  94. isSigned: false,
  95. calendarDays: [],
  96. signedDates: [], // 已签到的日期字符串数组,格式:YYYY-MM-DD
  97. currentMonthText: '' // 当前月份显示文本
  98. }
  99. },
  100. computed: {
  101. progressWidth() {
  102. // 假设7天为一个周期
  103. const progress = (this.consecutiveDays % 7) / 7 * 100
  104. return progress + '%'
  105. }
  106. },
  107. onLoad() {
  108. this.initData()
  109. },
  110. onShow() {
  111. // 每次显示页面时刷新数据
  112. if (this.makerId) {
  113. this.loadSignInData()
  114. }
  115. },
  116. methods: {
  117. goBack() {
  118. uni.navigateBack()
  119. },
  120. async initData() {
  121. // 获取红娘ID
  122. const userInfo = uni.getStorageSync('userInfo')
  123. if (userInfo && userInfo.matchmakerId) {
  124. this.makerId = userInfo.matchmakerId
  125. } else if (userInfo && userInfo.userId) {
  126. // 如果没有matchmakerId,通过API获取
  127. try {
  128. const res = await api.matchmaker.getByUserId(userInfo.userId)
  129. let matchmaker = res
  130. if (res && res.data) {
  131. matchmaker = res.data
  132. }
  133. if (matchmaker && (matchmaker.matchmakerId || matchmaker.matchmaker_id)) {
  134. this.makerId = matchmaker.matchmakerId || matchmaker.matchmaker_id
  135. userInfo.matchmakerId = this.makerId
  136. uni.setStorageSync('userInfo', userInfo)
  137. }
  138. } catch (e) {
  139. console.error('获取红娘信息失败:', e)
  140. }
  141. }
  142. await this.loadSignInData()
  143. this.generateCalendar()
  144. },
  145. async loadSignInData() {
  146. if (!this.makerId) return
  147. try {
  148. // 获取签到统计数据
  149. const statsRes = await api.matchmaker.checkinStats(this.makerId)
  150. let stats = statsRes
  151. if (statsRes && statsRes.data) {
  152. stats = statsRes.data
  153. }
  154. this.totalSignDays = stats.totalDays || 0
  155. this.consecutiveDays = stats.continuousDays || 0
  156. // 获取积分余额
  157. const balanceRes = await api.pointsMall.getBalance(this.makerId)
  158. this.availablePoints = balanceRes.balance || 0
  159. // 获取本月所有签到日期
  160. const checkinInfoRes = await api.matchmaker.checkinInfo(this.makerId, this.currentYear, this.currentMonth)
  161. console.log('签到信息API响应:', JSON.stringify(checkinInfoRes, null, 2))
  162. let checkinInfo = checkinInfoRes
  163. if (checkinInfoRes && checkinInfoRes.data) {
  164. checkinInfo = checkinInfoRes.data
  165. }
  166. console.log('处理后的签到信息:', JSON.stringify(checkinInfo, null, 2))
  167. // 获取已签到日期列表
  168. this.signedDates = checkinInfo.checkedDates || []
  169. console.log('已签到日期列表:', JSON.stringify(this.signedDates, null, 2))
  170. console.log('已签到日期数量:', this.signedDates.length)
  171. // 检查今日签到状态
  172. const statusRes = await api.matchmaker.checkinStatus(this.makerId)
  173. console.log('签到状态API响应:', statusRes)
  174. let statusData = statusRes
  175. if (statusRes && statusRes.data !== undefined) {
  176. statusData = statusRes.data
  177. }
  178. this.isSigned = statusData === true || statusData?.isCheckedIn === true || statusData?.checked === true
  179. console.log('今日签到状态:', this.isSigned)
  180. // 更新日历,显示所有已签到日期
  181. this.generateCalendar()
  182. } catch (e) {
  183. console.error('加载签到数据失败:', e)
  184. }
  185. },
  186. generateCalendar() {
  187. console.log('开始生成日历,已签到日期:', this.signedDates)
  188. const year = this.currentYear
  189. const month = this.currentMonth
  190. console.log('当前年月:', year, month)
  191. // 更新月份显示文本
  192. this.currentMonthText = `${year}年${month}月`
  193. console.log('月份显示文本:', this.currentMonthText)
  194. // 获取当月第一天是星期几
  195. const firstDay = new Date(year, month - 1, 1).getDay()
  196. // 获取当月天数
  197. const daysInMonth = new Date(year, month, 0).getDate()
  198. // 获取上月天数
  199. const prevMonthDays = new Date(year, month - 1, 0).getDate()
  200. console.log('日历参数: 第一天星期几:', firstDay, '当月天数:', daysInMonth, '上月天数:', prevMonthDays)
  201. // 获取今天的日期字符串
  202. const today = new Date()
  203. const todayStr = this.formatDate(today)
  204. console.log('今天日期:', todayStr)
  205. const days = []
  206. // 添加上月日期
  207. for (let i = firstDay - 1; i >= 0; i--) {
  208. days.push({
  209. day: prevMonthDays - i,
  210. isOtherMonth: true,
  211. isSigned: false,
  212. isToday: false
  213. })
  214. }
  215. // 添加当月日期
  216. for (let i = 1; i <= daysInMonth; i++) {
  217. const date = new Date(year, month - 1, i)
  218. const dateStr = this.formatDate(date)
  219. const isToday = dateStr === todayStr
  220. const isSigned = this.signedDates.includes(dateStr)
  221. console.log('生成日期:', dateStr, '是否已签到:', isSigned, '是否今天:', isToday)
  222. days.push({
  223. day: i,
  224. isOtherMonth: false,
  225. isSigned: isSigned,
  226. isToday: isToday
  227. })
  228. }
  229. // 添加下月日期补齐
  230. const remainingDays = 42 - days.length // 6行7列
  231. for (let i = 1; i <= remainingDays; i++) {
  232. days.push({
  233. day: i,
  234. isOtherMonth: true,
  235. isSigned: false,
  236. isToday: false
  237. })
  238. }
  239. this.calendarDays = days
  240. console.log('生成的日历数据:', this.calendarDays)
  241. },
  242. // 格式化日期为YYYY-MM-DD
  243. formatDate(date) {
  244. const year = date.getFullYear()
  245. const month = String(date.getMonth() + 1).padStart(2, '0')
  246. const day = String(date.getDate()).padStart(2, '0')
  247. return `${year}-${month}-${day}`
  248. },
  249. async handleSignIn() {
  250. if (this.isSigned) {
  251. uni.showToast({
  252. title: '今日已签到',
  253. icon: 'none'
  254. })
  255. return
  256. }
  257. if (!this.makerId) {
  258. uni.showToast({ title: '请先登录', icon: 'none' })
  259. return
  260. }
  261. try {
  262. // 调用后端签到接口
  263. const signRes = await api.matchmaker.doCheckin(this.makerId)
  264. // 签到成功后更新状态
  265. this.isSigned = true
  266. this.totalSignDays++
  267. this.consecutiveDays++
  268. // 更新今天的签到状态
  269. const today = new Date()
  270. const todayStr = this.formatDate(today)
  271. if (!this.signedDates.includes(todayStr)) {
  272. this.signedDates.push(todayStr)
  273. }
  274. this.generateCalendar()
  275. // 刷新积分余额
  276. const balanceRes = await api.pointsMall.getBalance(this.makerId)
  277. this.availablePoints = balanceRes.balance || 0
  278. // 重新加载签到信息,确保数据最新
  279. await this.loadSignInData()
  280. this.generateCalendar()
  281. uni.showToast({
  282. title: '签到成功 +5积分',
  283. icon: 'success'
  284. })
  285. } catch (e) {
  286. console.error('签到失败:', e)
  287. // 如果是已签到的错误,更新状态
  288. if (e.message && e.message.includes('已签到')) {
  289. this.isSigned = true
  290. }
  291. uni.showToast({
  292. title: e.message || '签到失败',
  293. icon: 'none'
  294. })
  295. }
  296. }
  297. }
  298. }
  299. </script>
  300. <style lang="scss" scoped>
  301. .sign-in-page {
  302. min-height: 100vh;
  303. background: linear-gradient(180deg, #F3E5F5 0%, #FFF9F9 40%);
  304. display: flex;
  305. flex-direction: column;
  306. }
  307. /* 顶部导航栏 */
  308. .header {
  309. display: flex;
  310. align-items: center;
  311. justify-content: space-between;
  312. padding: 20rpx 30rpx;
  313. padding-top: calc(20rpx + env(safe-area-inset-top));
  314. background: transparent;
  315. .back-btn {
  316. width: 50rpx;
  317. height: 50rpx;
  318. display: flex;
  319. align-items: center;
  320. justify-content: center;
  321. .back-icon {
  322. font-size: 40rpx;
  323. color: #333;
  324. }
  325. }
  326. .header-title {
  327. font-size: 32rpx;
  328. font-weight: bold;
  329. color: #333;
  330. }
  331. .placeholder {
  332. width: 50rpx;
  333. }
  334. }
  335. .content {
  336. flex: 1;
  337. padding: 10rpx 20rpx 20rpx;
  338. }
  339. /* 日历标题 */
  340. .calendar-header {
  341. margin-bottom: 20rpx;
  342. .calendar-title {
  343. display: block;
  344. font-size: 32rpx;
  345. font-weight: bold;
  346. color: #333;
  347. margin-bottom: 8rpx;
  348. }
  349. .calendar-subtitle {
  350. display: block;
  351. font-size: 24rpx;
  352. color: #FF6B9D;
  353. }
  354. }
  355. /* 月份选择 */
  356. .month-selector {
  357. display: flex;
  358. align-items: center;
  359. justify-content: space-between;
  360. margin-bottom: 20rpx;
  361. .month-text {
  362. font-size: 28rpx;
  363. font-weight: bold;
  364. color: #7B1FA2;
  365. }
  366. .streak-badge {
  367. background: linear-gradient(135deg, #9C27B0 0%, #BA68C8 100%);
  368. color: #FFFFFF;
  369. font-size: 22rpx;
  370. font-weight: bold;
  371. padding: 6rpx 14rpx;
  372. border-radius: 12rpx;
  373. }
  374. .calendar-icon {
  375. font-size: 32rpx;
  376. }
  377. }
  378. /* 日历 */
  379. .calendar {
  380. background: #FFFFFF;
  381. border-radius: 16rpx;
  382. padding: 20rpx;
  383. margin-bottom: 20rpx;
  384. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  385. .weekdays {
  386. display: grid;
  387. grid-template-columns: repeat(7, 1fr);
  388. margin-bottom: 15rpx;
  389. .weekday {
  390. text-align: center;
  391. font-size: 24rpx;
  392. color: #666;
  393. font-weight: bold;
  394. }
  395. }
  396. .days {
  397. display: grid;
  398. grid-template-columns: repeat(7, 1fr);
  399. gap: 8rpx;
  400. .day-item {
  401. aspect-ratio: 1;
  402. display: flex;
  403. align-items: center;
  404. justify-content: center;
  405. border-radius: 12rpx;
  406. background: #F5F5F5;
  407. &.other-month {
  408. background: transparent;
  409. .day-number {
  410. color: #CCC;
  411. }
  412. }
  413. &.signed {
  414. background: #F3E5F5;
  415. border: 2rpx solid #CE93D8;
  416. .day-number {
  417. color: #7B1FA2;
  418. font-weight: bold;
  419. }
  420. }
  421. &.today {
  422. background: #9C27B0;
  423. border: 2rpx solid #7B1FA2;
  424. .day-number {
  425. color: #FFFFFF;
  426. font-weight: bold;
  427. }
  428. }
  429. &.today.signed {
  430. background: #9C27B0;
  431. border: 2rpx solid #7B1FA2;
  432. .day-number {
  433. color: #FFFFFF;
  434. font-weight: bold;
  435. }
  436. }
  437. .day-number {
  438. font-size: 24rpx;
  439. color: #333;
  440. }
  441. }
  442. }
  443. }
  444. /* 签到统计 */
  445. .sign-stats {
  446. display: flex;
  447. align-items: center;
  448. justify-content: space-between;
  449. background: #FFFFFF;
  450. border-radius: 16rpx;
  451. padding: 15rpx 20rpx;
  452. margin-bottom: 15rpx;
  453. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);
  454. .stat-item {
  455. display: flex;
  456. align-items: center;
  457. gap: 8rpx;
  458. flex: 1;
  459. .stat-icon {
  460. font-size: 24rpx;
  461. }
  462. .stat-text {
  463. font-size: 24rpx;
  464. color: #666;
  465. }
  466. }
  467. .gift-icon {
  468. font-size: 32rpx;
  469. }
  470. }
  471. /* 连续签到进度 */
  472. .streak-progress {
  473. display: flex;
  474. align-items: center;
  475. justify-content: space-between;
  476. margin-bottom: 10rpx;
  477. .progress-label {
  478. font-size: 24rpx;
  479. color: #666;
  480. }
  481. .progress-days {
  482. font-size: 24rpx;
  483. color: #9C27B0;
  484. font-weight: bold;
  485. }
  486. }
  487. .progress-bar {
  488. width: 100%;
  489. height: 12rpx;
  490. background: #E0E0E0;
  491. border-radius: 6rpx;
  492. margin-bottom: 25rpx;
  493. overflow: hidden;
  494. .progress-fill {
  495. height: 100%;
  496. background: linear-gradient(90deg, #9C27B0 0%, #BA68C8 100%);
  497. border-radius: 6rpx;
  498. transition: width 0.3s ease;
  499. }
  500. }
  501. /* 签到按钮 */
  502. .sign-button {
  503. background: linear-gradient(135deg, #9C27B0 0%, #BA68C8 100%);
  504. border-radius: 16rpx;
  505. padding: 20rpx;
  506. margin-bottom: 20rpx;
  507. box-shadow: 0 4rpx 12rpx rgba(156, 39, 176, 0.3);
  508. &.disabled {
  509. background: #E0E0E0;
  510. box-shadow: none;
  511. }
  512. .button-text {
  513. display: block;
  514. text-align: center;
  515. font-size: 28rpx;
  516. font-weight: bold;
  517. color: #FFFFFF;
  518. }
  519. }
  520. /* 提示文字 */
  521. .tips {
  522. text-align: center;
  523. .tips-text {
  524. display: block;
  525. font-size: 26rpx;
  526. color: #666;
  527. margin-bottom: 8rpx;
  528. }
  529. .tips-sub {
  530. display: block;
  531. font-size: 22rpx;
  532. color: #999;
  533. }
  534. }
  535. </style>