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.

420 lines
12 KiB

1 year ago
  1. // pages/permissions/permissions.js
  2. const WXAPI = require('../../utils/request')
  3. import ICINLocker from "../../utils/ICINPakage/ICINLocker"
  4. var util = require('../../utils/util');
  5. import {
  6. BLE
  7. } from "../../utils/btls/ble";
  8. const app = getApp();
  9. const emitter = app.globalData.emitter
  10. // ArrayBuffer转16进度字符串示例
  11. function ab2hex(buffer) {
  12. var hexArr = Array.prototype.map.call(
  13. new Uint8Array(buffer),
  14. function (bit) {
  15. return ('00' + bit.toString(16)).slice(-2)
  16. }
  17. )
  18. return hexArr.join(',');
  19. }
  20. function CurentTime() {
  21. var now = new Date();
  22. var year = now.getFullYear(); //年
  23. var month = now.getMonth() + 1; //月
  24. var day = now.getDate(); //日
  25. var hh = now.getHours(); //时
  26. var mm = now.getMinutes(); //分
  27. var clock = year + "-";
  28. if (month < 10)
  29. clock += "0";
  30. clock += month + "-";
  31. if (day < 10)
  32. clock += "0";
  33. clock += day + " ";
  34. if (hh < 10)
  35. clock += "0";
  36. clock += hh + ":";
  37. if (mm < 10) clock += '0';
  38. clock += mm;
  39. return (clock);
  40. }
  41. var date = new Date()
  42. Page({
  43. /**
  44. * 页面的初始数据
  45. */
  46. data: {
  47. personInfo: {},
  48. password: '',
  49. datetimerange: [CurentTime(), CurentTime()],
  50. deviceinfo: {},
  51. endtime: '',
  52. starttime: '',
  53. minDate: CurentTime(),
  54. minDate1: CurentTime(),
  55. },
  56. /**
  57. * 生命周期函数--监听页面加载
  58. */
  59. onLoad: function (options) {
  60. var userInfo1 = wx.getStorageSync('userInfo')
  61. if (userInfo1.type != 3) {
  62. this.data.personInfo = wx.getStorageSync('info')
  63. } else if (userInfo1.type == 3) {
  64. this.data.personInfo = wx.getStorageSync('deviceinfo')
  65. }
  66. this.data.deviceinfo = wx.getStorageSync('deviceinfo')
  67. },
  68. onChange(event) {
  69. // event.detail 为当前输入的值
  70. this.setData({
  71. password: event.detail
  72. })
  73. },
  74. bindDateChange: function (e) {
  75. var str = e.detail.value + " 00:00:00"
  76. this.data.datetimerange[0] = str
  77. this.setData({
  78. starttime: e.detail.value,
  79. minDate1: e.detail.value
  80. })
  81. },
  82. bindDateChange1: function (e) {
  83. var str = e.detail.value + " 00:00:00"
  84. this.data.datetimerange[1] = str
  85. this.setData({
  86. endtime: e.detail.value
  87. })
  88. },
  89. addwordfun() {
  90. var _this=this
  91. if (this.data.deviceinfo.manufactureId == 201||this.data.deviceinfo.manufactureId == 221) {
  92. if (app.globalData.zgBlueStatus) {
  93. var deviceinfo = this.data.deviceinfo
  94. var info = JSON.parse(this.data.personInfo.info)
  95. var cgInfo = wx.getStorageSync('cgInfo')
  96. ICINLocker.getUserSignKey(cgInfo.lockerId, cgInfo.superId, info.userPassword, cgInfo.superId, cgInfo.authKey, '01', (callback) => {
  97. console.log(callback)
  98. if (callback.status == '00') {
  99. var userSignKey = callback.userSignKey
  100. ICINLocker.getCommonKey(cgInfo.lockerId, info.userPassword, info.userPassword, userSignKey, (callback) => {
  101. console.log(callback, 'commonKey')
  102. var commonKey = callback.commonKey
  103. ICINLocker.handlePwd(deviceinfo.deviceNum, cgInfo.superId, info.userPassword, this.data.password, '00', cgInfo.superId, cgInfo.authKey, '', '00000000', 'FFFFFFFF', 255, commonKey, (callback) => {
  104. console.log(callback.status)
  105. if(callback.status='00'){
  106. var passwordStartTime = _this.data.datetimerange[0].replaceAll('-', '').replaceAll(':', '').replaceAll(
  107. ' ',
  108. '')
  109. var passwordEndTime = _this.data.datetimerange[1].replaceAll('-', '').replaceAll(':', '').replaceAll(
  110. ' ', '')
  111. var str = {
  112. // cardId: this.personInfo.cardId,
  113. imei: _this.data.deviceinfo.imei,
  114. password: _this.data.password,
  115. userTelephone: JSON.parse(_this.data.personInfo.info).userTelephone,
  116. operateMode: 7,
  117. userPassword: JSON.parse(_this.data.personInfo.info).userPassword,
  118. passwordStartTime: passwordStartTime,
  119. passwordEndTime: passwordEndTime,
  120. is: false
  121. }
  122. var data = {
  123. cardId: _this.data.personInfo.card,
  124. content: JSON.stringify(str), //operateMode:NFC-10、把人员添加到凭证-1
  125. deviceNum: _this.data.personInfo.deviceNum,
  126. type: 4,
  127. imei: _this.data.deviceinfo.imei,
  128. startTime: passwordStartTime,
  129. endTime: passwordEndTime,
  130. };
  131. WXAPI.addCertificates(data).then(res => {
  132. if(res.success){
  133. wx.showToast({
  134. title: '添加成功',
  135. })
  136. }
  137. })
  138. }
  139. })
  140. })
  141. }
  142. })
  143. } else {
  144. this.goBlue()
  145. }
  146. } else {
  147. if (app.globalData.bleStatus == '蓝牙已连接') {
  148. var _this = this
  149. if (this.data.password.length != 6) {
  150. wx.showToast({
  151. title: "密码为6位数字",
  152. icon: 'none',
  153. duration: 2000
  154. })
  155. } else {
  156. var passwordStartTime = _this.data.datetimerange[0].replaceAll('-', '').replaceAll(':', '').replaceAll(
  157. ' ',
  158. '')
  159. var passwordEndTime = _this.data.datetimerange[1].replaceAll('-', '').replaceAll(':', '').replaceAll(
  160. ' ', '')
  161. var str = {
  162. // cardId: this.personInfo.cardId,
  163. imei: _this.data.deviceinfo.imei,
  164. password: _this.data.password,
  165. userTelephone: JSON.parse(_this.data.personInfo.info).userTelephone,
  166. operateMode: 7,
  167. userPassword: JSON.parse(_this.data.personInfo.info).userPassword,
  168. passwordStartTime: passwordStartTime,
  169. passwordEndTime: passwordEndTime,
  170. is: false,
  171. }
  172. var data = {
  173. cardId: _this.data.personInfo.card,
  174. content: JSON.stringify(str), //operateMode:NFC-10、把人员添加到凭证-1
  175. deviceNum: _this.data.personInfo.deviceNum,
  176. type: 4,
  177. imei: _this.data.deviceinfo.imei,
  178. startTime: passwordStartTime,
  179. endTime: passwordEndTime,
  180. };
  181. WXAPI.addCertificates(data).then(res => {
  182. var data = {
  183. managerTelephone: "18270949468",
  184. managerPassword: "202108",
  185. userTelephone: JSON.parse(_this.data.personInfo.info).userTelephone,
  186. userPassword: JSON.parse(_this.data.personInfo.info).userPassword,
  187. startTime: passwordStartTime,
  188. password: _this.data.password,
  189. endTime: passwordEndTime
  190. }
  191. _this.startFp_63(data.managerTelephone, data.managerPassword, data.userTelephone, data
  192. .userPassword, data.password,
  193. data.startTime, data.endTime, 7)
  194. })
  195. }
  196. } else {
  197. this.startconnect()
  198. }
  199. }
  200. },
  201. goBlue() {
  202. var that = this
  203. wx.showLoading({
  204. title: '连接中',
  205. })
  206. ICINLocker.scanDevices(this.data.deviceinfo.deviceNum)
  207. .then((res) => {
  208. console.log(res)
  209. if (res.status == 0) {
  210. //成功找到蓝牙情况下进行锁连接
  211. ICINLocker.connect(res.deviceId) // 此处 deviceId 存起来,当断开连接时使用
  212. .then((res) => {
  213. if (res.status == 0) {
  214. this.setData({
  215. bleStatus: '蓝牙已连接'
  216. })
  217. app.globalData.zgBlueStatus = true
  218. wx.hideLoading()
  219. wx.showToast({
  220. title: '连接成功',
  221. icon: "none",
  222. duration: 2000
  223. })
  224. } else {
  225. wx.hideLoading()
  226. var msg = ''
  227. switch (res.errCode) {
  228. case 10001:
  229. msg = '调用失败,请打开蓝牙!';
  230. break;
  231. }
  232. wx.showToast({
  233. title: msg,
  234. duration: 2000,
  235. icon: 'none'
  236. })
  237. //此处处理返回值不为成功(0)的情况
  238. }
  239. }).catch((res) => {
  240. wx.hideLoading()
  241. console.log(res, '失败')
  242. // 此处处理未能连接成功问题
  243. })
  244. }
  245. })
  246. .catch((res) => {
  247. //此处处理未能连接到蓝牙的问题
  248. switch (res.errCode) {
  249. case 10001:
  250. wx.showToast({
  251. title: '请打开蓝牙',
  252. duration: 2000,
  253. icon: 'none'
  254. });
  255. break;
  256. // default:this.goBlue();
  257. }
  258. this.goBlue()
  259. })
  260. },
  261. startconnect() {
  262. //if(checkWechatVersion())
  263. // {
  264. wx.showLoading({
  265. title: "连接中"
  266. })
  267. const ble = new BLE(this.data.deviceinfo.deviceNum, emitter)
  268. app.globalData.ble = ble
  269. this.watchBLE()
  270. app.globalData.ble.init()
  271. // }
  272. },
  273. watchBLE() {
  274. var that = this
  275. if (app.globalData.ble) {
  276. app.globalData.ble.listen(res => {
  277. if (res.type == 'connect') {
  278. if (res.data == "未打开适配器") {
  279. wx.hideLoading()
  280. wx.showModal({
  281. title: "提示",
  282. content: "请检查手机蓝牙和定位功能是否打开?",
  283. showCancel: false,
  284. confirmText: "确定",
  285. success(res) {}
  286. });
  287. } else {
  288. if (res.data == '蓝牙已连接') {
  289. this.data.info = res.data
  290. app.globalData.bleStatus = res.data
  291. this.addwordfun()
  292. }
  293. }
  294. }
  295. })
  296. }
  297. },
  298. closeblue() {
  299. wx.showLoading({
  300. title: "正在停止...",
  301. });
  302. app.globalData.ble.close()
  303. this.data.info = ''
  304. setTimeout(() => {
  305. wx.hideLoading()
  306. }, 1000);
  307. app.globalData.bleStatus = false
  308. this.data.bleStatus = false,
  309. this.data.respond = []
  310. },
  311. startFp_63(managerTelephone, managerPassword, userTelephone, userPassword, password, startTime, endTime, num) {
  312. wx.showLoading({
  313. title: "正在发送...",
  314. });
  315. setTimeout(() => {
  316. wx.hideLoading()
  317. }, 1000);
  318. let cmd = 0x63
  319. let mt = util.stringToUint8Array(managerTelephone)
  320. let mp = util.stringToUint8Array(managerPassword)
  321. let ut = util.stringToUint8Array(userTelephone)
  322. let up = util.stringToUint8Array(userPassword)
  323. let pd = util.hexStringToBytesWithPadding(password, 10, 0xff)
  324. let ust = util.time40ToBytes(startTime)
  325. let uet = util.time40ToBytes(endTime)
  326. let fst = util.time40ToBytes(startTime)
  327. let fet = util.time40ToBytes(endTime)
  328. let length = 1 + 11 + 6 + 11 + 6 + 5 + 5 + 4 + 2 + 10 + 5 + 5 + 1 + 2 + 1
  329. let b = new Uint8Array(length)
  330. b[0] = cmd
  331. b.set(mt, 1)
  332. b.set(mp, 12)
  333. b.set(ut, 18)
  334. b.set(up, 29)
  335. b.set(ust, 35)
  336. b.set(uet, 40)
  337. //权限
  338. b[45] = 0xff;
  339. b[46] = 0xff;
  340. b[47] = 0xff;
  341. b[48] = 0xff;
  342. //密码长度 uint16
  343. b[49] = 0
  344. b[50] = 0x0a
  345. //密码数据
  346. b.set(pd, 51)
  347. b.set(fst, 61)
  348. b.set(fet, 66)
  349. b[71] = 0xff;
  350. b[72] = 0xff;
  351. b[73] = 0xff;
  352. b[74] = num; //添加指纹
  353. app.globalData.ble.send(cmd, b)
  354. },
  355. change(e) {
  356. this.data.single = e
  357. console.log('-change事件:', e);
  358. },
  359. /**
  360. * 生命周期函数--监听页面初次渲染完成
  361. */
  362. onReady: function () {
  363. },
  364. /**
  365. * 生命周期函数--监听页面显示
  366. */
  367. onShow: function () {
  368. },
  369. /**
  370. * 生命周期函数--监听页面隐藏
  371. */
  372. onHide: function () {
  373. },
  374. /**
  375. * 生命周期函数--监听页面卸载
  376. */
  377. onUnload: function () {
  378. },
  379. /**
  380. * 页面相关事件处理函数--监听用户下拉动作
  381. */
  382. onPullDownRefresh: function () {
  383. },
  384. /**
  385. * 页面上拉触底事件的处理函数
  386. */
  387. onReachBottom: function () {
  388. },
  389. /**
  390. * 用户点击右上角分享
  391. */
  392. onShareAppMessage: function () {
  393. }
  394. })