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.

3003 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 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. }else if(event == 8){
  1161. wx.navigateTo({
  1162. url: '/pages/personManger/personManger',
  1163. })
  1164. }
  1165. wx.hideLoading()
  1166. } else {
  1167. that.goBlue()
  1168. }
  1169. } else if (deviceinfo.manufactureId == 101) {
  1170. if (app.globalData.bleStatus == '蓝牙已连接') {
  1171. if (event == 0) {
  1172. this.btntest2()
  1173. } else if (event == 3) {
  1174. this.btntest3()
  1175. } else if (event == 6) {
  1176. // this.setVoice()
  1177. this.setData({
  1178. show: true
  1179. })
  1180. } else if (event == 7) {
  1181. wx.showModal({
  1182. title: "常开设置",
  1183. cancelText: '关闭',
  1184. confirmText: '开启',
  1185. success: (res) => {
  1186. if (res.confirm) {
  1187. that.setOpen()
  1188. } else if (res.cancel) {
  1189. that.setOpen1()
  1190. }
  1191. }
  1192. })
  1193. }
  1194. } else {
  1195. this.startconnect()
  1196. }
  1197. } else if (deviceinfo.manufactureId == 1201 || deviceinfo.manufactureId == 1241) {
  1198. console.log("1202", this.data.isSearch, event);
  1199. if (this.data.isSearch) {
  1200. if (event == '0') {
  1201. // let iotLwj = new LwJ(this.data.lockDevice,isConnected,connectedDeviceId,isInitReady,this.data.lockDevice.lockModel)
  1202. // iotLwj.openLock(41)
  1203. this.LwjopenLock()
  1204. } else if (event == '6') {
  1205. this.setData({
  1206. show: true
  1207. })
  1208. }else if(event == '3') {
  1209. this.configurewifj()
  1210. }else if(event == '8') {
  1211. this.personnelmanagement()
  1212. }
  1213. } else {
  1214. this.searchDevicesMethods()
  1215. }
  1216. }
  1217. },
  1218. onQueryBindState: function () {
  1219. var that = this
  1220. wx.showLoading({
  1221. title: '查询中',
  1222. })
  1223. var bytes = plugin.queryLockState(lockDevice.name)
  1224. console.debug('bytes', bytes)
  1225. that.sendBytes(bytes)
  1226. },
  1227. searchDevicesMethods() {
  1228. var that = this;
  1229. wx.showLoading({
  1230. title: '正在搜索',
  1231. // mask: true
  1232. })
  1233. this.setData({
  1234. searchClick: false
  1235. })
  1236. wx.openBluetoothAdapter({
  1237. success: function (res) {
  1238. wx.startBluetoothDevicesDiscovery({
  1239. success: function (res) {
  1240. wx.getBluetoothDevices({
  1241. success: function (res) {
  1242. console.log(res)
  1243. res.devices.map(it => {
  1244. var status = it.name.indexOf('WSL')
  1245. if (status != -1) {
  1246. if (it.RSSI != 127) {
  1247. if (it.advertisData != null) {
  1248. it.adData = util1.ab2hex(it.advertisData)
  1249. if (it.adData.length > 10) {
  1250. var macStr = it.adData.substring(4, 16).toUpperCase()
  1251. //console.log('macStr', macStr)
  1252. 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)
  1253. if (it.adData.substring(1, 2) == '1') {
  1254. it.isLightOn = true
  1255. it.textColor = 'green'
  1256. } else {
  1257. it.isLightOn = false
  1258. it.textColor = 'black'
  1259. }
  1260. if (it.adData.substring(3, 4) == '1') {
  1261. it.textColor = 'pink'
  1262. it.isBind = true
  1263. } else {
  1264. it.isBind = false
  1265. }
  1266. }
  1267. } else {
  1268. it.textColor = 'black'
  1269. }
  1270. lockModel = util1.getLockModel(it.name)
  1271. it.lockType = util1.getLockType(lockModel)
  1272. if (it.name == that.data.deviceinfo.deviceNum) {
  1273. app.addDeviceWithDevName(it.name, it)
  1274. connectedDeviceId = it.deviceId
  1275. lockDevice = it
  1276. console.log(lockDevice, it, 'lockDevice,it')
  1277. app.globalData.isSearch = true
  1278. that.setData({
  1279. isSearch: true,
  1280. // lockDevice:it
  1281. })
  1282. // let iotLwj = new LwJ(it,isConnected,connectedDeviceId,isInitReady,it.lockModel)
  1283. // iotLwj.connectTo()
  1284. if (that.data.event == '0') {
  1285. // iotLwj.openLock(41)
  1286. that.LwjopenLock()
  1287. } else if (that.data.event == 6) {
  1288. that.setData({
  1289. show: true
  1290. })
  1291. } else if (that.data.event == '3') {
  1292. console.log("1298wifi");
  1293. that.configurewifj()
  1294. }else if(that.data.event == '8') {
  1295. console.log("1301person");
  1296. that.personnelmanagement()
  1297. }
  1298. wx.hideLoading({
  1299. success: (res) => {},
  1300. })
  1301. }
  1302. }
  1303. }
  1304. })
  1305. if (!that.data.isSearch && that.data.toSearch) {
  1306. that.searchDevicesMethods()
  1307. }
  1308. }
  1309. })
  1310. },
  1311. fail: function (res) {
  1312. wx.hideLoading({
  1313. success: (res) => {
  1314. wx.showToast({
  1315. title: res.errMsg,
  1316. duration: 2000,
  1317. icon: 'none'
  1318. })
  1319. },
  1320. })
  1321. }
  1322. })
  1323. },
  1324. fail: function (res) {
  1325. console.log('请打开蓝牙和定位功能')
  1326. wx.hideLoading({
  1327. success: (res) => {
  1328. wx.showToast({
  1329. title: '请打开蓝牙和定位功能',
  1330. duration: 2000,
  1331. icon: 'none'
  1332. })
  1333. },
  1334. })
  1335. }
  1336. })
  1337. },
  1338. LwjopenLock: function () {
  1339. var that = this
  1340. taskId = 41
  1341. that.initBluetooth()
  1342. },
  1343. configurewifj() {
  1344. var that = this
  1345. taskId = 121
  1346. that.initBluetooth()
  1347. },
  1348. // 人员管理
  1349. personnelmanagement() {
  1350. var that = this
  1351. taskId = 9010
  1352. that.initBluetooth()
  1353. },
  1354. LwjonOpenLock: function () {
  1355. var that = this
  1356. wx.showLoading({
  1357. title: that.data.isJack ? '上电中' : '解锁中',
  1358. })
  1359. console.log(lockDevice.name, 'lockDevice.name0开锁中')
  1360. var bytes = plugin.sendOpenLockP1(lockDevice.name, basecode)
  1361. console.debug('bytes', bytes)
  1362. that.sendBytes(bytes)
  1363. },
  1364. initBluetooth: function () {
  1365. var that = this
  1366. wx.getConnectedBluetoothDevices({
  1367. services: [config.uuid.serviceUuid],
  1368. success: function (res) {
  1369. res.devices.map(function (it) {
  1370. if (it.deviceId == connectedDeviceId || it.name == lockDevice.name) {
  1371. isConnected = true
  1372. }
  1373. })
  1374. that.setData({
  1375. isConnected: isConnected
  1376. })
  1377. console.log('isConnected', isConnected)
  1378. if (isConnected) {
  1379. if (isInitReady) {
  1380. console.log(isInitReady, 'isInitReady', taskId)
  1381. setTimeout(() => {
  1382. that.doWork()
  1383. }, 500)
  1384. } else {
  1385. that.setupConnection()
  1386. }
  1387. } else {
  1388. that.connectTo()
  1389. }
  1390. },
  1391. })
  1392. },
  1393. doWork: function () {
  1394. console.log(taskId);
  1395. var that = this
  1396. switch (taskId) {
  1397. case 11: {
  1398. that.onBindLock()
  1399. break
  1400. }
  1401. case 12: {
  1402. that.onUnbindLock()
  1403. break
  1404. }
  1405. case 21: {
  1406. that.onQueryBindState()
  1407. break
  1408. }
  1409. case 22: {
  1410. that.onQueryBattery()
  1411. break
  1412. }
  1413. case 31: {
  1414. that.onQueryNbImei()
  1415. break
  1416. }
  1417. case 32: {
  1418. that.onLogin()
  1419. break
  1420. }
  1421. case 41: {
  1422. that.LwjonOpenLock()
  1423. break
  1424. }
  1425. case 42: {
  1426. that.onSyncClock()
  1427. break
  1428. }
  1429. case 51: {
  1430. that.onAddPincode()
  1431. break
  1432. }
  1433. case 52: {
  1434. that.onDelPincode()
  1435. break
  1436. }
  1437. case 61: {
  1438. if (lockModel > 70) {
  1439. that.onAddRfCard()
  1440. } else {
  1441. wx.showModal({
  1442. title: '提示',
  1443. content: '请刷卡!',
  1444. showCancel: false
  1445. })
  1446. }
  1447. break
  1448. }
  1449. case 62: {
  1450. that.onDelRfCard()
  1451. break
  1452. }
  1453. case 71: {
  1454. that.onAddFingerprint()
  1455. break
  1456. }
  1457. case 72: {
  1458. that.onDelFingerprint()
  1459. break
  1460. }
  1461. case 81: {
  1462. that.onChangeAdminPincode()
  1463. break
  1464. }
  1465. case 91: {
  1466. that.onQueryLockSlotState()
  1467. break
  1468. }
  1469. case 92: {
  1470. that.onQueryLockUnlockState()
  1471. break
  1472. }
  1473. case 101: {
  1474. that.onChangeLockUnlockState()
  1475. break
  1476. }
  1477. case 102: {
  1478. that.onChangeLockMutedState()
  1479. break
  1480. }
  1481. case 111: {
  1482. this.onChangeLockNbState()
  1483. }
  1484. case 121: {
  1485. this.onConfigurewifi()
  1486. break
  1487. }
  1488. case 9010: {
  1489. this.gotoperson()
  1490. break
  1491. }
  1492. default: {
  1493. }
  1494. }
  1495. },
  1496. connectTo: function () {
  1497. var that = this
  1498. wx.showLoading({
  1499. title: '连接中',
  1500. })
  1501. wx.createBLEConnection({
  1502. deviceId: lockDevice.deviceId,
  1503. timeout: config.ble.connectTimeOut,
  1504. success: function (res) {
  1505. console.debug('connectTo', res)
  1506. isConnected = true
  1507. wx.hideLoading()
  1508. setTimeout(function () {
  1509. that.setupConnection()
  1510. }, 500)
  1511. },
  1512. fail: function (e) {
  1513. console.debug('connectTo', e)
  1514. wx.hideLoading()
  1515. switch (e.errCode) {
  1516. case 10003: {
  1517. wx.showModal({
  1518. title: '连接失败',
  1519. content: '是否重新连接?',
  1520. success: function (res) {
  1521. if (res.confirm) {
  1522. that.connectTo()
  1523. }
  1524. }
  1525. })
  1526. break
  1527. }
  1528. case 10012: {
  1529. wx.showModal({
  1530. title: '连接失败',
  1531. content: '蓝牙连接超时',
  1532. showCancel: false
  1533. })
  1534. break
  1535. }
  1536. case 10002: {
  1537. wx.showModal({
  1538. title: '连接失败',
  1539. content: '没有找到指定设备',
  1540. showCancel: false
  1541. })
  1542. break
  1543. }
  1544. }
  1545. }
  1546. })
  1547. },
  1548. setupConnection: function () {
  1549. var that = this
  1550. wx.showLoading({
  1551. title: '初始化连接中',
  1552. })
  1553. wx.getBLEDeviceServices({
  1554. deviceId: lockDevice.deviceId,
  1555. success: function (res) {
  1556. console.log('Services', res)
  1557. wx.getBLEDeviceCharacteristics({
  1558. deviceId: lockDevice.deviceId,
  1559. serviceId: config.uuid.serviceUuid.toUpperCase(),
  1560. success: function (res) {
  1561. console.log('Characteristics', res)
  1562. wx.showLoading({
  1563. title: '连接成功',
  1564. })
  1565. wx.onBLECharacteristicValueChange(function (res) {
  1566. var bytes = new Uint8Array(res.value)
  1567. console.log('bytes', bytes)
  1568. console.log(lockDevice.name, 'lockDevice.name1')
  1569. var data = plugin.parseBytes(lockDevice.name, basecode, bytes)
  1570. console.log('data', data)
  1571. switch (data.cmd) {
  1572. case 'reportLockBattery': {
  1573. if (data.data.battery < 10) {
  1574. wx.showModal({
  1575. title: '电量报警',
  1576. content: `门锁当前电量${data.data.battery}%!\n电量严重不足,请立即更换电池!`,
  1577. showCancel: false
  1578. })
  1579. } else if (data.data.battery < 30) {
  1580. wx.showModal({
  1581. title: '电量预警',
  1582. content: `门锁当前电量${data.data.battery}%!\n请及时更换电池!`,
  1583. showCancel: false
  1584. })
  1585. }
  1586. break
  1587. }
  1588. case 'reportRfCardResult': {
  1589. if (taskId == 61) {
  1590. wx.showModal({
  1591. title: '检测到新卡',
  1592. content: `是否添加此房卡,卡号:${data.data.cardId}`,
  1593. success: function (res) {
  1594. if (res.confirm) {
  1595. rfCardId = data.data.cardId
  1596. that.onAddRfCard()
  1597. }
  1598. }
  1599. })
  1600. } else {
  1601. if (lockModel > 70) {
  1602. if (data.data.isValid) {
  1603. wx.showModal({
  1604. title: '提示',
  1605. content: `房卡开锁成功,序号:${data.data.index}`,
  1606. showCancel: false
  1607. })
  1608. } else {
  1609. wx.showModal({
  1610. title: '提示',
  1611. content: `多次刷房卡开锁失败!`,
  1612. showCancel: false
  1613. })
  1614. }
  1615. } else {
  1616. if (data.data.isValid) {
  1617. wx.showModal({
  1618. title: '提示',
  1619. content: `房卡开锁成功,卡号:${data.data.cardId}`,
  1620. showCancel: false
  1621. })
  1622. } else {
  1623. wx.showModal({
  1624. title: '提示',
  1625. content: `房卡未授权,卡号:${data.data.cardId}`,
  1626. showCancel: false
  1627. })
  1628. }
  1629. }
  1630. }
  1631. break
  1632. }
  1633. case 'reportPincodeResult': {
  1634. if (lockModel > 70) {
  1635. if (data.data.isValid) {
  1636. wx.showModal({
  1637. title: '提示',
  1638. content: `密码开锁成功,序号:${data.data.index}`,
  1639. showCancel: false
  1640. })
  1641. } else {
  1642. wx.showModal({
  1643. title: '提示',
  1644. content: `多次密码开锁失败!`,
  1645. showCancel: false
  1646. })
  1647. }
  1648. } else {
  1649. if (data.data.isValid) {
  1650. wx.showModal({
  1651. title: '提示',
  1652. content: `密码:${data.data.pincode}开锁成功!`,
  1653. showCancel: false
  1654. })
  1655. } else {
  1656. wx.showModal({
  1657. title: '提示',
  1658. content: `密码:${data.data.pincode}未授权!`,
  1659. showCancel: false
  1660. })
  1661. }
  1662. }
  1663. break
  1664. }
  1665. case 'reportFingerprintResult': {
  1666. if (data.data.isValid) {
  1667. wx.showModal({
  1668. title: '提示',
  1669. content: `指纹开锁成功,序号:${data.data.index}`,
  1670. showCancel: false
  1671. })
  1672. } else {
  1673. wx.showModal({
  1674. title: '提示',
  1675. content: `多次指纹开锁失败!`,
  1676. showCancel: false
  1677. })
  1678. }
  1679. break
  1680. }
  1681. case 'queryLockState': {
  1682. wx.hideLoading()
  1683. if (data.code != 200) {
  1684. wx.showModal({
  1685. title: '提示',
  1686. content: '查询失败!',
  1687. showCancel: false,
  1688. success: function (res) {}
  1689. })
  1690. break
  1691. }
  1692. that.setData({
  1693. lock: {
  1694. mac: data.data.mac,
  1695. lockType: util1.getLockType(lockModel),
  1696. },
  1697. isBindLock: data.data.isBind
  1698. })
  1699. lockMac = data.data.mac
  1700. if (taskId > 0) {
  1701. if (data.data.isBind) {
  1702. wx.showModal({
  1703. title: data.data.mac,
  1704. content: '此设备已经硬件绑定!',
  1705. showCancel: false,
  1706. success: function (res) {}
  1707. })
  1708. } else {
  1709. wx.showModal({
  1710. title: data.data.mac,
  1711. content: '此设备硬件未绑定!',
  1712. showCancel: false,
  1713. success: function (res) {}
  1714. })
  1715. }
  1716. }
  1717. taskId = 0
  1718. break
  1719. }
  1720. case 'queryLockBattery': {
  1721. wx.hideLoading()
  1722. if (data.code != 200) {
  1723. wx.showModal({
  1724. title: '提示',
  1725. content: '查询失败!',
  1726. showCancel: false,
  1727. success: function (res) {}
  1728. })
  1729. break
  1730. }
  1731. taskId = 0
  1732. wx.showModal({
  1733. title: '提示',
  1734. content: `此门锁电量为${data.data.battery}%`,
  1735. showCancel: false,
  1736. success: function (res) {}
  1737. })
  1738. break
  1739. }
  1740. case 'queryNbImei': {
  1741. wx.hideLoading()
  1742. if (data.code != 200) {
  1743. wx.showModal({
  1744. title: '提示',
  1745. content: '查询失败!',
  1746. showCancel: false,
  1747. success: function (res) {}
  1748. })
  1749. break
  1750. }
  1751. taskId = 0
  1752. wx.showModal({
  1753. title: '提示',
  1754. content: `此NB设备IMEI为${data.data.imei}`,
  1755. showCancel: false,
  1756. success: function (res) {}
  1757. })
  1758. break
  1759. }
  1760. case 'setNbFun': {
  1761. wx.hideLoading()
  1762. if (data.code != 200) {
  1763. wx.showModal({
  1764. title: '提示',
  1765. content: '设置Nb功能失败!',
  1766. showCancel: false,
  1767. success: function (res) {}
  1768. })
  1769. break
  1770. }
  1771. taskId = 0
  1772. wx.showModal({
  1773. title: '提示',
  1774. content: that.data.isNbEnable ? 'NB功能已禁用' : 'NB功能已打开',
  1775. showCancel: false,
  1776. })
  1777. that.setData({
  1778. isNbEnable: !that.data.isNbEnable
  1779. })
  1780. break
  1781. }
  1782. case 'sendBindLock': {
  1783. wx.hideLoading()
  1784. if (data.code != 200) {
  1785. wx.showModal({
  1786. title: '提示',
  1787. content: '绑定失败!',
  1788. showCancel: false,
  1789. success: function (res) {}
  1790. })
  1791. break
  1792. }
  1793. taskId = 0
  1794. that.setData({
  1795. isBindLock: data.data.isBind
  1796. })
  1797. if (data.data.isBind) {
  1798. wx.showToast({
  1799. title: '绑定成功!',
  1800. })
  1801. } else {
  1802. wx.showToast({
  1803. title: '绑定失败!',
  1804. })
  1805. }
  1806. break
  1807. }
  1808. case 'sendUnbindLock': {
  1809. wx.hideLoading()
  1810. if (data.code != 200) {
  1811. wx.showModal({
  1812. title: '提示',
  1813. content: '解绑失败!',
  1814. showCancel: false,
  1815. success: function (res) {}
  1816. })
  1817. break
  1818. }
  1819. taskId = 0
  1820. that.setData({
  1821. isBindLock: data.data.isBind
  1822. })
  1823. if (data.data.isBind) {
  1824. wx.showToast({
  1825. title: '解绑失败!',
  1826. })
  1827. } else {
  1828. wx.showToast({
  1829. title: '解绑成功!',
  1830. })
  1831. }
  1832. break
  1833. }
  1834. case 'login1':
  1835. case 'sendOpenLockP1': {
  1836. if (data.code != 200) {
  1837. wx.hideLoading()
  1838. if (taskId == 41) {
  1839. wx.showModal({
  1840. title: '提示',
  1841. content: that.data.isJack ? '上电失败!' : '解锁失败!',
  1842. showCancel: false,
  1843. success: function (res) {}
  1844. })
  1845. } else if (taskId == 32) {
  1846. wx.showModal({
  1847. title: '提示',
  1848. content: '登录失败!',
  1849. showCancel: false,
  1850. success: function (res) {}
  1851. })
  1852. }
  1853. break
  1854. }
  1855. if (taskId == 41) {
  1856. console.log(lockDevice.name, 'lockDevice.name2')
  1857. var bytes = plugin.sendOpenLockP2(lockDevice.name, basecode, data.data.randomN)
  1858. console.debug('bytes', bytes)
  1859. that.sendBytes(bytes)
  1860. } else if (taskId == 32) {
  1861. console.log(lockDevice.name, 'lockDevice.name3')
  1862. var bytes = plugin.login2(lockDevice.name, basecode, data.data.randomN)
  1863. console.debug('bytes', bytes)
  1864. that.sendBytes(bytes)
  1865. }
  1866. break;
  1867. }
  1868. case 'login':
  1869. case 'sendOpenLockP2': {
  1870. wx.hideLoading()
  1871. if (data.code != 200) {
  1872. if (taskId == 41) {
  1873. wx.showModal({
  1874. title: '提示',
  1875. content: that.data.isJack ? '上电失败!' : '解锁失败!',
  1876. showCancel: false,
  1877. success: function (res) {}
  1878. })
  1879. } else if (taskId == 32) {
  1880. wx.showModal({
  1881. title: '提示',
  1882. content: '登录失败!',
  1883. showCancel: false,
  1884. success: function (res) {}
  1885. })
  1886. }
  1887. break
  1888. }
  1889. isLockLogin = true
  1890. if (taskId == 41) {
  1891. var data = {
  1892. code: "ble/bleUnlockRecord",
  1893. data: {
  1894. deviceNum: that.data.deviceinfo.deviceNum,
  1895. roomId: that.data.deviceinfo.roomId,
  1896. roomName: that.data.deviceinfo.roomName,
  1897. cardName: that.data.userInfo.username,
  1898. productId: that.data.deviceinfo.manufactureId,
  1899. userId: '29283991'
  1900. }
  1901. }
  1902. WXAPI.sendCommand(data).then(res => {
  1903. console.log(res)
  1904. })
  1905. wx.showToast({
  1906. title: that.data.isJack ? '上电成功!' : '解锁成功!',
  1907. })
  1908. } else if (taskId == 32) {
  1909. wx.showToast({
  1910. title: '登录成功!',
  1911. })
  1912. }
  1913. taskId = 0
  1914. break;
  1915. }
  1916. case 'syncClock': {
  1917. wx.hideLoading()
  1918. if (data.code != 200) {
  1919. wx.showModal({
  1920. title: '提示',
  1921. content: '同步时间失败!' + data.data.msg,
  1922. showCancel: false,
  1923. success: function (res) {}
  1924. })
  1925. break
  1926. }
  1927. taskId = 0
  1928. wx.showModal({
  1929. title: '提示',
  1930. content: '时间更新成功!',
  1931. showCancel: false
  1932. })
  1933. break;
  1934. }
  1935. case 'addPincode': {
  1936. wx.hideLoading()
  1937. if (data.code != 200) {
  1938. wx.showModal({
  1939. title: '提示',
  1940. content: '添加密码失败!' + data.data.msg,
  1941. showCancel: false,
  1942. success: function (res) {}
  1943. })
  1944. break
  1945. }
  1946. taskId = 0
  1947. that.setData({
  1948. isPincodeAdd: true
  1949. })
  1950. wx.showModal({
  1951. title: '提示',
  1952. content: `添加密码${pincode}成功!`,
  1953. showCancel: false
  1954. })
  1955. break
  1956. }
  1957. case 'delPincode': {
  1958. wx.hideLoading()
  1959. if (data.code != 200) {
  1960. wx.showModal({
  1961. title: '提示',
  1962. content: '删除密码失败!',
  1963. showCancel: false,
  1964. success: function (res) {}
  1965. })
  1966. break
  1967. }
  1968. taskId = 0
  1969. wx.showModal({
  1970. title: '提示',
  1971. content: `删除密码${pincode}成功!`,
  1972. showCancel: false,
  1973. })
  1974. that.setData({
  1975. isPincodeAdd: false
  1976. })
  1977. pincode = -1
  1978. pincodeIndex = -1
  1979. break
  1980. }
  1981. case 'addRfCard': {
  1982. wx.hideLoading()
  1983. if (data.code == 100) {
  1984. wx.showLoading({
  1985. title: data.data.msg,
  1986. })
  1987. break
  1988. } else if (data.code != 200) {
  1989. wx.showModal({
  1990. title: '提示',
  1991. content: '添加房卡失败!' + data.data.msg,
  1992. showCancel: false,
  1993. success: function (res) {}
  1994. })
  1995. break
  1996. }
  1997. taskId = 0
  1998. that.setData({
  1999. isRfCardAdd: true
  2000. })
  2001. if (lockModel > 70) {
  2002. wx.showModal({
  2003. title: '提示',
  2004. content: `添加房卡成功,序号${rfCardIndex}`,
  2005. showCancel: false
  2006. })
  2007. } else {
  2008. wx.showModal({
  2009. title: '提示',
  2010. content: `添加房卡成功,卡号${rfCardId}`,
  2011. showCancel: false
  2012. })
  2013. }
  2014. break
  2015. }
  2016. case 'delRfCard': {
  2017. wx.hideLoading()
  2018. if (data.code != 200) {
  2019. wx.showModal({
  2020. title: '提示',
  2021. content: '删除房卡失败!',
  2022. showCancel: false,
  2023. success: function (res) {}
  2024. })
  2025. break
  2026. }
  2027. taskId = 0
  2028. that.setData({
  2029. isRfCardAdd: false
  2030. })
  2031. if (lockModel > 70) {
  2032. wx.showModal({
  2033. title: '提示',
  2034. content: `删除房卡成功,序号${rfCardIndex}`,
  2035. showCancel: false,
  2036. })
  2037. } else {
  2038. wx.showModal({
  2039. title: '提示',
  2040. content: `删除房卡成功,卡号${rfCardId}`,
  2041. showCancel: false,
  2042. })
  2043. }
  2044. rfCardId = -1
  2045. rfCardIndex = -1
  2046. break
  2047. }
  2048. case 'addFingerprint': {
  2049. wx.hideLoading()
  2050. if (data.code == 100) {
  2051. wx.showLoading({
  2052. title: data.data.msg,
  2053. })
  2054. break
  2055. } else if (data.code != 200) {
  2056. wx.showModal({
  2057. title: '提示',
  2058. content: '添加指纹失败!' + data.data.msg,
  2059. showCancel: false,
  2060. success: function (res) {}
  2061. })
  2062. break
  2063. }
  2064. taskId = 0
  2065. that.setData({
  2066. isFingerprintAdd: true
  2067. })
  2068. wx.showModal({
  2069. title: '提示',
  2070. content: `添加指纹成功,序号${fingerprintIndex}`,
  2071. showCancel: false
  2072. })
  2073. break
  2074. }
  2075. case 'delFingerprint': {
  2076. wx.hideLoading()
  2077. if (data.code != 200) {
  2078. wx.showModal({
  2079. title: '提示',
  2080. content: '删除指纹失败!',
  2081. showCancel: false,
  2082. success: function (res) {}
  2083. })
  2084. break
  2085. }
  2086. taskId = 0
  2087. wx.showModal({
  2088. title: '提示',
  2089. content: `删除指纹成功,序号${fingerprintIndex}`,
  2090. showCancel: false,
  2091. })
  2092. that.setData({
  2093. isFingerprintAdd: false
  2094. })
  2095. fingerprintIndex = -1
  2096. break
  2097. }
  2098. case 'changeAdminPincode': {
  2099. wx.hideLoading()
  2100. if (data.code != 200) {
  2101. wx.showModal({
  2102. title: '提示',
  2103. content: '修改管理密码失败!',
  2104. showCancel: false,
  2105. success: function (res) {}
  2106. })
  2107. break
  2108. }
  2109. taskId = 0
  2110. wx.showToast({
  2111. title: '修改管理密码成功!',
  2112. })
  2113. }
  2114. case 'queryLockSlotState': {
  2115. wx.hideLoading()
  2116. if (data.code != 200) {
  2117. wx.showModal({
  2118. title: '提示',
  2119. content: '查询失败!',
  2120. showCancel: false,
  2121. success: function (res) {}
  2122. })
  2123. break
  2124. }
  2125. taskId = 0
  2126. wx.showModal({
  2127. title: '提示',
  2128. content: data.data.state == 0 ? '此门锁方舌关闭' : '此门锁方舌打开',
  2129. showCancel: false,
  2130. success: function (res) {}
  2131. })
  2132. break
  2133. }
  2134. case 'queryLockUnlockState': {
  2135. wx.hideLoading()
  2136. if (data.code != 200) {
  2137. wx.showModal({
  2138. title: '提示',
  2139. content: '查询失败!',
  2140. showCancel: false,
  2141. success: function (res) {}
  2142. })
  2143. break
  2144. }
  2145. taskId = 0
  2146. wx.showModal({
  2147. title: '提示',
  2148. content: data.data.state == 0 ? '此门锁已关闭' : '此门锁已打开',
  2149. showCancel: false,
  2150. success: function (res) {}
  2151. })
  2152. break
  2153. }
  2154. case 'setLockUnlockState': {
  2155. wx.hideLoading()
  2156. if (data.code != 200) {
  2157. wx.showModal({
  2158. title: '提示',
  2159. content: '设置常开指令失败!',
  2160. showCancel: false,
  2161. success: function (res) {}
  2162. })
  2163. break
  2164. }
  2165. taskId = 0
  2166. wx.showModal({
  2167. title: '提示',
  2168. content: data.data.state == 0 ? '常开模式已退出' : '常开模式已开启',
  2169. showCancel: false,
  2170. })
  2171. that.setData({
  2172. isUnloked: data.data.state == 1
  2173. })
  2174. break
  2175. }
  2176. case 'setLockMuteState': {
  2177. wx.hideLoading()
  2178. if (data.code != 200) {
  2179. wx.showModal({
  2180. title: '提示',
  2181. content: '设置静音指令失败!',
  2182. showCancel: false,
  2183. success: function (res) {}
  2184. })
  2185. break
  2186. }
  2187. taskId = 0
  2188. wx.showModal({
  2189. title: '提示',
  2190. content: data.data.state == 1 ? '静音模式已退出' : '静音模式已开启',
  2191. showCancel: false,
  2192. })
  2193. that.setData({
  2194. isMuted: data.data.state == 0
  2195. })
  2196. break
  2197. }
  2198. }
  2199. })
  2200. wx.notifyBLECharacteristicValueChange({
  2201. deviceId: lockDevice.deviceId,
  2202. serviceId: config.uuid.serviceUuid.toUpperCase(),
  2203. characteristicId: config.uuid.notifyUuid.toUpperCase(),
  2204. state: true,
  2205. success: function (res) {
  2206. console.log('setupNotify', res)
  2207. isInitReady = true
  2208. setTimeout(function () {
  2209. wx.hideLoading()
  2210. // that.onQueryBindState()
  2211. // if (taskId == '41' && that.data.event == '0') {
  2212. // that.doWork()
  2213. // }else if(taskId == '121' && that.data.event == '3') {
  2214. // that.doWork()
  2215. // }else if(taskId == '9010' && that.data.event == '8') {
  2216. // that.doWork()
  2217. // }
  2218. that.doWork()
  2219. }, 500)
  2220. },
  2221. })
  2222. },
  2223. })
  2224. },
  2225. })
  2226. },
  2227. addPincode: function () {
  2228. var that = this
  2229. taskId = 51
  2230. that.initBluetooth()
  2231. },
  2232. onAddPincode: function () {
  2233. var that = this
  2234. wx.showLoading({
  2235. title: '添加密码中',
  2236. })
  2237. var startTime = util1.getLocalTime(8)
  2238. var endTime = new Date(startTime.getTime() + 5 * 60 * 1000) //有效期5分钟
  2239. pincode = 949468 //util1.randomPrime6()
  2240. pincodeIndex = 22 //Math.floor(Math.random() * 100)
  2241. var bytes = plugin.addPincode(lockDevice.name, basecode, pincode, pincodeIndex, startTime, endTime)
  2242. console.debug('bytes', bytes)
  2243. that.sendBytes(bytes)
  2244. },
  2245. sendBytes(bytes) {
  2246. console.log(lockDevice, config, bytes, 'sendBytes---')
  2247. setTimeout(function () {
  2248. wx.writeBLECharacteristicValue({
  2249. deviceId: lockDevice.deviceId,
  2250. serviceId: config.uuid.serviceUuid.toUpperCase(),
  2251. characteristicId: config.uuid.writeUuid.toUpperCase(),
  2252. value: bytes,
  2253. success: function (res) {
  2254. },
  2255. })
  2256. }, 50)
  2257. },
  2258. goBlue() {
  2259. var that = this
  2260. wx.showLoading({
  2261. title: '搜索设备中',
  2262. mask: true
  2263. })
  2264. // wx.removeStorageSync("connectedDeviceId")
  2265. ICINLocker.scanDevices(this.data.deviceinfo.deviceNum)
  2266. .then((res) => {
  2267. if (res.status === 0) {
  2268. var deviceId = res.deviceId
  2269. //成功找到蓝牙情况下进行锁连接
  2270. app.globalData.blueDeviceId = res.deviceId
  2271. ICINLocker.connect(res.deviceId, false) // 此处 deviceId 存起来,当断开连接时使用
  2272. .then((res) => {
  2273. if (res.status === 0) {
  2274. clearInterval(that.data.timer)
  2275. this.setData({
  2276. bleStatus: '蓝牙已连接',
  2277. deviceId: deviceId,
  2278. isSearch: true
  2279. })
  2280. app.globalData.zgBlueStatus = true
  2281. wx.hideLoading()
  2282. wx.showToast({
  2283. title: '连接成功',
  2284. icon: "none",
  2285. duration: 2000
  2286. })
  2287. setTimeout(() => {
  2288. if (this.data.event === 0) {
  2289. this.openTheDoor()
  2290. } else if (this.data.event == 3) {
  2291. this.addFingerPrints()
  2292. } else if (this.data.event == 6) {
  2293. this.setData({
  2294. show: true
  2295. })
  2296. } else if (this.data.event == 11) {
  2297. this.synchroPlatformTask()
  2298. }
  2299. }, 500)
  2300. } else {
  2301. wx.hideLoading()
  2302. var msg = ''
  2303. switch (res.errCode) {
  2304. case 10001:
  2305. msg = '调用失败,请打开蓝牙!';
  2306. break;
  2307. }
  2308. wx.showToast({
  2309. title: msg,
  2310. duration: 2000,
  2311. icon: 'none'
  2312. })
  2313. //此处处理返回值不为成功(0)的情况
  2314. }
  2315. }).catch((res) => {
  2316. wx.hideLoading()
  2317. console.log(res, '失败')
  2318. // 此处处理未能连接成功问题
  2319. })
  2320. }
  2321. })
  2322. .catch((res) => {
  2323. //此处处理未能连接到蓝牙的问题
  2324. console.log(res)
  2325. if (res.status == 1) {
  2326. if (res.statusReason.errCode == 10001) {
  2327. wx.hideLoading()
  2328. wx.showToast({
  2329. title: '请检查手机蓝牙和定位功能是否打开?',
  2330. duration: 2000,
  2331. icon: 'none'
  2332. })
  2333. } else {
  2334. if (!app.globalData.zgBlueStatus && that.data.bluestatus) {
  2335. that.goBlue()
  2336. } else {
  2337. wx.showToast({
  2338. title: res.statusReason,
  2339. duration: 2000,
  2340. icon: 'none'
  2341. })
  2342. wx.hideLoading()
  2343. }
  2344. }
  2345. } else {
  2346. wx.hideLoading()
  2347. }
  2348. })
  2349. },
  2350. /**
  2351. * *设置音量
  2352. * @param {门锁ID} _lockId
  2353. * @param {用户ID 最多20字节} _userId
  2354. * @param {r 声音选项 00: 静音 01: 低音 02高音} _action
  2355. * @param _commonKey
  2356. * @param {回调方法} callback
  2357. **/
  2358. // 设置音量
  2359. setVolume1(volume) {
  2360. wx.showLoading({
  2361. title: "正在发送...",
  2362. mask: true
  2363. });
  2364. setTimeout(() => {
  2365. wx.hideLoading()
  2366. }, 1000);
  2367. let cmd = 0xF2
  2368. // let dn = util.stringToUint8ArrayWithPadding(deviceNum, 20, 0xff)
  2369. let length = 2
  2370. let b = new Uint8Array(length)
  2371. b[0] = cmd
  2372. b[1] = volume;
  2373. app.globalData.ble.send(cmd, b, function (res) {
  2374. })
  2375. var dataOpenReport = {
  2376. code: "ble/volumeReport",
  2377. data: {
  2378. deviceNum: this.data.deviceinfo.deviceNum,
  2379. operate: parseInt(volume),
  2380. order: 61001
  2381. }
  2382. }
  2383. WXAPI.sendCommand(dataOpenReport).then(res => {
  2384. })
  2385. },
  2386. setVolume(volume) {
  2387. ICINLocker.getSignKey(this.data.cgInfo.lockerId, this.data.cgInfo.superId, this.data.cgInfo.superId,
  2388. (callback) => {
  2389. console.log(callback, '-----')
  2390. if (callback.status == '00') {
  2391. ICINLocker.getCommonKey(this.data.cgInfo.lockerId, this.data.cgInfo.superId, this.data.cgInfo.superId, callback.signKey, (callback1) => {
  2392. console.log(callback1, '2222222')
  2393. if (callback1.status == '00') {
  2394. ICINLocker.setVolume(this.data.cgInfo.lockerId, this.data.cgInfo.superId, volume, callback1.commonKey, (callback2) => {
  2395. console.log(callback2, '333333333')
  2396. if (callback2.status == '00') {
  2397. var dataOpenReport = {
  2398. code: "ble/volumeReport",
  2399. data: {
  2400. deviceNum: this.data.deviceinfo.deviceNum,
  2401. operate: parseInt(volume),
  2402. order: 61001
  2403. }
  2404. }
  2405. WXAPI.sendCommand(dataOpenReport).then(res => {
  2406. })
  2407. wx.hideLoading()
  2408. wx.showToast({
  2409. title: '设置成功',
  2410. duration: 2000,
  2411. icon: 'none'
  2412. })
  2413. } else {
  2414. wx.hideLoading()
  2415. wx.showToast({
  2416. title: '设置失败',
  2417. duration: 2000,
  2418. icon: 'none'
  2419. })
  2420. }
  2421. })
  2422. } else {
  2423. wx.hideLoading()
  2424. wx.showToast({
  2425. title: '设置失败',
  2426. duration: 2000,
  2427. icon: 'none'
  2428. })
  2429. }
  2430. })
  2431. } else {
  2432. wx.hideLoading()
  2433. wx.showToast({
  2434. title: '设置失败',
  2435. duration: 2000,
  2436. icon: 'none'
  2437. })
  2438. }
  2439. });
  2440. },
  2441. //设置常开
  2442. setOpen() {
  2443. wx.showLoading({
  2444. title: "正在开启...",
  2445. mask: true
  2446. });
  2447. setTimeout(() => {
  2448. wx.hideLoading()
  2449. }, 1000);
  2450. let cmd = 0xF3
  2451. // let dn = util.stringToUint8ArrayWithPadding(deviceNum, 20, 0xff)
  2452. let length = 2
  2453. let b = new Uint8Array(length)
  2454. b[0] = cmd
  2455. b[1] = 1;
  2456. app.globalData.ble.send(cmd, b, function (res) {})
  2457. var dataOpenReport = {
  2458. code: "ble/alwaysOpenReport",
  2459. data: {
  2460. deviceNum: this.data.deviceinfo.deviceNum,
  2461. roomName: this.data.deviceinfo.roomName,
  2462. operate: 1,
  2463. order: 61101
  2464. }
  2465. }
  2466. WXAPI.sendCommand(dataOpenReport).then(res => {
  2467. })
  2468. },
  2469. setOpen1() {
  2470. wx.showLoading({
  2471. title: "正在关闭...",
  2472. mask: true
  2473. });
  2474. setTimeout(() => {
  2475. wx.hideLoading()
  2476. }, 1000);
  2477. let cmd = 0xF3
  2478. // let dn = util.stringToUint8ArrayWithPadding(deviceNum, 20, 0xff)
  2479. let length = 2
  2480. let b = new Uint8Array(length)
  2481. b[0] = cmd
  2482. b[1] = 0;
  2483. app.globalData.ble.send(cmd, b, function (res) {})
  2484. var dataOpenReport = {
  2485. code: "ble/alwaysOpenReport",
  2486. data: {
  2487. deviceNum: this.data.deviceinfo.deviceNum,
  2488. roomName: this.data.deviceinfo.roomName,
  2489. operate: 0,
  2490. order: 61101
  2491. }
  2492. }
  2493. WXAPI.sendCommand(dataOpenReport).then(res => {
  2494. })
  2495. },
  2496. //设置音量
  2497. setVoice() {
  2498. },
  2499. startconnect() {
  2500. //if(checkWechatVersion())
  2501. // {
  2502. wx.showLoading({
  2503. title: "连接蓝牙中",
  2504. mask: true
  2505. })
  2506. var deviceinfo = this.data.deviceinfo
  2507. const ble = new BLE(this.data.deviceinfo.deviceNum, emitter)
  2508. app.globalData.ble = ble
  2509. this.watchBLE()
  2510. app.globalData.ble.init()
  2511. // }
  2512. },
  2513. watchBLE() {
  2514. var that = this
  2515. var deviceinfo = this.data.deviceinfo
  2516. if (app.globalData.ble) {
  2517. app.globalData.ble.listen(res => {
  2518. if (res.type == 'connect') {
  2519. if (res.data == "未打开适配器") {
  2520. wx.hideLoading()
  2521. wx.showToast({
  2522. title: '请检查手机蓝牙和定位功能是否打开?',
  2523. duration: 2000,
  2524. icon: 'none'
  2525. })
  2526. } else {
  2527. if (res.data == '蓝牙已连接') {
  2528. this.data.info = res.data
  2529. app.globalData.bleStatus = res.data
  2530. console.log(res.data, 'pppp')
  2531. if (this.data.event == 0) {
  2532. this.btntest2()
  2533. } else if (this.data.event == 3) {
  2534. this.btntest3()
  2535. } else if (this.data.event == 6) {
  2536. // this.setVoice()
  2537. this.setData({
  2538. show: true
  2539. })
  2540. } else if (this.data.event == 7) {
  2541. wx.showModal({
  2542. title: "常开设置",
  2543. cancelText: '关闭',
  2544. confirmText: '开启',
  2545. success: (res) => {
  2546. if (res.confirm) {
  2547. that.setOpen()
  2548. } else if (res.cancel) {
  2549. that.setOpen1()
  2550. }
  2551. }
  2552. })
  2553. } else {
  2554. wx.hideLoading()
  2555. }
  2556. }
  2557. }
  2558. }
  2559. })
  2560. }
  2561. },
  2562. closeblue() {
  2563. wx.showLoading({
  2564. title: "正在停止...",
  2565. mask: true
  2566. });
  2567. setTimeout(() => {
  2568. wx.hideLoading()
  2569. }, 1000);
  2570. app.globalData.ble.close()
  2571. this.data.info = ''
  2572. app.globalData.bleStatus = false
  2573. this.data.bleStatus = false,
  2574. this.data.respond = []
  2575. },
  2576. closeblue1() {
  2577. // wx.showLoading({
  2578. // title: "正在停止...",
  2579. // });
  2580. app.globalData.ble.close()
  2581. // setTimeout(() => {
  2582. // wx.hideLoading()
  2583. // }, 1000);
  2584. this.data.info = ''
  2585. app.globalData.bleStatus = false
  2586. this.data.bleStatus = false,
  2587. this.data.respond = []
  2588. },
  2589. btntest3(event) {
  2590. var that = this
  2591. let deviceNum = that.data.deviceinfo.deviceNum
  2592. this.data.backstatus = true
  2593. that.startFp_29(deviceNum, 0x03)
  2594. // setTimeout(() => {
  2595. // that.getcartcount()
  2596. // }, 300)
  2597. },
  2598. btntest2(event) {
  2599. var that = this
  2600. let userTelephone = "18270949468"
  2601. let userPassword = "202108"
  2602. let pwd = "2021110309"
  2603. let openLockType = 2
  2604. var userInfo = wx.getStorageSync('userInfo')
  2605. var timestamp = moment().format("x")
  2606. bleall.openLock(userTelephone, userPassword, pwd, openLockType, timestamp, function (res) {
  2607. if (res.success) {
  2608. var rqTime = moment().format("X")
  2609. var username = wx.getStorageSync('username'),
  2610. password = wx.getStorageSync('password')
  2611. var str = `${username}+${password}+${rqTime}`
  2612. var signature = md5(str)
  2613. wx.hideLoading()
  2614. var data = {
  2615. code: "ld/bleUnlockRecord",
  2616. data: {
  2617. deviceNum: that.data.deviceNum,
  2618. userTelephone: userTelephone,
  2619. password: userPassword,
  2620. roomId: that.data.deviceinfo.roomId,
  2621. roomName: that.data.deviceinfo.roomName,
  2622. cardName: userInfo.username,
  2623. signature: signature
  2624. }
  2625. }
  2626. WXAPI.sendCommand(data).then(res => {
  2627. })
  2628. }
  2629. console.log(res.data)
  2630. if (res.data == "蓝牙功能暂不支持") {
  2631. wx.showToast({
  2632. title: '重新连接蓝牙中',
  2633. duration: 2000,
  2634. icon: "none"
  2635. })
  2636. that.startconnect()
  2637. }
  2638. })
  2639. },
  2640. openLock(userTelephone, userPassword, pwd, openLockType) {
  2641. wx.showLoading({
  2642. title: "正在发送...",
  2643. mask: true
  2644. });
  2645. setTimeout(() => {
  2646. wx.hideLoading()
  2647. }, 1000);
  2648. let cmd = 0x41
  2649. console.log(util, userTelephone)
  2650. let ut = util.stringToUint8Array(userTelephone)
  2651. let up = util.stringToUint8Array(userPassword)
  2652. let p = util.hexStringToBytesWithPadding(pwd, 10, 0xff)
  2653. let length = 1 + 11 + 6 + 10 + 1
  2654. let b = new Uint8Array(length)
  2655. b[0] = cmd
  2656. b.set(ut, 1)
  2657. b.set(up, 12)
  2658. b.set(p, 18)
  2659. b[28] = openLockType
  2660. console.log(b)
  2661. app.globalData.ble.send(cmd, b, length)
  2662. },
  2663. //远程开锁
  2664. remoteUnlocking() {
  2665. if (this.data.info == '蓝牙已连接') {
  2666. this.closeblue1()
  2667. }
  2668. if (this.data.deviceinfo.manufactureId == 101) {
  2669. var str2 = {
  2670. imei: this.data.deviceinfo.imei,
  2671. openLockType: 2,
  2672. is: true,
  2673. roomId: this.data.deviceinfo.roomId,
  2674. roomName: this.data.deviceinfo.roomName,
  2675. cardName: this.data.userInfo.username
  2676. };
  2677. WXAPI.openLock(str2).then(res => {
  2678. if (res.success) {
  2679. wx.showToast({
  2680. title: "已发送远程开门指令",
  2681. icon: "none",
  2682. duration: 2000
  2683. })
  2684. }
  2685. })
  2686. } else if (this.data.deviceinfo.manufactureId == 1201) {
  2687. var str2 = {
  2688. code: 'lwj/openLock',
  2689. data: {
  2690. lockerId: this.data.deviceinfo.deviceNum,
  2691. order: 6301,
  2692. is: true,
  2693. roomId: this.data.deviceinfo.roomId,
  2694. roomName: this.data.deviceinfo.roomName,
  2695. cardName: this.data.userInfo.username
  2696. }
  2697. };
  2698. WXAPI.sendCommand(str2).then(res => {
  2699. if (res.success) {
  2700. wx.showToast({
  2701. title: "已发送远程开门指令",
  2702. icon: "none",
  2703. duration: 2000
  2704. })
  2705. }
  2706. })
  2707. }
  2708. },
  2709. /**
  2710. * 生命周期函数--监听页面初次渲染完成
  2711. */
  2712. onReady: function () {
  2713. app.globalData.zgBlueStatus = false
  2714. },
  2715. AddressPermissions() {
  2716. wx.openBluetoothAdapter({
  2717. success: function (res) {
  2718. console.log('蓝牙已开启!');
  2719. },
  2720. fail: function (e) {
  2721. console.log(e)
  2722. wx.showToast({
  2723. title: '请打开蓝牙!',
  2724. duration: 2000,
  2725. icon: 'none'
  2726. })
  2727. }
  2728. });
  2729. },
  2730. getLocationPermissionMethods() {
  2731. ICINLocker.getLocationPermission().then((res) => {
  2732. if (res.status == 0) {
  2733. } else {
  2734. // 连接出现问题
  2735. }
  2736. }).catch((res) => {
  2737. //处理未能获得地理位置情况
  2738. wx.showToast({
  2739. title: '请打开微信定位功能!',
  2740. duration: 2000,
  2741. icon: 'none'
  2742. })
  2743. })
  2744. },
  2745. /**
  2746. * 生命周期函数--监听页面显示
  2747. */
  2748. onShow: function () {
  2749. var deviceinfo = wx.getStorageSync('deviceinfo')
  2750. var userInfo1 = wx.getStorageSync('userInfo')
  2751. if (deviceinfo.roomId) {
  2752. if (userInfo1.type != 3) {
  2753. var data2 = {
  2754. q: JSON.stringify({
  2755. id: deviceinfo.roomId
  2756. }),
  2757. orderBy: "createTime",
  2758. orderType: 'desc'
  2759. }
  2760. WXAPI.getSiteTenant(data2).then(res1 => {
  2761. if (res1.data.list.length > 0) {
  2762. var array = res1.data.list.reverse()
  2763. wx.setStorageSync('info', array[0])
  2764. deviceinfo.info = array[0].info
  2765. this.setData({
  2766. deviceinfo: deviceinfo
  2767. })
  2768. } else {
  2769. wx.showToast({
  2770. title: "还未下发人员,请先下发人员",
  2771. duration: 2000,
  2772. icon: 'none'
  2773. })
  2774. }
  2775. })
  2776. } else if (userInfo1.type == 3) {
  2777. this.setData({
  2778. deviceinfo: deviceinfo
  2779. })
  2780. }
  2781. } else {
  2782. wx.navigateTo({
  2783. url: "../index/index"
  2784. })
  2785. }
  2786. this.setData({
  2787. bleStatus: app.globalData.bleStatus
  2788. })
  2789. },
  2790. /**
  2791. * 生命周期函数--监听页面隐藏
  2792. */
  2793. /**
  2794. * 生命周期函数--监听页面卸载
  2795. */
  2796. onUnload: function () {
  2797. console.log(isConnected, connectedDeviceId)
  2798. if (isConnected) {
  2799. wx.closeBLEConnection({
  2800. deviceId: connectedDeviceId,
  2801. success: function (res) {
  2802. isConnected = false
  2803. },
  2804. })
  2805. }
  2806. // if (app.globalData.zgBlueStatus) {
  2807. // ICINLocker.disConnect(app.globalData.blueDeviceId);
  2808. // this.onReady()
  2809. // }
  2810. this.setData({
  2811. bluestatus: false,
  2812. toSearch: false
  2813. })
  2814. // wx.removeStorageSync("connectedDeviceId")
  2815. if (app.globalData.bleStatus) {
  2816. this.closeblue()
  2817. }
  2818. wx.hideLoading()
  2819. },
  2820. onHide() {
  2821. // wx.removeStorageSync("connectedDeviceId")
  2822. this.setData({
  2823. toSearch: false
  2824. })
  2825. wx.hideLoading()
  2826. if (app.globalData.bleStatus) {
  2827. this.closeblue()
  2828. }
  2829. },
  2830. /**
  2831. * 页面相关事件处理函数--监听用户下拉动作
  2832. */
  2833. onPullDownRefresh: function () {
  2834. // this.AddressPermissions()
  2835. var deviceinfo = wx.getStorageSync('deviceinfo')
  2836. app.globalData.deviceNum = deviceinfo.deviceNum
  2837. var userInfo1 = wx.getStorageSync('userInfo')
  2838. if (deviceinfo.manufactureId == 201 || deviceinfo.manufactureId == 221) {
  2839. var data3 = {
  2840. code: "zg/bleRegisterInfo",
  2841. data: {
  2842. lockerId: deviceinfo.deviceNum
  2843. }
  2844. }
  2845. WXAPI.sendCommand(data3).then(res1 => {
  2846. if (res1.success) {
  2847. wx.setStorageSync('cgInfo', res1.data)
  2848. this.setData({
  2849. cgInfo: res1.data
  2850. })
  2851. } else {
  2852. }
  2853. })
  2854. }
  2855. if (deviceinfo.roomId) {
  2856. if (userInfo1.type != 3) {
  2857. var data2 = {
  2858. q: JSON.stringify({
  2859. id: deviceinfo.roomId
  2860. }),
  2861. orderBy: "createTime",
  2862. orderType: 'desc'
  2863. }
  2864. WXAPI.getSiteTenant(data2).then(res1 => {
  2865. if (res1.data.list.length > 0) {
  2866. var array = res1.data.list.reverse()
  2867. wx.setStorageSync('info', array[0])
  2868. deviceinfo.info = array[0].info
  2869. this.setData({
  2870. deviceinfo: deviceinfo
  2871. })
  2872. } else {
  2873. wx.showToast({
  2874. title: "还未下发人员,请先下发人员",
  2875. duration: 2000,
  2876. icon: 'none'
  2877. })
  2878. }
  2879. })
  2880. } else if (userInfo1.type == 3) {
  2881. this.setData({
  2882. deviceinfo: deviceinfo
  2883. })
  2884. }
  2885. } else {
  2886. wx.navigateTo({
  2887. url: "../index/index"
  2888. })
  2889. }
  2890. this.setData({
  2891. info: app.globalData.bleStatus,
  2892. deviceinfo: deviceinfo
  2893. })
  2894. wx.stopPullDownRefresh()
  2895. },
  2896. /**
  2897. * 页面上拉触底事件的处理函数
  2898. */
  2899. onReachBottom: function () {
  2900. },
  2901. /**
  2902. * 用户点击右上角分享
  2903. */
  2904. onShareAppMessage: function () {
  2905. }
  2906. })