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.

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