index.vue 4.6 KB

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