index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <view class="container">
  3. <!-- 消息列表 -->
  4. <view class="message-list">
  5. <view class="message-item" v-for="(item, index) in messageList" :key="index">
  6. <view class="message-icon">
  7. <image :src="item.icon" :style="{ backgroundColor: item.iconBgColor }"></image>
  8. </view>
  9. <view class="message-content">
  10. <text class="message-title">{{item.title}}</text>
  11. <text class="message-subtitle">{{item.subtitle}}</text>
  12. </view>
  13. </view>
  14. </view>
  15. </view>
  16. </template>
  17. <script>
  18. export default {
  19. data() {
  20. return {
  21. time: '18:31',
  22. speed: '83.0',
  23. battery: '36',
  24. messageList: [
  25. {
  26. icon: '/static/course-精选-icon.png',
  27. iconBgColor: '#FFE4E1',
  28. title: '课程精选',
  29. subtitle: '暂无新消息'
  30. },
  31. {
  32. icon: '/static/course-上新-icon.png',
  33. iconBgColor: '#E3F2FD',
  34. title: '课程上新',
  35. subtitle: '暂无新消息'
  36. },
  37. {
  38. icon: '/static/直播预约-icon.png',
  39. iconBgColor: '#E8EAF6',
  40. title: '直播预约',
  41. subtitle: '暂无新消息'
  42. },
  43. {
  44. icon: '/static/圈子消息-icon.png',
  45. iconBgColor: '#E8F5E9',
  46. title: '圈子消息',
  47. subtitle: '暂无新消息'
  48. }
  49. ],
  50. socketTask: null
  51. };
  52. },
  53. onLoad() {
  54. this.connectWebSocket();
  55. },
  56. methods: {
  57. connectWebSocket() {
  58. this.socketTask = uni.connectSocket({
  59. url: 'wss://your-websocket-server-url' // 替换为实际的WebSocket服务器地址
  60. });
  61. this.socketTask.onOpen(() => {
  62. console.log('WebSocket连接成功');
  63. // 可以在这里发送初始化消息等
  64. });
  65. this.socketTask.onMessage((res) => {
  66. const data = JSON.parse(res.data);
  67. // 假设收到的消息格式 { type: '课程精选', content: '有新课程啦' }
  68. const index = this.messageList.findIndex((item) => item.title === data.type);
  69. if (index > -1) {
  70. this.messageList[index].subtitle = data.content;
  71. }
  72. });
  73. this.socketTask.onError((err) => {
  74. console.error('WebSocket连接错误', err);
  75. });
  76. this.socketTask.onClose(() => {
  77. console.log('WebSocket连接关闭');
  78. });
  79. },
  80. goToPage(page) {
  81. uni.navigateTo({
  82. url: `/${page}`
  83. });
  84. }
  85. },
  86. onUnload() {
  87. if (this.socketTask) {
  88. this.socketTask.close();
  89. }
  90. }
  91. };
  92. </script>
  93. <style>
  94. /* 全局样式 */
  95. .container {
  96. background-color: #f7fafc;
  97. height: 100vh;
  98. display: flex;
  99. flex-direction: column;
  100. }
  101. .status-bar {
  102. display: flex;
  103. justify-content: space-between;
  104. align-items: center;
  105. padding: 10rpx 20rpx;
  106. background-color: #ebf4ff;
  107. }
  108. .time {
  109. font-size: 30rpx;
  110. }
  111. .network-status {
  112. display: flex;
  113. align-items: center;
  114. }
  115. .speed,
  116. .signal,
  117. .battery {
  118. font-size: 24rpx;
  119. margin-left: 10rpx;
  120. }
  121. .header {
  122. display: flex;
  123. justify-content: center;
  124. align-items: center;
  125. height: 80rpx;
  126. background-color: #ebf4ff;
  127. }
  128. .title {
  129. font-size: 40rpx;
  130. font-weight: bold;
  131. }
  132. .message-list {
  133. flex: 1;
  134. overflow-y: auto;
  135. padding: 20rpx;
  136. }
  137. .message-item {
  138. display: flex;
  139. align-items: center;
  140. padding: 20rpx 0;
  141. border-bottom: 1rpx solid #e5e7eb;
  142. }
  143. .message-icon {
  144. width: 60rpx;
  145. height: 60rpx;
  146. border-radius: 50%;
  147. margin-right: 20rpx;
  148. display: flex;
  149. justify-content: center;
  150. align-items: center;
  151. }
  152. .message-icon image {
  153. width: 40rpx;
  154. height: 40rpx;
  155. }
  156. .message-content {
  157. flex: 1;
  158. }
  159. .message-title {
  160. font-size: 32rpx;
  161. font-weight: bold;
  162. margin-bottom: 10rpx;
  163. }
  164. .message-subtitle {
  165. font-size: 28rpx;
  166. color: #6b7280;
  167. }
  168. .tab-bar {
  169. display: flex;
  170. justify-content: space-around;
  171. align-items: center;
  172. height: 80rpx;
  173. background-color: #fff;
  174. position: fixed;
  175. bottom: 0;
  176. left: 0;
  177. right: 0;
  178. box-shadow: 0 -2rpx 10rpx rgba(0, 0, 0, 0.1);
  179. }
  180. .tab-item {
  181. display: flex;
  182. flex-direction: column;
  183. align-items: center;
  184. justify-content: center;
  185. }
  186. .tab-icon {
  187. width: 40rpx;
  188. height: 40rpx;
  189. margin-bottom: 5rpx;
  190. }
  191. .tab-text {
  192. font-size: 24rpx;
  193. }
  194. </style>