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.

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