index.vue 4.5 KB

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