FamilyFeed.vue 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. <template>
  2. <view class="family-feed">
  3. <!-- 单条轮播行:标题 + 从下往上翻动 -->
  4. <view class="feed-row" v-if="visibleList.length > 0">
  5. <view class="feed-header" v-if="showHeader">
  6. <text class="feed-title">动态</text>
  7. <text class="feed-icon animate-spin">📢</text>
  8. </view>
  9. <view class="feed-viewport"
  10. @touchstart="onTouchStart"
  11. @touchend="onTouchEnd"
  12. @touchcancel="onTouchEnd">
  13. <!--
  14. 单元素 + @keyframes 关键帧动画:方向由 keyframes 的 from/to 固定,
  15. 不依赖 CSS transition 的起点推导,换条时用 cycleKey 强制重建元素(动画必然从 from 重播)。
  16. - feed-anim-in : from translateY(100%)(底部) → to translateY(0)(从下往上进入)
  17. - feed-anim-out : from translateY(0) → to translateY(-100%)(顶部)(向上翻出)
  18. -->
  19. <view
  20. :key="cycleKey"
  21. class="feed-item"
  22. :class="phase === 'out' ? 'feed-anim-out' : 'feed-anim-in'"
  23. :style="animPauseStyle"
  24. @click="onItemTap(currentItem)">
  25. <text class="feed-item-text">{{ currentItem.text }}</text>
  26. <view class="feed-item-dot" v-if="currentItem.unread"></view>
  27. </view>
  28. </view>
  29. <text class="feed-count" v-if="visibleList.length > 1">{{ visibleList.length }} 条</text>
  30. </view>
  31. <view v-else class="feed-empty">
  32. <text class="feed-empty-text">暂无动态</text>
  33. </view>
  34. <!-- 详情弹层 -->
  35. <view class="feed-detail-mask" v-if="detailVisible" @click="closeDetail">
  36. <view class="feed-detail-card" @click.stop>
  37. <view class="feed-detail-head">
  38. <view class="feed-detail-icon-wrap">
  39. <text class="feed-detail-icon">{{ detailIcon }}</text>
  40. </view>
  41. <view class="feed-detail-head-texts">
  42. <text class="feed-detail-type">{{ detailTypeLabel }}</text>
  43. <text class="feed-detail-time" v-if="detailTime">{{ detailTime }}</text>
  44. </view>
  45. <text class="feed-detail-close" @click="closeDetail">×</text>
  46. </view>
  47. <text class="feed-detail-title">{{ detailTitle }}</text>
  48. <text class="feed-detail-content">{{ detailContent }}</text>
  49. <view class="feed-detail-amount" v-if="detailAmount">
  50. <text class="feed-detail-amount-text">{{ detailAmount }}</text>
  51. </view>
  52. <view class="feed-detail-btn" @click="closeDetail">
  53. <text class="feed-detail-btn-text">知道了</text>
  54. </view>
  55. </view>
  56. </view>
  57. </view>
  58. </template>
  59. <script>
  60. // 动态类型 → 图标 / 类型名(详情弹层使用)
  61. var TYPE_META = {
  62. energy: { icon: '⚡', label: '能量变动' },
  63. points: { icon: '🪙', label: '积分变动' },
  64. cf: { icon: '💰', label: 'CF 值变动' },
  65. task: { icon: '✅', label: '任务完成' },
  66. notice: { icon: '📢', label: '家庭公告' },
  67. notification: { icon: '📬', label: '系统通知' },
  68. feed: { icon: '📣', label: '家庭动态' }
  69. }
  70. var DEFAULT_META = { icon: '📣', label: '消息' }
  71. // 入场/出场动画时长(ms),需与 CSS keyframes 的 duration 保持一致
  72. var ANIM_MS = 400
  73. export default {
  74. name: 'FamilyFeed',
  75. props: {
  76. items: { type: Array, default: function() { return [] } },
  77. notices: { type: Array, default: function() { return [] } },
  78. showHeader: { type: Boolean, default: true },
  79. // 每条动态展示时长(毫秒)
  80. interval: { type: Number, default: 3000 }
  81. },
  82. data: function() {
  83. return {
  84. // phase: 'in' | 'show' | 'out'
  85. // in : 正在从底部翻入(feed-anim-in 动画中)
  86. // show : 已翻入到中间,停留 interval 毫秒
  87. // out : 正在向上翻出(feed-anim-out 动画中)
  88. phase: 'in',
  89. // 切条时 +1 强制重建元素,保证 keyframes 动画从 from 重播
  90. cycleKey: 0,
  91. currentIndex: 0,
  92. _touchPaused: false,
  93. detail: null,
  94. detailVisible: false,
  95. _timer: null
  96. }
  97. },
  98. computed: {
  99. // 合并 items + notices,去重(父页已通过 feedItems 过滤已查看项)
  100. visibleList: function() {
  101. var list = [], seen = {}, i
  102. var items = this.items || []
  103. for (i = 0; i < items.length; i++) {
  104. var it = items[i]
  105. if (!it) continue
  106. var key = it._feedKey || ''
  107. if (key && seen[key]) continue
  108. if (key) seen[key] = true
  109. list.push(it)
  110. }
  111. var notices = this.notices || []
  112. for (i = 0; i < notices.length; i++) {
  113. var n = notices[i]
  114. if (!n) continue
  115. var raw = n.notice || n
  116. var nkey = 'notice-' + (raw.id !== undefined ? raw.id : '')
  117. if (nkey !== 'notice-' && seen[nkey]) continue
  118. if (nkey !== 'notice-') seen[nkey] = true
  119. list.push({
  120. text: n.text || raw.title || '',
  121. time: n.time || '',
  122. type: 'notice',
  123. title: raw.title || '公告',
  124. content: raw.content || '',
  125. amount: '',
  126. unread: false,
  127. _feedKey: nkey,
  128. _srcId: raw.id
  129. })
  130. }
  131. return list
  132. },
  133. currentItem: function() {
  134. var list = this.visibleList
  135. if (!list || list.length === 0) return null
  136. return list[this.currentIndex % list.length]
  137. },
  138. // 触摸/弹层打开时暂停动画,避免后台空转
  139. animPauseStyle: function() {
  140. if (this._touchPaused || this.detailVisible) {
  141. return { animationPlayState: 'paused' }
  142. }
  143. return {}
  144. },
  145. detailMeta: function() {
  146. var type = (this.detail && this.detail.type) || ''
  147. return TYPE_META[type] || DEFAULT_META
  148. },
  149. detailIcon: function() { return this.detailMeta.icon },
  150. detailTypeLabel: function() { return this.detailMeta.label },
  151. detailTitle: function() { return (this.detail && this.detail.title) || '消息详情' },
  152. detailContent: function() { return (this.detail && this.detail.content) || (this.detail && this.detail.text) || '暂无详细内容' },
  153. detailTime: function() { return (this.detail && this.detail.time) || '' },
  154. detailAmount: function() { return (this.detail && this.detail.amount) || '' }
  155. },
  156. watch: {
  157. visibleList: function(newList) {
  158. if (!newList || newList.length === 0) {
  159. this._stopAutoPlay()
  160. return
  161. }
  162. if (this.currentIndex >= newList.length) { this.currentIndex = 0 }
  163. // 列表变化时强制重播入场动画
  164. this.cycleKey++
  165. this.phase = 'in'
  166. this._startAutoPlay()
  167. }
  168. },
  169. mounted: function() { this._startAutoPlay() },
  170. beforeDestroy: function() { this._stopAutoPlay() },
  171. methods: {
  172. // ─── 状态机(定时器链驱动) ───────────────────────────────────
  173. // 元素动画由 keyframes 自行播放;这里只在正确时机切换 phase
  174. _startAutoPlay: function() {
  175. this._stopAutoPlay()
  176. var self = this
  177. this._timer = setTimeout(function() { self._tick() }, ANIM_MS + 50)
  178. },
  179. _stopAutoPlay: function() {
  180. if (this._timer) { clearTimeout(this._timer); this._timer = null }
  181. },
  182. _tick: function() {
  183. var self = this
  184. if (self.visibleList.length === 0) return
  185. if (self._touchPaused || self.detailVisible) {
  186. self._timer = setTimeout(function() { self._tick() }, 200)
  187. return
  188. }
  189. if (self.phase === 'in') {
  190. // 翻入动画结束,进入展示期
  191. self.phase = 'show'
  192. self._timer = setTimeout(function() { self._tick() }, self.interval)
  193. } else if (self.phase === 'show') {
  194. // 展示期结束,开始向上翻出
  195. self.phase = 'out'
  196. self._timer = setTimeout(function() { self._tick() }, ANIM_MS + 50)
  197. } else if (self.phase === 'out') {
  198. // 翻出结束:切到下一条,重建元素重新翻入
  199. self.currentIndex = (self.currentIndex + 1) % (self.visibleList.length || 1)
  200. self.cycleKey++
  201. self.phase = 'in'
  202. self._startAutoPlay()
  203. }
  204. },
  205. // ─── 用户交互 ───────────────────────────────────────────────────
  206. onTouchStart: function() { this._touchPaused = true; this._stopAutoPlay() },
  207. onTouchEnd: function() { this._touchPaused = false; this._startAutoPlay() },
  208. onItemTap: function(item) {
  209. if (!item) return
  210. this.detail = item
  211. this.detailVisible = true
  212. this._touchPaused = false
  213. this._stopAutoPlay()
  214. this.$emit('viewed', item)
  215. },
  216. closeDetail: function() {
  217. this.detailVisible = false
  218. this.detail = null
  219. this._startAutoPlay()
  220. }
  221. }
  222. }
  223. </script>
  224. <style lang="scss">
  225. .family-feed {
  226. margin: 12rpx 30rpx 0;
  227. background: $app-surface;
  228. border-radius: 16rpx;
  229. padding: 10rpx 16rpx;
  230. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  231. overflow: hidden;
  232. }
  233. .feed-row {
  234. display: flex;
  235. flex-direction: row;
  236. align-items: center;
  237. height: 48rpx;
  238. line-height: 48rpx;
  239. overflow: hidden;
  240. }
  241. .feed-header {
  242. display: flex;
  243. flex-direction: row;
  244. align-items: center;
  245. gap: 4rpx;
  246. flex-shrink: 0;
  247. margin-right: 12rpx;
  248. }
  249. .feed-title { font-size: 24rpx; font-weight: bold; color: $facade-text-strong; }
  250. .feed-icon { font-size: 20rpx; }
  251. @keyframes spin {
  252. 0%, 100% { transform: rotate(0deg); }
  253. 25% { transform: rotate(-15deg); }
  254. 75% { transform: rotate(15deg); }
  255. }
  256. .animate-spin {
  257. display: inline-block;
  258. animation: spin 1.2s ease-in-out infinite;
  259. }
  260. /* 视口:固定一行高度,溢出裁剪 */
  261. .feed-viewport {
  262. flex: 1;
  263. min-width: 0;
  264. height: 48rpx;
  265. overflow: hidden;
  266. position: relative;
  267. }
  268. /* 条目基础样式:绝对定位,占满视口一行 */
  269. .feed-item {
  270. position: absolute;
  271. top: 0; left: 0; right: 0;
  272. height: 48rpx;
  273. line-height: 48rpx;
  274. display: flex;
  275. flex-direction: row;
  276. align-items: center;
  277. }
  278. /*
  279. 关键帧动画方向固定,不受初始样式影响:
  280. - 入场(从下往上):底部 translateY(100%) → 中间 translateY(0)
  281. - 出场(向上翻出):中间 translateY(0) → 顶部 translateY(-100%)
  282. forwards 保持动画结束后的位置(元素停在 translateY(0) / translateY(-100%))
  283. */
  284. .feed-anim-in {
  285. animation: feedSlideUpIn 0.4s ease-in-out forwards;
  286. }
  287. .feed-anim-out {
  288. animation: feedSlideUpOut 0.4s ease-in-out forwards;
  289. }
  290. @keyframes feedSlideUpIn {
  291. from { transform: translateY(100%); }
  292. to { transform: translateY(0); }
  293. }
  294. @keyframes feedSlideUpOut {
  295. from { transform: translateY(0); }
  296. to { transform: translateY(-100%); }
  297. }
  298. .feed-item-text {
  299. flex: 1; min-width: 0;
  300. font-size: 24rpx; color: $facade-text-body;
  301. white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  302. }
  303. .feed-item-dot {
  304. width: 12rpx; height: 12rpx;
  305. border-radius: 50%; background: $status-danger;
  306. margin-left: 8rpx; flex-shrink: 0;
  307. }
  308. .feed-count {
  309. flex-shrink: 0; margin-left: 12rpx;
  310. font-size: 20rpx; color: $brand-primary;
  311. background: rgba(249,115,22,0.1);
  312. border-radius: 999rpx; padding: 2rpx 12rpx; line-height: 1.4;
  313. }
  314. .feed-empty {
  315. height: 48rpx; line-height: 48rpx; text-align: center;
  316. }
  317. .feed-empty-text { font-size: 24rpx; color: $facade-text-ghost; }
  318. /* ===== 消息详情弹层 ===== */
  319. .feed-detail-mask {
  320. position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  321. background: rgba(0,0,0,0.5); z-index: 2000;
  322. display: flex; align-items: center; justify-content: center;
  323. padding: 0 60rpx;
  324. }
  325. .feed-detail-card {
  326. width: 100%; max-height: 80vh; overflow-y: auto;
  327. background: $app-surface; border-radius: 24rpx;
  328. padding: 32rpx 28rpx 24rpx;
  329. animation: feedDetailIn 0.25s ease;
  330. }
  331. @keyframes feedDetailIn {
  332. from { opacity: 0; transform: scale(0.92); }
  333. to { opacity: 1; transform: scale(1); }
  334. }
  335. .feed-detail-head { display: flex; flex-direction: row; align-items: center; }
  336. .feed-detail-icon-wrap {
  337. width: 64rpx; height: 64rpx; border-radius: 20rpx;
  338. background: rgba(249,115,22,0.1);
  339. display: flex; align-items: center; justify-content: center;
  340. margin-right: 16rpx; flex-shrink: 0;
  341. }
  342. .feed-detail-icon { font-size: 34rpx; }
  343. .feed-detail-head-texts { flex: 1; min-width: 0; display: flex; flex-direction: column; }
  344. .feed-detail-type { font-size: 26rpx; font-weight: bold; color: $facade-text-strong; }
  345. .feed-detail-time { font-size: 20rpx; color: $facade-text-muted; margin-top: 4rpx; }
  346. .feed-detail-close { font-size: 40rpx; color: $facade-text-ghost-light; line-height: 1; padding: 0 8rpx; flex-shrink: 0; }
  347. .feed-detail-title {
  348. display: block; font-size: 30rpx; font-weight: bold; color: $facade-text-strong;
  349. line-height: 1.4; margin-top: 24rpx;
  350. }
  351. .feed-detail-content {
  352. display: block; font-size: 26rpx; color: $facade-text-body; line-height: 1.6; margin-top: 12rpx;
  353. }
  354. .feed-detail-amount {
  355. display: inline-block; margin-top: 16rpx;
  356. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  357. border-radius: 999rpx; padding: 6rpx 20rpx;
  358. }
  359. .feed-detail-amount-text { font-size: 24rpx; color: $app-surface; font-weight: bold; }
  360. .feed-detail-btn {
  361. margin-top: 28rpx; height: 76rpx; border-radius: 38rpx;
  362. background: linear-gradient(135deg, $brand-primary, $brand-primary-light);
  363. display: flex; align-items: center; justify-content: center;
  364. }
  365. .feed-detail-btn-text { font-size: 28rpx; color: $app-surface; font-weight: bold; }
  366. </style>