index.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. <template>
  2. <view class="nt-wrap" :style="{ '--font-scale': fontScale }">
  3. <view class="nt-loading" v-if="loading">加载中...</view>
  4. <view class="nt-empty" v-else-if="list.length === 0">
  5. <text class="nt-empty-icon">🔔</text>
  6. <text class="nt-empty-text">暂无通知</text>
  7. </view>
  8. <view class="nt-list" v-else>
  9. <view class="nt-item" v-for="(item, ni) in list" :key="ni" @click="onTap(item)">
  10. <view class="nt-item-icon">
  11. <text class="nt-icon">{{ getIcon(item.type) }}</text>
  12. <view class="nt-unread" v-if="item.is_read === 0 || item.isRead === 0"></view>
  13. </view>
  14. <view class="nt-item-body">
  15. <view class="nt-item-top">
  16. <text class="nt-item-title">{{ item.title }}</text>
  17. <text class="nt-item-time">{{ formatTime(item.created_at || item.createdAt) }}</text>
  18. </view>
  19. <text class="nt-item-content">{{ item.content }}</text>
  20. </view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. import { getNotificationList } from '../../utils/api.js'
  27. import { readNotification } from '../../utils/api.js'
  28. import { parseDate } from '../../utils/format.js'
  29. import fontScaleMixin from '@/mixins/font-scale.js'
  30. export default {
  31. mixins: [fontScaleMixin],
  32. data() {
  33. return {
  34. list: [],
  35. loading: false
  36. }
  37. },
  38. onLoad() {
  39. this.loadList()
  40. this._onLoadFired = true
  41. },
  42. onShow() {
  43. if (this._onLoadFired) {
  44. this._onLoadFired = false
  45. } else {
  46. this.loadList()
  47. }
  48. },
  49. onPullDownRefresh() {
  50. this.loadList()
  51. },
  52. methods: {
  53. getIcon(type) {
  54. if (type === 'report_ready') return '📋'
  55. if (type === 'reward_granted') return '🎁'
  56. return '🔔'
  57. },
  58. formatTime(dateStr) {
  59. var d = parseDate(dateStr)
  60. if (!d) return dateStr || ''
  61. var y = d.getFullYear()
  62. var m = d.getMonth() + 1
  63. var day = d.getDate()
  64. var h = d.getHours()
  65. var min = d.getMinutes()
  66. return y + '-' + pad(m) + '-' + pad(day) + ' ' + pad(h) + ':' + pad(min)
  67. },
  68. loadList() {
  69. var self = this
  70. self.loading = true
  71. uni.showLoading({ title: '加载中...', mask: true })
  72. getNotificationList().then(function(res) {
  73. uni.hideLoading()
  74. self.loading = false
  75. uni.stopPullDownRefresh()
  76. var data = res && res.data
  77. self.list = Array.isArray(data) ? data : []
  78. }).catch(function() {
  79. uni.hideLoading()
  80. self.loading = false
  81. uni.stopPullDownRefresh()
  82. self.list = []
  83. uni.showToast({ title: '加载失败', icon: 'none' })
  84. })
  85. },
  86. onTap(item) {
  87. var self = this
  88. var isRead = item.is_read === 0 || item.isRead === 0
  89. if (isRead) {
  90. // 标记已读
  91. readNotification({ id: item.id }).then(function() {
  92. item.is_read = 1
  93. item.isRead = 1
  94. self.$forceUpdate()
  95. }).catch(function() {})
  96. }
  97. // 导航
  98. var refType = item.ref_type || item.refType || ''
  99. var refId = item.ref_id || item.refId
  100. if (refType === 'task') {
  101. uni.navigateTo({ url: '/pages/tasks/tasks' })
  102. }
  103. }
  104. }
  105. }
  106. function pad(n) {
  107. return n < 10 ? '0' + n : '' + n
  108. }
  109. </script>
  110. <style scoped lang="scss">
  111. .nt-wrap {
  112. min-height: 100vh;
  113. background: $uni-primary-pale;
  114. padding: 20rpx 0;
  115. box-sizing: border-box;
  116. }
  117. .nt-loading {
  118. text-align: center;
  119. font-size: calc(28rpx * var(--font-scale, 1));
  120. color: $facade-text-muted;
  121. padding: 80rpx 0;
  122. }
  123. .nt-empty {
  124. text-align: center;
  125. padding: 120rpx 0;
  126. }
  127. .nt-empty-icon {
  128. font-size: calc($uni-font-size-5xl * var(--font-scale, 1));
  129. display: block;
  130. }
  131. .nt-empty-text {
  132. display: block;
  133. font-size: calc(32rpx * var(--font-scale, 1));
  134. color: $facade-text-muted;
  135. margin-top: 20rpx;
  136. }
  137. .nt-item {
  138. display: flex;
  139. align-items: flex-start;
  140. background: $app-surface;
  141. padding: 28rpx 32rpx;
  142. margin-bottom: 2rpx;
  143. }
  144. .nt-item:active {
  145. background: $facade-fill-gray-50;
  146. }
  147. .nt-item-icon {
  148. position: relative;
  149. width: 64rpx;
  150. height: 64rpx;
  151. border-radius: 24rpx;
  152. background: rgba(249, 115, 22, 0.1);
  153. display: flex;
  154. align-items: center;
  155. justify-content: center;
  156. margin-right: 20rpx;
  157. flex-shrink: 0;
  158. }
  159. .nt-icon {
  160. font-size: calc(32rpx * var(--font-scale, 1));
  161. }
  162. .nt-unread {
  163. position: absolute;
  164. top: -2rpx;
  165. right: -2rpx;
  166. width: 16rpx;
  167. height: 16rpx;
  168. border-radius: 50%;
  169. background: $status-danger;
  170. border: 2rpx solid $app-surface;
  171. }
  172. .nt-item-body {
  173. flex: 1;
  174. }
  175. .nt-item-top {
  176. display: flex;
  177. justify-content: space-between;
  178. align-items: center;
  179. margin-bottom: 8rpx;
  180. }
  181. .nt-item-title {
  182. font-size: calc(28rpx * var(--font-scale, 1));
  183. font-weight: 600;
  184. color: $facade-text-strong;
  185. flex: 1;
  186. }
  187. .nt-item-time {
  188. font-size: calc(22rpx * var(--font-scale, 1));
  189. color: $facade-text-muted;
  190. margin-left: 12rpx;
  191. flex-shrink: 0;
  192. }
  193. .nt-item-content {
  194. display: block;
  195. font-size: calc(26rpx * var(--font-scale, 1));
  196. color: $facade-text-body;
  197. line-height: 1.5;
  198. }
  199. </style>