wifiSet.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header title='WiFi配置' bgColor="transparent"></cus-header>
  4. <div class="box">
  5. <div class="b_top adffcacjc">
  6. <image src="http://106.54.209.120:8188/static/wifi.png"></image>
  7. <text>WiFi配置</text>
  8. </div>
  9. <div class="b_pre">
  10. <div class="bp_text">SSID:</div>
  11. <div class="bp_inp">
  12. <input type="text" v-model="wifiSSID">
  13. </div>
  14. </div>
  15. <div class="b_pre">
  16. <div class="bp_text">密码:</div>
  17. <div class="bp_inp">
  18. <input type="password" v-model="wifiPwd">
  19. </div>
  20. </div>
  21. <div class="zt_btn" @tap="toConnectWiFi">连接</div>
  22. </div>
  23. <div class="box">
  24. <div class="b_title">从下面列表选择2.4GWiFi:</div>
  25. <div class="b_wifi adfac" v-for="(item,index) in wifi2gList" :key="index" @tap="toCode(item)">
  26. <text>{{item.SSID}}</text>
  27. <image src="http://106.54.209.120:8188/static/lock.png"></image>
  28. </div>
  29. </div>
  30. <u-popup :show="show" mode="center" @close="close">
  31. <div class="fail_box adffcacjc">
  32. <image src="http://106.54.209.120:8188/static/connect_off.png"></image>
  33. <text>网络连接失败</text>
  34. <p>请检查你的网络设置后刷新</p>
  35. <div class="zt_btn" @tap="toTryAgain">重试</div>
  36. </div>
  37. </u-popup>
  38. </view>
  39. </template>
  40. <script>
  41. export default {
  42. data(){
  43. return {
  44. wifiSSID:'',
  45. wifiPwd:'',
  46. wifi2gList:[],
  47. show:false,
  48. tcp:'',
  49. }
  50. },
  51. onLoad() {
  52. this.initWiFi();
  53. },
  54. methods:{
  55. initWiFi(){
  56. uni.showLoading({
  57. title:'获取WiFi列表中'
  58. })
  59. try{
  60. // wx.request({
  61. // url:'http://192.168.4.1/scan',
  62. // success:res=>{
  63. // console.log(res,'res');
  64. // uni.hideLoading();
  65. // },
  66. // fail:err=>{
  67. // console.log(err,'err');
  68. // uni.hideLoading();
  69. // }
  70. // })
  71. }catch(e){
  72. console.log(e,'e');
  73. uni.hideLoading();
  74. }
  75. // let that = this;
  76. // wx.startWifi({
  77. // complete (res) {
  78. // wx.getWifiList({
  79. // success(res2) {
  80. // wx.onGetWifiList(res3=> {
  81. // let temp = [];
  82. // res3.wifiList.forEach(w=>{
  83. // if(w.SSID&&w.SSID.indexOf('Xiaozhi-')<0){
  84. // let exit = temp.find(t=>t.SSID===w.SSID);
  85. // if(!exit) temp = [...temp,w]
  86. // }
  87. // })
  88. // that.$nextTick(()=>{
  89. // that.wifi2gList = temp;
  90. // that.$forceUpdate();
  91. // uni.hideLoading();
  92. // })
  93. // })
  94. // },
  95. // fail(err) {
  96. // console.log(err,'err')
  97. // }
  98. // })
  99. // }
  100. // })
  101. },
  102. toConnectWiFi(){
  103. uni.navigateTo({
  104. url:'/pagesMy/wifiSuccess'
  105. })
  106. return
  107. console.log(111);
  108. console.log(this.wifiSSID,'this.wifiSSID');
  109. console.log(this.wifiPwd,'this.wifiPwd');
  110. uni.connectWifi({
  111. SSID: this.wifiSSID,
  112. password: this.wifiPwd,
  113. success: (res) => {
  114. console.log(res,'res');
  115. this.show = false;
  116. this.$showToast('网络连接成功,开始TCP链接');
  117. this.connectTCP();
  118. },
  119. fail: (err) => {
  120. console.log(err,'err');
  121. this.show = true;
  122. }
  123. });
  124. },
  125. toTryAgain(){
  126. this.toConnectWiFi();
  127. },
  128. // 链接TCP
  129. connectTCP() {
  130. console.log(222);
  131. try{
  132. this.tcp = wx.createTCPSocket();
  133. this.tcp.connect({
  134. address: '192.168.4.1',
  135. // port: 8266,
  136. });
  137. this.$nextTick(()=>{
  138. this.sendDataTCP();
  139. })
  140. }catch(e){
  141. console.log(e,'e');
  142. this.$showModal(JSON.stringify(e))
  143. }
  144. },
  145. // 发送命令-账号密码
  146. sendDataTCP() {
  147. console.log(333);
  148. this.tcp.write(`AT+CJWAP=${this.wifiSSID},${this.wifiPwd}`);
  149. this.$nextTick(()=>{
  150. this.sendRST();
  151. })
  152. },
  153. // 发送命令-重启
  154. sendRST(){
  155. console.log(444);
  156. this.tcp.write('AT+RST');
  157. this.$showToast('配置成功,设备已重启')
  158. },
  159. close(){
  160. this.show = false;
  161. },
  162. toCode(item){
  163. this.wifiSSID = item.SSID;
  164. }
  165. }
  166. }
  167. </script>
  168. <style scoped lang="less">
  169. .page{
  170. background: #F7F7F7;
  171. padding: 0 30rpx 30rpx;
  172. box-sizing: border-box;
  173. .box{
  174. background: #FFFFFF;
  175. border-radius: 16rpx;
  176. padding: 40rpx 30rpx;
  177. margin-top: 20rpx;
  178. .b_top{
  179. image{
  180. width: 114rpx;
  181. height: 114rpx;
  182. }
  183. text{
  184. font-family: PingFang-SC, PingFang-SC;
  185. font-weight: bold;
  186. font-size: 40rpx;
  187. color: #111111;
  188. line-height: 40rpx;
  189. text-align: center;
  190. margin-top: 30rpx;
  191. }
  192. }
  193. .b_pre{
  194. margin-top: 40rpx;
  195. .bp_text{
  196. font-family: PingFang-SC, PingFang-SC;
  197. font-weight: bold;
  198. font-size: 30rpx;
  199. color: #111111;
  200. line-height: 32rpx;
  201. }
  202. .bp_inp{
  203. width: 100%;
  204. height: 90rpx;
  205. border-radius: 16rpx;
  206. border: 1rpx solid #E2E2E2;
  207. margin-top: 24rpx;
  208. padding: 24rpx 40rpx;
  209. box-sizing: border-box;
  210. input{
  211. border: none;
  212. }
  213. }
  214. }
  215. .zt_btn{
  216. margin-top: 40rpx;
  217. }
  218. .b_title{
  219. font-family: PingFang-SC, PingFang-SC;
  220. font-weight: bold;
  221. font-size: 32rpx;
  222. color: #111111;
  223. line-height: 45rpx;
  224. }
  225. .b_wifi{
  226. margin-top: 48rpx;
  227. text{
  228. font-family: PingFangSC, PingFang SC;
  229. font-weight: 400;
  230. font-size: 30rpx;
  231. color: #0066FE;
  232. line-height: 30rpx;
  233. }
  234. image{
  235. width: 36rpx;
  236. height: 36rpx;
  237. margin-left: 10rpx;
  238. }
  239. }
  240. }
  241. .fail_box{
  242. width: 570rpx;
  243. background: #FFFFFF;
  244. border-radius: 28rpx;
  245. padding: 34rpx 125rpx 54rpx;
  246. box-sizing: border-box;
  247. image{
  248. width: 232rpx;
  249. height: 232rpx;
  250. }
  251. text{
  252. font-family: PingFangSC, PingFang SC;
  253. font-weight: 400;
  254. font-size: 30rpx;
  255. color: #333333;
  256. line-height: 42rpx;
  257. margin-top: 10rpx;
  258. }
  259. p{
  260. font-family: PingFangSC, PingFang SC;
  261. font-weight: 400;
  262. font-size: 24rpx;
  263. color: #A6A6A6;
  264. line-height: 24rpx;
  265. margin-top: 24rpx;
  266. }
  267. .zt_btn{
  268. margin-top: 40rpx;
  269. }
  270. }
  271. }
  272. </style>