index.vue 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660
  1. <template>
  2. <view style="height: 100vh">
  3. <view v-if="!connected">
  4. <scroll-view class="main-container" scroll-y="true"
  5. :refresher-enabled="false">
  6. <view v-for="(item, index) in deviceListDataShow" :key="item.id" class="list-item" hover-class="list-item-hover"
  7. hover-start-time="0" hover-stay-time="100" @click="listViewTap(item.id)">
  8. <image v-if="item.manufacturer==='eciot'" src="/static/img/ecble.png" class="list-item-img"></image>
  9. <image v-else src="/static/img/ble.png" class="list-item-img"></image>
  10. <text class="list-item-name">{{item.name}}</text>
  11. <image v-if="item.rssi >= -41" src="/static/img/s5.png" mode="aspectFit" class="list-item-rssi-img"></image>
  12. <image v-else-if="item.rssi >= -55" src="/static/img/s4.png" mode="aspectFit" class="list-item-rssi-img"></image>
  13. <image v-else-if="item.rssi >= -65" src="/static/img/s3.png" mode="aspectFit" class="list-item-rssi-img"></image>
  14. <image v-else-if="item.rssi >= -75" src="/static/img/s2.png" mode="aspectFit" class="list-item-rssi-img"></image>
  15. <image v-else="item.rssi < -75" src="/static/img/s1.png" mode="aspectFit" class="list-item-rssi-img"></image>
  16. <text class="list-item-rssi">{{item.rssi}}</text>
  17. <view class="list-item-line"></view>
  18. </view>
  19. <u-popup :show="showSendData" >
  20. <view class="slot-content">
  21. <view>
  22. <u--input
  23. :placeholder="i18('请输入值')"
  24. border="surround"
  25. v-model="sendData"
  26. ></u--input>
  27. </view>
  28. <view style="margin:10px;text-decoration: underline;margin-bottom: 0px" @click="sendLanyaData">
  29. <u-button>发送</u-button>
  30. </view>
  31. </view>
  32. </u-popup>
  33. <view v-if="deviceListDataShow.length==0" class="notice"> - {{ $t('buletooth.nodevice') }} -</view>
  34. <view class="gap"></view>
  35. </scroll-view>
  36. </view>
  37. <view v-if="connected">
  38. <view class="text-area">
  39. <u--form style="width: 100%;"
  40. labelPosition="left"
  41. ref="uForm"
  42. >
  43. <u-form-item
  44. label="WIFI名称:"
  45. borderBottom
  46. labelWidth="auto"
  47. @click="openChooseWifi"
  48. ref="item1"
  49. >
  50. <u--input
  51. v-model="SSID"
  52. disabled
  53. disabledColor="#ffffff"
  54. placeholder="请选择wifi:"
  55. border="none"
  56. ></u--input>
  57. <u-icon
  58. slot="right"
  59. name="arrow-right"
  60. ></u-icon>
  61. </u-form-item>
  62. <u-form-item
  63. label="密码:"
  64. borderBottom
  65. ref="item1"
  66. >
  67. <u--input
  68. v-model="password"
  69. border="none"
  70. ></u--input>
  71. </u-form-item>
  72. <u-form-item
  73. label="设备编号:"
  74. borderBottom
  75. ref="item1"
  76. labelWidth="auto"
  77. >
  78. <u--input
  79. v-model="deviceno"
  80. border="none"
  81. ></u--input>
  82. </u-form-item>
  83. <u-form-item
  84. label="二维码ID:"
  85. borderBottom
  86. ref="item1"
  87. labelWidth="auto"
  88. >
  89. <u--input
  90. v-model="qrcodeid"
  91. border="none"
  92. ></u--input>
  93. </u-form-item>
  94. <u-form-item
  95. label="产品ID:"
  96. borderBottom
  97. ref="item1"
  98. labelWidth="auto"
  99. >
  100. <u--input
  101. v-model="productId"
  102. border="none"
  103. ></u--input>
  104. </u-form-item>
  105. </u--form>
  106. </view>
  107. <view style="margin:10px">
  108. <u-button text="开始配网" @click="doConnect" size="small" type="primary"></u-button>
  109. </view>
  110. <u-picker @cancel="showWiftList=false" @confirm="chooseWifi" :show="showWiftList" :columns="wifiList"></u-picker>
  111. </view>
  112. </view>
  113. </template>
  114. <script>
  115. // #ifdef APP
  116. import ecUI from '@/utils/ecUI.js'
  117. import ecBLE from '@/utils/ecBLE/ecBLE.js'
  118. // #endif
  119. // #ifdef MP
  120. const ecUI = require('@/utils/ecUI.js')
  121. const ecBLE = require('@/utils/ecBLE/ecBLE.js')
  122. // #endif
  123. import i18 from '@/utils/i18.js'
  124. let ctx
  125. let deviceListData = []
  126. export default {
  127. data() {
  128. return {
  129. showPwd:false,
  130. rightPwd:"",
  131. pwd:"",
  132. timer:"",
  133. buleid:"",
  134. deviceListDataShow: [],
  135. showTimer:null,
  136. connected:false,
  137. uuid:"",
  138. showDeviceNo:false,
  139. inputDeviceNo:"",
  140. commonCode:"",
  141. showSendData:false,
  142. sendData:"",
  143. qrcodeid:"",
  144. deviceno:"",
  145. SSID:"",
  146. password:"",
  147. showWiftList:false,
  148. productId:56,
  149. wifiList: [
  150. []
  151. ],
  152. }
  153. },
  154. onLoad() {
  155. uni.setNavigationBarTitle({
  156. title: "蓝牙配网"
  157. })
  158. ecUI.showLoading("正在初始化蓝牙模块")
  159. ctx = this
  160. clearInterval(this.timer);
  161. this.timer = setInterval(() => {
  162. ctx.deviceListDataShow = JSON.parse(JSON.stringify(deviceListData))
  163. }, 800)
  164. console.log(this.commonCode)
  165. },
  166. onUnload(){
  167. ecBLE.stopBluetoothDevicesDiscovery();
  168. ecBLE.closeBLEConnection()
  169. },
  170. onShow() {
  171. if(this.showTimer!= null){
  172. clearTimeout(this.showTimer);
  173. }
  174. this.showTimer = setTimeout(() => {
  175. ctx.openBluetoothAdapter()
  176. }, 100)
  177. },
  178. methods: {
  179. getAuth(){
  180. wx.getSetting({
  181. success(res) {
  182. if (!res.authSetting['scope.userLocation']) {
  183. wx.authorize({
  184. scope: 'scope.userLocation',
  185. success () {
  186. // 用户已经同意小程序使用录音功能,后续调用 wx.startRecord 接口不会弹窗询问
  187. }
  188. })
  189. }
  190. }
  191. })
  192. },
  193. chooseWifi(e){
  194. this.SSID = e.value[0];
  195. this.showWiftList= false;
  196. },
  197. openChooseWifi(){
  198. if(this.wifiList[0].length == 0){
  199. this.getWifiList();
  200. }else{
  201. this.showWiftList = true;
  202. }
  203. },
  204. openWifi(){
  205. let self = this;
  206. // #ifdef MP-WEIXIN
  207. wx.startWifi({
  208. success(res) {
  209. console.log(res);
  210. // self.getWifiList();
  211. },
  212. fail(res) {
  213. console.log(res)
  214. uni.showToast({
  215. title: '请打开WIFI',
  216. icon: 'none',
  217. duration: 3000
  218. });
  219. },
  220. })
  221. // #endif
  222. },
  223. getWifi(){
  224. // #ifdef MP-WEIXIN
  225. var that = this
  226. wx.getConnectedWifi({
  227. success(res) {
  228. console.log(res)
  229. that.BSSID = res.wifi.BSSID
  230. that.WIFIName = res.wifi.SSID
  231. },
  232. fail(res) {
  233. console.log(res)
  234. //报错的相关处理
  235. },
  236. })
  237. // #endif
  238. },
  239. getWifiList(){
  240. // #ifdef MP-WEIXIN
  241. var that = this
  242. uni.showLoading();
  243. wx.getWifiList({
  244. success(res) {
  245. console.log(res)
  246. wx.onGetWifiList(function(res) {
  247. that.showWiftList = true;
  248. uni.hideLoading();
  249. console.log("获取wifi列表");
  250. that.wifiList = [[]];
  251. console.log(res.wifiList); //在这里提取列表数据
  252. //通过遍历将WIFI名字存入集合,以便下卡框等组件使用
  253. for (var i = 0; i < res.wifiList.length; i++) {
  254. that.wifiList[0].push(res.wifiList[i].SSID)
  255. }
  256. })
  257. },
  258. fail(res) {
  259. console.log(res)
  260. uni.showToast({
  261. title: '获取wifi失败,请检查wifi',
  262. icon: 'none',
  263. duration: 2000
  264. });
  265. },
  266. })
  267. // #endif
  268. },
  269. doConnect(){
  270. if(this.SSID == ""){
  271. uni.showToast({
  272. title: '请选择WIFI',
  273. icon: 'none',
  274. duration: 3000
  275. });
  276. return;
  277. }
  278. if(this.password == ""){
  279. uni.showToast({
  280. title: '请输入wifi密码',
  281. icon: 'none',
  282. duration: 3000
  283. });
  284. return;
  285. }
  286. if(this.qrcodeid == ""){
  287. uni.showToast({
  288. title: '请输入二维码ID',
  289. icon: 'none',
  290. duration: 3000
  291. });
  292. return;
  293. }
  294. if(this.deviceno == ""){
  295. uni.showToast({
  296. title: '请输入设备编号',
  297. icon: 'none',
  298. duration: 3000
  299. });
  300. return;
  301. }
  302. /**
  303. * ID:XXX+回车换行(设备编号)
  304. * QR:XXXX+回车换行(二维码ID)
  305. * ssid:WiFi名字+回车换行
  306. * psd:wifi密码+回车换行
  307. * 最后+OK
  308. */
  309. let self = this;
  310. let endStr = "$$";
  311. ecUI.showLoading("正在配置网络信息")
  312. setTimeout(function(){
  313. self.sendBlueData("ID:"+self.deviceno+endStr);
  314. },200);
  315. setTimeout(function(){
  316. self.sendBlueData("QR:"+self.qrcodeid+endStr);
  317. },400);
  318. setTimeout(function(){
  319. self.sendBlueData("ssid:"+self.SSID+endStr);
  320. },600);
  321. setTimeout(function(){
  322. self.sendBlueData("psd:"+self.password+endStr);
  323. },800);
  324. setTimeout(function(){
  325. self.sendBlueData("pid:"+self.productId+endStr);
  326. },1000);
  327. setTimeout(function(){
  328. self.sendBlueData("OK");
  329. },1200);
  330. },
  331. stringToHex(str) {
  332. let hex = '';
  333. for (let i = 0; i < str.length; i++) {
  334. const char = str.charCodeAt(i);
  335. const hexChar = char.toString(16).padStart(2, '0');
  336. hex += hexChar;
  337. }
  338. return hex;
  339. },
  340. doByTime(func,time){
  341. setTimeout(function (){
  342. func();
  343. },time);
  344. },
  345. sendLanyaData(){
  346. this.$modal.showToast("正在发送");
  347. this.sendBlueData(this.sendData);
  348. },
  349. i18(text){
  350. return text;
  351. },
  352. cancel(){
  353. this.showPwd = false;
  354. uni.navigateBack({
  355. });
  356. },
  357. resetPwd(){
  358. let self = this;
  359. if(!this.inputDeviceNo){
  360. self.$modal.showToast("请输入序列号");
  361. return;
  362. }
  363. let uuidRight = false;
  364. if(this.inputDeviceNo == this.commonCode || this.uuid == this.inputDeviceNo){
  365. uuidRight = true;
  366. }
  367. if(!uuidRight){
  368. self.$modal.showToast("序列号有误");
  369. return;
  370. }
  371. if(uuidRight){
  372. this.$modal.confirm("密码将被重置为123456").then(res=>{
  373. setPwd("123456")
  374. self.$modal.showToast("密码修改成功");
  375. this.rightPwd = "123456";
  376. self.loginSuccess();
  377. });
  378. }
  379. },
  380. getBeijingTime() {
  381. const date = new Date();
  382. const utcTime = date.getTime() + (date.getTimezoneOffset() * 60 * 1000);
  383. const beijingTime = new Date(utcTime + (8 * 60 * 60 * 1000));
  384. return beijingTime;
  385. },
  386. generateUniqueNumber(date) {
  387. let dateString = date.toISOString().slice(0, 10).replace(/-/g, '');
  388. console.log(dateString)
  389. let hash = w_md5.hex_md5_32(dateString);
  390. console.log(hash);//32位小写
  391. let str = "";
  392. for (let i = 0; i < 6; i++) {
  393. const c = hash.charCodeAt(i);
  394. str = str+""+c+""
  395. }
  396. return str.substr(0,6);
  397. },
  398. goBack(){
  399. uni.navigateBack({
  400. });
  401. },
  402. $t(title){
  403. return title;
  404. },
  405. inputPwd(){
  406. if(!this.pwd ){
  407. this.$modal.showToast(this.$t('buletooth.errpwd'));
  408. }else{
  409. if(this.rightPwd && this.rightPwd === this.pwd){
  410. this.loginSuccess();
  411. }else{
  412. this.$modal.showToast(this.$t('buletooth.errpwd'));
  413. }
  414. }
  415. },
  416. loginSuccess(){
  417. this.showPwd = false;
  418. this.showDeviceNo = false;
  419. uni.setStorageSync("pwd",this.rightPwd);
  420. uni.setStorageSync('blueid', this.buleid);
  421. this.pwd = "";
  422. ecBLE.stopBluetoothDevicesDiscovery();
  423. uni.navigateTo({
  424. url: '/pages/weitiandi/bluetooth/status'
  425. });
  426. },
  427. getLocalPwd(){
  428. let pwd = uni.getStorageSync("pwd");
  429. return pwd;
  430. },
  431. sendBlueData(tempSendData){
  432. tempSendData = this.stringToHex(tempSendData);
  433. let data = tempSendData
  434. .replace(/\s*/g, '')
  435. .replace(/\n/g, '')
  436. .replace(/\r/g, '')
  437. console.log("写入数据:"+data);
  438. ecBLE.writeBLECharacteristicValue(data, false)
  439. },
  440. listViewTap(id){
  441. let self = this;
  442. ecUI.showLoading("正在连接蓝牙")
  443. ecBLE.onBLEConnectionStateChange(res => {
  444. console.log(res);
  445. if (res.ok) {
  446. self.connected = true;
  447. self.openWifi();
  448. ecUI.hideLoading()
  449. self.showSendData = true;
  450. ecBLE.stopBluetoothDevicesDiscovery();
  451. //
  452. } else {
  453. ecUI.hideLoading()
  454. this.$modal.showToast("请检查是否配置成功");
  455. }
  456. });
  457. //receive data
  458. ecBLE.onBLECharacteristicValueChange((str, strHex) => {
  459. console.log("数据来了")
  460. let isCheckRevHex = true;
  461. let data =
  462. (isCheckRevHex ? strHex.replace(/[0-9a-fA-F]{2}/g, ' $&') : str)
  463. console.log(data)
  464. self.$modal.closeLoading();
  465. data = parseDataObj(data);
  466. })
  467. self.connected = false;
  468. ecBLE.createBLEConnection(id);
  469. setTimeout(function (){
  470. if(!self.connected){
  471. self.$modal.showToast(i18('连接失败'));
  472. self.startBluetoothDevicesDiscovery()
  473. }
  474. },10000);
  475. },
  476. showInputPwd(){
  477. this.showPwd = true;
  478. },
  479. messageCallback(data){
  480. let self = this;
  481. console.log(data);
  482. let type = data.type;
  483. let real_data = data.real_data;
  484. if(type == 253){
  485. self.$modal.closeLoading();
  486. self.uuid = real_data.substr(0,6);
  487. }
  488. self.$forceUpdate();
  489. console.log('收到服务器内容:' + JSON.stringify(data));
  490. },
  491. forgetPwd(){
  492. this.$modal.loading("正在读取设备ID");
  493. getUUID()
  494. this.showDeviceNo = true;
  495. },
  496. openBluetoothAdapter() {
  497. let self = this;
  498. ecBLE.onBluetoothAdapterStateChange(res => {
  499. ecUI.hideLoading()
  500. if (res.ok) {
  501. ctx.startBluetoothDevicesDiscovery()
  502. } else {
  503. ecUI.showModal(
  504. this.$t('buletooth.tip'),
  505. `Bluetooth adapter error | ${res.errCode} | ${res.errMsg}`,
  506. () => {
  507. }
  508. )
  509. }
  510. })
  511. ecBLE.openBluetoothAdapter()
  512. },
  513. startBluetoothDevicesDiscovery() {
  514. ecBLE.stopBluetoothDevicesDiscovery();
  515. console.log('start search')
  516. ecBLE.onBluetoothDeviceFound(res => {
  517. let isRight = true;
  518. // if(res.name.startsWith('BT_') || res.name.startsWith('FC41D')){
  519. // isRight = true;
  520. // }
  521. if(!isRight){
  522. return;
  523. }
  524. for (const item of deviceListData) {
  525. if (item.id === res.id) {
  526. item.name = res.name
  527. item.rssi = res.rssi
  528. return
  529. }
  530. }
  531. let manufacturer = ''
  532. if (res.name.length === 11 && res.name.startsWith('@')) {
  533. manufacturer = 'eciot'
  534. }
  535. if (res.name.length === 15 && res.name.startsWith('BT_')) {
  536. manufacturer = 'eciot'
  537. }
  538. manufacturer = 'eciot'
  539. deviceListData.push({
  540. id: res.id,
  541. name: res.name,
  542. rssi: res.rssi,
  543. manufacturer,
  544. })
  545. })
  546. ecBLE.startBluetoothDevicesDiscovery()
  547. },
  548. }
  549. }
  550. </script>
  551. <style>
  552. .main-container {
  553. height: 100vh;
  554. }
  555. .list-item {
  556. height: 57px;
  557. position: relative;
  558. }
  559. .list-item-hover {
  560. background-color: #e5e4e9;
  561. }
  562. .list-item-img {
  563. position: absolute;
  564. width: 36px;
  565. height: 36px;
  566. left: 20px;
  567. top: 10px;
  568. }
  569. .list-item-name {
  570. position: absolute;
  571. font-size: 22px;
  572. left: 76px;
  573. top: 0px;
  574. line-height: 56px;
  575. }
  576. .list-item-rssi-img {
  577. position: absolute;
  578. width: 20px;
  579. height: 20px;
  580. right: 20px;
  581. top: 13px;
  582. }
  583. .list-item-rssi {
  584. position: absolute;
  585. width: 40px;
  586. height: 20px;
  587. right: 10px;
  588. top: 33px;
  589. font-size: 12px;
  590. font-weight: bold;
  591. display: flex;
  592. justify-content: center;
  593. }
  594. .list-item-line {
  595. position: absolute;
  596. height: 1px;
  597. width: 100vw;
  598. left: 20px;
  599. top: 56px;
  600. background-color: #c6c6c8;
  601. }
  602. .notice {
  603. display: flex;
  604. justify-content: center;
  605. align-items: center;
  606. margin-top: 10px;
  607. font-size: 13px;
  608. color: #909399;
  609. }
  610. .gap {
  611. height: 57px;
  612. }
  613. .text-area {
  614. display: flex;
  615. justify-content: center;
  616. background: white;
  617. width: 100%;
  618. height: 100%;
  619. }
  620. </style>