FamilyFeed.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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. var style = {}
  130. if (this.state === 'entering') { style.transition = 'none' }
  131. return style
  132. },
  133. detailMeta: function() {
  134. var type = (this.detail && this.detail.type) || ''
  135. return TYPE_META[type] || DEFAULT_META
  136. },
  137. detailIcon: function() { return this.detailMeta.icon },
  138. detailTypeLabel: function() { return this.detailMeta.label },
  139. detailTitle: function() { return (this.detail && this.detail.title) || '消息详情' },
  140. detailContent: function() { return (this.detail && this.detail.content) || (this.detail && this.detail.text) || '暂无详细内容' },
  141. detailTime: function() { return (this.detail && this.detail.time) || '' },
  142. detailAmount: function() { return (this.detail && this.detail.amount) || '' }
  143. },
  144. watch: {
  145. visibleList: function(newList) {
  146. if (!newList || newList.length === 0) {
  147. this._stopAutoPlay()
  148. return
  149. }
  150. if (this.currentIndex >= newList.length) { this.currentIndex = 0 }
  151. this.state = 'entering'
  152. this._startAutoPlay()
  153. }
  154. },
  155. mounted: function() { this._startAutoPlay() },
  156. beforeDestroy: function() { this._stopAutoPlay() },
  157. methods: {
  158. _startAutoPlay: function() {
  159. this._stopAutoPlay()
  160. var self = this
  161. this._timer = setTimeout(function() { self._tick() }, 50)
  162. },
  163. _stopAutoPlay: function() {
  164. if (this._timer) { clearTimeout(this._timer); this._timer = null }
  165. },
  166. _tick: function() {
  167. var self = this
  168. if (self.visibleList.length === 0) return
  169. if (self._touchPaused || self.detailVisible) {
  170. self._timer = setTimeout(function() { self._tick() }, 200)
  171. return
  172. }
  173. if (self.state === 'entering') {
  174. self._timer = setTimeout(function() {
  175. if (self._touchPaused || self.detailVisible) { self._tick(); return }
  176. self.state = 'visible'
  177. self._timer = setTimeout(function() { self._tick() }, 400)
  178. }, 50)
  179. } else if (self.state === 'visible') {
  180. self._timer = setTimeout(function() {
  181. if (self._touchPaused || self.detailVisible) { self._tick(); return }
  182. self.state = 'leaving'
  183. self._timer = setTimeout(function() { self._tick() }, 400)
  184. }, self.interval)
  185. } else if (self.state === 'leaving') {
  186. self._timer = setTimeout(function() {
  187. if (self._touchPaused || self.detailVisible) { self._tick(); return }
  188. self.currentIndex = (self.currentIndex + 1) % (self.visibleList.length || 1)
  189. self.state = 'entering'
  190. self._tick()
  191. }, 400)
  192. }
  193. },
  194. onTouchStart: function() { this._touchPaused = true; this._stopAutoPlay() },
  195. onTouchEnd: function() { this._touchPaused = false; this._startAutoPlay() },
  196. onItemTap: function(item) {
  197. if (!item) return
  198. this.detail = item
  199. this.detailVisible = true
  200. this._touchPaused = false
  201. this._stopAutoPlay()
  202. this.$emit('viewed', item)
  203. },
  204. closeDetail: function() {
  205. this.detailVisible = false
  206. this.detail = null
  207. this._startAutoPlay()
  208. }
  209. }
  210. }
  211. </script>
  212. <style>
  213. .family-feed {
  214. margin: 12rpx 30rpx 0;
  215. background: #fff;
  216. border-radius: 16rpx;
  217. padding: 10rpx 16rpx;
  218. box-shadow: 0 2rpx 8rpx rgba(0,0,0,0.04);
  219. overflow: hidden;
  220. }
  221. .feed-row {
  222. display: flex;
  223. flex-direction: row;
  224. align-items: center;
  225. height: 48rpx;
  226. line-height: 48rpx;
  227. overflow: hidden;
  228. }
  229. .feed-header {
  230. display: flex;
  231. flex-direction: row;
  232. align-items: center;
  233. gap: 4rpx;
  234. flex-shrink: 0;
  235. margin-right: 12rpx;
  236. }
  237. .feed-title { font-size: 24rpx; font-weight: bold; color: #333; }
  238. .feed-icon { font-size: 20rpx; }
  239. @keyframes spin {
  240. 0%, 100% { transform: rotate(0deg); }
  241. 25% { transform: rotate(-15deg); }
  242. 75% { transform: rotate(15deg); }
  243. }
  244. .animate-spin {
  245. display: inline-block;
  246. animation: spin 1.2s ease-in-out infinite;
  247. }
  248. /* 视口:固定一行高度,溢出裁剪 */
  249. .feed-viewport {
  250. flex: 1;
  251. min-width: 0;
  252. height: 48rpx;
  253. overflow: hidden;
  254. position: relative;
  255. }
  256. /* 条目:绝对定位叠放,CSS transition 驱动翻转 */
  257. .feed-item {
  258. position: absolute;
  259. top: 0; left: 0; right: 0;
  260. height: 48rpx;
  261. line-height: 48rpx;
  262. display: flex;
  263. flex-direction: row;
  264. align-items: center;
  265. transition: transform 0.4s ease-in-out;
  266. }
  267. .feed-item-entering { transform: translateY(100%); }
  268. .feed-item-visible { transform: translateY(0); }
  269. .feed-item-leaving { transform: translateY(-100%); }
  270. .feed-item-text {
  271. flex: 1; min-width: 0;
  272. font-size: 24rpx; color: #666;
  273. white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  274. }
  275. .feed-item-dot {
  276. width: 12rpx; height: 12rpx;
  277. border-radius: 50%; background: #EF4444;
  278. margin-left: 8rpx; flex-shrink: 0;
  279. }
  280. .feed-count {
  281. flex-shrink: 0; margin-left: 12rpx;
  282. font-size: 20rpx; color: #F97316;
  283. background: rgba(249,115,22,0.1);
  284. border-radius: 999rpx; padding: 2rpx 12rpx; line-height: 1.4;
  285. }
  286. .feed-empty {
  287. height: 48rpx; line-height: 48rpx; text-align: center;
  288. }
  289. .feed-empty-text { font-size: 24rpx; color: #ccc; }
  290. /* ===== 详情弹层 ===== */
  291. .feed-detail-mask {
  292. position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  293. background: rgba(0,0,0,0.5); z-index: 2000;
  294. display: flex; align-items: center; justify-content: center;
  295. padding: 0 60rpx;
  296. }
  297. .feed-detail-card {
  298. width: 100%; max-height: 80vh; overflow-y: auto;
  299. background: #fff; border-radius: 24rpx;
  300. padding: 32rpx 28rpx 24rpx;
  301. animation: feedDetailIn 0.25s ease;
  302. }
  303. @keyframes feedDetailIn {
  304. from { opacity: 0; transform: scale(0.92); }
  305. to { opacity: 1; transform: scale(1); }
  306. }
  307. .feed-detail-head { display: flex; flex-direction: row; align-items: center; }
  308. .feed-detail-icon-wrap {
  309. width: 64rpx; height: 64rpx; border-radius: 20rpx;
  310. background: rgba(249,115,22,0.1);
  311. display: flex; align-items: center; justify-content: center;
  312. margin-right: 16rpx; flex-shrink: 0;
  313. }
  314. .feed-detail-icon { font-size: 34rpx; }
  315. .feed-detail-head-texts { flex: 1; min-width: 0; display: flex; flex-direction: column; }
  316. .feed-detail-type { font-size: 26rpx; font-weight: bold; color: #333; }
  317. .feed-detail-time { font-size: 20rpx; color: #999; margin-top: 4rpx; }
  318. .feed-detail-close { font-size: 40rpx; color: #bbb; line-height: 1; padding: 0 8rpx; flex-shrink: 0; }
  319. .feed-detail-title {
  320. display: block; font-size: 30rpx; font-weight: bold; color: #333;
  321. line-height: 1.4; margin-top: 24rpx;
  322. }
  323. .feed-detail-content {
  324. display: block; font-size: 26rpx; color: #666; line-height: 1.6; margin-top: 12rpx;
  325. }
  326. .feed-detail-amount {
  327. display: inline-block; margin-top: 16rpx;
  328. background: linear-gradient(135deg, #F97316, #FB923C);
  329. border-radius: 999rpx; padding: 6rpx 20rpx;
  330. }
  331. .feed-detail-amount-text { font-size: 24rpx; color: #fff; font-weight: bold; }
  332. .feed-detail-btn {
  333. margin-top: 28rpx; height: 76rpx; border-radius: 38rpx;
  334. background: linear-gradient(135deg, #F97316, #FB923C);
  335. display: flex; align-items: center; justify-content: center;
  336. }
  337. .feed-detail-btn-text { font-size: 28rpx; color: #fff; font-weight: bold; }
  338. </style>