index.vue 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  1. <template>
  2. <view class="container">
  3. <view class="header">
  4. <text class="title">接种留观人员信息表</text>
  5. <text class="current-time">{{ currentTime }}</text>
  6. </view>
  7. <!-- 表格 -->
  8. <view class="table-container">
  9. <!-- 表头 -->
  10. <view class="table-header">
  11. <view class="table-row header-row">
  12. <view class="cell name title_color">姓名</view>
  13. <view class="cell time title_color">留观时间</view>
  14. <view class="cell time title_color">离开时间</view>
  15. <view class="cell status title_color">状态</view>
  16. </view>
  17. </view>
  18. <!-- 表格主体 - 支持纵向轮播滚动 -->
  19. <view class="table-body-container">
  20. <view class="table-body-wrapper" :class="{ 'animate': shouldAnimate }">
  21. <view v-for="(item, index) in carouselList" :key="index" class="table-row body-row"
  22. :class="`item-${getClass(item.status)}`">
  23. <view class="cell name">{{ item.patientName }}</view>
  24. <view class="cell time">{{ getTime(item.createTime) }}</view>
  25. <view class="cell time">{{ getTime(item.outTime) }}</view>
  26. <view class="cell status">
  27. <text class="status-tag" :class="`status-${getClass(item.status)}`">
  28. {{ getStatusText(item.status) }}
  29. </text>
  30. </view>
  31. </view>
  32. </view>
  33. </view>
  34. </view>
  35. <!-- 连接状态与IP编辑栏 -->
  36. <view class="status-bar-bottom" :class="`status-${connectionStatus}`">
  37. <view class="status-text-box">
  38. <text class="dot" :class="`dot-${connectionStatus}`">●</text>
  39. <text class="status-text">{{ connectionText }}</text>
  40. </view>
  41. <!-- IP 编辑区域 -->
  42. <view class="ip-input-group">
  43. <text class="ip-label">IP:</text>
  44. <input type="text" :value="serverIp" @input="onIpInput" placeholder="192.168.0.41" class="ip-input" />
  45. <text class="colon">:</text>
  46. <input type="number" :value="serverPort" @input="onPortInput" placeholder="8811" class="port-input" />
  47. <button class="btn-reconnect" size="mini" @click="handleReconnect">
  48. 更新并重连
  49. </button>
  50. <button class="btn-reconnect" size="mini" @click="goback">
  51. 测试
  52. </button>
  53. </view>
  54. </view>
  55. </view>
  56. </template>
  57. <script>
  58. export default {
  59. data() {
  60. return {
  61. currentTime: '', // 当前时间
  62. timeTimer: null, // 时间更新定时器
  63. // WebSocket 实例
  64. ws: null,
  65. reconnectTimer: null,
  66. heartbeatTimer: null, // 心跳定时器
  67. heartbeatTimeout: null, // 心跳响应超时计时器
  68. heartbeatInterval: 30000, // 30秒发一次心跳
  69. heartbeatTimeoutTime: 10000, // 10秒内未响应视为超时
  70. isPongReceived: true, // 标记是否收到 pong
  71. // 连接状态
  72. connectionStatus: 'connecting', // connecting, connected, disconnected
  73. // 留观数据列表
  74. list: [],
  75. statusText: {
  76. observing: '留观中',
  77. completed: '留观完成,可离开',
  78. warning: '提前离开',
  79. hasleft: '已离开',
  80. },
  81. // 可编辑的服务器地址
  82. serverIp: '192.168.11.132',
  83. serverPort: '8811',
  84. };
  85. },
  86. computed: {
  87. connectionText() {
  88. const {
  89. connectionStatus,
  90. serverIp,
  91. serverPort
  92. } = this;
  93. return {
  94. connecting: `正在连接 ${serverIp}:${serverPort}...`,
  95. connected: `已连接到 ${serverIp}:${serverPort}`,
  96. disconnected: `连接已断开`
  97. } [connectionStatus];
  98. },
  99. // 轮播列表数据
  100. carouselList() {
  101. if (this.list.length === 0) return [];
  102. // 如果数据较少,不需要轮播
  103. if (this.list.length <= 5) return this.list;
  104. // 复制数据实现无缝轮播
  105. return [...this.list, ...this.list];
  106. },
  107. // 是否需要启动轮播动画
  108. shouldAnimate() {
  109. return this.list.length > 5;
  110. }
  111. },
  112. methods: {
  113. getClass(status) {
  114. var title = '';
  115. if (status == 0) {
  116. title = 'observing'
  117. } else if (status == 1) {
  118. title = 'completed'
  119. } else if (status == 3) {
  120. title = 'warning'
  121. } else if (status == 4) {
  122. title = 'completed'
  123. }
  124. return title
  125. },
  126. getStatusText(status) {
  127. var title = '';
  128. if (status == 0) {
  129. title = this.statusText.observing
  130. } else if (status == 1) {
  131. title = this.statusText.completed
  132. } else if (status == 3) {
  133. title = this.statusText.warning
  134. } else if (status == 4) {
  135. title = this.statusText.hasleft
  136. }
  137. return title
  138. },
  139. // 格式时分
  140. getTime(dateTimeStr) {
  141. // 创建 Date 对象
  142. const date = new Date(dateTimeStr);
  143. // 提取小时和分钟,并格式化为两位数
  144. const hours = String(date.getHours()).padStart(2, '0');
  145. const minutes = String(date.getMinutes()).padStart(2, '0');
  146. const time = `${hours}:${minutes}`;
  147. return time
  148. },
  149. // 格式化时间范围:09:00 - 09:30
  150. formatTimeRange(start, end) {
  151. return start && end ? `${start} - ${end}` : '--';
  152. },
  153. // 输入框事件
  154. onIpInput(e) {
  155. this.serverIp = e.detail.value;
  156. },
  157. onPortInput(e) {
  158. this.serverPort = e.detail.value;
  159. },
  160. // 构建 WebSocket URL
  161. getWebSocketUrl() {
  162. return `ws://${this.serverIp}:${this.serverPort}`;
  163. },
  164. // 处理重连
  165. handleReconnect() {
  166. uni.showLoading({
  167. title: '正在重连...'
  168. });
  169. this.connectionStatus = 'connecting';
  170. // 关闭旧连接
  171. if (this.ws) {
  172. this.ws.close({
  173. success: () => {
  174. console.log('旧连接已关闭');
  175. this.connectWebSocket('link');
  176. },
  177. fail: () => {
  178. this.connectWebSocket('link'); // 即使关闭失败也尝试重连
  179. }
  180. });
  181. } else {
  182. this.connectWebSocket('link');
  183. }
  184. setTimeout(() => {
  185. uni.hideLoading();
  186. }, 2000);
  187. },
  188. // 建立 WebSocket 连接
  189. connectWebSocket(type) {
  190. const url = this.getWebSocketUrl();
  191. // 创建 SocketTask
  192. this.ws = uni.connectSocket({
  193. url: url,
  194. success: (res) => {
  195. console.log('connectSocket success', res);
  196. },
  197. fail: (err) => {
  198. console.error('connectSocket 失败', err);
  199. this.connectionStatus = 'disconnected';
  200. this.reconnect();
  201. }
  202. });
  203. // --- WebSocket 事件监听 ---
  204. this.ws.onOpen((res) => {
  205. // console.log('WebSocket 连接成功', res);
  206. this.connectionStatus = 'connected';
  207. this.ws.send({
  208. data: type,
  209. });
  210. // ✅ 连接成功后启动心跳
  211. this.startHeartbeat();
  212. uni.hideLoading();
  213. });
  214. this.ws.onMessage((res) => {
  215. // ✅ 处理心跳响应 pong
  216. if (res.data === 'PONG' || res.data === '{"type":"PONG"}') {
  217. this.isPongReceived = true;
  218. // console.log('收到 pong,心跳正常');
  219. // ✅ 清除等待 pong 的超时计时器
  220. if (this.heartbeatTimeout) {
  221. clearTimeout(this.heartbeatTimeout);
  222. this.heartbeatTimeout = null;
  223. }
  224. return;
  225. }
  226. try {
  227. const data = JSON.parse(res.data);
  228. // console.log('收到消息:', data);
  229. this.handleMessage(data);
  230. } catch (e) {
  231. console.warn('非 JSON 消息,已忽略', res.data);
  232. }
  233. });
  234. this.ws.onClose((res) => {
  235. console.log('WebSocket 连接关闭', res);
  236. this.connectionStatus = 'disconnected';
  237. this.stopHeartbeat(); // 停止心跳
  238. this.reconnect(); // 断线重连
  239. });
  240. this.ws.onError((err) => {
  241. console.error('WebSocket 错误', err);
  242. this.connectionStatus = 'disconnected';
  243. });
  244. },
  245. // 处理收到的消息
  246. handleMessage(data) {
  247. // 1. 全量数据:数组(不是对象,或没有 action 字段)
  248. if (data.action == 'link') {
  249. this.updateListWithArray(data.data);
  250. return;
  251. }
  252. // 2. 增量操作:对象
  253. if (typeof data === 'object' && data !== null) {
  254. const action = data.action;
  255. if (!action) {
  256. console.warn('消息缺少 action 字段', data);
  257. return;
  258. }
  259. switch (action) {
  260. case 'add':
  261. case 'create':
  262. this.batchAdd(data.data);
  263. break;
  264. case 'update':
  265. this.batchUpdate(data.data);
  266. break;
  267. case 'remove':
  268. case 'delete':
  269. this.batchRemove(data.data);
  270. break;
  271. default:
  272. console.warn('未知操作类型:', action);
  273. }
  274. } else {
  275. console.warn('收到未知格式消息:', data);
  276. }
  277. },
  278. // 批量新增
  279. batchAdd(data) {
  280. if (!data) return;
  281. const items = Array.isArray(data) ? data : [data]; // 兼容单条
  282. const validItems = items.filter(item => item && item.id !== undefined);
  283. if (validItems.length === 0) {
  284. console.warn('没有有效数据用于新增', data);
  285. return;
  286. }
  287. // 避免重复添加
  288. const updated = [...this.list];
  289. validItems.forEach(item => {
  290. const index = updated.findIndex(i => i.id == item.id);
  291. if (index > -1) {
  292. // 已存在 → 更新
  293. updated[index] = {
  294. ...updated[index],
  295. ...item
  296. };
  297. } else {
  298. updated.unshift(item);
  299. }
  300. });
  301. this.list = updated;
  302. },
  303. // 批量修改
  304. batchUpdate(data) {
  305. if (!data) return;
  306. const items = Array.isArray(data) ? data : [data]; // 支持单条或数组
  307. const updated = [...this.list];
  308. items.forEach(item => {
  309. if (!item || item.id === undefined) return;
  310. const index = updated.findIndex(i => i.id == item.id);
  311. if (index > -1) {
  312. updated[index] = {
  313. ...updated[index],
  314. ...item
  315. };
  316. }
  317. // else { this.list.push(item); } // 可选:当作新增
  318. });
  319. this.list = updated;
  320. },
  321. // 批量删除
  322. batchRemove(data) {
  323. let idsToRemove = [];
  324. if (Array.isArray(data.id)) {
  325. // remove: { id: [1,2,3] }
  326. idsToRemove = data.id;
  327. } else if (data.id !== undefined) {
  328. // remove: { id: 1 }
  329. idsToRemove = [data.id];
  330. } else if (Array.isArray(data.data)) {
  331. // remove: { data: [ {id:1}, {id:2} ] }
  332. idsToRemove = data.data.map(item => item.id).filter(id => id !== undefined);
  333. } else {
  334. idsToRemove = [data.id];
  335. console.warn('无法解析删除指令', data);
  336. return;
  337. }
  338. if (idsToRemove.length === 0) return;
  339. const updated = this.list.filter(item => !idsToRemove.includes(item.id));
  340. this.list = updated;
  341. },
  342. updateListWithArray(newList) {
  343. if (!Array.isArray(newList)) return;
  344. const map = new Map();
  345. newList.forEach(item => {
  346. if (item.id !== undefined) {
  347. map.set(item.id, item);
  348. }
  349. });
  350. const updated = [...this.list];
  351. // 更新或新增
  352. for (const [id, item] of map.entries()) {
  353. const index = updated.findIndex(i => i.id == id);
  354. if (index > -1) {
  355. updated[index] = item;
  356. } else {
  357. updated.push(item);
  358. }
  359. }
  360. const final = updated.filter(item => map.has(item.id));
  361. this.list = final;
  362. },
  363. // 断线重连(指数退避)
  364. reconnect() {
  365. if (this.reconnectTimer) return; // 防止重复定时器
  366. this.reconnectTimer = setTimeout(() => {
  367. console.log('正在尝试重新连接...');
  368. this.connectWebSocket('link');
  369. this.reconnectTimer = null;
  370. }, 3000);
  371. },
  372. // 启动心跳
  373. startHeartbeat() {
  374. console.log(2324)
  375. // 清除旧心跳
  376. this.stopHeartbeat();
  377. // 发送 ping 的定时器
  378. this.heartbeatTimer = setInterval(() => {
  379. if (this.ws && this.connectionStatus === 'connected') {
  380. // 检查上一次的 pong 是否已收到,未收到则判定为断线
  381. if (!this.isPongReceived) {
  382. console.warn('上一次心跳未收到 pong,可能已断线');
  383. this.ws.close();
  384. return;
  385. }
  386. // 发送心跳
  387. this.isPongReceived = false; // 等待 pong
  388. // 设置超时检测:10秒内没收到 pong 就断开
  389. this.heartbeatTimeout = setTimeout(() => {
  390. if (!this.isPongReceived) {
  391. console.warn('⚠️ 心跳超时:未在规定时间内收到 pong,即将重连');
  392. uni.showToast({
  393. title: '连接异常,正在重连...',
  394. icon: 'none',
  395. duration: 2000
  396. });
  397. this.ws.close(); // 触发 onClose → 重连
  398. }
  399. }, this.heartbeatTimeoutTime); // 使用配置的超时时间(如 10000ms)
  400. try {
  401. this.ws.send({
  402. data: 'PING',
  403. success: () => {
  404. // console.log('ping 已发送');
  405. },
  406. fail: (err) => {
  407. console.error('ping 发送失败', err);
  408. this.ws.close();
  409. }
  410. });
  411. } catch (e) {
  412. console.error('发送 ping 异常', e);
  413. this.ws.close();
  414. }
  415. }
  416. }, this.heartbeatInterval);
  417. // 可选:启动响应超时检测(更严格)
  418. // 通常靠"发 ping 后未收到 pong"即可判断
  419. },
  420. // 停止心跳(断开连接时调用)
  421. stopHeartbeat() {
  422. if (this.heartbeatTimer) {
  423. clearInterval(this.heartbeatTimer);
  424. this.heartbeatTimer = null;
  425. }
  426. if (this.heartbeatTimeout) {
  427. clearTimeout(this.heartbeatTimeout);
  428. this.heartbeatTimeout = null;
  429. }
  430. },
  431. updateCurrentTime() {
  432. const now = new Date();
  433. const year = now.getFullYear();
  434. const month = String(now.getMonth() + 1).padStart(2, '0');
  435. const day = String(now.getDate()).padStart(2, '0');
  436. const hours = String(now.getHours()).padStart(2, '0');
  437. const minutes = String(now.getMinutes()).padStart(2, '0');
  438. const seconds = String(now.getSeconds()).padStart(2, '0');
  439. this.currentTime = `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
  440. },
  441. goback() {
  442. uni.navigateTo({
  443. url: '/pages/index/home'
  444. });
  445. }
  446. },
  447. mounted() {
  448. this.connectWebSocket('link');
  449. this.updateCurrentTime(); // 立即更新一次时间
  450. this.timeTimer = setInterval(() => {
  451. this.updateCurrentTime();
  452. }, 1000); // 每秒更新一次时间
  453. },
  454. // 页面卸载时关闭连接
  455. beforeDestroy() {
  456. if (this.ws) {
  457. this.ws.close();
  458. }
  459. if (this.reconnectTimer) {
  460. clearTimeout(this.reconnectTimer);
  461. }
  462. this.stopHeartbeat();
  463. // 清理时间更新定时器
  464. if (this.timeTimer) {
  465. clearInterval(this.timeTimer);
  466. }
  467. }
  468. };
  469. </script>
  470. <style lang="scss" scoped>
  471. .container {
  472. background-color: #f4f6f8;
  473. display: flex;
  474. flex-direction: column;
  475. height: 100vh;
  476. padding: 20rpx 30rpx 30rpx 30rpx;
  477. background-color: #f4f6f8;
  478. font-family: 'Arial', 'Microsoft YaHei', sans-serif;
  479. box-sizing: border-box;
  480. overflow: hidden;
  481. /* 隐藏页面滚动,让轮播在表格内进行 */
  482. }
  483. .header {
  484. padding-top: 10rpx;
  485. text-align: center;
  486. margin-bottom: 30rpx;
  487. position: relative;
  488. display: flex;
  489. align-items: center;
  490. justify-content: center;
  491. height: 100rpx;
  492. }
  493. .title {
  494. font-size: 60rpx;
  495. font-weight: bold;
  496. color: #1e3a8a;
  497. }
  498. .current-time {
  499. display: flex;
  500. align-items: center;
  501. position: absolute;
  502. display: flex;
  503. height: 100rpx;
  504. right: 0rpx;
  505. font-size: 55rpx;
  506. color: #666;
  507. background-color: rgba(30, 58, 138, 0.1);
  508. padding: 0rpx 30rpx;
  509. border-radius: 12rpx;
  510. font-weight: bold;
  511. }
  512. .table-container {
  513. flex: 1;
  514. margin-bottom: 30rpx;
  515. border-radius: 24rpx;
  516. overflow: hidden;
  517. box-shadow: 0 8rpx 24rpx rgba(0, 0, 0, 0.08);
  518. background-color: #ffffff;
  519. border: 2rpx solid #e0e0e0;
  520. display: flex;
  521. flex-direction: column;
  522. }
  523. .table-header {
  524. background-color: #f0f4f8;
  525. color: white;
  526. font-weight: bold;
  527. }
  528. .header-row {
  529. background-color: #1e3a8a;
  530. color: white;
  531. font-weight: bold;
  532. }
  533. .table-row {
  534. display: flex;
  535. border-bottom: 2rpx solid #dedede;
  536. }
  537. .body-row {
  538. padding: 10rpx 0rpx;
  539. }
  540. .table-row:last-child {
  541. border-bottom: none;
  542. }
  543. .cell {
  544. padding: 24rpx 16rpx;
  545. font-size: 50rpx;
  546. text-align: center;
  547. color: #333;
  548. }
  549. .name {
  550. width: 25%;
  551. }
  552. .time {
  553. width: 25%;
  554. }
  555. .status {
  556. width: 25%;
  557. }
  558. .status-tag {
  559. padding: 8rpx 20rpx;
  560. border-radius: 32rpx;
  561. font-size: 48rpx;
  562. font-weight: 500;
  563. }
  564. .status-observing {
  565. background-color: #e3f2fd;
  566. color: #007bff;
  567. border: 2rpx solid #9acffa;
  568. }
  569. .status-completed {
  570. background-color: #e8f5e8;
  571. color: #28a745;
  572. border: 2rpx solid #8bc34a;
  573. }
  574. .status-warning {
  575. background-color: #ffebee; // 浅红背景
  576. color: #c62828; // 深红色文字
  577. border: 2rpx solid #ef9a9a;
  578. }
  579. .item-observing {
  580. background-color: #e3f2fd;
  581. }
  582. .item-completed {
  583. background-color: #e8f5e8;
  584. }
  585. .item-warning {
  586. background-color: #ffebee; // 浅红背景
  587. }
  588. .item-hasleft {
  589. background-color: #e3f2fd;
  590. }
  591. .table-header {
  592. flex: 0 0 auto;
  593. overflow: hidden;
  594. }
  595. /* 保持表格形式的轮播样式 */
  596. .table-body-container {
  597. flex: 1;
  598. overflow: hidden;
  599. background-color: #fafafa;
  600. position: relative;
  601. }
  602. .table-body-wrapper {
  603. display: block;
  604. }
  605. .table-body-wrapper.animate {
  606. animation: scrollUp 20s linear infinite;
  607. }
  608. @keyframes scrollUp {
  609. 0% {
  610. transform: translateY(0);
  611. }
  612. 100% {
  613. transform: translateY(calc(-50% - 1px));
  614. /* 减去1px确保无缝衔接 */
  615. }
  616. }
  617. .empty {
  618. text-align: center;
  619. padding: 60rpx;
  620. color: #999;
  621. font-style: italic;
  622. font-size: 40rpx;
  623. }
  624. // 连接状态栏
  625. .status-bar-bottom {
  626. padding: 24rpx 30rpx;
  627. background-color: #ffffff;
  628. box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.05);
  629. border-radius: 24rpx;
  630. display: flex;
  631. gap: 16rpx;
  632. }
  633. .status-text-box {
  634. display: flex;
  635. align-items: center;
  636. font-size: 28rpx;
  637. }
  638. .dot {
  639. color: #d32f2f;
  640. font-size: 40rpx;
  641. }
  642. .dot-connecting {
  643. color: #f9ae3d;
  644. }
  645. .dot-connected {
  646. color: #28a745;
  647. }
  648. .dot-disconnected {
  649. color: #d32f2f;
  650. }
  651. .status-text {
  652. color: #555;
  653. }
  654. .status-text {
  655. margin-left: 16rpx;
  656. }
  657. .title_color {
  658. color: #fff;
  659. }
  660. // IP 输入区域
  661. .ip-input-group {
  662. display: flex;
  663. align-items: center;
  664. margin-left: 16rpx;
  665. flex-wrap: wrap;
  666. gap: 12rpx;
  667. }
  668. .ip-label {
  669. font-size: 28rpx;
  670. color: #555;
  671. }
  672. .ip-input,
  673. .port-input {
  674. border: 2rpx solid #ddd;
  675. border-radius: 12rpx;
  676. padding: 12rpx 20rpx;
  677. font-size: 28rpx;
  678. width: 200rpx;
  679. }
  680. .port-input {
  681. width: 160rpx;
  682. }
  683. .colon {
  684. font-size: 32rpx;
  685. color: #666;
  686. margin: 0 8rpx;
  687. }
  688. .btn-reconnect {
  689. background-color: #007aff;
  690. color: white;
  691. font-size: 24rpx;
  692. padding: 0 16rpx;
  693. border-radius: 12rpx;
  694. }
  695. </style>