| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- <template>
- <view class="content"style="background: white">
- <view class="text-area">
- <u--form style="width: 100%;"
- labelPosition="left"
- ref="uForm"
- >
- <u-form-item
- label="WIFI名称"
- borderBottom
- labelWidth="auto"
- @click="openChooseWifi"
- ref="item1"
- >
- <u--input
- v-model="SSID"
- disabled
- disabledColor="#ffffff"
- placeholder="请选择wifi"
- border="none"
- ></u--input>
- <u-icon
- slot="right"
- name="arrow-right"
- ></u-icon>
- </u-form-item>
- <u-form-item
- label="密码"
- borderBottom
- ref="item1"
- >
- <u--input
- v-model="password"
- border="none"
- ></u--input>
- </u-form-item>
- </u--form>
- </view>
- <view style="margin:10px">
- <u-button text="开始配网" @click="doConnect" size="small" type="primary"></u-button>
- </view>
- <u-picker @cancel="showWiftList=false" @confirm="chooseWifi" :show="showWiftList" :columns="wifiList"></u-picker>
- </view>
- </template>
- <script>
- export default {
- data(){
- return{
- showWiftList:false,
- wifiList: [
- []
- ],
- SSID:"",
- password:""
- }
- },
- onLoad: function() {
- this.openWifi();
- },
- methods:{
- getAuth(){
- wx.getSetting({
- success(res) {
- if (!res.authSetting['scope.userLocation']) {
- wx.authorize({
- scope: 'scope.userLocation',
- success () {
- // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
- }
- })
- }
- }
- })
- },
- chooseWifi(e){
- this.SSID = e.value[0];
- this.showWiftList= false;
- },
- openChooseWifi(){
- if(this.wifiList[0].length == 0){
- this.getWifiList();
- }else{
- this.showWiftList = true;
- }
- },
- openWifi(){
- let self = this;
- // #ifdef MP-WEIXIN
- wx.startWifi({
- success(res) {
- console.log(res);
- // self.getWifiList();
- },
- fail(res) {
- console.log(res)
- uni.showToast({
- title: '请打开WIFI',
- icon: 'none',
- duration: 3000
- });
- },
- })
- // #endif
- },
- getWifi(){
- // #ifdef MP-WEIXIN
- var that = this
- wx.getConnectedWifi({
- success(res) {
- console.log(res)
- that.BSSID = res.wifi.BSSID
- that.WIFIName = res.wifi.SSID
- },
- fail(res) {
- console.log(res)
- //报错的相关处理
- },
- })
- // #endif
- },
- getWifiList(){
- // #ifdef MP-WEIXIN
- var that = this
- uni.showLoading();
- wx.getWifiList({
- success(res) {
- console.log(res)
- wx.onGetWifiList(function(res) {
- that.showWiftList = true;
- uni.hideLoading();
- console.log("获取wifi列表");
- that.wifiList = [[]];
- console.log(res.wifiList); //在这里提取列表数据
- //通过遍历将WIFI名字存入集合,以便下卡框等组件使用
- for (var i = 0; i < res.wifiList.length; i++) {
- that.wifiList[0].push(res.wifiList[i].SSID)
- }
- })
- },
- fail(res) {
- console.log(res)
- uni.showToast({
- title: '获取wifi失败,请检查wifi',
- icon: 'none',
- duration: 2000
- });
- },
- })
- // #endif
- },
- doConnect(){
- // #ifdef MP-WEIXIN
- const airkiss = requirePlugin('airkiss');
- if (this.SSID != '' && this.password != '') {
- console.log(airkiss)
- uni.showLoading({
- title: '配网中请稍后..'
- });
- airkiss.startAirkiss(this.SSID, this.password, function(res) {
- console.log(res)
- switch (res.code) {
- case 0:
- uni.hideLoading();
- uni.showModal({
- title: '初始化失败',
- content: res.result,
- showCancel: false,
- confirmText: '收到',
- })
- break;
- case 1:
- uni.hideLoading();
- uni.showModal({
- title: '配网成功',
- content: '设备IP:' + res.ip + '\r\n 设备Mac:' + res.bssid,
- showCancel: false,
- confirmText: '好的',
- })
- break;
- case 2:
- uni.hideLoading();
- uni.showModal({
- title: '配网失败',
- content: '请检查密码是否正确',
- showCancel: false,
- confirmText: '收到',
- })
- break;
- default:
- uni.hideLoading();
- break;
- }
- })
- } else {
- uni.showToast({
- title: '请选择WIFI并输入密码',
- icon: 'none',
- duration: 3000
- });
- }
- // #endif
- }
- }
- }
- </script>
- <style>
- .content {
- display: flex;
- flex-direction: column;
- align-items: center;
- justify-content: center;
- }
- .text-area {
- display: flex;
- justify-content: center;
- background: white;
- width: 100%;
- height: 100%;
- }
- .title {
- font-size: 36rpx;
- color: #8f8f94;
- }
- </style>
|