index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. },
  39. onShow() {
  40. this.loadList()
  41. },
  42. onPullDownRefresh() {
  43. this.loadList()
  44. },
  45. methods: {
  46. getKey(item) {
  47. return item.id || Math.random()
  48. },
  49. getIcon(type) {
  50. if (type === 'report_ready') return '📋'
  51. if (type === 'reward_granted') return '🎁'
  52. return '🔔'
  53. },
  54. formatTime(dateStr) {
  55. var d = parseDate(dateStr)
  56. if (!d) return dateStr || ''
  57. var y = d.getFullYear()
  58. var m = d.getMonth() + 1
  59. var day = d.getDate()
  60. var h = d.getHours()
  61. var min = d.getMinutes()
  62. return y + '-' + pad(m) + '-' + pad(day) + ' ' + pad(h) + ':' + pad(min)
  63. },
  64. loadList() {
  65. var self = this
  66. self.loading = true
  67. uni.showLoading({ title: '加载中...', mask: true })
  68. getNotificationList().then(function(res) {
  69. uni.hideLoading()
  70. self.loading = false
  71. uni.stopPullDownRefresh()
  72. var data = res && res.data
  73. self.list = Array.isArray(data) ? data : []
  74. }).catch(function() {
  75. uni.hideLoading()
  76. self.loading = false
  77. uni.stopPullDownRefresh()
  78. self.list = []
  79. uni.showToast({ title: '加载失败', icon: 'none' })
  80. })
  81. },
  82. onTap(item) {
  83. var self = this
  84. var isRead = item.is_read === 0 || item.isRead === 0
  85. if (isRead) {
  86. // 标记已读
  87. readNotification({ id: item.id }).then(function() {
  88. item.is_read = 1
  89. item.isRead = 1
  90. self.$forceUpdate()
  91. }).catch(function() {})
  92. }
  93. // 导航
  94. var refType = item.ref_type || item.refType || ''
  95. var refId = item.ref_id || item.refId
  96. if (refType === 'report' || refType === 'plan') {
  97. uni.navigateTo({ url: '/pages/problem/plan-confirm' })
  98. } else if (refType === 'package' || refType === 'logistics') {
  99. uni.navigateTo({ url: '/pages/problem/logistics' })
  100. } else 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>
  111. .nt-wrap {
  112. min-height: 100vh;
  113. background: #FFF7ED;
  114. padding: 20rpx 0;
  115. box-sizing: border-box;
  116. }
  117. .nt-loading {
  118. text-align: center;
  119. font-size: 28rpx;
  120. color: #999;
  121. padding: 80rpx 0;
  122. }
  123. .nt-empty {
  124. text-align: center;
  125. padding: 120rpx 0;
  126. }
  127. .nt-empty-icon {
  128. font-size: 80rpx;
  129. display: block;
  130. }
  131. .nt-empty-text {
  132. display: block;
  133. font-size: 32rpx;
  134. color: #999;
  135. margin-top: 20rpx;
  136. }
  137. .nt-item {
  138. display: flex;
  139. align-items: flex-start;
  140. background: #fff;
  141. padding: 28rpx 32rpx;
  142. margin-bottom: 2rpx;
  143. }
  144. .nt-item:active {
  145. background: #F9FAFB;
  146. }
  147. .nt-item-icon {
  148. position: relative;
  149. width: 64rpx;
  150. height: 64rpx;
  151. border-radius: 32rpx;
  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: 32rpx;
  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: #EF4444;
  170. border: 2rpx solid #fff;
  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: 28rpx;
  183. font-weight: 600;
  184. color: #333;
  185. flex: 1;
  186. }
  187. .nt-item-time {
  188. font-size: 22rpx;
  189. color: #999;
  190. margin-left: 12rpx;
  191. flex-shrink: 0;
  192. }
  193. .nt-item-content {
  194. display: block;
  195. font-size: 26rpx;
  196. color: #666;
  197. line-height: 1.5;
  198. }
  199. </style>