FamilyFeed.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. <view class="feed-item"
  14. :class="itemClass"
  15. :style="itemInlineStyle"
  16. @click="onItemTap(currentItem)">
  17. <text class="feed-item-text">{{ currentItem.text }}</text>
  18. <view class="feed-item-dot" v-if="currentItem.unread"></view>
  19. </view>
  20. </view>
  21. <text class="feed-count" v-if="visibleList.length > 1">{{ visibleList.length }} 条</text>
  22. </view>
  23. <view v-else class="feed-empty">
  24. <text class="feed-empty-text">暂无动态</text>
  25. </view>
  26. <!-- 详情弹层 -->
  27. <view class="feed-detail-mask" v-if="detailVisible" @click="closeDetail">
  28. <view class="feed-detail-card" @click.stop>
  29. <view class="feed-detail-head">
  30. <view class="feed-detail-icon-wrap">
  31. <text class="feed-detail-icon">{{ detailIcon }}</text>
  32. </view>
  33. <view class="feed-detail-head-texts">
  34. <text class="feed-detail-type">{{ detailTypeLabel }}</text>
  35. <text class="feed-detail-time" v-if="detailTime">{{ detailTime }}</text>
  36. </view>
  37. <text class="feed-detail-close" @click="closeDetail">×</text>
  38. </view>
  39. <text class="feed-detail-title">{{ detailTitle }}</text>
  40. <text class="feed-detail-content">{{ detailContent }}</text>
  41. <view class="feed-detail-amount" v-if="detailAmount">
  42. <text class="feed-detail-amount-text">{{ detailAmount }}</text>
  43. </view>
  44. <view class="feed-detail-btn" @click="closeDetail">
  45. <text class="feed-detail-btn-text">知道了</text>
  46. </view>
  47. </view>
  48. </view>
  49. </view>
  50. </template>
  51. <script>
  52. var TYPE_META = {
  53. energy: { icon: '⚡', label: '能量变动' },
  54. points: { icon: '🪙', label: '积分变动' },
  55. cf: { icon: '💰', label: 'CF 值变动' },
  56. task: { icon: '✅', label: '任务完成' },
  57. notice: { icon: '📢', label: '家庭公告' },
  58. notification: { icon: '📬', label: '系统通知' },
  59. feed: { icon: '📣', label: '家庭动态' }
  60. }
  61. var DEFAULT_META = { icon: '📣', label: '消息' }
  62. export default {
  63. name: 'FamilyFeed',
  64. props: {
  65. items: { type: Array, default: function() { return [] } },
  66. notices: { type: Array, default: function() { return [] } },
  67. showHeader: { type: Boolean, default: true },
  68. interval: { type: Number, default: 3000 }
  69. },
  70. data: function() {
  71. return {
  72. // state: 'entering' | 'visible' | 'leaving'
  73. // entering: jump to translateY(100%), next frame animates to 0
  74. // visible: displayed for interval ms, then animates out
  75. // leaving: animates to translateY(-100%), then advance to next item
  76. state: 'entering',
  77. currentIndex: 0,
  78. _touchPaused: false,
  79. detail: null,
  80. detailVisible: false,
  81. _timer: null
  82. }
  83. },
  84. computed: {
  85. visibleList: function() {
  86. var list = [], seen = {}, i
  87. var items = this.items || []
  88. for (i = 0; i < items.length; i++) {
  89. var it = items[i]
  90. if (!it) continue
  91. var key = it._feedKey || ''
  92. if (key && seen[key]) continue
  93. if (key) seen[key] = true
  94. list.push(it)
  95. }
  96. var notices = this.notices || []
  97. for (i = 0; i < notices.length; i++) {
  98. var n = notices[i]
  99. if (!n) continue
  100. var raw = n.notice || n
  101. var nkey = 'notice-' + (raw.id !== undefined ? raw.id : '')
  102. if (nkey !== 'notice-' && seen[nkey]) continue
  103. if (nkey !== 'notice-') seen[nkey] = true
  104. list.push({
  105. text: n.text || raw.title || '',
  106. time: n.time || '',
  107. type: 'notice',
  108. title: raw.title || '公告',
  109. content: raw.content || '',
  110. amount: '',
  111. unread: false,
  112. _feedKey: nkey,
  113. _srcId: raw.id
  114. })
  115. }
  116. return list
  117. },
  118. currentItem: function() {
  119. var list = this.visibleList
  120. if (!list || list.length === 0) return null
  121. return list[this.currentIndex % list.length]
  122. },
  123. itemClass: function() {
  124. if (this.state === 'visible') return 'feed-item-visible'
  125. if (this.state === 'leaving') return 'feed-item-leaving'
  126. return 'feed-item-entering'
  127. },
  128. itemInlineStyle: function() {
  129. // leaving 时保持 transition:none,防止从顶部跳到下一条时产生反向动画
  130. // entering 时也保持 none,让下一帧再恢复 transition 做入场动画
  131. if (this.state === 'entering' || this.state === 'leaving') {
  132. return { transition: 'none' }
  133. }
  134. return {}
  135. },
  136. detailMeta: function() {
  137. var type = (this.detail && this.detail.type) || ''
  138. return TYPE_META[type] || DEFAULT_META
  139. },
  140. detailIcon: function() { return this.detailMeta.icon },
  141. detailTypeLabel: function() { return this.detailMeta.label },
  142. detailTitle: function() { return (this.detail && this.detail.title) || '消息详情' },
  143. detailContent: function() { return (this.detail && this.detail.content) || (this.detail && this.detail.text) || '暂无详细内容' },
  144. detailTime: function() { return (this.detail && this.detail.time) || '' },
  145. detailAmount: function() { return (this.detail && this.detail.amount) || '' }
  146. },
  147. watch: {
  148. visibleList: function(newList) {
  149. if (!newList || newList.length === 0) {
  150. this._stopAutoPlay()
  151. return
  152. }
  153. if (this.currentIndex >= newList.length) { this.currentIndex = 0 }
  154. this.state = 'entering'
  155. this._startAutoPlay()
  156. }
  157. },
  158. mounted: function() { this._startAutoPlay() },
  159. beforeDestroy: function() { this._stopAutoPlay() },
  160. methods: {
  161. _startAutoPlay: function() {
  162. this._stopAutoPlay()
  163. var self = this
  164. this._timer = setTimeout(function() { self._tick() }, 50)
  165. },
  166. _stopAutoPlay: function() {
  167. if (this._timer) { clearTimeout(this._timer); this._timer = null }
  168. },
  169. _tick: function() {
  170. var self = this
  171. if (self.visibleList.length === 0) return
  172. if (self._touchPaused || self.detailVisible) {
  173. self._timer = setTimeout(function() { self._tick() }, 200)
  174. return
  175. }
  176. if (self.state === 'entering') {
  177. self._timer = setTimeout(function() {
  178. if (self._touchPaused || self.detailVisible) { self._tick(); return }
  179. self.state = 'visible'
  180. self._timer = setTimeout(function() { self._tick() }, 400)
  181. }, 50)
  182. } else if (self.state === 'visible') {
  183. self._timer = setTimeout(function() {
  184. if (self._touchPaused || self.detailVisible) { self._tick(); return }
  185. self.state = 'leaving'
  186. self._timer = setTimeout(function() { self._tick() }, 400)
  187. }, self.interval)
  188. } else if (self.state === 'leaving') {
  189. self._timer = setTimeout(function() {
  190. if (self._touchPaused || self.detailVisible) { self._tick(); return }
  191. self.currentIndex = (self.currentIndex + 1) % (self.visibleList.length || 1)
  192. self.state = 'entering'
  193. self._tick()
  194. }, 400)
  195. }
  196. },
  197. onTouchStart: function() { this._touchPaused = true; this._stopAutoPlay() },
  198. onTouchEnd: function() { this._touchPaused = false; this._startAutoPlay() },
  199. onItemTap: function(item) {
  200. if (!item) return
  201. this.detail = item
  202. this.detailVisible = true
  203. this._touchPaused = false
  204. this._stopAutoPlay()
  205. this.$emit('viewed', item)
  206. },
  207. closeDetail: function() {
  208. this.detailVisible = false
  209. this.detail = null
  210. this._startAutoPlay()
  211. }
  212. }
  213. }
  214. </script>
  215. <style>
  216. .family-feed {
  217. margin: 12rpx 30rpx 0;
  218. background: #fff;
  219. border-radius: 16rpx;
  220. padding: 10rpx 16rpx;
  221. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  222. overflow: hidden;
  223. }
  224. .feed-row {
  225. display: flex;
  226. flex-direction: row;
  227. align-items: center;
  228. height: 48rpx;
  229. line-height: 48rpx;
  230. overflow: hidden;
  231. }
  232. .feed-header {
  233. display: flex;
  234. flex-direction: row;
  235. align-items: center;
  236. gap: 4rpx;
  237. flex-shrink: 0;
  238. margin-right: 12rpx;
  239. }
  240. .feed-title { font-size: 24rpx; font-weight: bold; color: #333; }
  241. .feed-icon { font-size: 20rpx; }
  242. @keyframes spin {
  243. 0%, 100% { transform: rotate(0deg); }
  244. 25% { transform: rotate(-15deg); }
  245. 75% { transform: rotate(15deg); }
  246. }
  247. .animate-spin {
  248. display: inline-block;
  249. animation: spin 1.2s ease-in-out infinite;
  250. }
  251. /* 视口:固定一行高度,溢出裁剪 */
  252. .feed-viewport {
  253. flex: 1;
  254. min-width: 0;
  255. height: 48rpx;
  256. overflow: hidden;
  257. position: relative;
  258. }
  259. /* 条目:绝对定位叠放,CSS transition 驱动翻转 */
  260. .feed-item {
  261. position: absolute;
  262. top: 0; left: 0; right: 0;
  263. height: 48rpx;
  264. line-height: 48rpx;
  265. display: flex;
  266. flex-direction: row;
  267. align-items: center;
  268. transition: transform 0.4s ease-in-out;
  269. }
  270. .feed-item-entering { transform: translateY(100%); }
  271. .feed-item-visible { transform: translateY(0); }
  272. .feed-item-leaving { transform: translateY(-100%); }
  273. .feed-item-text {
  274. flex: 1; min-width: 0;
  275. font-size: 24rpx; color: #666;
  276. white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  277. }
  278. .feed-item-dot {
  279. width: 12rpx; height: 12rpx;
  280. border-radius: 50%; background: #EF4444;
  281. margin-left: 8rpx; flex-shrink: 0;
  282. }
  283. .feed-count {
  284. flex-shrink: 0; margin-left: 12rpx;
  285. font-size: 20rpx; color: #F97316;
  286. background: rgba(249,115,22,0.1);
  287. border-radius: 999rpx; padding: 2rpx 12rpx; line-height: 1.4;
  288. }
  289. .feed-empty {
  290. height: 48rpx; line-height: 48rpx; text-align: center;
  291. }
  292. .feed-empty-text { font-size: 24rpx; color: #ccc; }
  293. /* ===== 详情弹层 ===== */
  294. .feed-detail-mask {
  295. position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  296. background: rgba(0,0,0,0.5); z-index: 2000;
  297. display: flex; align-items: center; justify-content: center;
  298. padding: 0 60rpx;
  299. }
  300. .feed-detail-card {
  301. width: 100%; max-height: 80vh; overflow-y: auto;
  302. background: #fff; border-radius: 24rpx;
  303. padding: 32rpx 28rpx 24rpx;
  304. animation: feedDetailIn 0.25s ease;
  305. }
  306. @keyframes feedDetailIn {
  307. from { opacity: 0; transform: scale(0.92); }
  308. to { opacity: 1; transform: scale(1); }
  309. }
  310. .feed-detail-head { display: flex; flex-direction: row; align-items: center; }
  311. .feed-detail-icon-wrap {
  312. width: 64rpx; height: 64rpx; border-radius: 20rpx;
  313. background: rgba(249,115,22,0.1);
  314. display: flex; align-items: center; justify-content: center;
  315. margin-right: 16rpx; flex-shrink: 0;
  316. }
  317. .feed-detail-icon { font-size: 34rpx; }
  318. .feed-detail-head-texts { flex: 1; min-width: 0; display: flex; flex-direction: column; }
  319. .feed-detail-type { font-size: 26rpx; font-weight: bold; color: #333; }
  320. .feed-detail-time { font-size: 20rpx; color: #999; margin-top: 4rpx; }
  321. .feed-detail-close { font-size: 40rpx; color: #bbb; line-height: 1; padding: 0 8rpx; flex-shrink: 0; }
  322. .feed-detail-title {
  323. display: block; font-size: 30rpx; font-weight: bold; color: #333;
  324. line-height: 1.4; margin-top: 24rpx;
  325. }
  326. .feed-detail-content {
  327. display: block; font-size: 26rpx; color: #666; line-height: 1.6; margin-top: 12rpx;
  328. }
  329. .feed-detail-amount {
  330. display: inline-block; margin-top: 16rpx;
  331. background: linear-gradient(135deg, #F97316, #FB923C);
  332. border-radius: 999rpx; padding: 6rpx 20rpx;
  333. }
  334. .feed-detail-amount-text { font-size: 24rpx; color: #fff; font-weight: bold; }
  335. .feed-detail-btn {
  336. margin-top: 28rpx; height: 76rpx; border-radius: 38rpx;
  337. background: linear-gradient(135deg, #F97316, #FB923C);
  338. display: flex; align-items: center; justify-content: center;
  339. }
  340. .feed-detail-btn-text { font-size: 28rpx; color: #fff; font-weight: bold; }
  341. </style>