index.vue 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <template>
  2. <view class="index-box">
  3. <image src="@/static/img/box-bj.png" mode="scaleToFill"></image>
  4. <view class="box-border-box plr-28">
  5. <view class="save-box fs-42 fc-f text-align-center" @click="toList()">开始结账</view>
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. import mqttClient from '@/common/js/mqtt.js'
  11. import ttsManager from '@/common/js/ttsManager.js';
  12. export default {
  13. data() {
  14. return {};
  15. },
  16. async onLoad() {
  17. // #ifdef APP-PLUS
  18. mqttClient.on('message', (data) => {
  19. // console.log('收到MQTT消息 (原始数据)', data); // 首先打印整个data
  20. try {
  21. // 假设 data 本身是 JSON 字符串,将其解析成对象
  22. const parsedData = JSON.parse(data);
  23. console.log('解析后的消息内容:', parsedData);
  24. if (parsedData.is_cancel) {
  25. return
  26. } else {
  27. uni.$emit('mqttEmit', parsedData.amount)
  28. uni.redirectTo({
  29. url: '/pages/index/success?amount=' + parsedData.amount
  30. })
  31. }
  32. // 收到消息后直接进行语音播报
  33. // ttsManager.speak(parsedData.msg);
  34. } catch (error) {
  35. console.error('解析MQTT消息失败,数据可能不是有效的JSON格式:', error);
  36. // console.log('原始消息内容:', data);
  37. }
  38. });
  39. // 连接服务器
  40. await this.connectToMQTT();
  41. // 检查连接状态
  42. await mqttClient.checkConnection();
  43. //#endif
  44. },
  45. methods: {
  46. toList() {
  47. uni.redirectTo({
  48. url: "/pages/index/order"
  49. })
  50. },
  51. connectToMQTT() {
  52. // 连接MQTT服务器
  53. console.log("连接MQTT服务器------");
  54. try {
  55. mqttClient.on('connected', () => {
  56. console.log('MQTT连接成功,开始订阅主题...');
  57. setTimeout(() => {
  58. this.subscribeToTopic(); // 在连接成功后自动订阅
  59. }, 1000)
  60. });
  61. // 发起连接
  62. mqttClient.connect({
  63. url: 'tcp://mqtt.jhcs888.com:21883',
  64. username: 'hjkj',
  65. password: 'ali2021',
  66. clientId: 'client_' + Math.random().toString(16).substr(2, 8),
  67. keepalive: 60, // 增加心跳间隔,单位秒
  68. clean: false, // 允许断线重连后恢复会话
  69. reconnectPeriod: 1000, // 断开后自动重连的间隔
  70. connectTimeout: 4000 // 连接超时时间(毫秒)
  71. });
  72. console.log('连接成功');
  73. } catch (error) {
  74. console.error('连接失败:', error);
  75. }
  76. },
  77. subscribeToTopic() {
  78. // 订阅主题
  79. try {
  80. mqttClient.subscribe('zsshop128');
  81. console.log('订阅指令已发送');
  82. } catch (error) {
  83. console.error('订阅失败:', error);
  84. }
  85. }
  86. },
  87. };
  88. </script>
  89. <style scoped lang="scss">
  90. .index-box {
  91. width: 100%;
  92. image {
  93. width: 100%;
  94. height: 1122rpx;
  95. margin-bottom: 45rpx;
  96. }
  97. .save-box {
  98. width: 100%;
  99. height: 83rpx;
  100. line-height: 83rpx;
  101. background: linear-gradient(270deg, #1778FD 0%, #4AB6FF 100%);
  102. box-shadow: 0rpx 0rpx 6rpx 0rpx #358AFD;
  103. border-radius: 69rpx;
  104. }
  105. }
  106. </style>