addMattresses.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  1. // subpages/addMattresses/addMattresses.js
  2. const app = getApp();
  3. const util = require('../../utils/util.js');
  4. const timeOut = 40; //超时时间
  5. var timeId = "";
  6. var sequenceControl = 0;
  7. Page({
  8. /**
  9. * 页面的初始数据
  10. */
  11. data: {
  12. isEdit: false,
  13. showBluetoothList: false,
  14. showBluetoothConnected: false,//连接中
  15. isBluetoothConnected: false,//连接成功
  16. bluetoothConnectedStep: 0,
  17. placeOfUse: "",
  18. usingHuman: "",
  19. pid: "",
  20. sn: "",
  21. ssid: "",
  22. password: "",
  23. bluetoothSelectIndex: -1,
  24. activeIcon: "/subpages/images/ic_blue_radio_check.png",
  25. normalIcon: "/subpages/images/ic_blue_radio_uncheck.png",
  26. deviceList: [],
  27. deviceId: "",
  28. deviceIds: [],
  29. wifiInfo: null,
  30. deviceId: "",
  31. password: "",
  32. notice: "",
  33. value: 90,
  34. gradientColor: {
  35. '0%': '#00CEB2',
  36. '50%': '#00C7FF',
  37. '100%': '#53DAFF',
  38. },
  39. statusDefault: ['蓝牙连接...', '蓝牙连接成功', '成功获取设备信息', '获取属性信息成功', '发送配置信息...', '配置信息发送成功', '连接成功'],
  40. descFailList: ["蓝牙连接失败", "获取设备信息失败", "获取属性信息失败", "发送配置信息失败", "分配网络失败"],
  41. failure: false,
  42. value: 0,
  43. desc: "设备连接...",
  44. blueConnectNum: 0,
  45. isChecksum: true,
  46. isEncrypt: false,
  47. flagEnd: false,
  48. defaultData: 1,
  49. ssidType: 2,
  50. passwordType: 3,
  51. meshIdType: 3,
  52. deviceNo: 0,
  53. uuid: "",
  54. serviceId: "",
  55. meshId: "",
  56. processList: [],
  57. result: [],
  58. },
  59. onClose() {
  60. wx.offBluetoothDeviceFound();
  61. this.setData({ showBluetoothList: false, showBluetoothConnected: false });
  62. },
  63. closeEndDialog() {
  64. this.onClose()
  65. wx.navigateBack()
  66. },
  67. checkBluetoothSelectIndex(e) {
  68. const index = e.currentTarget.dataset.index;
  69. this.setData({ bluetoothSelectIndex: index });
  70. },
  71. commitData() {
  72. console.log(this.data.placeOfUse);
  73. console.log(this.data.usingHuman);
  74. console.log(this.data.sn);
  75. console.log(this.data.ssid);
  76. console.log(this.data.password);
  77. this.checkSn(this.data.sn);
  78. },
  79. commitUserData() {
  80. const that = this;
  81. if (that.data.isEdit) {
  82. wx.request({
  83. url: 'https://aipush.aidsleep.cn/customeredit',
  84. method: 'POST',
  85. data: {
  86. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  87. pid: this.data.pid,
  88. sntram: this.data.sn,
  89. per: this.data.usingHuman,
  90. loc: this.data.placeOfUse,
  91. },
  92. success: (res) => {
  93. var codes = res.data.codes;
  94. if (codes && codes == 'success') {
  95. wx.showToast({
  96. icon: 'none',
  97. title: "添加成功"
  98. });
  99. } else {
  100. wx.showToast({
  101. icon: 'none',
  102. title: "添加失败"
  103. });
  104. }
  105. },
  106. fail: () => {
  107. wx.showToast({
  108. icon: 'none',
  109. title: "添加失败"
  110. });
  111. }
  112. });
  113. return
  114. }
  115. wx.request({
  116. url: 'https://aipush.aidsleep.cn/wxsaveunionsn',
  117. method: 'POST',
  118. data: {
  119. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  120. unionid: wx.getStorageSync("unionid"),
  121. sntram: this.data.sn,
  122. per: this.data.usingHuman,
  123. loc: this.data.placeOfUse,
  124. },
  125. success: (res) => {
  126. var codes = res.data.codes;
  127. if (codes && codes == 'success') {
  128. wx.showToast({
  129. icon: 'none',
  130. title: "添加成功"
  131. });
  132. } else {
  133. wx.showToast({
  134. icon: 'none',
  135. title: "添加失败"
  136. });
  137. }
  138. },
  139. fail: () => {
  140. wx.showToast({
  141. icon: 'none',
  142. title: "添加失败"
  143. });
  144. }
  145. });
  146. },
  147. // 开启蓝牙
  148. bindViewBlue: function () {
  149. const that = this;
  150. wx.offBluetoothDeviceFound();
  151. wx.closeBluetoothAdapter();
  152. wx.openBluetoothAdapter({
  153. success(res) {
  154. //console.log(res);
  155. that.getBluDevice()
  156. },
  157. fail(res) {
  158. console.log(res);
  159. wx.showToast({
  160. icon: 'none',
  161. title: `${JSON.stringify(res)}`
  162. });
  163. }
  164. })
  165. },
  166. // 获取蓝牙设备列表
  167. getBluDevice() {
  168. var self = this;
  169. //第一步检查蓝牙适配器是否可用
  170. wx.onBluetoothAdapterStateChange(function (res) {
  171. if (!res.available) {
  172. util.showToast('蓝牙不可用');
  173. return false;
  174. }
  175. });
  176. var oldDeviceList = wx.getStorageSync('deviceList');
  177. if (oldDeviceList && oldDeviceList.length) {
  178. self.setData({
  179. deviceList: oldDeviceList,
  180. showBluetoothList: true
  181. });
  182. }
  183. //第二步关闭适配器,重新来搜索
  184. wx.openBluetoothAdapter({
  185. success: function (res) {
  186. wx.getBluetoothAdapterState({
  187. success: function (res) {
  188. wx.stopBluetoothDevicesDiscovery({
  189. success: function (res) {
  190. wx.startBluetoothDevicesDiscovery({
  191. services: ['0000FFF0-0000-1000-8000-00805F9B34FB'],
  192. success: function (res) {
  193. wx.onBluetoothDeviceFound(function (res) {
  194. console.log(res);
  195. var list = util.filterDevice(res.devices, "name");
  196. if (list.length > 0) {
  197. console.log('==========onBluetoothDeviceFound===========');
  198. console.log(list);
  199. var deviceList = self.data.deviceList;
  200. var doDevices = wx.getStorageSync('doDevices');
  201. if (doDevices) {
  202. } else {
  203. doDevices = [];
  204. }
  205. console.log(deviceList);
  206. console.log(doDevices);
  207. wx.setStorageSync('doDevices', doDevices);
  208. wx.hideLoading();
  209. list.forEach(function (item) {
  210. var flag = true;
  211. for (var i = 0; i < deviceList.length; i++) {
  212. var itemSub = deviceList[i];
  213. if (itemSub.deviceId === item.deviceId) {
  214. flag = false;
  215. break;
  216. }
  217. }
  218. if (flag) {
  219. var arrD = item['name'].split(' ');
  220. item['sn'] = arrD[1];
  221. // wx.request({
  222. // url: 'https://aipush.aidsleep.cn/checkbeds',
  223. // method: 'POST',
  224. // header: {
  225. // 'content-type': 'application/json' // 设置请求的 header
  226. // },
  227. // data: {
  228. // "snlists": arrD[1], // 需要发送的数据
  229. // "token": "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3"
  230. // },
  231. // success: function (res) {
  232. // //console.log(res);
  233. // if (res.data.data[arrD[1]].trim() == 'yes') {
  234. // item['beOnline'] = 'icon-deng-ok';
  235. // } else {
  236. // item['beOnline'] = '';
  237. // // if (doDevices.indexOf(item['deviceId']) >= 0) {
  238. // // item['beOnline'] = 'icon-deng-ok';
  239. // // }
  240. // }
  241. deviceList.push(item);
  242. // },
  243. // fail: function (error) {
  244. // console.error(error);
  245. // }
  246. // });
  247. }
  248. })
  249. self.setData({
  250. deviceList: deviceList,
  251. showBluetoothList: true
  252. });
  253. wx.setStorageSync('deviceList', deviceList);
  254. }
  255. })
  256. }
  257. })
  258. },
  259. fail: function (res) {
  260. console.log(res);
  261. }
  262. });
  263. },
  264. fail: function (res) {
  265. console.log(res);
  266. }
  267. });
  268. },
  269. fail: function (res) {
  270. console.log(res);
  271. }
  272. });
  273. },
  274. commitBluetooth() {
  275. this.onClose();
  276. if (this.data.bluetoothSelectIndex < 0) {
  277. util.showToast("请选择一个床垫");
  278. wx.offBluetoothDeviceFound()
  279. return false;
  280. }
  281. var deviceId = this.data.deviceList[this.data.bluetoothSelectIndex].deviceId;
  282. var name = this.data.deviceList[this.data.bluetoothSelectIndex].name;
  283. var nameList = this.data.deviceList[this.data.bluetoothSelectIndex].name.split(' ');
  284. this.setData({
  285. deviceId: deviceId,
  286. sn: nameList[1]
  287. })
  288. wx.offBluetoothDeviceFound()
  289. },
  290. // 开始配网
  291. bindViewConnect() {
  292. const that = this;
  293. if (that.data.bluetoothSelectIndex < 0) {
  294. util.showToast("没有指定要配网的设备");
  295. return false;
  296. }
  297. var deviceIds = this.data.deviceList[this.data.bluetoothSelectIndex];
  298. if (this.data.deviceList.length == 0 || deviceIds.length === 0) {
  299. util.showToast("没有指定要配网的设备");
  300. return false;
  301. }
  302. that.gotoProvision()
  303. },
  304. // 获取Wifi
  305. getWifiInfo: function () {
  306. wx.startWifi({
  307. success(res) {
  308. //console.log(' ================== startWifi ================== ');
  309. //console.log(res);
  310. wx.getConnectedWifi({
  311. success(res) {
  312. },
  313. fail: function (err) {
  314. console.log(' ================== startWifi fail ================== ');
  315. console.log(err);
  316. if (app.data.sytem == 'ios') {
  317. util.showToast("获取Wi-Fi信息失败,请更新微信到最新版");
  318. } else {
  319. util.showToast("获取Wi-Fi信息失败");
  320. }
  321. }
  322. });
  323. }
  324. });
  325. const self = this;
  326. wx.startWifi({
  327. success(res) {
  328. //console.log(res)
  329. // 先取一次,防止IOS获取不到
  330. wx.getConnectedWifi({
  331. success(res) {
  332. self.setData({
  333. wifiInfo: res.wifi,
  334. ssid: res.wifi.SSID
  335. });
  336. //console.log('== getConnectedWifi ==');
  337. //console.log(res);
  338. if (res.frequency >= 2412 && res.frequency <= 2472) { } else {
  339. self.setData({
  340. notice: "温馨提示:当前您连接的Wi-Fi网络是5G频段,建议连接2.4G频段配网"
  341. })
  342. }
  343. },
  344. fail: function (err) {
  345. util.showToast("获取Wi-Fi信息失败");
  346. }
  347. })
  348. wx.onWifiConnected(function (res) {
  349. // console.log('== onWifiConnected ==');
  350. // console.log(res);
  351. self.setData({
  352. wifiInfo: res.wifi
  353. })
  354. })
  355. },
  356. fail(res) {
  357. console.log('WIFI初始化失败', res)
  358. util.showToast("WIFI初始化失败");
  359. }
  360. })
  361. },
  362. gotoProvision: function () {
  363. const wifiInfo = this.data.wifiInfo;
  364. const ssid = wifiInfo != null ? wifiInfo.SSID : null;
  365. const bssid = wifiInfo != null ? wifiInfo.BSSID : null;
  366. const password = this.data.password;
  367. if (wifiInfo == null) {
  368. util.showToast("没有Wi-Fi连接");
  369. return;
  370. } else {
  371. if (ssid == null || ssid == '' || password == '') {
  372. util.showToast("WiFi登录名和密码不能为空");
  373. return;
  374. } else {
  375. const info = {
  376. ssid: ssid,
  377. bssid: bssid,
  378. password: password,
  379. deviceIds: this.data.deviceId
  380. }
  381. this.blueConnect();
  382. }
  383. }
  384. },
  385. // 蓝牙链接
  386. blueConnect: function (event) {
  387. //console.log(' =============== blueConnect start ===============')
  388. var self = this;
  389. sequenceControl = 0;
  390. self.setData({
  391. flagEnd: false,
  392. serviceId: "",
  393. uuid: "",
  394. showBluetoothConnected: true
  395. });
  396. self.setProcess(0, util.descSucList[0], 1);
  397. wx.offBluetoothAdapterStateChange();
  398. //第一步检查蓝牙适配器是否可用
  399. wx.onBluetoothAdapterStateChange(function (res) {
  400. if (!res.available) {
  401. util.showToast('蓝牙不可用');
  402. return false;
  403. }
  404. });
  405. //console.log(app.data);
  406. if (app.data.sytem == 'ios') {
  407. }
  408. self.onTimeout(0);
  409. //console.log('deviceId = ' + self.data.deviceId)
  410. wx.createBLEConnection({
  411. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  412. deviceId: self.data.deviceId,
  413. timeout: 10000,
  414. success: function (res) {
  415. wx.setBLEMTU({
  416. deviceId: self.data.deviceId,
  417. mtu: 128
  418. })
  419. //console.log("============ createBLEConnection success =============")
  420. //console.log(self.data.deviceId)
  421. self.getDeviceServices(self.data.deviceId);
  422. },
  423. fail: function (res) {
  424. console.log("============ createBLEConnection fail =============")
  425. console.log(res)
  426. console.log(self.data.blueConnectNum)
  427. var num = self.data.blueConnectNum;
  428. if (num < 3) {
  429. self.blueConnect();
  430. num++;
  431. self.setData({
  432. blueConnectNum: num
  433. })
  434. } else {
  435. self.setFailProcess(true, res.errCode + "::" + util.descFailList[0]);
  436. }
  437. }
  438. })
  439. },
  440. getDeviceServices: function (deviceId) {
  441. var self = this;
  442. wx.getBLEDeviceServices({
  443. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  444. deviceId: deviceId,
  445. success: function (res) {
  446. self.setProcess(10, util.descSucList[1], 2);
  447. var services = res.services;
  448. if (services.length > 0) {
  449. for (var i = 0; i < services.length; i++) {
  450. var uuid = services[i].uuid;
  451. if (uuid == app.data.service_uuid) {
  452. self.getDeviceCharacteristics(deviceId, uuid);
  453. return false;
  454. }
  455. }
  456. }
  457. },
  458. fail: function (res) {
  459. self.setFailProcess(true, res.errCode + "::" + util.descFailList[1]);
  460. }
  461. })
  462. },
  463. getDeviceCharacteristics: function (deviceId, serviceId) {
  464. //console.log(" ============== getDeviceCharacteristics =============== ");
  465. var self = this;
  466. wx.getBLEDeviceCharacteristics({
  467. // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  468. deviceId: deviceId,
  469. serviceId: serviceId,
  470. success: function (res) {
  471. self.setProcess(40, util.descSucList[2], 3);
  472. var list = res.characteristics;
  473. if (list.length > 0) {
  474. for (var i = 0; i < list.length; i++) {
  475. var uuid = list[i].uuid;
  476. if (uuid == app.data.characteristic_write_uuid) {
  477. self.openNotify(deviceId, serviceId, uuid);
  478. self.setData({
  479. serviceId: serviceId,
  480. uuid: uuid,
  481. })
  482. return false;
  483. }
  484. }
  485. }
  486. },
  487. fail: function (res) {
  488. console.log(res);
  489. self.setFailProcess(true, res.errCode + "::" + util.descFailList[2]);
  490. }
  491. })
  492. },
  493. //连接超时
  494. onTimeout: function (num) {
  495. const self = this;
  496. timeId = setInterval(function () {
  497. if (num < timeOut) {
  498. num++;
  499. } else {
  500. clearInterval(timeId);
  501. util.showToast('配网失败,请检查wifi密码或设备是否连接正常');
  502. self.setFailProcess(true, util.descFailList[4]);
  503. }
  504. }, 1000)
  505. },
  506. //启用通知
  507. openNotify: function (deviceId, serviceId, characteristicId) {
  508. //console.log("=========== openNotify ============");
  509. var self = this;
  510. wx.notifyBLECharacteristicValueChange({
  511. state: true, // 启用 notify 功能
  512. deviceId: deviceId,
  513. serviceId: serviceId,
  514. characteristicId: app.data.characteristic_read_uuid,
  515. success: function (res) {
  516. //console.log(res);
  517. self.writeDevice(deviceId, serviceId, characteristicId, null);
  518. self.onNotify();
  519. },
  520. fail: function (res) {
  521. console.log(res);
  522. }
  523. })
  524. },
  525. //监听通知
  526. onNotify: function () {
  527. //console.log("=========== onNotify ============");
  528. var self = this;
  529. wx.onBLECharacteristicValueChange(function (res) {
  530. //console.log(res);
  531. self.getResultType(util.ab2hex(res.value));
  532. })
  533. },
  534. getCharCodeat: function (str) {
  535. let utf8Bytes = [];
  536. for (let i = 0; i < str.length; i++) {
  537. let char = str.charCodeAt(i);
  538. if (char < 0x80) {
  539. utf8Bytes.push(char);
  540. } else if (char < 0x800) {
  541. utf8Bytes.push(0xc0 | (char >> 6));
  542. utf8Bytes.push(0x80 | (char & 0x3f));
  543. } else if (char < 0x10000) {
  544. utf8Bytes.push(0xe0 | (char >> 12));
  545. utf8Bytes.push(0x80 | ((char >> 6) & 0x3f));
  546. utf8Bytes.push(0x80 | (char & 0x3f));
  547. }
  548. }
  549. return utf8Bytes;
  550. // var list = [];
  551. // for (var i = 0; i < str.length; i++) {
  552. //   list.push(str.charCodeAt(i));
  553. // }
  554. // return list;
  555. },
  556. // 设备数据开始写入
  557. writeDevice: function (deviceId, serviceId, characteristicId, data) {
  558. //console.log("===== writeDevice =====");
  559. var self = this,
  560. obj = {};
  561. self.setProcess(60, util.descSucList[4], 3);
  562. sequenceControl = parseInt(sequenceControl) + 1;
  563. if (!util._isEmpty(data)) {
  564. obj = util.isSubcontractor(data, true, sequenceControl);
  565. } else {
  566. data = util.writeData(self.getCharCodeat('"' + self.data.ssid + '","' + self.data.password + '"'));
  567. obj = util.isSubcontractor(data, self.data.isChecksum, sequenceControl);
  568. }
  569. var typedArray = new Uint8Array(obj.lenData)
  570. wx.writeBLECharacteristicValue({
  571. deviceId: deviceId,
  572. serviceId: serviceId,
  573. characteristicId: characteristicId,
  574. value: typedArray.buffer,
  575. success: function (res) {
  576. //console.log(res);
  577. if (obj.flag) {
  578. self.writeDevice(deviceId, serviceId, characteristicId, obj.laveData);
  579. } else {
  580. self.setProcess(80, util.descSucList[5], 4);
  581. }
  582. },
  583. fail: function (res) {
  584. console.log(res);
  585. self.setFailProcess(true, res.errCode + "::" + util.descFailList[3]);
  586. }
  587. })
  588. },
  589. getResultType: function (list) {
  590. console.log(' ========= getResultType ========= ');
  591. var self = this;
  592. console.log(list);
  593. var deviceList = wx.getStorageSync('deviceList');
  594. console.log(deviceList);
  595. if (list.length < 4) {
  596. self.setFailProcess(true, util.descFailList[4]);
  597. return false;
  598. }
  599. var val = list[4];
  600. if (val == "00") {
  601. var idx = self.data.deviceNo + 1;
  602. var doDevices = wx.getStorageSync('doDevices');
  603. if (doDevices) {
  604. if (doDevices.indexOf(self.data.deviceId) < 0) {
  605. doDevices.push(self.data.deviceId);
  606. }
  607. } else {
  608. doDevices = [self.data.deviceId];
  609. }
  610. wx.setStorageSync('doDevices', doDevices);
  611. var deviceSn = self.data.deviceId;
  612. for (var i = 0; i < deviceList.length; i++) {
  613. var itemSub = deviceList[i];
  614. if (itemSub.deviceId === self.data.deviceId) {
  615. deviceSn = itemSub.sn;
  616. break;
  617. }
  618. }
  619. if (idx < self.data.deviceIds.length) {
  620. self.setProcess(90, 'SN:' + deviceSn + ' 连接成功', 5);
  621. wx.closeBLEConnection({
  622. deviceId: self.data.deviceId,
  623. success: function (res) { }
  624. })
  625. self.setData({
  626. deviceNo: idx,
  627. deviceId: self.data.deviceIds[idx],
  628. blueConnectNum: 0
  629. });
  630. self.blueConnect();
  631. } else {
  632. self.setProcess(90, 'SN:' + deviceSn + ' 连接成功', 5);
  633. self.setProcess(100, util.descSucList[6], 7);
  634. self.setData({
  635. result: [],
  636. })
  637. }
  638. } else {
  639. self.setFailProcess(true, util.descFailList[4])
  640. }
  641. },
  642. setProcess: function (value, desc, step = 0) {
  643. var self = this,
  644. list = [];
  645. list = self.data.processList;
  646. if (list.indexOf(desc) == -1) {
  647. list.push(desc);
  648. }
  649. if (self.data.value <= value) {
  650. self.setData({
  651. value: value,
  652. bluetoothConnectedStep: step
  653. });
  654. }
  655. self.setData({
  656. processList: list
  657. });
  658. //console.log(value);
  659. //console.log(desc);
  660. if (value == 100) {
  661. self.closeConnect();
  662. wx.closeBluetoothAdapter();
  663. self.setData({
  664. isBluetoothConnected: true,
  665. desc: util.descSucList[6]
  666. });
  667. clearInterval(timeId);
  668. sequenceControl = 0;
  669. self.commitUserData();
  670. }
  671. },
  672. setFailProcess: function (flag, desc) {
  673. var self = this,
  674. list = [];
  675. list = self.data.processList;
  676. list.push(desc);
  677. self.setData({
  678. failure: flag,
  679. processList: list
  680. });
  681. },
  682. closeConnect: function () {
  683. var self = this;
  684. wx.closeBLEConnection({
  685. deviceId: self.data.deviceId,
  686. success: function (res) { }
  687. })
  688. },
  689. /**
  690. * 生命周期函数--监听页面加载
  691. */
  692. onLoad(options) {
  693. wx.authorize({
  694. scope: 'scope.bluetooth',
  695. success() {
  696. // 用户同意授权后再次获取位置
  697. },
  698. fail() {
  699. // 用户拒绝授权
  700. }
  701. });
  702. this.getWifiInfo();
  703. if (options.type && options.type == "edit") {
  704. wx.setNavigationBarTitle({
  705. title: "编辑床垫"
  706. })
  707. const item = wx.getStorageSync("item");
  708. if (item) {
  709. this.setData({
  710. isEdit: true,
  711. pid: item.pid,
  712. placeOfUse: item.location,
  713. usingHuman: item.person,
  714. deviceId: item.sn,
  715. sn: item.sn,
  716. ssid: "",
  717. password: "",
  718. })
  719. wx.removeStorageSync("item")
  720. }
  721. }
  722. },
  723. // 实验室
  724. // 测试
  725. // 332016850591
  726. // sleep
  727. // sleep1234
  728. checkSn(sntram) {
  729. const that = this;
  730. wx.request({
  731. url: 'https://aipush.aidsleep.cn/wxchecksn',
  732. method: 'POST',
  733. data: {
  734. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  735. sntram: sntram,
  736. },
  737. success: (res) => {
  738. var incustom = res.data.incustom;
  739. if (incustom == "true") {
  740. that.bindViewConnect();
  741. } else {
  742. wx.showToast({
  743. icon: 'none',
  744. title: "sn 不合法"
  745. });
  746. }
  747. },
  748. fail: () => {
  749. wx.showToast({
  750. icon: 'none',
  751. title: "sn 不合法"
  752. });
  753. }
  754. });
  755. },
  756. /**
  757. * 生命周期函数--监听页面初次渲染完成
  758. */
  759. onReady() {
  760. },
  761. /**
  762. * 生命周期函数--监听页面显示
  763. */
  764. onShow() {
  765. },
  766. /**
  767. * 生命周期函数--监听页面隐藏
  768. */
  769. onHide() {
  770. },
  771. /**
  772. * 生命周期函数--监听页面卸载
  773. */
  774. onUnload() {
  775. wx.offWifiConnected({});
  776. wx.stopWifi({})
  777. wx.closeBluetoothAdapter();
  778. wx.offBluetoothDeviceFound();
  779. wx.removeStorageSync('doDevices')
  780. wx.removeStorageSync('deviceList')
  781. clearInterval(timeId);
  782. this.closeConnect()
  783. },
  784. /**
  785. * 页面相关事件处理函数--监听用户下拉动作
  786. */
  787. onPullDownRefresh() {
  788. },
  789. /**
  790. * 页面上拉触底事件的处理函数
  791. */
  792. onReachBottom() {
  793. },
  794. /**
  795. * 用户点击右上角分享
  796. */
  797. onShareAppMessage() {
  798. }
  799. })