与牧同行-小程序用户端
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

114 lines
3.2 KiB

  1. import http from '../../../utils/api';
  2. const baseUrl = require('../../../utils/baseUrl');
  3. Page({
  4. /**
  5. * 页面的初始数据
  6. */
  7. data: {
  8. baseUrl: baseUrl,
  9. chatSessions: [] // 存储聊天列表数据
  10. },
  11. /**
  12. * 生命周期函数--监听页面加载
  13. */
  14. onLoad(options) {
  15. this.getsessions();
  16. },
  17. // 获取聊天列表
  18. getsessions() {
  19. http.sessions({
  20. data: {},
  21. success: (res) => {
  22. console.log('接口返回数据:', res);
  23. if (res.code === 200 && Array.isArray(res.data)) {
  24. // 预处理数据:为每条数据添加格式化后的时间字段
  25. const formattedSessions = res.data.map(item => {
  26. return {
  27. ...item,
  28. formattedTime: this.formatTime(item.lastMessageTime) // 新增字段,在JS里直接格式化好
  29. };
  30. });
  31. this.setData({
  32. chatSessions: formattedSessions
  33. });
  34. } else {
  35. console.error('数据格式错误或接口异常', res);
  36. }
  37. },
  38. fail: (err) => {
  39. console.error('请求失败', err);
  40. }
  41. });
  42. },
  43. // 格式化时间显示 - 兼容多种格式
  44. formatTime(timeValue) {
  45. console.log('时间原始值:', timeValue);
  46. if (!timeValue) return '';
  47. let date;
  48. // 判断是否是时间戳(数字或数字字符串)
  49. if (typeof timeValue === 'number' || (typeof timeValue === 'string' && /^\d+$/.test(timeValue))) {
  50. // 如果是时间戳,直接使用
  51. date = new Date(Number(timeValue));
  52. }
  53. // 判断是否是日期字符串
  54. else if (typeof timeValue === 'string') {
  55. // 尝试解析字符串日期
  56. // 将 '2026-03-06 15:50:11' 替换为 '2026/03/06 15:50:11' (兼容 iOS)
  57. const formattedStr = timeValue.replace(/-/g, '/');
  58. date = new Date(formattedStr);
  59. }
  60. else {
  61. console.log('未知的时间格式:', timeValue);
  62. return '';
  63. }
  64. // 检查日期是否有效
  65. if (isNaN(date.getTime())) {
  66. console.log('无效的日期:', timeValue);
  67. return '';
  68. }
  69. const now = new Date();
  70. const today = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
  71. const yesterday = today - 24 * 60 * 60 * 1000;
  72. const targetDate = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
  73. if (targetDate === today) {
  74. // 今天: 显示 时:分
  75. const hours = date.getHours().toString().padStart(2, '0');
  76. const minutes = date.getMinutes().toString().padStart(2, '0');
  77. return `${hours}:${minutes}`;
  78. } else if (targetDate === yesterday) {
  79. return '昨天';
  80. } else {
  81. // 更早: 显示 月-日
  82. const month = (date.getMonth() + 1).toString().padStart(2, '0');
  83. const day = date.getDate().toString().padStart(2, '0');
  84. return `${month}-${day}`;
  85. }
  86. },
  87. // 跳转到聊天详情页面
  88. goToChat(e) {
  89. console.log(e);
  90. const id = e.currentTarget.dataset.id
  91. wx.navigateTo({
  92. url: `/pagesA/pages/expertChat/expertChat?id=${id}`
  93. });
  94. },
  95. /**
  96. * 页面相关事件处理函数--监听用户下拉动作
  97. */
  98. onPullDownRefresh() {
  99. this.getsessions();
  100. wx.stopPullDownRefresh();
  101. }
  102. });