addMattresses.js 24 KB

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