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.

424 lines
13 KiB

1 year ago
  1. // pages/addBlueButton/addBlueButton.js
  2. const WXAPI = require('../../utils/request')
  3. import ICINLocker from "../../utils/ICINPakage/ICINLocker"
  4. var util = require('../../utils/util');
  5. import moment from "../../utils/moment"
  6. const app = getApp();
  7. const CurentTime=moment().format("YYYY-MM-DD HH:mm:ss")
  8. const CurentTime1=moment().add(60, "years").format("YYYY-MM-DD HH:mm:ss");
  9. Page({
  10. /**
  11. * 页面的初始数据
  12. */
  13. data: {
  14. show:true,
  15. deviceList:[],
  16. personList:[],
  17. timer:null,
  18. blueKeyId:"",
  19. personli:{},
  20. selectName:'',
  21. signKey:'',
  22. starttime:"",
  23. minDate:"",
  24. minDate1:"",
  25. endtime:"",
  26. datetimerange:[CurentTime, CurentTime1],
  27. minDate: CurentTime,
  28. minDate1: CurentTime,
  29. endtime: CurentTime1,
  30. starttime: CurentTime,
  31. },
  32. /**
  33. * 生命周期函数--监听页面加载
  34. */
  35. onLoad: function (options) {
  36. // let timer=setInterval(()=>{
  37. this.linkBlue()
  38. // },5000)
  39. var deviceinfo=wx.getStorageSync('deviceinfo')
  40. // var personli=wx.getStorageSync('personli')
  41. this.getSiteTenant(deviceinfo.roomId)
  42. this.setData({
  43. deviceinfo,
  44. // personli,
  45. // timer
  46. })
  47. },
  48. bindDateChange: function (e) {
  49. var str = e.detail.value + " 00:00:00"
  50. this.data.datetimerange[0] = str
  51. this.setData({
  52. starttime: str,
  53. minDate1: str
  54. })
  55. },
  56. bindDateChange1: function (e) {
  57. var str = e.detail.value + " 23:59:59"
  58. this.data.datetimerange[1] = str
  59. this.setData({
  60. endtime: str
  61. })
  62. },
  63. btntest1(e){
  64. var personli=e.currentTarget.dataset.personli
  65. this.setData({
  66. personli:personli
  67. })
  68. if (app.globalData.zgBlueStatus) {
  69. this.addUser()
  70. } else {
  71. this.goBlue()
  72. }
  73. },
  74. setDeviceKeyId(){
  75. var info=JSON.parse(this.data.personli.info)
  76. var cgInfo=wx.getStorageSync('cgInfo')
  77. var comm=this.stringToHex(this.data.selectName)
  78. var keyId="0o01weit9ti9ti"
  79. var password="3030000000000000000000000000000000000000"
  80. var infoKeyId=info.keyId?info.keyId:info.userPassword
  81. ICINLocker.connect(this.data.deviceId,true).then((res) => {
  82. if (res.status == 0) {
  83. console.log(res,'蓝牙扣连接')
  84. ICINLocker.readBluetoothBuckle(cgInfo.lockerId, comm, callback2 => {
  85. if(callback2.status==0){
  86. console.log(infoKeyId, this.data.selectName, keyId, password, "", this.data.signKey, comm,'--------蓝牙扣')
  87. ICINLocker.registeBluetoothBuckle(infoKeyId, this.data.selectName, keyId, password, "", this.data.signKey, comm, callback3 => {
  88. if(callback3.status==0){
  89. wx.hideLoading()
  90. ICINLocker.disConnect(this.data.deviceId).then(res=>{
  91. console.log(res)
  92. wx.removeStorageSync("connectedDeviceId");
  93. })
  94. this.setData({
  95. show:false
  96. })
  97. wx.showToast({
  98. title: '添加成功',
  99. duration:2000,
  100. icon:"none"
  101. })
  102. }else{
  103. ICINLocker.disConnect(this.data.deviceId).then(res=>{
  104. console.log(res)
  105. wx.removeStorageSync("connectedDeviceId");
  106. })
  107. wx.hideLoading()
  108. this.setData({
  109. show:false
  110. })
  111. wx.showToast({
  112. title: '添加成功',
  113. duration:2000,
  114. icon:"none"
  115. })
  116. }
  117. })
  118. }else{
  119. ICINLocker.disConnect(this.data.deviceId).then(res=>{
  120. console.log(res)
  121. wx.removeStorageSync("connectedDeviceId");
  122. })
  123. wx.hideLoading()
  124. wx.showToast({
  125. title: '读取失败',
  126. duration:2000,
  127. icon:"none"
  128. })
  129. }
  130. });
  131. }
  132. })
  133. },
  134. stringToHex(str){
  135.     var val="";
  136.     for(var i = 0; i < str.length; i++){
  137.       if(val == "")
  138.         val = str.charCodeAt(i).toString(16);
  139.       else
  140.         val += str.charCodeAt(i).toString(16);
  141.     }
  142.     return val;
  143.   },
  144. addUser(){
  145. console.log("添加蓝牙扣用户")
  146. wx.showLoading({
  147. title: '添加中'
  148. })
  149. var that=this
  150. var cgInfo=wx.getStorageSync('cgInfo')
  151. var personli=this.data.personli
  152. var info=JSON.parse(personli.info)
  153. console.log("添加蓝牙扣用户",personli,cgInfo)
  154. var infoKeyId=info.keyId?info.keyId:info.userPassword
  155. ICINLocker.getUserSignKey(cgInfo.lockerId, cgInfo.superId, infoKeyId, cgInfo.superId,cgInfo.authKey,"01",
  156. (callback) => {
  157. console.log(callback,'callbackcallbackcallbackcallback')
  158. if (callback.status == '00') {
  159. ICINLocker.getCommonKey(cgInfo.lockerId, infoKeyId, infoKeyId, callback.userSignKey, (callback1) => {
  160. if (callback1.status == '00') {
  161. var startTime=parseInt(moment(this.data.datetimerange[0]).format("X")).toString(16).toUpperCase()
  162. var endTime=parseInt(moment(this.data.datetimerange[1]).format("X")).toString(16).toUpperCase()
  163. console.log(cgInfo.lockerId, infoKeyId, this.data.blueKeyId, "0o01weit9ti9ti", "00", callback.userSignKey, "", callback1.commonKey, startTime, endTime)
  164. ICINLocker.addUserToLock(cgInfo.lockerId, infoKeyId, this.data.blueKeyId, "0o01weit9ti9ti", "00", callback.userSignKey, "", callback1.commonKey, startTime, endTime, (callback2) => {
  165. console.log(callback2)
  166. if(callback2.status=='00'){
  167. this.setData({
  168. signKey:callback.userSignKey
  169. })
  170. wx.closeBLEConnection({
  171. deviceId:app.globalData.blueDeviceId,
  172. success (res) {
  173. app.globalData.zgBlueStatus=false
  174. that.setDeviceKeyId()
  175. }
  176. })
  177. var str = {
  178. username: personli.name,
  179. deviceNum:this.data.deviceinfo.deviceNum,
  180. imei:this.data.deviceinfo.imei,
  181. iotId:infoKeyId,
  182. manufactureId:this.data.deviceinfo.manufactureId,
  183. userPassword: info.userPassword,
  184. roomName:this.data.deviceinfo.roomName,
  185. password: "0o01weit9ti9ti",
  186. passwordStartTime: moment(this.data.datetimerange[0]).format("X"),
  187. passwordEndTime: moment(this.data.datetimerange[1]).format("X"),
  188. operateMode: 26,
  189. order: 6326
  190. };
  191. var dataCerti={
  192. cardId: personli.cardId,
  193. content: JSON.stringify(str),
  194. type: 36,
  195. startTime: moment(this.data.datetimerange[0]).format("X"),
  196. endTime: moment(this.data.datetimerange[1]).format("X"),
  197. }
  198. console.log(dataCerti,"蓝牙扣添加蓝牙扣用户凭证")
  199. WXAPI.addCertificates(dataCerti).then(res=>{
  200. if(res.code==200){
  201. var dataPasswod = {
  202. lockerId: this.data.deviceinfo.deviceNum,
  203. imei: this.data.deviceinfo.imei,
  204. lockerUserId: infoKeyId,
  205. keyId: infoKeyId,
  206. role: 0,
  207. order: 6226,
  208. action: 1,
  209. roomName:this.data.deviceinfo.roomName,
  210. cardName:'蓝牙扣'+personli.name,
  211. type: 1,
  212. result: 1
  213. }
  214. WXAPI.ZGduserOperation(dataPasswod).then(res6 => {
  215. console.log(res6,"上报添加蓝牙扣用户")
  216. })
  217. }
  218. })
  219. // ICINLocker.disConnect(app.globalData.blueDeviceId).then(res=>{
  220. // if(res.status=='00'){
  221. // app.globalData.zgBlueStatus=false
  222. // that.setDeviceKeyId()
  223. // }
  224. // })
  225. }
  226. })
  227. }
  228. })
  229. }
  230. })
  231. },
  232. linkBlue(){
  233. var that=this;
  234. ICINLocker.searchDevices().then(res=>{
  235. if (res.status == 0) {
  236. this.setData({
  237. deviceList:res.deviceList
  238. })
  239. }
  240. }).catch((res) => {
  241. if (res.status == 1) {
  242. if (res.statusReason.errCode == 10001) {
  243. wx.showToast({
  244. title: '请检查手机蓝牙和定位功能是否打开?',
  245. duration: 2000,
  246. icon: 'none'
  247. })
  248. } else {
  249. if (!app.globalData.zgBlueStatus && that.data.bluestatus) {
  250. that.linkBlue()
  251. }
  252. }
  253. }
  254. })
  255. },
  256. searchBlueButton(e) {
  257. var that = this
  258. var cgInfo=wx.getStorageSync('cgInfo')
  259. var comm=this.stringToHex(e.currentTarget.dataset.select.name)
  260. var deviceId=e.currentTarget.dataset.select.deviceId
  261. var keyId="0o01weit9ti9ti"
  262. var password="3030000000000000000000000000000000000000"
  263. // app.globalData.blueDeviceId = e.currentTarget.dataset.select.name
  264. var blueKeyId=e.currentTarget.dataset.select.name
  265. if(this.data.datetimerange[0]==''||this.data.datetimerange[1]==''){
  266. wx.showToast({
  267. title: '请选择开始结束时间',
  268. duration:2000,
  269. icon:"none"
  270. })
  271. }else{
  272. this.setData({
  273. bleStatus: '蓝牙已连接',
  274. deviceId: deviceId,
  275. blueKeyId:blueKeyId,
  276. show:false,
  277. selectName:e.currentTarget.dataset.select.name
  278. })
  279. app.globalData.zgBlueStatus = false
  280. }
  281. },
  282. // 获取入住人员
  283. getSiteTenant(roomId) {
  284. var data2 = {
  285. q: JSON.stringify({
  286. id: roomId
  287. }),
  288. orderBy: "createTime",
  289. orderType: 'desc'
  290. }
  291. WXAPI.getSiteTenant(data2).then(res1 => {
  292. if (res1.data.list.length > 0) {
  293. this.setData({
  294. personList: res1.data.list,
  295. roomId: roomId
  296. })
  297. } else {
  298. this.setData({
  299. personList: []
  300. })
  301. wx.showToast({
  302. title: "还未下发人员,请先下发人员",
  303. duration: 2000,
  304. icon: 'none'
  305. })
  306. }
  307. })
  308. },
  309. goBlue() {
  310. var that = this
  311. wx.showLoading({
  312. title: '连接中',
  313. })
  314. var deviceinfo=wx.getStorageSync('deviceinfo')
  315. ICINLocker.scanDevices(deviceinfo.deviceNum,false).then((res) => {
  316. if (res.status == 0) {
  317. var deviceId = res.deviceId
  318. //成功找到蓝牙情况下进行锁连接
  319. app.globalData.blueDeviceId = res.deviceId
  320. ICINLocker.connect(res.deviceId,false).then(res1 => {
  321. if (res1.status == "0") {
  322. console.log(res1)
  323. that.addUser()
  324. wx.showToast({
  325. title: '连接成功',
  326. icon: "none",
  327. duration: 2000
  328. })
  329. app.globalData.zgBlueStatus = true
  330. that.setData({
  331. bleStatus: '蓝牙已连接',
  332. })
  333. } else {
  334. wx.hideLoading()
  335. var msg = ''
  336. switch (res1.errCode) {
  337. case 10001:
  338. msg = '调用失败,请打开蓝牙!';
  339. break;
  340. }
  341. wx.showToast({
  342. title: msg,
  343. duration: 2000,
  344. icon: 'none'
  345. })
  346. //此处处理返回值不为成功(0)的情况
  347. }
  348. }).catch((res) => {
  349. wx.hideLoading()
  350. // 此处处理未能连接成功问题
  351. })
  352. }
  353. })
  354. .catch((res) => {
  355. //此处处理未能连接到蓝牙的问题
  356. if (res.status == 1) {
  357. if (res.statusReason.errCode == 10001) {
  358. wx.hideLoading()
  359. wx.showToast({
  360. title: '请检查手机蓝牙和定位功能是否打开?',
  361. duration: 2000,
  362. icon: 'none'
  363. })
  364. } else {
  365. if (!app.globalData.zgBlueStatus && that.data.bluestatus) {
  366. that.goBlue()
  367. } else {
  368. wx.hideLoading()
  369. }
  370. }
  371. }
  372. })
  373. },
  374. /**
  375. * 生命周期函数--监听页面初次渲染完成
  376. */
  377. onReady: function () {
  378. },
  379. /**
  380. * 生命周期函数--监听页面显示
  381. */
  382. onShow: function () {
  383. },
  384. /**
  385. * 生命周期函数--监听页面隐藏
  386. */
  387. onHide: function () {
  388. },
  389. /**
  390. * 生命周期函数--监听页面卸载
  391. */
  392. onUnload: function () {
  393. },
  394. /**
  395. * 页面相关事件处理函数--监听用户下拉动作
  396. */
  397. onPullDownRefresh: function () {
  398. },
  399. /**
  400. * 页面上拉触底事件的处理函数
  401. */
  402. onReachBottom: function () {
  403. },
  404. /**
  405. * 用户点击右上角分享
  406. */
  407. onShareAppMessage: function () {
  408. }
  409. })