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.

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