index.uvue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. <template>
  2. <view class="page-container">
  3. <view>
  4. <text class="page-header"> 我的消息 </text>
  5. </view>
  6. <view class="list-page">
  7. <!-- 搜索栏 -->
  8. <view class="search-bar">
  9. <view class="search-box">
  10. <image class="search-icon" src="/static/images/workbench/list/1.png" mode="aspectFit"></image>
  11. <input class="search-input" type="text" placeholder="请输入关键字查询" v-model="keyword" @input="handleInput" @confirm="handleSearch" />
  12. <view v-if="keyword.length > 0" class="clear-btn" @tap="handleClear">
  13. <text class="clear-btn-text">×</text>
  14. </view>
  15. <view class="search-btn" @tap="handleSearch">
  16. <text class="search-btn-text">搜索</text>
  17. </view>
  18. </view>
  19. </view>
  20. <!-- 状态标签 -->
  21. <view class="status-tabs">
  22. <view
  23. class="status-tab"
  24. :class="{ 'active': currentStatus === '' }"
  25. @tap="handleStatusChange('')"
  26. >
  27. <text class="status-tab-text" :class="{ 'active-text': currentStatus === '' }">全部</text>
  28. </view>
  29. <view
  30. class="status-tab"
  31. :class="{ 'active': currentStatus === 'UNREAD' }"
  32. @tap="handleStatusChange('UNREAD')"
  33. >
  34. <text class="status-tab-text" :class="{ 'active-text': currentStatus === 'UNREAD' }">未读</text>
  35. </view>
  36. <view
  37. class="status-tab"
  38. :class="{ 'active': currentStatus === 'READ' }"
  39. @tap="handleStatusChange('READ')"
  40. >
  41. <text class="status-tab-text" :class="{ 'active-text': currentStatus === 'READ' }">已读</text>
  42. </view>
  43. </view>
  44. <!-- 列表内容 -->
  45. <common-list
  46. :dataList="dataList"
  47. :loading="loading"
  48. :refreshing="refreshing"
  49. :hasMore="hasMore"
  50. @refresh="handleRefresh"
  51. @loadMore="loadMore"
  52. @itemClick="handleItemClick"
  53. >
  54. <template #default="{ item, index }">
  55. <view class="list-item" :class="{ 'unread': getStatus(item) === 'UNREAD' }">
  56. <view class="item-container">
  57. <view class="item-header">
  58. <text class="item-title">{{ getMessageTitle(item) }}</text>
  59. <text class="item-status" :class="'status-' + getStatus(item)">{{ getStatusText(item) }}</text>
  60. </view>
  61. <view class="item-content">
  62. <text class="content-text">{{ getMessageContent(item) }}</text>
  63. </view>
  64. <view class="item-footer">
  65. <text class="create-time">{{ getCreateTime(item) }}</text>
  66. </view>
  67. </view>
  68. </view>
  69. </template>
  70. </common-list>
  71. <custom-tabbar :current="2" />
  72. </view>
  73. </view>
  74. </template>
  75. <script setup lang="uts">
  76. import { ref, onBeforeUnmount, onMounted } from 'vue'
  77. import { getMessageList, markMessageAsRead } from '../../api/message/index'
  78. // 列表数据
  79. const dataList = ref<any[]>([])
  80. const keyword = ref<string>("")
  81. const currentStatus = ref<string>("")
  82. const page = ref<number>(1)
  83. const pageSize: number = 10
  84. const hasMore = ref<boolean>(true)
  85. const loading = ref<boolean>(false)
  86. const refreshing = ref<boolean>(false)
  87. const total = ref<number>(0)
  88. // 获取消息标题
  89. const getMessageTitle = (item: any | null): string => {
  90. if (item == null) return ''
  91. const jsonItem = item as UTSJSONObject
  92. const val = jsonItem['messageTitle']
  93. return val != null ? val.toString() : ''
  94. }
  95. // 获取消息内容
  96. const getMessageContent = (item: any | null): string => {
  97. if (item == null) return ''
  98. const jsonItem = item as UTSJSONObject
  99. const val = jsonItem['messageContent']
  100. return val != null ? val.toString() : ''
  101. }
  102. // 获取消息状态
  103. const getStatus = (item: any | null): string => {
  104. if (item == null) return ''
  105. const jsonItem = item as UTSJSONObject
  106. const val = jsonItem['status']
  107. return val != null ? val.toString() : ''
  108. }
  109. // 获取状态文本
  110. const getStatusText = (item: any | null): string => {
  111. const status = getStatus(item)
  112. switch (status) {
  113. case 'UNREAD': return '未读'
  114. case 'READ': return '已读'
  115. default: return status
  116. }
  117. }
  118. // 获取创建时间
  119. const getCreateTime = (item: any | null): string => {
  120. if (item == null) return ''
  121. const jsonItem = item as UTSJSONObject
  122. const val = jsonItem['createTime']
  123. return val != null ? val.toString() : ''
  124. }
  125. // 获取消息ID
  126. const getMessageId = (item: any | null): string => {
  127. if (item == null) return ''
  128. const jsonItem = item as UTSJSONObject
  129. const val = jsonItem['messageId']
  130. return val != null ? val.toString() : ''
  131. }
  132. // 加载列表数据
  133. const loadData = async (isRefresh: boolean): Promise<void> => {
  134. if (loading.value) {
  135. refreshing.value = false
  136. return
  137. }
  138. try {
  139. loading.value = true
  140. if (isRefresh) {
  141. page.value = 1
  142. }
  143. // 调用 API
  144. const result = await getMessageList(page.value, pageSize, keyword.value, currentStatus.value)
  145. // 提取响应数据
  146. const resultObj = result as UTSJSONObject
  147. const rows = resultObj['rows']
  148. const responseTotal = resultObj['total'] as number
  149. if (rows != null) {
  150. const newData = rows as any[]
  151. if (isRefresh) {
  152. dataList.value = newData
  153. } else {
  154. dataList.value = [...dataList.value, ...newData]
  155. }
  156. total.value = responseTotal
  157. hasMore.value = dataList.value.length < responseTotal
  158. } else {
  159. if (isRefresh) {
  160. dataList.value = []
  161. }
  162. hasMore.value = false
  163. }
  164. } catch (e: any) {
  165. uni.showToast({
  166. title: e.message ?? '加载失败',
  167. icon: 'none'
  168. })
  169. } finally {
  170. loading.value = false
  171. setTimeout(() => {
  172. refreshing.value = false
  173. }, 100)
  174. }
  175. }
  176. // 下拉刷新
  177. const handleRefresh = async (): Promise<void> => {
  178. refreshing.value = true
  179. await loadData(true)
  180. }
  181. // 加载更多
  182. const loadMore = (): void => {
  183. if (!hasMore.value || loading.value) {
  184. return
  185. }
  186. page.value++
  187. loadData(false)
  188. }
  189. // 搜索
  190. const handleSearch = (): void => {
  191. page.value = 1
  192. loadData(true)
  193. }
  194. // 输入时搜索
  195. const handleInput = (): void => {
  196. page.value = 1
  197. loadData(true)
  198. }
  199. // 清除搜索
  200. const handleClear = (): void => {
  201. keyword.value = ''
  202. page.value = 1
  203. loadData(true)
  204. }
  205. // 切换状态标签
  206. const handleStatusChange = (status: string): void => {
  207. currentStatus.value = status
  208. page.value = 1
  209. loadData(true)
  210. }
  211. // 点击列表项
  212. const handleItemClick = (item: any | null, index: number): void => {
  213. if (item == null) return
  214. const messageId = getMessageId(item)
  215. if (messageId.length == 0) return
  216. // 标记为已读
  217. if (getStatus(item) == 'UNREAD') {
  218. markMessageAsRead(messageId).then(() => {
  219. // 重新加载数据
  220. loadData(true)
  221. }).catch((e) => {
  222. console.error('标记已读失败:', e)
  223. })
  224. }
  225. // 可以在这里添加跳转到消息详情页的逻辑
  226. }
  227. // 组件卸载前清理
  228. onBeforeUnmount(() => {
  229. refreshing.value = false
  230. loading.value = false
  231. })
  232. // 初始化
  233. onMounted(() => {
  234. loadData(true)
  235. })
  236. </script>
  237. <style lang="scss">
  238. .page-container {
  239. flex: 1;
  240. background-color: #f5f8fe;
  241. padding-top: env(safe-area-inset-top);
  242. background-color: #e8f0f9;
  243. padding: 20rpx 10rpx 20rpx 10rpx;
  244. }
  245. .page-header{
  246. font-size: 32rpx;
  247. color: #333333;
  248. font-weight: bold;
  249. padding-top:20px;
  250. padding-bottom: 10px;
  251. }
  252. .list-page {
  253. flex: 1;
  254. background-color: #e8f0f9;
  255. }
  256. .search-bar {
  257. padding: 20rpx 30rpx;
  258. background-color: #d7eafe;
  259. }
  260. .search-box {
  261. flex-direction: row;
  262. align-items: center;
  263. height: 72rpx;
  264. padding: 0 24rpx;
  265. background-color: #f5f5f5;
  266. border-radius: 36rpx;
  267. .search-icon {
  268. width: 32rpx;
  269. height: 32rpx;
  270. margin-right: 12rpx;
  271. }
  272. .search-input {
  273. flex: 1;
  274. font-size: 28rpx;
  275. color: #333333;
  276. }
  277. .search-btn {
  278. padding: 10rpx 20rpx;
  279. background-color: #007aff;
  280. border-radius: 8rpx;
  281. margin-left: 10rpx;
  282. }
  283. .search-btn-text {
  284. color: #ffffff;
  285. font-size: 24rpx;
  286. }
  287. .clear-btn {
  288. width: 36rpx;
  289. height: 36rpx;
  290. border-radius: 18rpx;
  291. background-color: #cccccc;
  292. align-items: center;
  293. justify-content: center;
  294. margin-left: 10rpx;
  295. }
  296. .clear-btn-text {
  297. color: #ffffff;
  298. font-size: 24rpx;
  299. font-weight: bold;
  300. }
  301. }
  302. .status-tabs {
  303. display: flex;
  304. flex-direction: row;
  305. padding: 0rpx 30rpx 20rpx;
  306. background-color: #d7eafe;
  307. }
  308. .status-tab {
  309. flex: 1;
  310. padding: 16rpx 0;
  311. text-align: center;
  312. margin-right: 16rpx;
  313. border-radius: 8rpx;
  314. background-color: #f5f5f5;
  315. justify-content: center;
  316. align-items: center;
  317. &:last-child {
  318. margin-right: 0;
  319. }
  320. &.active {
  321. background-color: #007aff;
  322. }
  323. .status-tab-text {
  324. font-size: 26rpx;
  325. color: #666666;
  326. &.active-text {
  327. color: #ffffff;
  328. font-weight: bold;
  329. }
  330. }
  331. }
  332. .list-item {
  333. margin: 16rpx 30rpx;
  334. background-color: #ffffff;
  335. border-radius: 12rpx;
  336. box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.08);
  337. &.unread {
  338. border-left: 4rpx solid #007aff;
  339. }
  340. }
  341. .item-container {
  342. padding: 20rpx;
  343. }
  344. .item-header {
  345. flex-direction: row;
  346. align-items: center;
  347. justify-content: space-between;
  348. margin-bottom: 12rpx;
  349. .item-title {
  350. flex: 1;
  351. font-size: 28rpx;
  352. color: #333333;
  353. font-weight: bold;
  354. }
  355. .item-status {
  356. font-size: 20rpx;
  357. padding: 6rpx 12rpx;
  358. border-radius: 6rpx;
  359. &.status-UNREAD {
  360. background-color: #e6f7ff;
  361. color: #007aff;
  362. }
  363. &.status-READ {
  364. background-color: #f6ffed;
  365. color: #52c41a;
  366. }
  367. }
  368. }
  369. .item-content {
  370. margin-bottom: 12rpx;
  371. .content-text {
  372. font-size: 24rpx;
  373. color: #666666;
  374. line-height: 36rpx;
  375. }
  376. }
  377. .item-footer {
  378. display: flex;
  379. justify-content: flex-end;
  380. .create-time {
  381. font-size: 20rpx;
  382. color: #999999;
  383. }
  384. }
  385. </style>