checkin.vue 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. <template>
  2. <scroll-view class="container" scroll-y>
  3. <!-- 能量提示 -->
  4. <view class="energy-tip">
  5. <text class="energy-tip-text">⚡ 每次打卡可获得 5 能量值</text>
  6. </view>
  7. <!-- 快速打卡 -->
  8. <view class="quick-checkin">
  9. <view class="section-title">
  10. <text class="section-title-text">今日打卡</text>
  11. </view>
  12. <view class="checkin-types">
  13. <view
  14. v-for="(item, index) in checkinTypes"
  15. :key="index"
  16. class="checkin-btn"
  17. :class="{ disabled: todayTimer }"
  18. @click="doCheckin(item.type)">
  19. <text class="checkin-icon">{{ item.icon }}</text>
  20. <text class="checkin-label">{{ item.label }}</text>
  21. </view>
  22. </view>
  23. <view v-if="todayCount > 0" class="today-count">
  24. <text>今日已打卡 {{ todayCount }} 次</text>
  25. </view>
  26. </view>
  27. <!-- 统计卡片 -->
  28. <view class="stats-card">
  29. <view class="stat-item">
  30. <text class="stat-value">{{ stats.totalCheckins || 0 }}</text>
  31. <text class="stat-label">总打卡</text>
  32. </view>
  33. <view class="stat-divider"></view>
  34. <view class="stat-item">
  35. <text class="stat-value">{{ stats.totalEnergy || 0 }}</text>
  36. <text class="stat-label">总能量</text>
  37. </view>
  38. <view class="stat-divider"></view>
  39. <view class="stat-item">
  40. <text class="stat-value">{{ stats.dailyPlanCount || 0 }}</text>
  41. <text class="stat-label">计划</text>
  42. </view>
  43. <view class="stat-divider"></view>
  44. <view class="stat-item">
  45. <text class="stat-value">{{ stats.savingCount || 0 }}</text>
  46. <text class="stat-label">储蓄</text>
  47. </view>
  48. </view>
  49. <!-- 打卡记录 -->
  50. <view class="record-section">
  51. <view class="section-title">
  52. <text class="section-title-text">打卡记录</text>
  53. </view>
  54. <view v-if="checkins.length === 0" class="empty-state">
  55. <text>暂无打卡记录,开始打卡吧!</text>
  56. </view>
  57. <view
  58. v-for="(item, index) in checkins"
  59. :key="index"
  60. class="record-item"
  61. @longpress="confirmDelete(item)">
  62. <view class="record-left">
  63. <text class="record-icon">{{ getTypeIcon(item.checkinType) }}</text>
  64. <view class="record-info">
  65. <text class="record-type">{{ getTypeLabel(item.checkinType) }}</text>
  66. <text class="record-date">{{ formatDate(item.createdAt) }}</text>
  67. </view>
  68. </view>
  69. <view class="record-right">
  70. <text class="record-energy">+{{ item.earnedEnergy || 5 }}</text>
  71. </view>
  72. </view>
  73. </view>
  74. </scroll-view>
  75. </template>
  76. <script>
  77. import { getCheckinList, getCheckinStats, createCheckin, deleteCheckin } from '../../utils/api'
  78. export default {
  79. data() {
  80. return {
  81. checkinTypes: [
  82. { type: 'daily_plan', label: '记账规划', icon: '📋' },
  83. { type: 'saving', label: '储蓄记录', icon: '💰' },
  84. { type: 'expense', label: '消费记录', icon: '🛒' },
  85. { type: 'review', label: '财务回顾', icon: '📊' }
  86. ],
  87. checkins: [],
  88. stats: {},
  89. todayTimer: null,
  90. todayCount: 0
  91. }
  92. },
  93. onShow() {
  94. this.loadData()
  95. },
  96. methods: {
  97. async loadData() {
  98. try {
  99. const res = await getCheckinList({})
  100. if (res && res.code === 200) {
  101. this.checkins = res.data || []
  102. }
  103. const statsRes = await getCheckinStats({})
  104. if (statsRes && statsRes.code === 200) {
  105. this.stats = statsRes.data || {}
  106. }
  107. this.calcTodayCount()
  108. } catch (e) {
  109. console.error('加载打卡数据失败', e)
  110. }
  111. },
  112. calcTodayCount() {
  113. const today = new Date()
  114. const todayStr = today.toISOString().split('T')[0]
  115. this.todayCount = this.checkins.filter(function(c) {
  116. if (!c.createdAt) return false
  117. return c.createdAt.indexOf(todayStr) === 0
  118. }).length
  119. },
  120. async doCheckin(type) {
  121. if (this.todayTimer) {
  122. uni.showToast({ title: '操作太频繁,请稍后', icon: 'none' })
  123. return
  124. }
  125. try {
  126. const res = await createCheckin({
  127. checkinType: type,
  128. checkinDate: new Date().toISOString().split('T')[0],
  129. earnedEnergy: 5
  130. })
  131. if (res && res.code === 200) {
  132. uni.showToast({ title: '打卡成功!+5能量', icon: 'success' })
  133. this.loadData()
  134. } else {
  135. uni.showToast({ title: res && res.message || '打卡失败', icon: 'none' })
  136. }
  137. } catch (e) {
  138. uni.showToast({ title: '网络错误', icon: 'none' })
  139. }
  140. },
  141. confirmDelete(item) {
  142. var self = this
  143. uni.showModal({
  144. title: '提示',
  145. content: '确定删除此打卡记录?',
  146. success: function(res) {
  147. if (res.confirm) {
  148. self.deleteRecord(item)
  149. }
  150. }
  151. })
  152. },
  153. async deleteRecord(item) {
  154. try {
  155. const res = await deleteCheckin(item.id)
  156. if (res && res.code === 200) {
  157. uni.showToast({ title: '已删除', icon: 'success' })
  158. this.loadData()
  159. }
  160. } catch (e) {
  161. console.error('删除失败', e)
  162. }
  163. },
  164. getTypeIcon(type) {
  165. const map = { daily_plan: '📋', saving: '💰', expense: '🛒', review: '📊' }
  166. return map[type] || '📌'
  167. },
  168. getTypeLabel(type) {
  169. const map = { daily_plan: '记账规划', saving: '储蓄记录', expense: '消费记录', review: '财务回顾' }
  170. return map[type] || type
  171. },
  172. formatDate(dateStr) {
  173. if (!dateStr) return ''
  174. return dateStr.substring(0, 16).replace('T', ' ')
  175. }
  176. }
  177. }
  178. </script>
  179. <style>
  180. .container {
  181. min-height: 100vh;
  182. background: #f5f7fa;
  183. padding: 20rpx 30rpx 120rpx;
  184. }
  185. .energy-tip {
  186. background: linear-gradient(135deg, #F97316, #FB923C);
  187. border-radius: 16rpx;
  188. padding: 20rpx 30rpx;
  189. margin-bottom: 24rpx;
  190. }
  191. .energy-tip-text {
  192. color: #fff;
  193. font-size: 26rpx;
  194. font-weight: 500;
  195. }
  196. .section-title {
  197. margin-bottom: 20rpx;
  198. }
  199. .section-title-text {
  200. font-size: 30rpx;
  201. font-weight: 600;
  202. color: #333;
  203. }
  204. .quick-checkin {
  205. background: #fff;
  206. border-radius: 20rpx;
  207. padding: 24rpx;
  208. margin-bottom: 24rpx;
  209. }
  210. .checkin-types {
  211. display: flex;
  212. flex-direction: row;
  213. justify-content: space-between;
  214. }
  215. .checkin-btn {
  216. display: flex;
  217. flex-direction: column;
  218. align-items: center;
  219. justify-content: center;
  220. width: 150rpx;
  221. height: 120rpx;
  222. background: #FFF7ED;
  223. border-radius: 16rpx;
  224. border: 2rpx solid #FED7AA;
  225. }
  226. .checkin-btn.disabled {
  227. opacity: 0.5;
  228. }
  229. .checkin-icon {
  230. font-size: 40rpx;
  231. margin-bottom: 8rpx;
  232. }
  233. .checkin-label {
  234. font-size: 24rpx;
  235. color: #666;
  236. }
  237. .today-count {
  238. text-align: center;
  239. margin-top: 16rpx;
  240. font-size: 24rpx;
  241. color: #999;
  242. }
  243. .stats-card {
  244. background: #fff;
  245. border-radius: 20rpx;
  246. padding: 24rpx;
  247. display: flex;
  248. flex-direction: row;
  249. justify-content: space-around;
  250. align-items: center;
  251. margin-bottom: 24rpx;
  252. }
  253. .stat-item {
  254. display: flex;
  255. flex-direction: column;
  256. align-items: center;
  257. }
  258. .stat-value {
  259. font-size: 36rpx;
  260. font-weight: 700;
  261. color: #F97316;
  262. }
  263. .stat-label {
  264. font-size: 24rpx;
  265. color: #999;
  266. margin-top: 4rpx;
  267. }
  268. .stat-divider {
  269. width: 2rpx;
  270. height: 60rpx;
  271. background: #eee;
  272. }
  273. .record-section {
  274. background: #fff;
  275. border-radius: 20rpx;
  276. padding: 24rpx;
  277. }
  278. .empty-state {
  279. text-align: center;
  280. padding: 48rpx 0;
  281. color: #999;
  282. font-size: 26rpx;
  283. }
  284. .record-item {
  285. display: flex;
  286. flex-direction: row;
  287. justify-content: space-between;
  288. align-items: center;
  289. padding: 20rpx 0;
  290. border-bottom: 2rpx solid #f5f5f5;
  291. }
  292. .record-item:last-child {
  293. border-bottom: none;
  294. }
  295. .record-left {
  296. display: flex;
  297. flex-direction: row;
  298. align-items: center;
  299. }
  300. .record-icon {
  301. font-size: 36rpx;
  302. margin-right: 16rpx;
  303. }
  304. .record-info {
  305. display: flex;
  306. flex-direction: column;
  307. }
  308. .record-type {
  309. font-size: 28rpx;
  310. color: #333;
  311. font-weight: 500;
  312. }
  313. .record-date {
  314. font-size: 22rpx;
  315. color: #999;
  316. margin-top: 4rpx;
  317. }
  318. .record-right {}
  319. .record-energy {
  320. font-size: 28rpx;
  321. color: #F97316;
  322. font-weight: 600;
  323. }
  324. </style>