You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

2970 lines
85 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
  1. // pages/roomDetail/roomDetail.js
  2. const config = require('../../config')
  3. const util1 = require('../../utils/util1.js')
  4. const plugin = requirePlugin("myPlugin")
  5. const WXAPI = require('../../utils/request')
  6. import ICINLocker from "../../utils/ICINPakage/ICINLocker"
  7. var util = require('../../utils/util');
  8. import md5 from "../../utils/ICINPakage/md5.min";
  9. import IotTask from "../../utils/IotTask"
  10. import LwJ from "../../utils/LWJ"
  11. import {
  12. BLE
  13. } from "../../utils/btls/ble";
  14. // import { clearInterval } from "timers";
  15. const app = getApp();
  16. var bleall = require('../../utils/bleall');
  17. const emitter = app.globalData.emitter
  18. import moment from "../../utils/moment"
  19. // ArrayBuffer转16进度字符串示例
  20. function ab2hex(buffer) {
  21. var hexArr = Array.prototype.map.call(
  22. new Uint8Array(buffer),
  23. function (bit) {
  24. return ('00' + bit.toString(16)).slice(-2)
  25. }
  26. )
  27. return hexArr.join(',');
  28. }
  29. function CurentTime() {
  30. var now = new Date();
  31. var year = now.getFullYear(); //年
  32. var month = now.getMonth() + 1; //月
  33. var day = now.getDate(); //日
  34. var hh = now.getHours(); //时
  35. var mm = now.getMinutes(); //分
  36. var clock = year + "-";
  37. if (month < 10)
  38. clock += "0";
  39. clock += month + "-";
  40. if (day < 10)
  41. clock += "0";
  42. clock += day + " ";
  43. // if (hh < 10)
  44. // clock += "0";
  45. // clock += '00' + ":";
  46. // // if (mm < 10) clock += '0';
  47. clock += '00:00:00';
  48. return (clock);
  49. }
  50. function CurentTime1() {
  51. var now = new Date();
  52. var year = now.getFullYear(); //年
  53. var month = now.getMonth() + 1; //月
  54. var day = now.getDate(); //日
  55. var hh = now.getHours(); //时
  56. var mm = now.getMinutes(); //分
  57. var clock = year + "-";
  58. if (month < 10)
  59. clock += "0";
  60. clock += month + "-";
  61. if (day < 10)
  62. clock += "0";
  63. clock += day + " ";
  64. // if (hh < 10)
  65. // clock += "0";
  66. // clock += hh + ":";
  67. // if (mm < 10) clock += '0';
  68. // clock += mm;
  69. clock += '00:00:00';
  70. return (clock);
  71. }
  72. var isConnected = false
  73. var connectedDeviceId = ''
  74. var isSearch = false
  75. var isInitReady = false
  76. var basecode = 29283991
  77. var lockDevice = {
  78. name: ''
  79. }
  80. var taskId = 0
  81. var lockModel = ''
  82. var lockMac = ''
  83. var isLockLogin = false
  84. var pincode = ''
  85. var pincodeIndex
  86. Page({
  87. /**
  88. * 页面的初始数据
  89. */
  90. data: {
  91. show2: false, // 配网
  92. wifiName: '',
  93. wifiPwd: '',
  94. toSearch: true,
  95. info: "", //显示框
  96. deviceinfo: {
  97. rssi: 0,
  98. battery: 0,
  99. lockStatus: 0,
  100. isOnline: 0,
  101. event: '',
  102. bleStatus: ''
  103. },
  104. index: 0,
  105. fingerprintId: '',
  106. openlock: '',
  107. IssuingPersonnel: true,
  108. isMuted: false,
  109. backstatus: false,
  110. cgInfo: '',
  111. zgBlueStatus: false,
  112. timer: '',
  113. count: 0,
  114. show: false,
  115. actions: [{
  116. name: '静音',
  117. color: '#ee0a24'
  118. },
  119. {
  120. name: '低音',
  121. color: '#333'
  122. },
  123. {
  124. name: '高音',
  125. color: '#333'
  126. },
  127. ],
  128. actionsLwj: [{
  129. name: '设置静音',
  130. color: '#ee0a24'
  131. },
  132. {
  133. name: '退出静音',
  134. color: '#333'
  135. }
  136. ],
  137. volume: '',
  138. bluestatus: app.globalData.backstatus,
  139. show1: false,
  140. TemporaryPassword: '',
  141. phone: '',
  142. userInfo: {
  143. type: ''
  144. },
  145. authority: false,
  146. NormallyOpenPermission: false,
  147. RemoteControlPermission: false,
  148. showbuleKey: false,
  149. deviceList: [],
  150. openShow: false,
  151. starttime: CurentTime(),
  152. endtime: CurentTime1(),
  153. datetimerange: [CurentTime(), CurentTime1()],
  154. startHour: 0,
  155. endHour: 23,
  156. array: ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24'],
  157. hexString: '',
  158. lockDevice: {},
  159. isSearch: false,
  160. genOfflinePincodeShow: false
  161. },
  162. /**
  163. * 生命周期函数--监听页面加载
  164. */
  165. onLoad: function (options) {
  166. app.globalData.zgBlueStatus = false
  167. this.AddressPermissions()
  168. this.getLocationPermissionMethods()
  169. let deviceinfo = wx.getStorageSync('deviceinfo')
  170. app.globalData.deviceNum = deviceinfo.deviceNum
  171. var userInfo1 = wx.getStorageSync('userInfo')
  172. let deviceinfoData = JSON.parse(JSON.stringify(deviceinfo))
  173. if (deviceinfo.manufactureId == 201 || deviceinfo.manufactureId == 221) {
  174. var data3 = {
  175. code: "zg/bleRegisterInfo",
  176. data: {
  177. lockerId: deviceinfo.deviceNum
  178. }
  179. }
  180. WXAPI.sendCommand(data3).then(res1 => {
  181. if (res1.success) {
  182. wx.setStorageSync('cgInfo', res1.data)
  183. this.setData({
  184. cgInfo: res1.data
  185. })
  186. }
  187. })
  188. }
  189. if (deviceinfo.roomId) {
  190. if (parseInt(userInfo1.type) < 3) {
  191. var data2 = {
  192. q: JSON.stringify({
  193. id: deviceinfo.roomId
  194. }),
  195. orderBy: "createTime",
  196. orderType: 'desc'
  197. }
  198. WXAPI.getSiteTenant(data2).then(res1 => {
  199. if (res1.data.list.length > 0) {
  200. var array = res1.data.list.reverse()
  201. wx.setStorageSync('info', array[0])
  202. deviceinfo['info'] = array[0].info
  203. this.setData({
  204. deviceinfo: deviceinfo
  205. })
  206. } else {
  207. wx.showToast({
  208. title: "还未下发人员,请先下发人员",
  209. duration: 2000,
  210. icon: 'none'
  211. })
  212. }
  213. })
  214. } else if (parseInt(userInfo1.type) == 3) {
  215. var data = {
  216. q: JSON.stringify({
  217. number: userInfo1.username
  218. }),
  219. pageNum: 0,
  220. pageSize: 0
  221. };
  222. WXAPI.getlistNum(data).then(res4 => {
  223. if (res4.success) {
  224. deviceinfo.info = res4.data.list[0].info
  225. this.setData({
  226. deviceinfo: deviceinfo
  227. })
  228. } else {
  229. wx.showToast({
  230. title: "还未下发人员,请先下发人员",
  231. duration: 2000,
  232. icon: 'none'
  233. })
  234. }
  235. })
  236. }
  237. } else {
  238. wx.navigateTo({
  239. url: "../index/index"
  240. })
  241. }
  242. if (userInfo1.type == 0) {
  243. this.setData({
  244. authority: true,
  245. NormallyOpenPermission: true,
  246. RemoteControlPermission: true
  247. })
  248. } else {
  249. userInfo1.authorities.map(li => {
  250. if (li == 'BLUETOOTH_INPUT') {
  251. this.setData({
  252. authority: true
  253. })
  254. }
  255. if (li == 'NORMALLY_OPEN_PERMISSION') {
  256. this.setData({
  257. NormallyOpenPermission: true
  258. })
  259. }
  260. if (li == 'REMOTE_CONTROL_PERMISSION') {
  261. this.setData({
  262. RemoteControlPermission: true
  263. })
  264. }
  265. })
  266. }
  267. this.setData({
  268. info: app.globalData.bleStatus,
  269. deviceinfo: deviceinfo,
  270. userInfo: userInfo1
  271. })
  272. if (options.open) {
  273. let eData = {
  274. currentTarget: {
  275. dataset: {
  276. i: 0
  277. }
  278. }
  279. }
  280. this.btntest1(eData)
  281. }
  282. if (deviceinfo.manufactureId == 101) {
  283. app.watch(this.getcartcount, 'backdata')
  284. app.watch(this.fingerprintFun, 'fingerprintId')
  285. app.watch(this.openlockfun, 'openlock')
  286. }
  287. },
  288. // 配网
  289. changeshow2(e) {
  290. var event = e.currentTarget.dataset.i
  291. var that = this;
  292. this.setData({
  293. event
  294. })
  295. var deviceinfo = this.data.deviceinfo
  296. if (deviceinfo.manufactureId == '1241') {
  297. if (event == '3') {
  298. this.setData({
  299. show2: true
  300. })
  301. }
  302. }else{
  303. wx.showToast({
  304. title: '该类型锁不支持配网功能',
  305. duration: 2000,
  306. icon: 'none'
  307. })
  308. }
  309. },
  310. configurationWifi() {
  311. let event = {
  312. currentTarget: {
  313. dataset: {
  314. i: '3'
  315. }
  316. }
  317. }
  318. if(!this.data.isSearch){
  319. this.btntest1(event)
  320. }else{
  321. this.configurewifj()
  322. }
  323. },
  324. onConfigurewifi() {
  325. var that = this
  326. var bytes = plugin.configureWifi(lockDevice.name, lockMac, that.data.wifiName, that.data.wifiPwd)
  327. that.sendBytes(bytes)
  328. this.setData({
  329. show2: false
  330. })
  331. },
  332. // 前往图像管理
  333. goImageManagement() {
  334. wx.navigateTo({
  335. url: '/pagesA/facepeople/facepeople',
  336. })
  337. },
  338. genOfflinePincodeShowMethods() {
  339. // if(this.data.isSearch){
  340. this.setData({
  341. genOfflinePincodeShow: true
  342. })
  343. // }else{
  344. // this.searchDevicesMethods()
  345. // }
  346. },
  347. genOfflinePincode: function () {
  348. var that = this
  349. // if(this.data.isSearch){
  350. wx.showLoading({
  351. title: '生成离线密码中',
  352. })
  353. var t1 = Number(moment(this.data.starttime).format('x')) + (parseInt(this.data.startHour) + 1) * 60 * 60 * 1000
  354. var t2 = new Date(this.data.endtime).getTime() + ((parseInt(this.data.endHour) + 1) * 60 * 60 * 1000)
  355. var startTime = util1.getLocalTime(8) //new Date(t1)//util1.getLocalTime(8)
  356. let time = t2 - startTime.getTime()
  357. var endTime = new Date(startTime.getTime() + time)
  358. plugin.genOfflinePincode(lockDevice.name, lockMac, basecode, 0, startTime, endTime)
  359. .then(function (data) {
  360. console.debug('data', data)
  361. wx.hideLoading()
  362. if (data.code == 200) {
  363. that.setData({
  364. genOfflinePincodeShow: false
  365. })
  366. wx.showModal({
  367. title: '提示',
  368. content: `生成离线密码${data.data},有效期至${util1.formatTime(endTime)}`,
  369. showCancel: false
  370. })
  371. } else {
  372. wx.showModal({
  373. title: '提示',
  374. content: data.data,
  375. showCancel: false
  376. })
  377. }
  378. })
  379. // }else{
  380. // this.searchDevicesMethods()
  381. // }
  382. },
  383. // 蓝牙同步任务
  384. synchroPlatformTask() {
  385. wx.showLoading({
  386. title: '同步平台任务中',
  387. icon: 'none',
  388. mask: true
  389. })
  390. const iotInstance = new IotTask(this.data.cgInfo.lockerId, this.iotCallBack)
  391. iotInstance.run()
  392. },
  393. iotCallBack(sendData) {
  394. let _this = this
  395. if (!sendData) return
  396. return new Promise((resolve, reject) => {
  397. console.log(_this.data.cgInfo.commonKey)
  398. ICINLocker.nbIotTransmit(sendData, _this.data.cgInfo.commonKey, (callback) => {
  399. console.log('发送数据' + new Date(), sendData)
  400. console.log('同步任务回调结果' + new Date(), callback)
  401. if (callback) {
  402. resolve(callback.nbIotResp)
  403. } else {
  404. reject('同步任务fail')
  405. }
  406. })
  407. })
  408. },
  409. bindDateChange: function (e) {
  410. var str = e.detail.value + " 00:00:00"
  411. this.data.datetimerange[0] = str
  412. this.setData({
  413. starttime: str,
  414. minDate1: str
  415. })
  416. },
  417. bindDateChange1: function (e) {
  418. var str = e.detail.value + " 00:00:00"
  419. this.data.datetimerange[1] = str
  420. this.setData({
  421. endtime: str
  422. })
  423. },
  424. bindPickerChangestartHour(e) {
  425. this.setData({
  426. startHour: e.detail.value
  427. })
  428. },
  429. bindPickerChangeendHour(e) {
  430. this.setData({
  431. endHour: e.detail.value
  432. })
  433. },
  434. onCloseBlue() {
  435. this.setData({
  436. showbuleKey: false
  437. })
  438. },
  439. addUser() {
  440. var cgInfo = this.data.cgInfo
  441. ICINLocker.getSignKey(cgInfo.lockerId, cgInfo.superId, cgInfo.superId,
  442. (callback) => {
  443. if (callback.status == '00') {
  444. ICINLocker.getCommonKey(cgInfo.lockerId, cgInfo.superId, cgInfo.superId, callback.signKey, (callback1) => {
  445. if (callback1.status == '00') {
  446. ICINLocker.addUserToLock(cgInfo.lockerId, cgInfo.superId, "BLEKEY_8d82e5d527e1", "0o01weit9ti9ti", "00", callback.signKey, "", callback1.commonKey, "00000000", "FFFFFFFF", (callback) => {
  447. console.log(callback)
  448. })
  449. }
  450. })
  451. }
  452. })
  453. },
  454. stringToHex(str) {
  455. var val = "";
  456. for (var i = 0; i < str.length; i++) {
  457. if (val == "")
  458. val = str.charCodeAt(i).toString(16);
  459. else
  460. val += str.charCodeAt(i).toString(16);
  461. }
  462. return val;
  463. },
  464. linkBlue() {
  465. wx.navigateTo({
  466. url: '/pages/addBlueButton/addBlueButton',
  467. })
  468. },
  469. searchBlueButton(e) {
  470. var that = this
  471. var cgInfo = this.data.cgInfo
  472. var comm = this.stringToHex(e.currentTarget.dataset.select.name)
  473. console.log(comm)
  474. var deviceId = e.currentTarget.dataset.select.deviceId
  475. var keyId = "0o01weit9ti9ti"
  476. var password = "3030000000000000000000000000000000000000"
  477. app.globalData.blueDeviceId = e.currentTarget.dataset.select.name
  478. var blueKeyId = e.currentTarget.dataset.select.name
  479. ICINLocker.connect(deviceId, true) // 此处 deviceId 存起来,当断开连接时使用
  480. .then((res) => {
  481. if (res.status === 0) {
  482. clearInterval(that.data.timer)
  483. this.setData({
  484. bleStatus: '蓝牙已连接',
  485. deviceId: deviceId
  486. })
  487. app.globalData.zgBlueStatus = true
  488. wx.showToast({
  489. title: '连接成功',
  490. icon: "none",
  491. duration: 2000
  492. })
  493. ICINLocker.readBluetoothBuckle(cgInfo.lockerId, comm, callback2 => {
  494. if (callback2.status == '00') {
  495. ICINLocker.registeBluetoothBuckle(cgInfo.superId, blueKeyId, keyId, password, "", cgInfo.authKey, comm, callback3 => {
  496. if (callback3.status == '00') {
  497. wx.hideLoading()
  498. wx.showToast({
  499. title: '添加成功',
  500. duration: 2000,
  501. icon: "none"
  502. })
  503. } else {
  504. wx.hideLoading()
  505. wx.showToast({
  506. title: '添加成功',
  507. duration: 2000,
  508. icon: "none"
  509. })
  510. }
  511. })
  512. } else {
  513. wx.hideLoading()
  514. wx.showToast({
  515. title: '读取失败',
  516. duration: 2000,
  517. icon: "none"
  518. })
  519. }
  520. });
  521. } else {
  522. wx.hideLoading()
  523. var msg = ''
  524. switch (res.errCode) {
  525. case 10001:
  526. msg = '调用失败,请打开蓝牙!';
  527. break;
  528. }
  529. wx.showToast({
  530. title: msg,
  531. duration: 2000,
  532. icon: 'none'
  533. })
  534. //此处处理返回值不为成功(0)的情况
  535. }
  536. }).catch((res) => {
  537. wx.hideLoading()
  538. console.log(res, '失败')
  539. // 此处处理未能连接成功问题
  540. })
  541. },
  542. onChange(event) {
  543. this.setData({
  544. phone: event.detail
  545. })
  546. },
  547. onChangewifi(e) {
  548. this.setData({
  549. wifiName: e.detail
  550. })
  551. },
  552. onChangewifi1(e) {
  553. this.setData({
  554. wifiPwd: e.detail
  555. })
  556. },
  557. // 发送临时密码
  558. sendMessage() {
  559. var re = /^1\d{10}$/
  560. if (re.test(this.data.phone)) {
  561. var data = {
  562. code: this.data.TemporaryPassword,
  563. phone: this.data.phone,
  564. device: this.data.deviceinfo.deviceNum,
  565. name: this.data.deviceinfo.roomName
  566. }
  567. WXAPI.sendEmergence(data).then(res => {
  568. if (res.success) {
  569. this.setData({
  570. show1: false
  571. })
  572. wx.showToast({
  573. title: "发送成功",
  574. duration: 2000,
  575. icon: "none"
  576. })
  577. } else {
  578. wx.showToast({
  579. title: "发送失败",
  580. duration: 2000,
  581. icon: "none"
  582. })
  583. }
  584. })
  585. } else {
  586. wx.showToast({
  587. title: "请输入正确的手机号",
  588. duration: 2000,
  589. icon: "none"
  590. })
  591. }
  592. },
  593. onCloseSet() {
  594. this.setData({
  595. openShow: false,
  596. genOfflinePincodeShow: false
  597. })
  598. },
  599. // zh设置常开
  600. setAlawyOpen() {
  601. this.setData({
  602. openShow: true
  603. })
  604. },
  605. UnsetMethods() {
  606. if (this.data.deviceinfo.manufactureId == 201 || this.data.deviceinfo.manufactureId == 221) {
  607. var data = {
  608. code: "zg/longopen/operation",
  609. data: {
  610. opendDoorType: 0,
  611. roomName: this.data.deviceinfo.roomName,
  612. lockerUserId: this.data.cgInfo.superId,
  613. lockerId: this.data.cgInfo.lockerId,
  614. startTime: this.data.datetimerange[0],
  615. endTime: this.data.datetimerange[1],
  616. openDoorSegmentList: [{
  617. startHour: parseInt(this.data.startHour) + 1,
  618. endHour: parseInt(this.data.endHour) + 1
  619. }],
  620. order: 61101
  621. }
  622. }
  623. WXAPI.sendCommand(data).then((res2) => {
  624. if (res2.data.success) {
  625. wx.showToast({
  626. title: '设置成功',
  627. icon: 'none',
  628. duration: 2000
  629. })
  630. this.setData({
  631. openShow: false
  632. })
  633. } else {
  634. wx.showToast({
  635. title: res2.msg,
  636. icon: 'none',
  637. duration: 2000
  638. })
  639. this.setData({
  640. openShow: false
  641. })
  642. }
  643. });
  644. }
  645. },
  646. setAlawalyOpen() {
  647. if (this.data.deviceinfo.manufactureId == 201 || this.data.deviceinfo.manufactureId == 221) {
  648. if (this.data.datetimerange[0] == '') {
  649. wx.showToast({
  650. title: '请选择开始日期',
  651. icon: 'none',
  652. duration: 2000
  653. })
  654. return
  655. }
  656. if (this.data.datetimerange[1] == '') {
  657. wx.showToast({
  658. title: '请选择结束日期',
  659. icon: 'none',
  660. duration: 2000
  661. })
  662. return
  663. }
  664. if (this.data.startHour != '' && this.data.endHour == '') {
  665. wx.showToast({
  666. title: '请选择结束时间段',
  667. icon: 'none',
  668. duration: 2000
  669. })
  670. return
  671. }
  672. if (this.data.startHour === '' && this.data.endHour != '') {
  673. wx.showToast({
  674. title: '请选择开始时间段',
  675. icon: 'none',
  676. duration: 2000
  677. })
  678. return
  679. }
  680. var data = {
  681. code: "zg/longopen/operation",
  682. data: {
  683. opendDoorType: 1,
  684. roomName: this.data.deviceinfo.roomName,
  685. lockerUserId: this.data.cgInfo.superId,
  686. lockerId: this.data.cgInfo.lockerId,
  687. startTime: this.data.datetimerange[0],
  688. endTime: this.data.datetimerange[1],
  689. openDoorSegmentList: [{
  690. startHour: parseInt(this.data.startHour) + 1,
  691. endHour: parseInt(this.data.endHour) + 1
  692. }],
  693. order: 61101
  694. }
  695. }
  696. if (this.data.startHour == '' && this.data.endHour == '') {
  697. data.data.openDoorSegmentList = []
  698. }
  699. WXAPI.sendCommand(data).then((res2) => {
  700. if (res2.data.success) {
  701. wx.showToast({
  702. title: '设置成功',
  703. icon: 'none',
  704. duration: 2000
  705. })
  706. this.setData({
  707. openShow: false
  708. })
  709. } else {
  710. wx.showToast({
  711. title: res2.msg,
  712. icon: 'none',
  713. duration: 2000
  714. })
  715. this.setData({
  716. openShow: false
  717. })
  718. }
  719. });
  720. } else {
  721. this.$message.error('该类型锁不支持此设置!');
  722. }
  723. },
  724. // 获取临时密码
  725. getTemporaryPassword() {
  726. var data = {
  727. code: "zg/getEmergence",
  728. data: {
  729. lockerId: this.data.deviceinfo.deviceNum
  730. }
  731. }
  732. WXAPI.sendCommand(data).then(res => {
  733. if (res.success) {
  734. this.setData({
  735. TemporaryPassword: res.data.data,
  736. show1: true
  737. })
  738. } else {
  739. wx.showToast({
  740. title: "获取失败",
  741. duration: 2000,
  742. icon: "none"
  743. })
  744. }
  745. })
  746. },
  747. // 关闭临时密码弹窗
  748. onClose1() {
  749. this.setData({
  750. show1: false
  751. });
  752. },
  753. // 关闭设置音量
  754. onClose() {
  755. this.setData({
  756. show: false
  757. });
  758. },
  759. onClose2() {
  760. this.setData({
  761. show2: false
  762. })
  763. },
  764. // 选择设置音量
  765. onSelect(event) {
  766. var volume = '',
  767. volume1 = '',
  768. isMuted = true
  769. if (event.detail.name == '静音' || event.detail.name == '设置静音') {
  770. volume = '00'
  771. volume1 = 0
  772. isMuted = false
  773. } else if (event.detail.name == '低音' || event.detail.name == '退出静音') {
  774. volume = '01'
  775. volume1 = 1
  776. isMuted = true
  777. } else if (event.detail.name == '高音') {
  778. volume = '02'
  779. volume1 = 2
  780. }
  781. this.setData({
  782. volume: volume
  783. })
  784. if (this.data.deviceinfo.manufactureId == 201 || this.data.deviceinfo.manufactureId == 221) {
  785. wx.showLoading({
  786. title: "正在设置...",
  787. mask: true
  788. });
  789. this.setVolume(volume)
  790. } else if (this.data.deviceinfo.manufactureId == 101) {
  791. wx.showLoading({
  792. title: "正在设置...",
  793. mask: true
  794. });
  795. this.setVolume1(volume)
  796. } else if (this.data.deviceinfo.manufactureId == 1201 || this.data.deviceinfo.manufactureId == 1241) {
  797. this.setData({
  798. isMuted: isMuted
  799. })
  800. this.changeLockMutedState()
  801. }
  802. },
  803. changeLockMutedState: function () {
  804. var that = this
  805. taskId = 102
  806. that.initBluetooth()
  807. },
  808. onChangeLockMutedState: function () {
  809. var that = this
  810. wx.showLoading({
  811. title: that.data.isMuted ? '退出静音模式' : '进入静音模式',
  812. })
  813. var bytes = plugin.setLockMuteState(lockDevice.name, !that.data.isMuted)
  814. console.debug('bytes', bytes)
  815. that.sendBytes(bytes)
  816. },
  817. fingerprintFun() {
  818. this.data.fingerprintId = app.globalData.fingerprintId
  819. },
  820. // 人员管理
  821. personManger(e) {
  822. if(this.data.isSearch){
  823. wx.navigateTo({
  824. url: '/pages/personManger/personManger',
  825. })
  826. }else{
  827. this.btntest1(e)
  828. }
  829. },
  830. gotoperson() {
  831. wx.navigateTo({
  832. url: '/pages/personManger/personManger',
  833. })
  834. },
  835. openlockfun() {
  836. var that = this
  837. this.data.openlock = app.globalData.openlock
  838. var userInfo = wx.getStorageSync('userInfo')
  839. if (app.globalData.openlock == 1) {
  840. var rqTime = moment().format("X")
  841. var username = wx.getStorageSync('username'),
  842. password = wx.getStorageSync('password')
  843. var str = `${username}+${password}+${rqTime}`
  844. var signature = md5(str)
  845. var data = {
  846. code: "ld/bleUnlockRecord",
  847. data: {
  848. deviceNum: this.data.deviceinfo.deviceNum,
  849. userTelephone: "18270949468",
  850. password: "2021",
  851. roomId: that.data.deviceinfo.roomId,
  852. roomName: that.data.deviceinfo.roomName,
  853. cardName: userInfo.username,
  854. signature: signature
  855. }
  856. }
  857. console.log(data)
  858. WXAPI.sendCommand(data).then(res => {
  859. })
  860. } else {
  861. wx.showToast({
  862. title: "开门失败,请重新开门",
  863. duration: 2000,
  864. icon: "none"
  865. })
  866. }
  867. },
  868. getcartcount(val) {
  869. var that = this
  870. let deviceNum = that.data.deviceinfo.deviceNum
  871. if (app.globalData.backdata == 3 && this.data.backstatus) {
  872. that.startFp_29(deviceNum, 0x02)
  873. } else if (app.globalData.backdata == 2 && this.data.backstatus) {
  874. that.startFp_29(deviceNum, 0x01)
  875. } else if (app.globalData.backdata == 1 && this.data.backstatus) {
  876. var data = {
  877. managerTelephone: "18270949468",
  878. managerPassword: "202108",
  879. userTelephone: JSON.parse(that.data.deviceinfo.info).userTelephone,
  880. userPassword: JSON.parse(that.data.deviceinfo.info).userPassword,
  881. startTime: "20210813010101",
  882. endTime: "20680813010101"
  883. }
  884. this.data.backstatus = false
  885. that.startFp_63(data.managerTelephone, data.managerPassword, data.userTelephone, data.userPassword,
  886. data.startTime, data.endTime, 16)
  887. }
  888. },
  889. startFp_63(managerTelephone, managerPassword, userTelephone, userPassword, startTime, endTime, num) {
  890. wx.showLoading({
  891. title: "正在发送...",
  892. mask: true
  893. });
  894. setTimeout(() => {
  895. wx.hideLoading()
  896. }, 1000);
  897. let cmd = 0x63
  898. let mt = util.stringToUint8Array(managerTelephone)
  899. let mp = util.stringToUint8Array(managerPassword)
  900. let ut = util.stringToUint8Array(userTelephone)
  901. let up = util.stringToUint8Array(userPassword)
  902. let ust = util.time40ToBytes(startTime)
  903. let uet = util.time40ToBytes(endTime)
  904. let fst = util.time40ToBytes(startTime)
  905. let fet = util.time40ToBytes(endTime)
  906. let length = 1 + 11 + 6 + 11 + 6 + 5 + 5 + 4 + 2 + 10 + 5 + 5 + 1 + 2 + 1
  907. let b = new Uint8Array(length)
  908. b[0] = cmd
  909. b.set(mt, 1)
  910. b.set(mp, 12)
  911. b.set(ut, 18)
  912. b.set(up, 29)
  913. b.set(ust, 35)
  914. b.set(uet, 40)
  915. //权限
  916. b[45] = 0x46;
  917. b[46] = 0x46;
  918. b[47] = 0x46;
  919. b[48] = 0x46;
  920. //密码长度 uint16
  921. b[49] = 0
  922. b[50] = 0x0a
  923. //密码数据
  924. b[51] = this.data.fingerprintId
  925. b[52] = 0xff
  926. b[53] = 0xff
  927. b[54] = 0xff
  928. b[55] = 0xff
  929. b[56] = 0xff
  930. b[57] = 0xff
  931. b[58] = 0xff
  932. b[59] = 0xff
  933. b[60] = 0xff
  934. b.set(fst, 61)
  935. b.set(fet, 66)
  936. b[71] = 0xff;
  937. b[72] = 0xff;
  938. b[73] = 0xff;
  939. b[74] = num; //添加指纹
  940. app.globalData.ble.send(cmd, b)
  941. },
  942. delete_63(managerTelephone, managerPassword, userTelephone, userPassword, startTime, endTime, id) {
  943. wx.showLoading({
  944. title: "正在发送...",
  945. mask: true
  946. });
  947. setTimeout(() => {
  948. wx.hideLoading()
  949. }, 1000);
  950. let cmd = 0x63
  951. let mt = util.stringToUint8Array(managerTelephone)
  952. let mp = util.stringToUint8Array(managerPassword)
  953. let ut = util.stringToUint8Array(userTelephone)
  954. let up = util.stringToUint8Array(userPassword)
  955. let ust = util.time40ToBytes(startTime)
  956. let uet = util.time40ToBytes(endTime)
  957. let fst = util.time40ToBytes(startTime)
  958. let fet = util.time40ToBytes(endTime)
  959. let length = 1 + 11 + 6 + 11 + 6 + 5 + 5 + 4 + 2 + 10 + 5 + 5 + 1 + 2 + 1
  960. let b = new Uint8Array(length)
  961. b[0] = cmd
  962. b.set(mt, 1)
  963. b.set(mp, 12)
  964. b.set(ut, 18)
  965. b.set(up, 29)
  966. b.set(ust, 35)
  967. b.set(uet, 40)
  968. //权限
  969. b[45] = 0x46;
  970. b[46] = 0x46;
  971. b[47] = 0x46;
  972. b[48] = 0x46;
  973. //密码长度 uint16
  974. b[49] = 0
  975. b[50] = 0x0a
  976. //密码数据
  977. b[51] = id
  978. b[52] = 0xff
  979. b[53] = 0xff
  980. b[54] = 0xff
  981. b[55] = 0xff
  982. b[56] = 0xff
  983. b[57] = 0xff
  984. b[58] = 0xff
  985. b[59] = 0xff
  986. b[60] = 0xff
  987. b.set(fst, 61)
  988. b.set(fet, 66)
  989. b[71] = 0xff;
  990. b[72] = 0xff;
  991. b[73] = 0xff;
  992. b[74] = 18; //删除指纹
  993. app.globalData.ble.send(cmd, b)
  994. },
  995. startFp_29(deviceNum, status) {
  996. wx.showLoading({
  997. title: "正在发送...",
  998. mask: true
  999. });
  1000. setTimeout(() => {
  1001. wx.hideLoading()
  1002. }, 1000);
  1003. let cmd = 0x29
  1004. let dn = util.stringToUint8ArrayWithPadding(deviceNum, 20, 0xff)
  1005. let length = 1 + 20 + 5 + 1 + 1
  1006. let b = new Uint8Array(length)
  1007. b[0] = cmd
  1008. b.set(dn, 1)
  1009. //send data=0x0036000008
  1010. b[21] = 0x00;
  1011. b[22] = 0x36;
  1012. b[23] = 0;
  1013. b[24] = 0;
  1014. b[25] = 0x08
  1015. b[26] = status //status: 0x03, 0x02, 0x01
  1016. b[27] = 0x01
  1017. app.globalData.ble.send(cmd, b)
  1018. },
  1019. // 设置密码
  1020. permissionsfun() {
  1021. wx.navigateTo({
  1022. url: "/pages/permissions/permissions"
  1023. })
  1024. },
  1025. // 跳转开锁记录页面
  1026. getlogList() {
  1027. wx.navigateTo({
  1028. url: "/pages/openLockLog/openLockLog?&roomId=" + this.data.deviceinfo.roomId
  1029. })
  1030. },
  1031. // zg蓝牙连接
  1032. openTheDoor() {
  1033. // if(this.data.deviceinfo.info){
  1034. // JSON.parse(this.data.deviceinfo.info)
  1035. wx.showLoading({
  1036. title: '连接中',
  1037. mask: true,
  1038. icon: 'none'
  1039. })
  1040. var userInfo = wx.getStorageSync('userInfo')
  1041. console.log(this.data.cgInfo.lockerId, this.data.cgInfo.superId, this.data.cgInfo.superId)
  1042. ICINLocker.getSignKey(this.data.cgInfo.lockerId, this.data.cgInfo.superId, this.data.cgInfo.superId, (callback) => {
  1043. console.log(callback, '管理员获取密钥')
  1044. if (callback.status == '00') {
  1045. var userSignKey = callback.signKey ? callback.signKey : callback.commonKey || ''
  1046. ICINLocker.getCommonKey(this.data.cgInfo.lockerId, this.data.cgInfo.superId, this.data.cgInfo.superId, userSignKey, (callback1) => {
  1047. console.log(callback1, '管理员获取通信')
  1048. if (callback1.status == '00') {
  1049. var commonKey = callback1.commonKey
  1050. ICINLocker.openTheDoor(this.data.cgInfo.lockerId, this.data.cgInfo.superId, userSignKey, this.data.cgInfo.superId, '', '00', commonKey, (callback2) => {
  1051. if (callback2.status == '00') {
  1052. var rqTime = moment().format("X")
  1053. var username = wx.getStorageSync('username'),
  1054. password = wx.getStorageSync('password')
  1055. var str = `${username}+${password}+${rqTime}`
  1056. var signature = md5(str)
  1057. var data = {
  1058. code: "zg/bleUnlockRecord",
  1059. data: {
  1060. lockerId: this.data.cgInfo.lockerId,
  1061. lockerUserId: this.data.cgInfo.superId,
  1062. roomId: this.data.deviceinfo.roomId,
  1063. roomName: this.data.deviceinfo.roomName,
  1064. cardName: userInfo.username,
  1065. signature: signature
  1066. }
  1067. }
  1068. console.log(data, '蓝牙开锁')
  1069. wx.hideLoading()
  1070. wx.showToast({
  1071. title: '开锁成功',
  1072. duration: 2000,
  1073. icon: 'none'
  1074. })
  1075. WXAPI.sendCommand(data).then(res => {
  1076. console.log(res)
  1077. })
  1078. } else {
  1079. wx.hideLoading()
  1080. wx.showToast({
  1081. title: '开锁失败',
  1082. duration: 2000,
  1083. icon: 'none'
  1084. })
  1085. }
  1086. })
  1087. } else {
  1088. wx.hideLoading()
  1089. wx.showToast({
  1090. title: '连接失败',
  1091. duration: 2000,
  1092. icon: 'none'
  1093. })
  1094. }
  1095. })
  1096. } else {
  1097. wx.hideLoading()
  1098. }
  1099. });
  1100. // }else{
  1101. // wx.showToast({
  1102. // title: '请先下发人员',
  1103. // duration:2000,
  1104. // icon:'none'
  1105. // })
  1106. // }
  1107. },
  1108. // 添加指纹
  1109. addFingerPrints() {
  1110. var userInfo = JSON.parse(this.data.deviceinfo.info)
  1111. ICINLocker.getCommonKey(this.data.cgInfo.lockerId, this.data.cgInfo.superId, this.data.cgInfo.superId, this.data.cgInfo.authKey, (callback) => {
  1112. ICINLocker.addFingerPrints(this.data.cgInfo.lockerId, this.data.cgInfo.superId, userInfo.userPassword, this.data.cgInfo.superId, '', this.data.cgInfo.authKey, '00000000', 'FFFFFFFF', '00', callback.commonKey, (callback) => {
  1113. console.log(callback)
  1114. })
  1115. })
  1116. },
  1117. // 蓝牙初始化
  1118. btntest1(e) {
  1119. wx.showLoading({
  1120. title: '正在搜索',
  1121. mask: true,
  1122. icon: 'none'
  1123. })
  1124. var event = e.currentTarget.dataset.i
  1125. var that = this;
  1126. this.data.event = event
  1127. var deviceinfo = this.data.deviceinfo
  1128. if (deviceinfo.manufactureId == 201 || deviceinfo.manufactureId == 221) {
  1129. if (app.globalData.zgBlueStatus) {
  1130. if (event == 0) {
  1131. this.openTheDoor()
  1132. } else if (event == 3) {
  1133. this.addFingerPrints()
  1134. } else if (event == 6) {
  1135. this.setData({
  1136. show: true
  1137. })
  1138. } else if (event == 11) {
  1139. this.synchroPlatformTask()
  1140. }
  1141. } else {
  1142. that.goBlue()
  1143. }
  1144. } else if (deviceinfo.manufactureId == 101) {
  1145. if (app.globalData.bleStatus == '蓝牙已连接') {
  1146. if (event == 0) {
  1147. this.btntest2()
  1148. } else if (event == 3) {
  1149. this.btntest3()
  1150. } else if (event == 6) {
  1151. // this.setVoice()
  1152. this.setData({
  1153. show: true
  1154. })
  1155. } else if (event == 7) {
  1156. wx.showModal({
  1157. title: "常开设置",
  1158. cancelText: '关闭',
  1159. confirmText: '开启',
  1160. success: (res) => {
  1161. if (res.confirm) {
  1162. that.setOpen()
  1163. } else if (res.cancel) {
  1164. that.setOpen1()
  1165. }
  1166. }
  1167. })
  1168. }
  1169. } else {
  1170. this.startconnect()
  1171. }
  1172. } else if (deviceinfo.manufactureId == 1201 || deviceinfo.manufactureId == 1241) {
  1173. if (this.data.isSearch) {
  1174. if (event == '0') {
  1175. // let iotLwj = new LwJ(this.data.lockDevice,isConnected,connectedDeviceId,isInitReady,this.data.lockDevice.lockModel)
  1176. // iotLwj.openLock(41)
  1177. this.LwjopenLock()
  1178. } else if (event == 6) {
  1179. this.setData({
  1180. show: true
  1181. })
  1182. }else if(event == '3') {
  1183. this.configurewifj()
  1184. }else if(event == '8') {
  1185. this.personnelmanagement()
  1186. }
  1187. } else {
  1188. this.searchDevicesMethods()
  1189. }
  1190. }
  1191. },
  1192. onQueryBindState: function () {
  1193. var that = this
  1194. wx.showLoading({
  1195. title: '查询中',
  1196. })
  1197. var bytes = plugin.queryLockState(lockDevice.name)
  1198. console.debug('bytes', bytes)
  1199. that.sendBytes(bytes)
  1200. },
  1201. searchDevicesMethods() {
  1202. var that = this;
  1203. wx.showLoading({
  1204. title: '正在搜索',
  1205. // mask: true
  1206. })
  1207. this.setData({
  1208. searchClick: false
  1209. })
  1210. wx.openBluetoothAdapter({
  1211. success: function (res) {
  1212. wx.startBluetoothDevicesDiscovery({
  1213. success: function (res) {
  1214. wx.getBluetoothDevices({
  1215. success: function (res) {
  1216. console.log(res)
  1217. res.devices.map(it => {
  1218. var status = it.name.indexOf('WSL')
  1219. if (status != -1) {
  1220. if (it.RSSI != 127) {
  1221. if (it.advertisData != null) {
  1222. it.adData = util1.ab2hex(it.advertisData)
  1223. if (it.adData.length > 10) {
  1224. var macStr = it.adData.substring(4, 16).toUpperCase()
  1225. //console.log('macStr', macStr)
  1226. it.mac = macStr.substr(10, 2) + ':' + macStr.substr(8, 2) + ':' + macStr.substr(6, 2) + ':' + macStr.substr(4, 2) + ':' + macStr.substr(2, 2) + ':' + macStr.substr(0, 2)
  1227. if (it.adData.substring(1, 2) == '1') {
  1228. it.isLightOn = true
  1229. it.textColor = 'green'
  1230. } else {
  1231. it.isLightOn = false
  1232. it.textColor = 'black'
  1233. }
  1234. if (it.adData.substring(3, 4) == '1') {
  1235. it.textColor = 'pink'
  1236. it.isBind = true
  1237. } else {
  1238. it.isBind = false
  1239. }
  1240. }
  1241. } else {
  1242. it.textColor = 'black'
  1243. }
  1244. lockModel = util1.getLockModel(it.name)
  1245. it.lockType = util1.getLockType(lockModel)
  1246. if (it.name == that.data.deviceinfo.deviceNum) {
  1247. app.addDeviceWithDevName(it.name, it)
  1248. connectedDeviceId = it.deviceId
  1249. lockDevice = it
  1250. console.log(lockDevice, it, 'lockDevice,it')
  1251. app.globalData.isSearch = true
  1252. that.setData({
  1253. isSearch: true,
  1254. // lockDevice:it
  1255. })
  1256. // let iotLwj = new LwJ(it,isConnected,connectedDeviceId,isInitReady,it.lockModel)
  1257. // iotLwj.connectTo()
  1258. if (that.data.event == '0') {
  1259. // iotLwj.openLock(41)
  1260. that.LwjopenLock()
  1261. } else if (that.data.event == 6) {
  1262. that.setData({
  1263. show: true
  1264. })
  1265. } else if (that.data.event == '3') {
  1266. that.configurewifj()
  1267. }else if(that.data.event == '8') {
  1268. that.personnelmanagement()
  1269. }
  1270. wx.hideLoading({
  1271. success: (res) => {},
  1272. })
  1273. }
  1274. }
  1275. }
  1276. })
  1277. if (!that.data.isSearch && that.data.toSearch) {
  1278. that.searchDevicesMethods()
  1279. }
  1280. }
  1281. })
  1282. },
  1283. fail: function (res) {
  1284. wx.hideLoading({
  1285. success: (res) => {
  1286. wx.showToast({
  1287. title: res.errMsg,
  1288. duration: 2000,
  1289. icon: 'none'
  1290. })
  1291. },
  1292. })
  1293. }
  1294. })
  1295. },
  1296. fail: function (res) {
  1297. console.log('请打开蓝牙和定位功能')
  1298. wx.hideLoading({
  1299. success: (res) => {
  1300. wx.showToast({
  1301. title: '请打开蓝牙和定位功能',
  1302. duration: 2000,
  1303. icon: 'none'
  1304. })
  1305. },
  1306. })
  1307. }
  1308. })
  1309. },
  1310. LwjopenLock: function () {
  1311. var that = this
  1312. taskId = 41
  1313. that.initBluetooth()
  1314. },
  1315. configurewifj() {
  1316. var that = this
  1317. taskId = 121
  1318. that.initBluetooth()
  1319. },
  1320. // 人员管理
  1321. personnelmanagement() {
  1322. var that = this
  1323. taskId = 9010
  1324. that.initBluetooth()
  1325. },
  1326. LwjonOpenLock: function () {
  1327. var that = this
  1328. wx.showLoading({
  1329. title: that.data.isJack ? '上电中' : '解锁中',
  1330. })
  1331. console.log(lockDevice.name, 'lockDevice.name0开锁中')
  1332. var bytes = plugin.sendOpenLockP1(lockDevice.name, basecode)
  1333. console.debug('bytes', bytes)
  1334. that.sendBytes(bytes)
  1335. },
  1336. initBluetooth: function () {
  1337. var that = this
  1338. wx.getConnectedBluetoothDevices({
  1339. services: [config.uuid.serviceUuid],
  1340. success: function (res) {
  1341. res.devices.map(function (it) {
  1342. if (it.deviceId == connectedDeviceId || it.name == lockDevice.name) {
  1343. isConnected = true
  1344. }
  1345. })
  1346. that.setData({
  1347. isConnected: isConnected
  1348. })
  1349. console.log('isConnected', isConnected)
  1350. if (isConnected) {
  1351. if (isInitReady) {
  1352. console.log(isInitReady, 'isInitReady', taskId)
  1353. setTimeout(() => {
  1354. that.doWork()
  1355. }, 500)
  1356. } else {
  1357. that.setupConnection()
  1358. }
  1359. } else {
  1360. that.connectTo()
  1361. }
  1362. },
  1363. })
  1364. },
  1365. doWork: function () {
  1366. var that = this
  1367. switch (taskId) {
  1368. case 11: {
  1369. that.onBindLock()
  1370. break
  1371. }
  1372. case 12: {
  1373. that.onUnbindLock()
  1374. break
  1375. }
  1376. case 21: {
  1377. that.onQueryBindState()
  1378. break
  1379. }
  1380. case 22: {
  1381. that.onQueryBattery()
  1382. break
  1383. }
  1384. case 31: {
  1385. that.onQueryNbImei()
  1386. break
  1387. }
  1388. case 32: {
  1389. that.onLogin()
  1390. break
  1391. }
  1392. case 41: {
  1393. that.LwjonOpenLock()
  1394. break
  1395. }
  1396. case 42: {
  1397. that.onSyncClock()
  1398. break
  1399. }
  1400. case 51: {
  1401. that.onAddPincode()
  1402. break
  1403. }
  1404. case 52: {
  1405. that.onDelPincode()
  1406. break
  1407. }
  1408. case 61: {
  1409. if (lockModel > 70) {
  1410. that.onAddRfCard()
  1411. } else {
  1412. wx.showModal({
  1413. title: '提示',
  1414. content: '请刷卡!',
  1415. showCancel: false
  1416. })
  1417. }
  1418. break
  1419. }
  1420. case 62: {
  1421. that.onDelRfCard()
  1422. break
  1423. }
  1424. case 71: {
  1425. that.onAddFingerprint()
  1426. break
  1427. }
  1428. case 72: {
  1429. that.onDelFingerprint()
  1430. break
  1431. }
  1432. case 81: {
  1433. that.onChangeAdminPincode()
  1434. break
  1435. }
  1436. case 91: {
  1437. that.onQueryLockSlotState()
  1438. break
  1439. }
  1440. case 92: {
  1441. that.onQueryLockUnlockState()
  1442. break
  1443. }
  1444. case 101: {
  1445. that.onChangeLockUnlockState()
  1446. break
  1447. }
  1448. case 102: {
  1449. that.onChangeLockMutedState()
  1450. break
  1451. }
  1452. case 111: {
  1453. this.onChangeLockNbState()
  1454. }
  1455. case 121: {
  1456. this.onConfigurewifi()
  1457. }
  1458. // 人员管理
  1459. case 9010: {
  1460. this.gotoperson()
  1461. }
  1462. default: {
  1463. }
  1464. }
  1465. },
  1466. connectTo: function () {
  1467. var that = this
  1468. wx.showLoading({
  1469. title: '连接中',
  1470. })
  1471. wx.createBLEConnection({
  1472. deviceId: lockDevice.deviceId,
  1473. timeout: config.ble.connectTimeOut,
  1474. success: function (res) {
  1475. console.debug('connectTo', res)
  1476. isConnected = true
  1477. wx.hideLoading()
  1478. setTimeout(function () {
  1479. that.setupConnection()
  1480. }, 500)
  1481. },
  1482. fail: function (e) {
  1483. console.debug('connectTo', e)
  1484. wx.hideLoading()
  1485. switch (e.errCode) {
  1486. case 10003: {
  1487. wx.showModal({
  1488. title: '连接失败',
  1489. content: '是否重新连接?',
  1490. success: function (res) {
  1491. if (res.confirm) {
  1492. that.connectTo()
  1493. }
  1494. }
  1495. })
  1496. break
  1497. }
  1498. case 10012: {
  1499. wx.showModal({
  1500. title: '连接失败',
  1501. content: '蓝牙连接超时',
  1502. showCancel: false
  1503. })
  1504. break
  1505. }
  1506. case 10002: {
  1507. wx.showModal({
  1508. title: '连接失败',
  1509. content: '没有找到指定设备',
  1510. showCancel: false
  1511. })
  1512. break
  1513. }
  1514. }
  1515. }
  1516. })
  1517. },
  1518. setupConnection: function () {
  1519. var that = this
  1520. wx.showLoading({
  1521. title: '初始化连接中',
  1522. })
  1523. wx.getBLEDeviceServices({
  1524. deviceId: lockDevice.deviceId,
  1525. success: function (res) {
  1526. console.log('Services', res)
  1527. wx.getBLEDeviceCharacteristics({
  1528. deviceId: lockDevice.deviceId,
  1529. serviceId: config.uuid.serviceUuid.toUpperCase(),
  1530. success: function (res) {
  1531. console.log('Characteristics', res)
  1532. wx.showLoading({
  1533. title: '连接成功',
  1534. })
  1535. wx.onBLECharacteristicValueChange(function (res) {
  1536. var bytes = new Uint8Array(res.value)
  1537. console.log('bytes', bytes)
  1538. console.log(lockDevice.name, 'lockDevice.name1')
  1539. var data = plugin.parseBytes(lockDevice.name, basecode, bytes)
  1540. console.log('data', data)
  1541. switch (data.cmd) {
  1542. case 'reportLockBattery': {
  1543. if (data.data.battery < 10) {
  1544. wx.showModal({
  1545. title: '电量报警',
  1546. content: `门锁当前电量${data.data.battery}%!\n电量严重不足,请立即更换电池!`,
  1547. showCancel: false
  1548. })
  1549. } else if (data.data.battery < 30) {
  1550. wx.showModal({
  1551. title: '电量预警',
  1552. content: `门锁当前电量${data.data.battery}%!\n请及时更换电池!`,
  1553. showCancel: false
  1554. })
  1555. }
  1556. break
  1557. }
  1558. case 'reportRfCardResult': {
  1559. if (taskId == 61) {
  1560. wx.showModal({
  1561. title: '检测到新卡',
  1562. content: `是否添加此房卡,卡号:${data.data.cardId}`,
  1563. success: function (res) {
  1564. if (res.confirm) {
  1565. rfCardId = data.data.cardId
  1566. that.onAddRfCard()
  1567. }
  1568. }
  1569. })
  1570. } else {
  1571. if (lockModel > 70) {
  1572. if (data.data.isValid) {
  1573. wx.showModal({
  1574. title: '提示',
  1575. content: `房卡开锁成功,序号:${data.data.index}`,
  1576. showCancel: false
  1577. })
  1578. } else {
  1579. wx.showModal({
  1580. title: '提示',
  1581. content: `多次刷房卡开锁失败!`,
  1582. showCancel: false
  1583. })
  1584. }
  1585. } else {
  1586. if (data.data.isValid) {
  1587. wx.showModal({
  1588. title: '提示',
  1589. content: `房卡开锁成功,卡号:${data.data.cardId}`,
  1590. showCancel: false
  1591. })
  1592. } else {
  1593. wx.showModal({
  1594. title: '提示',
  1595. content: `房卡未授权,卡号:${data.data.cardId}`,
  1596. showCancel: false
  1597. })
  1598. }
  1599. }
  1600. }
  1601. break
  1602. }
  1603. case 'reportPincodeResult': {
  1604. if (lockModel > 70) {
  1605. if (data.data.isValid) {
  1606. wx.showModal({
  1607. title: '提示',
  1608. content: `密码开锁成功,序号:${data.data.index}`,
  1609. showCancel: false
  1610. })
  1611. } else {
  1612. wx.showModal({
  1613. title: '提示',
  1614. content: `多次密码开锁失败!`,
  1615. showCancel: false
  1616. })
  1617. }
  1618. } else {
  1619. if (data.data.isValid) {
  1620. wx.showModal({
  1621. title: '提示',
  1622. content: `密码:${data.data.pincode}开锁成功!`,
  1623. showCancel: false
  1624. })
  1625. } else {
  1626. wx.showModal({
  1627. title: '提示',
  1628. content: `密码:${data.data.pincode}未授权!`,
  1629. showCancel: false
  1630. })
  1631. }
  1632. }
  1633. break
  1634. }
  1635. case 'reportFingerprintResult': {
  1636. if (data.data.isValid) {
  1637. wx.showModal({
  1638. title: '提示',
  1639. content: `指纹开锁成功,序号:${data.data.index}`,
  1640. showCancel: false
  1641. })
  1642. } else {
  1643. wx.showModal({
  1644. title: '提示',
  1645. content: `多次指纹开锁失败!`,
  1646. showCancel: false
  1647. })
  1648. }
  1649. break
  1650. }
  1651. case 'queryLockState': {
  1652. wx.hideLoading()
  1653. if (data.code != 200) {
  1654. wx.showModal({
  1655. title: '提示',
  1656. content: '查询失败!',
  1657. showCancel: false,
  1658. success: function (res) {}
  1659. })
  1660. break
  1661. }
  1662. that.setData({
  1663. lock: {
  1664. mac: data.data.mac,
  1665. lockType: util1.getLockType(lockModel),
  1666. },
  1667. isBindLock: data.data.isBind
  1668. })
  1669. lockMac = data.data.mac
  1670. if (taskId > 0) {
  1671. if (data.data.isBind) {
  1672. wx.showModal({
  1673. title: data.data.mac,
  1674. content: '此设备已经硬件绑定!',
  1675. showCancel: false,
  1676. success: function (res) {}
  1677. })
  1678. } else {
  1679. wx.showModal({
  1680. title: data.data.mac,
  1681. content: '此设备硬件未绑定!',
  1682. showCancel: false,
  1683. success: function (res) {}
  1684. })
  1685. }
  1686. }
  1687. taskId = 0
  1688. break
  1689. }
  1690. case 'queryLockBattery': {
  1691. wx.hideLoading()
  1692. if (data.code != 200) {
  1693. wx.showModal({
  1694. title: '提示',
  1695. content: '查询失败!',
  1696. showCancel: false,
  1697. success: function (res) {}
  1698. })
  1699. break
  1700. }
  1701. taskId = 0
  1702. wx.showModal({
  1703. title: '提示',
  1704. content: `此门锁电量为${data.data.battery}%`,
  1705. showCancel: false,
  1706. success: function (res) {}
  1707. })
  1708. break
  1709. }
  1710. case 'queryNbImei': {
  1711. wx.hideLoading()
  1712. if (data.code != 200) {
  1713. wx.showModal({
  1714. title: '提示',
  1715. content: '查询失败!',
  1716. showCancel: false,
  1717. success: function (res) {}
  1718. })
  1719. break
  1720. }
  1721. taskId = 0
  1722. wx.showModal({
  1723. title: '提示',
  1724. content: `此NB设备IMEI为${data.data.imei}`,
  1725. showCancel: false,
  1726. success: function (res) {}
  1727. })
  1728. break
  1729. }
  1730. case 'setNbFun': {
  1731. wx.hideLoading()
  1732. if (data.code != 200) {
  1733. wx.showModal({
  1734. title: '提示',
  1735. content: '设置Nb功能失败!',
  1736. showCancel: false,
  1737. success: function (res) {}
  1738. })
  1739. break
  1740. }
  1741. taskId = 0
  1742. wx.showModal({
  1743. title: '提示',
  1744. content: that.data.isNbEnable ? 'NB功能已禁用' : 'NB功能已打开',
  1745. showCancel: false,
  1746. })
  1747. that.setData({
  1748. isNbEnable: !that.data.isNbEnable
  1749. })
  1750. break
  1751. }
  1752. case 'sendBindLock': {
  1753. wx.hideLoading()
  1754. if (data.code != 200) {
  1755. wx.showModal({
  1756. title: '提示',
  1757. content: '绑定失败!',
  1758. showCancel: false,
  1759. success: function (res) {}
  1760. })
  1761. break
  1762. }
  1763. taskId = 0
  1764. that.setData({
  1765. isBindLock: data.data.isBind
  1766. })
  1767. if (data.data.isBind) {
  1768. wx.showToast({
  1769. title: '绑定成功!',
  1770. })
  1771. } else {
  1772. wx.showToast({
  1773. title: '绑定失败!',
  1774. })
  1775. }
  1776. break
  1777. }
  1778. case 'sendUnbindLock': {
  1779. wx.hideLoading()
  1780. if (data.code != 200) {
  1781. wx.showModal({
  1782. title: '提示',
  1783. content: '解绑失败!',
  1784. showCancel: false,
  1785. success: function (res) {}
  1786. })
  1787. break
  1788. }
  1789. taskId = 0
  1790. that.setData({
  1791. isBindLock: data.data.isBind
  1792. })
  1793. if (data.data.isBind) {
  1794. wx.showToast({
  1795. title: '解绑失败!',
  1796. })
  1797. } else {
  1798. wx.showToast({
  1799. title: '解绑成功!',
  1800. })
  1801. }
  1802. break
  1803. }
  1804. case 'login1':
  1805. case 'sendOpenLockP1': {
  1806. if (data.code != 200) {
  1807. wx.hideLoading()
  1808. if (taskId == 41) {
  1809. wx.showModal({
  1810. title: '提示',
  1811. content: that.data.isJack ? '上电失败!' : '解锁失败!',
  1812. showCancel: false,
  1813. success: function (res) {}
  1814. })
  1815. } else if (taskId == 32) {
  1816. wx.showModal({
  1817. title: '提示',
  1818. content: '登录失败!',
  1819. showCancel: false,
  1820. success: function (res) {}
  1821. })
  1822. }
  1823. break
  1824. }
  1825. if (taskId == 41) {
  1826. console.log(lockDevice.name, 'lockDevice.name2')
  1827. var bytes = plugin.sendOpenLockP2(lockDevice.name, basecode, data.data.randomN)
  1828. console.debug('bytes', bytes)
  1829. that.sendBytes(bytes)
  1830. } else if (taskId == 32) {
  1831. console.log(lockDevice.name, 'lockDevice.name3')
  1832. var bytes = plugin.login2(lockDevice.name, basecode, data.data.randomN)
  1833. console.debug('bytes', bytes)
  1834. that.sendBytes(bytes)
  1835. }
  1836. break;
  1837. }
  1838. case 'login':
  1839. case 'sendOpenLockP2': {
  1840. wx.hideLoading()
  1841. if (data.code != 200) {
  1842. if (taskId == 41) {
  1843. wx.showModal({
  1844. title: '提示',
  1845. content: that.data.isJack ? '上电失败!' : '解锁失败!',
  1846. showCancel: false,
  1847. success: function (res) {}
  1848. })
  1849. } else if (taskId == 32) {
  1850. wx.showModal({
  1851. title: '提示',
  1852. content: '登录失败!',
  1853. showCancel: false,
  1854. success: function (res) {}
  1855. })
  1856. }
  1857. break
  1858. }
  1859. isLockLogin = true
  1860. if (taskId == 41) {
  1861. var data = {
  1862. code: "ble/bleUnlockRecord",
  1863. data: {
  1864. deviceNum: that.data.deviceinfo.deviceNum,
  1865. roomId: that.data.deviceinfo.roomId,
  1866. roomName: that.data.deviceinfo.roomName,
  1867. cardName: that.data.userInfo.username,
  1868. productId: that.data.deviceinfo.manufactureId,
  1869. userId: '29283991'
  1870. }
  1871. }
  1872. WXAPI.sendCommand(data).then(res => {
  1873. console.log(res)
  1874. })
  1875. wx.showToast({
  1876. title: that.data.isJack ? '上电成功!' : '解锁成功!',
  1877. })
  1878. } else if (taskId == 32) {
  1879. wx.showToast({
  1880. title: '登录成功!',
  1881. })
  1882. }
  1883. taskId = 0
  1884. break;
  1885. }
  1886. case 'syncClock': {
  1887. wx.hideLoading()
  1888. if (data.code != 200) {
  1889. wx.showModal({
  1890. title: '提示',
  1891. content: '同步时间失败!' + data.data.msg,
  1892. showCancel: false,
  1893. success: function (res) {}
  1894. })
  1895. break
  1896. }
  1897. taskId = 0
  1898. wx.showModal({
  1899. title: '提示',
  1900. content: '时间更新成功!',
  1901. showCancel: false
  1902. })
  1903. break;
  1904. }
  1905. case 'addPincode': {
  1906. wx.hideLoading()
  1907. if (data.code != 200) {
  1908. wx.showModal({
  1909. title: '提示',
  1910. content: '添加密码失败!' + data.data.msg,
  1911. showCancel: false,
  1912. success: function (res) {}
  1913. })
  1914. break
  1915. }
  1916. taskId = 0
  1917. that.setData({
  1918. isPincodeAdd: true
  1919. })
  1920. wx.showModal({
  1921. title: '提示',
  1922. content: `添加密码${pincode}成功!`,
  1923. showCancel: false
  1924. })
  1925. break
  1926. }
  1927. case 'delPincode': {
  1928. wx.hideLoading()
  1929. if (data.code != 200) {
  1930. wx.showModal({
  1931. title: '提示',
  1932. content: '删除密码失败!',
  1933. showCancel: false,
  1934. success: function (res) {}
  1935. })
  1936. break
  1937. }
  1938. taskId = 0
  1939. wx.showModal({
  1940. title: '提示',
  1941. content: `删除密码${pincode}成功!`,
  1942. showCancel: false,
  1943. })
  1944. that.setData({
  1945. isPincodeAdd: false
  1946. })
  1947. pincode = -1
  1948. pincodeIndex = -1
  1949. break
  1950. }
  1951. case 'addRfCard': {
  1952. wx.hideLoading()
  1953. if (data.code == 100) {
  1954. wx.showLoading({
  1955. title: data.data.msg,
  1956. })
  1957. break
  1958. } else if (data.code != 200) {
  1959. wx.showModal({
  1960. title: '提示',
  1961. content: '添加房卡失败!' + data.data.msg,
  1962. showCancel: false,
  1963. success: function (res) {}
  1964. })
  1965. break
  1966. }
  1967. taskId = 0
  1968. that.setData({
  1969. isRfCardAdd: true
  1970. })
  1971. if (lockModel > 70) {
  1972. wx.showModal({
  1973. title: '提示',
  1974. content: `添加房卡成功,序号${rfCardIndex}`,
  1975. showCancel: false
  1976. })
  1977. } else {
  1978. wx.showModal({
  1979. title: '提示',
  1980. content: `添加房卡成功,卡号${rfCardId}`,
  1981. showCancel: false
  1982. })
  1983. }
  1984. break
  1985. }
  1986. case 'delRfCard': {
  1987. wx.hideLoading()
  1988. if (data.code != 200) {
  1989. wx.showModal({
  1990. title: '提示',
  1991. content: '删除房卡失败!',
  1992. showCancel: false,
  1993. success: function (res) {}
  1994. })
  1995. break
  1996. }
  1997. taskId = 0
  1998. that.setData({
  1999. isRfCardAdd: false
  2000. })
  2001. if (lockModel > 70) {
  2002. wx.showModal({
  2003. title: '提示',
  2004. content: `删除房卡成功,序号${rfCardIndex}`,
  2005. showCancel: false,
  2006. })
  2007. } else {
  2008. wx.showModal({
  2009. title: '提示',
  2010. content: `删除房卡成功,卡号${rfCardId}`,
  2011. showCancel: false,
  2012. })
  2013. }
  2014. rfCardId = -1
  2015. rfCardIndex = -1
  2016. break
  2017. }
  2018. case 'addFingerprint': {
  2019. wx.hideLoading()
  2020. if (data.code == 100) {
  2021. wx.showLoading({
  2022. title: data.data.msg,
  2023. })
  2024. break
  2025. } else if (data.code != 200) {
  2026. wx.showModal({
  2027. title: '提示',
  2028. content: '添加指纹失败!' + data.data.msg,
  2029. showCancel: false,
  2030. success: function (res) {}
  2031. })
  2032. break
  2033. }
  2034. taskId = 0
  2035. that.setData({
  2036. isFingerprintAdd: true
  2037. })
  2038. wx.showModal({
  2039. title: '提示',
  2040. content: `添加指纹成功,序号${fingerprintIndex}`,
  2041. showCancel: false
  2042. })
  2043. break
  2044. }
  2045. case 'delFingerprint': {
  2046. wx.hideLoading()
  2047. if (data.code != 200) {
  2048. wx.showModal({
  2049. title: '提示',
  2050. content: '删除指纹失败!',
  2051. showCancel: false,
  2052. success: function (res) {}
  2053. })
  2054. break
  2055. }
  2056. taskId = 0
  2057. wx.showModal({
  2058. title: '提示',
  2059. content: `删除指纹成功,序号${fingerprintIndex}`,
  2060. showCancel: false,
  2061. })
  2062. that.setData({
  2063. isFingerprintAdd: false
  2064. })
  2065. fingerprintIndex = -1
  2066. break
  2067. }
  2068. case 'changeAdminPincode': {
  2069. wx.hideLoading()
  2070. if (data.code != 200) {
  2071. wx.showModal({
  2072. title: '提示',
  2073. content: '修改管理密码失败!',
  2074. showCancel: false,
  2075. success: function (res) {}
  2076. })
  2077. break
  2078. }
  2079. taskId = 0
  2080. wx.showToast({
  2081. title: '修改管理密码成功!',
  2082. })
  2083. }
  2084. case 'queryLockSlotState': {
  2085. wx.hideLoading()
  2086. if (data.code != 200) {
  2087. wx.showModal({
  2088. title: '提示',
  2089. content: '查询失败!',
  2090. showCancel: false,
  2091. success: function (res) {}
  2092. })
  2093. break
  2094. }
  2095. taskId = 0
  2096. wx.showModal({
  2097. title: '提示',
  2098. content: data.data.state == 0 ? '此门锁方舌关闭' : '此门锁方舌打开',
  2099. showCancel: false,
  2100. success: function (res) {}
  2101. })
  2102. break
  2103. }
  2104. case 'queryLockUnlockState': {
  2105. wx.hideLoading()
  2106. if (data.code != 200) {
  2107. wx.showModal({
  2108. title: '提示',
  2109. content: '查询失败!',
  2110. showCancel: false,
  2111. success: function (res) {}
  2112. })
  2113. break
  2114. }
  2115. taskId = 0
  2116. wx.showModal({
  2117. title: '提示',
  2118. content: data.data.state == 0 ? '此门锁已关闭' : '此门锁已打开',
  2119. showCancel: false,
  2120. success: function (res) {}
  2121. })
  2122. break
  2123. }
  2124. case 'setLockUnlockState': {
  2125. wx.hideLoading()
  2126. if (data.code != 200) {
  2127. wx.showModal({
  2128. title: '提示',
  2129. content: '设置常开指令失败!',
  2130. showCancel: false,
  2131. success: function (res) {}
  2132. })
  2133. break
  2134. }
  2135. taskId = 0
  2136. wx.showModal({
  2137. title: '提示',
  2138. content: data.data.state == 0 ? '常开模式已退出' : '常开模式已开启',
  2139. showCancel: false,
  2140. })
  2141. that.setData({
  2142. isUnloked: data.data.state == 1
  2143. })
  2144. break
  2145. }
  2146. case 'setLockMuteState': {
  2147. wx.hideLoading()
  2148. if (data.code != 200) {
  2149. wx.showModal({
  2150. title: '提示',
  2151. content: '设置静音指令失败!',
  2152. showCancel: false,
  2153. success: function (res) {}
  2154. })
  2155. break
  2156. }
  2157. taskId = 0
  2158. wx.showModal({
  2159. title: '提示',
  2160. content: data.data.state == 1 ? '静音模式已退出' : '静音模式已开启',
  2161. showCancel: false,
  2162. })
  2163. that.setData({
  2164. isMuted: data.data.state == 0
  2165. })
  2166. break
  2167. }
  2168. }
  2169. })
  2170. wx.notifyBLECharacteristicValueChange({
  2171. deviceId: lockDevice.deviceId,
  2172. serviceId: config.uuid.serviceUuid.toUpperCase(),
  2173. characteristicId: config.uuid.notifyUuid.toUpperCase(),
  2174. state: true,
  2175. success: function (res) {
  2176. console.log('setupNotify', res)
  2177. isInitReady = true
  2178. setTimeout(function () {
  2179. wx.hideLoading()
  2180. // that.onQueryBindState()
  2181. if (taskId == 41 && that.data.event == '0') {
  2182. that.doWork()
  2183. }else if(taskId == 121 && that.data.event == '3') {
  2184. that.doWork()
  2185. }else if(taskId == 9010 && that.data.event == '8') {
  2186. that.doWork()
  2187. }
  2188. }, 500)
  2189. },
  2190. })
  2191. },
  2192. })
  2193. },
  2194. })
  2195. },
  2196. addPincode: function () {
  2197. var that = this
  2198. taskId = 51
  2199. that.initBluetooth()
  2200. },
  2201. onAddPincode: function () {
  2202. var that = this
  2203. wx.showLoading({
  2204. title: '添加密码中',
  2205. })
  2206. var startTime = util1.getLocalTime(8)
  2207. var endTime = new Date(startTime.getTime() + 5 * 60 * 1000) //有效期5分钟
  2208. pincode = 949468 //util1.randomPrime6()
  2209. pincodeIndex = 22 //Math.floor(Math.random() * 100)
  2210. var bytes = plugin.addPincode(lockDevice.name, basecode, pincode, pincodeIndex, startTime, endTime)
  2211. console.debug('bytes', bytes)
  2212. that.sendBytes(bytes)
  2213. },
  2214. sendBytes(bytes) {
  2215. console.log(lockDevice, config, bytes, 'sendBytes---')
  2216. setTimeout(function () {
  2217. wx.writeBLECharacteristicValue({
  2218. deviceId: lockDevice.deviceId,
  2219. serviceId: config.uuid.serviceUuid.toUpperCase(),
  2220. characteristicId: config.uuid.writeUuid.toUpperCase(),
  2221. value: bytes,
  2222. success: function (res) {
  2223. },
  2224. })
  2225. }, 50)
  2226. },
  2227. goBlue() {
  2228. var that = this
  2229. wx.showLoading({
  2230. title: '搜索设备中',
  2231. mask: true
  2232. })
  2233. // wx.removeStorageSync("connectedDeviceId")
  2234. ICINLocker.scanDevices(this.data.deviceinfo.deviceNum)
  2235. .then((res) => {
  2236. if (res.status === 0) {
  2237. var deviceId = res.deviceId
  2238. //成功找到蓝牙情况下进行锁连接
  2239. app.globalData.blueDeviceId = res.deviceId
  2240. ICINLocker.connect(res.deviceId, false) // 此处 deviceId 存起来,当断开连接时使用
  2241. .then((res) => {
  2242. if (res.status === 0) {
  2243. clearInterval(that.data.timer)
  2244. this.setData({
  2245. bleStatus: '蓝牙已连接',
  2246. deviceId: deviceId
  2247. })
  2248. app.globalData.zgBlueStatus = true
  2249. wx.hideLoading()
  2250. wx.showToast({
  2251. title: '连接成功',
  2252. icon: "none",
  2253. duration: 2000
  2254. })
  2255. setTimeout(() => {
  2256. if (this.data.event === 0) {
  2257. this.openTheDoor()
  2258. } else if (this.data.event == 3) {
  2259. this.addFingerPrints()
  2260. } else if (this.data.event == 6) {
  2261. this.setData({
  2262. show: true
  2263. })
  2264. } else if (this.data.event == 11) {
  2265. this.synchroPlatformTask()
  2266. }
  2267. }, 500)
  2268. } else {
  2269. wx.hideLoading()
  2270. var msg = ''
  2271. switch (res.errCode) {
  2272. case 10001:
  2273. msg = '调用失败,请打开蓝牙!';
  2274. break;
  2275. }
  2276. wx.showToast({
  2277. title: msg,
  2278. duration: 2000,
  2279. icon: 'none'
  2280. })
  2281. //此处处理返回值不为成功(0)的情况
  2282. }
  2283. }).catch((res) => {
  2284. wx.hideLoading()
  2285. console.log(res, '失败')
  2286. // 此处处理未能连接成功问题
  2287. })
  2288. }
  2289. })
  2290. .catch((res) => {
  2291. //此处处理未能连接到蓝牙的问题
  2292. console.log(res)
  2293. if (res.status == 1) {
  2294. if (res.statusReason.errCode == 10001) {
  2295. wx.hideLoading()
  2296. wx.showToast({
  2297. title: '请检查手机蓝牙和定位功能是否打开?',
  2298. duration: 2000,
  2299. icon: 'none'
  2300. })
  2301. } else {
  2302. if (!app.globalData.zgBlueStatus && that.data.bluestatus) {
  2303. that.goBlue()
  2304. } else {
  2305. wx.showToast({
  2306. title: res.statusReason,
  2307. duration: 2000,
  2308. icon: 'none'
  2309. })
  2310. wx.hideLoading()
  2311. }
  2312. }
  2313. } else {
  2314. wx.hideLoading()
  2315. }
  2316. })
  2317. },
  2318. /**
  2319. * *设置音量
  2320. * @param {门锁ID} _lockId
  2321. * @param {用户ID 最多20字节} _userId
  2322. * @param {r 声音选项 00: 静音 01: 低音 02高音} _action
  2323. * @param _commonKey
  2324. * @param {回调方法} callback
  2325. **/
  2326. // 设置音量
  2327. setVolume1(volume) {
  2328. wx.showLoading({
  2329. title: "正在发送...",
  2330. mask: true
  2331. });
  2332. setTimeout(() => {
  2333. wx.hideLoading()
  2334. }, 1000);
  2335. let cmd = 0xF2
  2336. // let dn = util.stringToUint8ArrayWithPadding(deviceNum, 20, 0xff)
  2337. let length = 2
  2338. let b = new Uint8Array(length)
  2339. b[0] = cmd
  2340. b[1] = volume;
  2341. app.globalData.ble.send(cmd, b, function (res) {
  2342. })
  2343. var dataOpenReport = {
  2344. code: "ble/volumeReport",
  2345. data: {
  2346. deviceNum: this.data.deviceinfo.deviceNum,
  2347. operate: parseInt(volume),
  2348. order: 61001
  2349. }
  2350. }
  2351. WXAPI.sendCommand(dataOpenReport).then(res => {
  2352. })
  2353. },
  2354. setVolume(volume) {
  2355. ICINLocker.getSignKey(this.data.cgInfo.lockerId, this.data.cgInfo.superId, this.data.cgInfo.superId,
  2356. (callback) => {
  2357. console.log(callback, '-----')
  2358. if (callback.status == '00') {
  2359. ICINLocker.getCommonKey(this.data.cgInfo.lockerId, this.data.cgInfo.superId, this.data.cgInfo.superId, callback.signKey, (callback1) => {
  2360. console.log(callback1, '2222222')
  2361. if (callback1.status == '00') {
  2362. ICINLocker.setVolume(this.data.cgInfo.lockerId, this.data.cgInfo.superId, volume, callback1.commonKey, (callback2) => {
  2363. console.log(callback2, '333333333')
  2364. if (callback2.status == '00') {
  2365. var dataOpenReport = {
  2366. code: "ble/volumeReport",
  2367. data: {
  2368. deviceNum: this.data.deviceinfo.deviceNum,
  2369. operate: parseInt(volume),
  2370. order: 61001
  2371. }
  2372. }
  2373. WXAPI.sendCommand(dataOpenReport).then(res => {
  2374. })
  2375. wx.hideLoading()
  2376. wx.showToast({
  2377. title: '设置成功',
  2378. duration: 2000,
  2379. icon: 'none'
  2380. })
  2381. } else {
  2382. wx.hideLoading()
  2383. wx.showToast({
  2384. title: '设置失败',
  2385. duration: 2000,
  2386. icon: 'none'
  2387. })
  2388. }
  2389. })
  2390. } else {
  2391. wx.hideLoading()
  2392. wx.showToast({
  2393. title: '设置失败',
  2394. duration: 2000,
  2395. icon: 'none'
  2396. })
  2397. }
  2398. })
  2399. } else {
  2400. wx.hideLoading()
  2401. wx.showToast({
  2402. title: '设置失败',
  2403. duration: 2000,
  2404. icon: 'none'
  2405. })
  2406. }
  2407. });
  2408. },
  2409. //设置常开
  2410. setOpen() {
  2411. wx.showLoading({
  2412. title: "正在开启...",
  2413. mask: true
  2414. });
  2415. setTimeout(() => {
  2416. wx.hideLoading()
  2417. }, 1000);
  2418. let cmd = 0xF3
  2419. // let dn = util.stringToUint8ArrayWithPadding(deviceNum, 20, 0xff)
  2420. let length = 2
  2421. let b = new Uint8Array(length)
  2422. b[0] = cmd
  2423. b[1] = 1;
  2424. app.globalData.ble.send(cmd, b, function (res) {})
  2425. var dataOpenReport = {
  2426. code: "ble/alwaysOpenReport",
  2427. data: {
  2428. deviceNum: this.data.deviceinfo.deviceNum,
  2429. roomName: this.data.deviceinfo.roomName,
  2430. operate: 1,
  2431. order: 61101
  2432. }
  2433. }
  2434. WXAPI.sendCommand(dataOpenReport).then(res => {
  2435. })
  2436. },
  2437. setOpen1() {
  2438. wx.showLoading({
  2439. title: "正在关闭...",
  2440. mask: true
  2441. });
  2442. setTimeout(() => {
  2443. wx.hideLoading()
  2444. }, 1000);
  2445. let cmd = 0xF3
  2446. // let dn = util.stringToUint8ArrayWithPadding(deviceNum, 20, 0xff)
  2447. let length = 2
  2448. let b = new Uint8Array(length)
  2449. b[0] = cmd
  2450. b[1] = 0;
  2451. app.globalData.ble.send(cmd, b, function (res) {})
  2452. var dataOpenReport = {
  2453. code: "ble/alwaysOpenReport",
  2454. data: {
  2455. deviceNum: this.data.deviceinfo.deviceNum,
  2456. roomName: this.data.deviceinfo.roomName,
  2457. operate: 0,
  2458. order: 61101
  2459. }
  2460. }
  2461. WXAPI.sendCommand(dataOpenReport).then(res => {
  2462. })
  2463. },
  2464. //设置音量
  2465. setVoice() {
  2466. },
  2467. startconnect() {
  2468. //if(checkWechatVersion())
  2469. // {
  2470. wx.showLoading({
  2471. title: "连接蓝牙中",
  2472. mask: true
  2473. })
  2474. var deviceinfo = this.data.deviceinfo
  2475. const ble = new BLE(this.data.deviceinfo.deviceNum, emitter)
  2476. app.globalData.ble = ble
  2477. this.watchBLE()
  2478. app.globalData.ble.init()
  2479. // }
  2480. },
  2481. watchBLE() {
  2482. var that = this
  2483. var deviceinfo = this.data.deviceinfo
  2484. if (app.globalData.ble) {
  2485. app.globalData.ble.listen(res => {
  2486. if (res.type == 'connect') {
  2487. if (res.data == "未打开适配器") {
  2488. wx.hideLoading()
  2489. wx.showToast({
  2490. title: '请检查手机蓝牙和定位功能是否打开?',
  2491. duration: 2000,
  2492. icon: 'none'
  2493. })
  2494. } else {
  2495. if (res.data == '蓝牙已连接') {
  2496. this.data.info = res.data
  2497. app.globalData.bleStatus = res.data
  2498. console.log(res.data, 'pppp')
  2499. if (this.data.event == 0) {
  2500. this.btntest2()
  2501. } else if (this.data.event == 3) {
  2502. this.btntest3()
  2503. } else if (this.data.event == 6) {
  2504. // this.setVoice()
  2505. this.setData({
  2506. show: true
  2507. })
  2508. } else if (this.data.event == 7) {
  2509. wx.showModal({
  2510. title: "常开设置",
  2511. cancelText: '关闭',
  2512. confirmText: '开启',
  2513. success: (res) => {
  2514. if (res.confirm) {
  2515. that.setOpen()
  2516. } else if (res.cancel) {
  2517. that.setOpen1()
  2518. }
  2519. }
  2520. })
  2521. } else {
  2522. wx.hideLoading()
  2523. }
  2524. }
  2525. }
  2526. }
  2527. })
  2528. }
  2529. },
  2530. closeblue() {
  2531. wx.showLoading({
  2532. title: "正在停止...",
  2533. mask: true
  2534. });
  2535. setTimeout(() => {
  2536. wx.hideLoading()
  2537. }, 1000);
  2538. app.globalData.ble.close()
  2539. this.data.info = ''
  2540. app.globalData.bleStatus = false
  2541. this.data.bleStatus = false,
  2542. this.data.respond = []
  2543. },
  2544. closeblue1() {
  2545. // wx.showLoading({
  2546. // title: "正在停止...",
  2547. // });
  2548. app.globalData.ble.close()
  2549. // setTimeout(() => {
  2550. // wx.hideLoading()
  2551. // }, 1000);
  2552. this.data.info = ''
  2553. app.globalData.bleStatus = false
  2554. this.data.bleStatus = false,
  2555. this.data.respond = []
  2556. },
  2557. btntest3(event) {
  2558. var that = this
  2559. let deviceNum = that.data.deviceinfo.deviceNum
  2560. this.data.backstatus = true
  2561. that.startFp_29(deviceNum, 0x03)
  2562. // setTimeout(() => {
  2563. // that.getcartcount()
  2564. // }, 300)
  2565. },
  2566. btntest2(event) {
  2567. var that = this
  2568. let userTelephone = "18270949468"
  2569. let userPassword = "202108"
  2570. let pwd = "2021110309"
  2571. let openLockType = 2
  2572. var userInfo = wx.getStorageSync('userInfo')
  2573. var timestamp = moment().format("x")
  2574. bleall.openLock(userTelephone, userPassword, pwd, openLockType, timestamp, function (res) {
  2575. if (res.success) {
  2576. var rqTime = moment().format("X")
  2577. var username = wx.getStorageSync('username'),
  2578. password = wx.getStorageSync('password')
  2579. var str = `${username}+${password}+${rqTime}`
  2580. var signature = md5(str)
  2581. wx.hideLoading()
  2582. var data = {
  2583. code: "ld/bleUnlockRecord",
  2584. data: {
  2585. deviceNum: that.data.deviceNum,
  2586. userTelephone: userTelephone,
  2587. password: userPassword,
  2588. roomId: that.data.deviceinfo.roomId,
  2589. roomName: that.data.deviceinfo.roomName,
  2590. cardName: userInfo.username,
  2591. signature: signature
  2592. }
  2593. }
  2594. WXAPI.sendCommand(data).then(res => {
  2595. })
  2596. }
  2597. console.log(res.data)
  2598. if (res.data == "蓝牙功能暂不支持") {
  2599. wx.showToast({
  2600. title: '重新连接蓝牙中',
  2601. duration: 2000,
  2602. icon: "none"
  2603. })
  2604. that.startconnect()
  2605. }
  2606. })
  2607. },
  2608. openLock(userTelephone, userPassword, pwd, openLockType) {
  2609. wx.showLoading({
  2610. title: "正在发送...",
  2611. mask: true
  2612. });
  2613. setTimeout(() => {
  2614. wx.hideLoading()
  2615. }, 1000);
  2616. let cmd = 0x41
  2617. console.log(util, userTelephone)
  2618. let ut = util.stringToUint8Array(userTelephone)
  2619. let up = util.stringToUint8Array(userPassword)
  2620. let p = util.hexStringToBytesWithPadding(pwd, 10, 0xff)
  2621. let length = 1 + 11 + 6 + 10 + 1
  2622. let b = new Uint8Array(length)
  2623. b[0] = cmd
  2624. b.set(ut, 1)
  2625. b.set(up, 12)
  2626. b.set(p, 18)
  2627. b[28] = openLockType
  2628. console.log(b)
  2629. app.globalData.ble.send(cmd, b, length)
  2630. },
  2631. //远程开锁
  2632. remoteUnlocking() {
  2633. if (this.data.info == '蓝牙已连接') {
  2634. this.closeblue1()
  2635. }
  2636. if (this.data.deviceinfo.manufactureId == 101) {
  2637. var str2 = {
  2638. imei: this.data.deviceinfo.imei,
  2639. openLockType: 2,
  2640. is: true,
  2641. roomId: this.data.deviceinfo.roomId,
  2642. roomName: this.data.deviceinfo.roomName,
  2643. cardName: this.data.userInfo.username
  2644. };
  2645. WXAPI.openLock(str2).then(res => {
  2646. if (res.success) {
  2647. wx.showToast({
  2648. title: "已发送远程开门指令",
  2649. icon: "none",
  2650. duration: 2000
  2651. })
  2652. }
  2653. })
  2654. } else if (this.data.deviceinfo.manufactureId == 1201) {
  2655. var str2 = {
  2656. code: 'lwj/openLock',
  2657. data: {
  2658. lockerId: this.data.deviceinfo.deviceNum,
  2659. order: 6301,
  2660. is: true,
  2661. roomId: this.data.deviceinfo.roomId,
  2662. roomName: this.data.deviceinfo.roomName,
  2663. cardName: this.data.userInfo.username
  2664. }
  2665. };
  2666. WXAPI.sendCommand(str2).then(res => {
  2667. if (res.success) {
  2668. wx.showToast({
  2669. title: "已发送远程开门指令",
  2670. icon: "none",
  2671. duration: 2000
  2672. })
  2673. }
  2674. })
  2675. }
  2676. },
  2677. /**
  2678. * 生命周期函数--监听页面初次渲染完成
  2679. */
  2680. onReady: function () {
  2681. app.globalData.zgBlueStatus = false
  2682. },
  2683. AddressPermissions() {
  2684. wx.openBluetoothAdapter({
  2685. success: function (res) {
  2686. console.log('蓝牙已开启!');
  2687. },
  2688. fail: function (e) {
  2689. console.log(e)
  2690. wx.showToast({
  2691. title: '请打开蓝牙!',
  2692. duration: 2000,
  2693. icon: 'none'
  2694. })
  2695. }
  2696. });
  2697. },
  2698. getLocationPermissionMethods() {
  2699. ICINLocker.getLocationPermission().then((res) => {
  2700. if (res.status == 0) {
  2701. } else {
  2702. // 连接出现问题
  2703. }
  2704. }).catch((res) => {
  2705. //处理未能获得地理位置情况
  2706. wx.showToast({
  2707. title: '请打开微信定位功能!',
  2708. duration: 2000,
  2709. icon: 'none'
  2710. })
  2711. })
  2712. },
  2713. /**
  2714. * 生命周期函数--监听页面显示
  2715. */
  2716. onShow: function () {
  2717. var deviceinfo = wx.getStorageSync('deviceinfo')
  2718. var userInfo1 = wx.getStorageSync('userInfo')
  2719. if (deviceinfo.roomId) {
  2720. if (userInfo1.type != 3) {
  2721. var data2 = {
  2722. q: JSON.stringify({
  2723. id: deviceinfo.roomId
  2724. }),
  2725. orderBy: "createTime",
  2726. orderType: 'desc'
  2727. }
  2728. WXAPI.getSiteTenant(data2).then(res1 => {
  2729. if (res1.data.list.length > 0) {
  2730. var array = res1.data.list.reverse()
  2731. wx.setStorageSync('info', array[0])
  2732. deviceinfo.info = array[0].info
  2733. this.setData({
  2734. deviceinfo: deviceinfo
  2735. })
  2736. } else {
  2737. wx.showToast({
  2738. title: "还未下发人员,请先下发人员",
  2739. duration: 2000,
  2740. icon: 'none'
  2741. })
  2742. }
  2743. })
  2744. } else if (userInfo1.type == 3) {
  2745. this.setData({
  2746. deviceinfo: deviceinfo
  2747. })
  2748. }
  2749. } else {
  2750. wx.navigateTo({
  2751. url: "../index/index"
  2752. })
  2753. }
  2754. this.setData({
  2755. bleStatus: app.globalData.bleStatus
  2756. })
  2757. },
  2758. /**
  2759. * 生命周期函数--监听页面隐藏
  2760. */
  2761. /**
  2762. * 生命周期函数--监听页面卸载
  2763. */
  2764. onUnload: function () {
  2765. console.log(isConnected, connectedDeviceId)
  2766. if (isConnected) {
  2767. wx.closeBLEConnection({
  2768. deviceId: connectedDeviceId,
  2769. success: function (res) {
  2770. isConnected = false
  2771. },
  2772. })
  2773. }
  2774. // if (app.globalData.zgBlueStatus) {
  2775. // ICINLocker.disConnect(app.globalData.blueDeviceId);
  2776. // this.onReady()
  2777. // }
  2778. this.setData({
  2779. bluestatus: false,
  2780. toSearch: false
  2781. })
  2782. // wx.removeStorageSync("connectedDeviceId")
  2783. if (app.globalData.bleStatus) {
  2784. this.closeblue()
  2785. }
  2786. wx.hideLoading()
  2787. },
  2788. onHide() {
  2789. // wx.removeStorageSync("connectedDeviceId")
  2790. this.setData({
  2791. toSearch: false
  2792. })
  2793. wx.hideLoading()
  2794. if (app.globalData.bleStatus) {
  2795. this.closeblue()
  2796. }
  2797. },
  2798. /**
  2799. * 页面相关事件处理函数--监听用户下拉动作
  2800. */
  2801. onPullDownRefresh: function () {
  2802. // this.AddressPermissions()
  2803. var deviceinfo = wx.getStorageSync('deviceinfo')
  2804. app.globalData.deviceNum = deviceinfo.deviceNum
  2805. var userInfo1 = wx.getStorageSync('userInfo')
  2806. if (deviceinfo.manufactureId == 201 || deviceinfo.manufactureId == 221) {
  2807. var data3 = {
  2808. code: "zg/bleRegisterInfo",
  2809. data: {
  2810. lockerId: deviceinfo.deviceNum
  2811. }
  2812. }
  2813. WXAPI.sendCommand(data3).then(res1 => {
  2814. if (res1.success) {
  2815. wx.setStorageSync('cgInfo', res1.data)
  2816. this.setData({
  2817. cgInfo: res1.data
  2818. })
  2819. } else {
  2820. }
  2821. })
  2822. }
  2823. if (deviceinfo.roomId) {
  2824. if (userInfo1.type != 3) {
  2825. var data2 = {
  2826. q: JSON.stringify({
  2827. id: deviceinfo.roomId
  2828. }),
  2829. orderBy: "createTime",
  2830. orderType: 'desc'
  2831. }
  2832. WXAPI.getSiteTenant(data2).then(res1 => {
  2833. if (res1.data.list.length > 0) {
  2834. var array = res1.data.list.reverse()
  2835. wx.setStorageSync('info', array[0])
  2836. deviceinfo.info = array[0].info
  2837. this.setData({
  2838. deviceinfo: deviceinfo
  2839. })
  2840. } else {
  2841. wx.showToast({
  2842. title: "还未下发人员,请先下发人员",
  2843. duration: 2000,
  2844. icon: 'none'
  2845. })
  2846. }
  2847. })
  2848. } else if (userInfo1.type == 3) {
  2849. this.setData({
  2850. deviceinfo: deviceinfo
  2851. })
  2852. }
  2853. } else {
  2854. wx.navigateTo({
  2855. url: "../index/index"
  2856. })
  2857. }
  2858. this.setData({
  2859. info: app.globalData.bleStatus,
  2860. deviceinfo: deviceinfo
  2861. })
  2862. wx.stopPullDownRefresh()
  2863. },
  2864. /**
  2865. * 页面上拉触底事件的处理函数
  2866. */
  2867. onReachBottom: function () {
  2868. },
  2869. /**
  2870. * 用户点击右上角分享
  2871. */
  2872. onShareAppMessage: function () {
  2873. }
  2874. })