addMattresses.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840
  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. wx.showModal({
  502. title: '提示',
  503. content: '配网失败,请检查wifi密码或设备是否连接正常',
  504. showCancel: false
  505. });
  506. self.setData({
  507. showBluetoothConnected: false
  508. });
  509. // self.setFailProcess(true, util.descFailList[4]);
  510. }
  511. }, 1000)
  512. },
  513. //启用通知
  514. openNotify: function (deviceId, serviceId, characteristicId) {
  515. //console.log("=========== openNotify ============");
  516. var self = this;
  517. wx.notifyBLECharacteristicValueChange({
  518. state: true, // 启用 notify 功能
  519. deviceId: deviceId,
  520. serviceId: serviceId,
  521. characteristicId: app.data.characteristic_read_uuid,
  522. success: function (res) {
  523. //console.log(res);
  524. self.writeDevice(deviceId, serviceId, characteristicId, null);
  525. self.onNotify();
  526. },
  527. fail: function (res) {
  528. console.log(res);
  529. }
  530. })
  531. },
  532. //监听通知
  533. onNotify: function () {
  534. //console.log("=========== onNotify ============");
  535. var self = this;
  536. wx.onBLECharacteristicValueChange(function (res) {
  537. //console.log(res);
  538. self.getResultType(util.ab2hex(res.value));
  539. })
  540. },
  541. getCharCodeat: function (str) {
  542. let utf8Bytes = [];
  543. for (let i = 0; i < str.length; i++) {
  544. let char = str.charCodeAt(i);
  545. if (char < 0x80) {
  546. utf8Bytes.push(char);
  547. } else if (char < 0x800) {
  548. utf8Bytes.push(0xc0 | (char >> 6));
  549. utf8Bytes.push(0x80 | (char & 0x3f));
  550. } else if (char < 0x10000) {
  551. utf8Bytes.push(0xe0 | (char >> 12));
  552. utf8Bytes.push(0x80 | ((char >> 6) & 0x3f));
  553. utf8Bytes.push(0x80 | (char & 0x3f));
  554. }
  555. }
  556. return utf8Bytes;
  557. // var list = [];
  558. // for (var i = 0; i < str.length; i++) {
  559. //   list.push(str.charCodeAt(i));
  560. // }
  561. // return list;
  562. },
  563. // 设备数据开始写入
  564. writeDevice: function (deviceId, serviceId, characteristicId, data) {
  565. //console.log("===== writeDevice =====");
  566. var self = this,
  567. obj = {};
  568. self.setProcess(60, util.descSucList[4], 3);
  569. sequenceControl = parseInt(sequenceControl) + 1;
  570. if (!util._isEmpty(data)) {
  571. obj = util.isSubcontractor(data, true, sequenceControl);
  572. } else {
  573. data = util.writeData(self.getCharCodeat('"' + self.data.ssid + '","' + self.data.password + '"'));
  574. obj = util.isSubcontractor(data, self.data.isChecksum, sequenceControl);
  575. }
  576. var typedArray = new Uint8Array(obj.lenData)
  577. wx.writeBLECharacteristicValue({
  578. deviceId: deviceId,
  579. serviceId: serviceId,
  580. characteristicId: characteristicId,
  581. value: typedArray.buffer,
  582. success: function (res) {
  583. //console.log(res);
  584. if (obj.flag) {
  585. self.writeDevice(deviceId, serviceId, characteristicId, obj.laveData);
  586. } else {
  587. self.setProcess(80, util.descSucList[5], 4);
  588. }
  589. },
  590. fail: function (res) {
  591. console.log(res);
  592. self.setFailProcess(true, res.errCode + "::" + util.descFailList[3]);
  593. }
  594. })
  595. },
  596. getResultType: function (list) {
  597. console.log(' ========= getResultType ========= ');
  598. var self = this;
  599. console.log(list);
  600. var deviceList = wx.getStorageSync('deviceList');
  601. console.log(deviceList);
  602. if (list.length < 4) {
  603. self.setFailProcess(true, util.descFailList[4]);
  604. return false;
  605. }
  606. var val = list[4];
  607. if (val == "00") {
  608. var idx = self.data.deviceNo + 1;
  609. var doDevices = wx.getStorageSync('doDevices');
  610. if (doDevices) {
  611. if (doDevices.indexOf(self.data.deviceId) < 0) {
  612. doDevices.push(self.data.deviceId);
  613. }
  614. } else {
  615. doDevices = [self.data.deviceId];
  616. }
  617. wx.setStorageSync('doDevices', doDevices);
  618. var deviceSn = self.data.deviceId;
  619. for (var i = 0; i < deviceList.length; i++) {
  620. var itemSub = deviceList[i];
  621. if (itemSub.deviceId === self.data.deviceId) {
  622. deviceSn = itemSub.sn;
  623. break;
  624. }
  625. }
  626. if (idx < self.data.deviceIds.length) {
  627. self.setProcess(90, 'SN:' + deviceSn + ' 连接成功', 5);
  628. wx.closeBLEConnection({
  629. deviceId: self.data.deviceId,
  630. success: function (res) { }
  631. })
  632. self.setData({
  633. deviceNo: idx,
  634. deviceId: self.data.deviceIds[idx],
  635. blueConnectNum: 0
  636. });
  637. self.blueConnect();
  638. } else {
  639. self.setProcess(90, 'SN:' + deviceSn + ' 连接成功', 5);
  640. self.setProcess(100, util.descSucList[6], 7);
  641. self.setData({
  642. result: [],
  643. })
  644. }
  645. } else {
  646. self.setFailProcess(true, util.descFailList[4])
  647. }
  648. },
  649. setProcess: function (value, desc, step = 0) {
  650. var self = this,
  651. list = [];
  652. list = self.data.processList;
  653. if (list.indexOf(desc) == -1) {
  654. list.push(desc);
  655. }
  656. if (self.data.value <= value) {
  657. self.setData({
  658. value: value,
  659. bluetoothConnectedStep: step
  660. });
  661. }
  662. self.setData({
  663. processList: list
  664. });
  665. //console.log(value);
  666. //console.log(desc);
  667. if (value == 100) {
  668. self.closeConnect();
  669. wx.closeBluetoothAdapter();
  670. self.setData({
  671. isBluetoothConnected: true,
  672. desc: util.descSucList[6]
  673. });
  674. clearInterval(timeId);
  675. sequenceControl = 0;
  676. self.commitUserData();
  677. }
  678. },
  679. setFailProcess: function (flag, desc) {
  680. var self = this,
  681. list = [];
  682. list = self.data.processList;
  683. list.push(desc);
  684. self.setData({
  685. failure: flag,
  686. processList: list
  687. });
  688. },
  689. closeConnect: function () {
  690. var self = this;
  691. wx.closeBLEConnection({
  692. deviceId: self.data.deviceId,
  693. success: function (res) { }
  694. })
  695. },
  696. /**
  697. * 生命周期函数--监听页面加载
  698. */
  699. onLoad(options) {
  700. wx.authorize({
  701. scope: 'scope.bluetooth',
  702. success() {
  703. // 用户同意授权后再次获取位置
  704. },
  705. fail() {
  706. // 用户拒绝授权
  707. }
  708. });
  709. this.getWifiInfo();
  710. if (options.type && options.type == "edit") {
  711. wx.setNavigationBarTitle({
  712. title: "编辑床垫"
  713. })
  714. const item = wx.getStorageSync("item");
  715. if (item) {
  716. this.setData({
  717. isEdit: true,
  718. pid: item.pid,
  719. placeOfUse: item.location,
  720. usingHuman: item.person,
  721. deviceId: item.sn,
  722. sn: item.sn,
  723. ssid: "",
  724. password: "",
  725. })
  726. wx.removeStorageSync("item")
  727. }
  728. }
  729. },
  730. // 实验室
  731. // 测试
  732. // 332016850591
  733. // sleep
  734. // sleep1234
  735. checkSn(sntram) {
  736. const that = this;
  737. wx.request({
  738. url: 'https://aipush.aidsleep.cn/wxchecksn',
  739. method: 'POST',
  740. data: {
  741. token: "89835e65993fee4a6a6cbbe4c271da51e5521822934e13769e61cadabaed72c3" || wx.getStorageSync('token'),
  742. sntram: sntram,
  743. },
  744. success: (res) => {
  745. var incustom = res.data.incustom;
  746. if (incustom == "true") {
  747. that.bindViewConnect();
  748. } else {
  749. wx.showToast({
  750. icon: 'none',
  751. title: "sn 不合法"
  752. });
  753. }
  754. },
  755. fail: () => {
  756. wx.showToast({
  757. icon: 'none',
  758. title: "sn 不合法"
  759. });
  760. }
  761. });
  762. },
  763. /**
  764. * 生命周期函数--监听页面初次渲染完成
  765. */
  766. onReady() {
  767. },
  768. /**
  769. * 生命周期函数--监听页面显示
  770. */
  771. onShow() {
  772. },
  773. /**
  774. * 生命周期函数--监听页面隐藏
  775. */
  776. onHide() {
  777. },
  778. /**
  779. * 生命周期函数--监听页面卸载
  780. */
  781. onUnload() {
  782. wx.offWifiConnected({});
  783. wx.stopWifi({})
  784. wx.closeBluetoothAdapter();
  785. wx.offBluetoothDeviceFound();
  786. wx.removeStorageSync('doDevices')
  787. wx.removeStorageSync('deviceList')
  788. clearInterval(timeId);
  789. this.closeConnect()
  790. },
  791. /**
  792. * 页面相关事件处理函数--监听用户下拉动作
  793. */
  794. onPullDownRefresh() {
  795. },
  796. /**
  797. * 页面上拉触底事件的处理函数
  798. */
  799. onReachBottom() {
  800. },
  801. /**
  802. * 用户点击右上角分享
  803. */
  804. onShareAppMessage() {
  805. }
  806. })