当前位置:   article > 正文

uniapp 小程序AP配网

uniapp 小程序AP配网

一、TCPSocket.js 封装TCP协议

  1. class socket {
  2. constructor() {
  3. this.connection = {};
  4. }
  5. // 创建一个TCP实例
  6. establish(monitor) {
  7. this.connection = wx.createTCPSocket();
  8. this.connection.connect({ address: "000.000.0.0", port: 6800 });
  9. }
  10. // 发送消息
  11. connect(message) {
  12. this.connection.onConnect(() => {
  13. this.connection.write(message);
  14. });
  15. }
  16. // 监听端口函数
  17. onMessage(success, failure) {
  18. this.connection.onMessage((res) => {
  19. success(res)
  20. });
  21. this.connection.onError((err) => {
  22. failure(err);
  23. });
  24. }
  25. // 关闭搜索事件
  26. TCPclose() {
  27. setTimeout(() => {
  28. // 3S后,关闭socket
  29. this.connection.close(() => {
  30. console.log("TCP关闭");
  31. });
  32. }, 3000);
  33. }
  34. }
  35. const TCPSocket = new socket();
  36. export default TCPSocket;

 二、vue文件中使用TCP

  1. <template>
  2. <view class="zai-box">
  3. <view class="container">
  4. <text class="textName">{{ wifiSSID }}</text>
  5. <input type="text" placeholder="请输入WLAN密码" v-model="password" />
  6. </view>
  7. <button
  8. class="loginButton"
  9. :loading="loadingWIfi"
  10. :disabled="JSON.stringify(password.length) < 8 || loadingWIfi"
  11. @click="settiing"
  12. >
  13. {{ loadingWIfi ? "连接中" : "连接" }}
  14. </button>
  15. </view>
  16. </template>
  17. <script>
  18. import TCP from "@/common/util/TCPSocket.js";
  19. export default {
  20. data() {
  21. return {
  22. wifiSSID: "WIFI name",
  23. password: "",
  24. isFirst: true,
  25. wifiCountInterval: null,
  26. wifiCountDown: 0,
  27. loadingWIfi: false,
  28. };
  29. },
  30. onHide() {
  31. this.clearInterval();
  32. },
  33. methods: {
  34. settiing() {
  35. this.loadingWIfi = true;
  36. this.onConfirm();
  37. },
  38. onConfirm() {
  39. this.wifiCountDown = 60;
  40. this.startSMSTimer();
  41. if (this.isFirst) {
  42. // TCP实例创建
  43. // 连接端口
  44. TCP.establish();
  45. this.isFirst = false;
  46. }
  47. // 请求设备上报状态
  48. this.udpsend();
  49. // 开始监听
  50. TCP.onMessage(
  51. (res) => {
  52. if (!res) return;
  53. // 关闭TCP连接
  54. TCP.TCPclose();
  55. console.log(res) // 查看返回数据
  56. },
  57. (err) => {
  58. // debug
  59. console.log(err, "err");
  60. }
  61. );
  62. },
  63. udpsend() {
  64. const msgData = {
  65. event: "network",
  66. data: {
  67. ssid: this.wifiSSID,
  68. password: this.password,
  69. },
  70. };
  71. TCP.connect(JSON.stringify(msgData));
  72. },
  73. startSMSTimer() {
  74. this.wifiCountInterval = setInterval(() => {
  75. this.wifiCountDown--;
  76. if (this.wifiCountDown <= 0) {
  77. clearInterval(this.wifiCountInterval);
  78. this.wifiCountInterval = null;
  79. // 响应超过60秒后的操作
  80. // ........
  81. }
  82. }, 1000);
  83. },
  84. clearInterval() {
  85. if (this.wifiCountInterval) {
  86. clearInterval(this.wifiCountInterval);
  87. this.wifiCountInterval = null;
  88. }
  89. },
  90. },
  91. };
  92. </script>
  93. <style scoped>
  94. .zai-box {
  95. padding: 0;
  96. margin: 0;
  97. }
  98. .container {
  99. padding: 30rpx;
  100. margin: 0 0 20rpx;
  101. font-size: 28rpx;
  102. color: #20212b;
  103. background-color: #fff;
  104. text-align: left;
  105. font-weight: 400;
  106. }
  107. .loginButton {
  108. font-size: 36rpx;
  109. width: 690rpx;
  110. color: #fff;
  111. background-color: #00a0e9;
  112. border-radius: 44rpx;
  113. margin: 60rpx auto 0;
  114. border: none;
  115. }
  116. .loginButton[disabled] {
  117. color: #fff;
  118. background-color: #dfdfdf;
  119. }
  120. button::after {
  121. border: none;
  122. }
  123. .textName {
  124. display: block;
  125. font-size: 28rpx;
  126. font-weight: 500;
  127. color: #20212b;
  128. margin-bottom: 30rpx;
  129. }
  130. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/215747
推荐阅读
相关标签
  

闽ICP备14008679号