addMattresses.js 24 KB

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