wifi.vue 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. <template>
  2. <view class="page" :style="{'min-height':h+'px', 'padding-top':mt+'px'}">
  3. <cus-header title='无线局域网' bgColor="transparent"></cus-header>
  4. <div class="box">
  5. <div class="info adffcacjc">
  6. <image src="https://transcend.ringzle.com/xiaozhi-app/profile/2025/05/29/f1cc6aee-2d47-46f0-ae29-3ef4923777cb.png"></image>
  7. <text>无线局域网</text>
  8. <p>接入无线局域网、查看可用网络,并管理加入网络及附近热点设置。</p>
  9. </div>
  10. <div class="pre adfacjb">
  11. <div class="p_l">无线局域网</div>
  12. <div class="p_r">
  13. <u-switch v-model="openWifi" size="46" activeColor="#5AC725"></u-switch>
  14. </div>
  15. </div>
  16. <div class="pre adfacjb" v-for="(item,index) in wifiList" :key="index" @tap="toSet(item)">
  17. <div class="p_l">
  18. <p>{{item.SSID}}</p>
  19. <span>{{item.secure?'高安全性':'低安全性'}}</span>
  20. </div>
  21. <div class="p_r"></div>
  22. </div>
  23. </div>
  24. <div class="box" v-if="myWifi">
  25. <div class="title">我的网络</div>
  26. <div class="pre adfacjb">
  27. <div class="p_l">{{myWifi}}</div>
  28. <div class="p_r"></div>
  29. </div>
  30. </div>
  31. </view>
  32. </template>
  33. <script>
  34. export default {
  35. data(){
  36. return {
  37. openWifi:true,
  38. wifiList:[],
  39. myWifi:'',
  40. platform:'',
  41. flag:true
  42. }
  43. },
  44. watch:{
  45. openWifi:{
  46. handler(newval,oldval){
  47. if(newval!==oldval){
  48. this.dealWifi(newval)
  49. }
  50. }
  51. }
  52. },
  53. onLoad() {
  54. this.getCurrentWifi();
  55. let sysInfo = wx.getSystemInfoSync();
  56. this.platform = sysInfo?.platform;
  57. if(this.platform=='ios'){
  58. uni.showModal({
  59. title:'温馨提示',
  60. content:'检测到您的手机为ios系统,即将自动跳转设置-微信,需要您在设置中打开无线局域网,等待获取到WiFi列表后返回小程序选择“Xiaozhi”开头的热点进行连接。',
  61. success: (res) => {
  62. if(res.confirm){
  63. this.dealWifi(true)
  64. }
  65. }
  66. })
  67. }else{
  68. this.dealWifi(true)
  69. }
  70. },
  71. onShow() {
  72. let sysInfo = wx.getSystemInfoSync();
  73. this.platform = sysInfo?.platform;
  74. },
  75. methods:{
  76. getCurrentWifi(){
  77. let that = this;
  78. wx.startWifi({
  79. success(res){
  80. wx.getConnectedWifi({
  81. success(res){
  82. if(res.errCode===0){
  83. that.myWifi = res.wifi?.SSID;
  84. }
  85. },
  86. fail: (err) => {
  87. if(err.errMsg.indexOf('getConnectedWifi:fail:wifi is disable')>-1) that.$showToast('WiFi未开启')
  88. }
  89. })
  90. }
  91. })
  92. },
  93. dealWifi(flag){
  94. let that = this;
  95. if(flag){
  96. uni.showLoading({
  97. title:'获取WiFi列表中'
  98. })
  99. wx.startWifi({
  100. success (res) {
  101. wx.getWifiList({
  102. success(res2) {
  103. wx.onGetWifiList(res3 => {
  104. let temp = [];
  105. res3.wifiList.forEach(w=>{
  106. if(w.SSID){
  107. let exit = temp.find(t=>t.SSID===w.SSID);
  108. if(!exit) temp = [...temp,w]
  109. }
  110. })
  111. that.$nextTick(()=>{
  112. that.wifiList = temp;
  113. that.$forceUpdate();
  114. uni.hideLoading();
  115. })
  116. })
  117. },
  118. fail: (err2) => {
  119. console.log(err2,'err2');
  120. }
  121. })
  122. },
  123. fail: (err) => {
  124. console.log(err,'err');
  125. }
  126. })
  127. }else{
  128. this.wifiList = [];
  129. }
  130. },
  131. toSet(item){
  132. let that = this;
  133. if(item.SSID.indexOf('Xiaozhi-')>-1){
  134. if(!this.flag) return
  135. this.flag = false;
  136. uni.showLoading({
  137. title:'热点连接中'
  138. })
  139. wx.connectWifi({
  140. SSID: item.SSID,
  141. password: '',
  142. success: (resu) => {
  143. uni.hideLoading();
  144. this.flag = true;
  145. // if(resu.errCode===0||resu.errMsg==='connectWifi:ok'){
  146. setTimeout(()=>{
  147. uni.navigateTo({
  148. url:'/pagesMy/wifiSet'
  149. })
  150. },2000)
  151. // }
  152. },
  153. fail: (err) => {
  154. uni.hideLoading();
  155. this.flag = true;
  156. console.log(err,'err:connectWifi');
  157. }
  158. });
  159. }else this.$showToast('请选择Xiaozhi开头的热点')
  160. }
  161. }
  162. }
  163. </script>
  164. <style scoped lang="less">
  165. .page{
  166. background: #FFFFFF;
  167. padding: 0 31rpx 30rpx;
  168. box-sizing: border-box;
  169. .box{
  170. width: 100%;
  171. padding: 50rpx 30rpx 0;
  172. box-sizing: border-box;
  173. background: #FFFFFF;
  174. .info{
  175. margin-bottom: 60rpx;
  176. image{
  177. width: 114rpx;
  178. height: 114rpx;
  179. }
  180. text{
  181. font-family: PingFang-SC, PingFang-SC;
  182. font-weight: bold;
  183. font-size: 40rpx;
  184. color: #111111;
  185. line-height: 40rpx;
  186. margin-top: 20rpx;
  187. }
  188. p{
  189. font-family: PingFang-SC, PingFang-SC;
  190. font-weight: 400;
  191. font-size: 34rpx;
  192. color: #111111;
  193. line-height: 40rpx;
  194. text-align: center;
  195. margin-top: 20rpx;
  196. }
  197. }
  198. .pre{
  199. padding: 20rpx 30rpx;
  200. border-top:1rpx solid #E5E5E5;
  201. .p_l{
  202. font-family: PingFang-SC, PingFang-SC;
  203. font-weight: 400;
  204. font-size: 32rpx;
  205. color: #111111;
  206. line-height: 40rpx;
  207. display: flex;
  208. flex-direction: column;
  209. span{
  210. font-size: 26rpx;
  211. }
  212. }
  213. }
  214. .title{
  215. color: #666666;
  216. margin-bottom: 40rpx;
  217. }
  218. }
  219. }
  220. </style>