addMattresses.js 23 KB

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