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.

638 lines
18 KiB

1 year ago
  1. // pages/selectPersonnel/selectPersonnel.js
  2. const app = getApp()
  3. const WXAPI = require('../../utils/request')
  4. import ICINLocker from "../../utils/ICINPakage/ICINLocker"
  5. var util = require('../../utils/util');
  6. var methods = require('../../utils/methods');
  7. var md5 = require('../../utils/md5.js');
  8. var bleall = require('../../utils/bleall');
  9. import {
  10. BLE
  11. } from "../../utils/btls/ble";
  12. const emitter = app.globalData.emitter
  13. // ArrayBuffer转16进度字符串示例
  14. function ab2hex(buffer) {
  15. var hexArr = Array.prototype.map.call(
  16. new Uint8Array(buffer),
  17. function (bit) {
  18. return ('00' + bit.toString(16)).slice(-2)
  19. }
  20. )
  21. return hexArr.join(',');
  22. }
  23. import moment from "../../utils/moment"
  24. function CurentTime() {
  25. var now = new Date();
  26. var year = now.getFullYear(); //年
  27. var month = now.getMonth() + 1; //月
  28. var day = now.getDate(); //日
  29. var hh = now.getHours(); //时
  30. var mm = now.getMinutes(); //分
  31. var clock = year + "-";
  32. if (month < 10)
  33. clock += "0";
  34. clock += month + "-";
  35. if (day < 10)
  36. clock += "0";
  37. clock += day + " ";
  38. clock += '00:00:00';
  39. return (clock);
  40. }
  41. function CurentTime1() {
  42. var now = new Date();
  43. var year = now.getFullYear() + 1; //年
  44. var month = now.getMonth() + 1; //月
  45. var day = now.getDate(); //日
  46. var hh = now.getHours(); //时
  47. var mm = now.getMinutes(); //分
  48. var clock = year + "-";
  49. if (month < 10)
  50. clock += "0";
  51. clock += month + "-";
  52. if (day < 10)
  53. clock += "0";
  54. clock += day + " ";
  55. clock += '23:59:59';
  56. return (clock);
  57. }
  58. Page({
  59. /**
  60. * 页面的初始数据
  61. */
  62. data: {
  63. datetimerange: [CurentTime(), CurentTime1()],
  64. endtime: CurentTime1(),
  65. starttime: CurentTime(),
  66. searchValue:"人员",
  67. pageSize:20,
  68. pageNum:1,
  69. personList:[],
  70. deviceinfo:{},
  71. cgInfo:{},
  72. show: false,
  73. phone: '',
  74. name: '',
  75. deviceId:'',
  76. personInfo:{},
  77. pullUpStatus:true
  78. },
  79. /**
  80. * 生命周期函数--监听页面加载
  81. */
  82. onLoad: function (options) {
  83. var deviceinfo = wx.getStorageSync('deviceinfo')
  84. var cgInfo = wx.getStorageSync('cgInfo')
  85. this.setData({
  86. deviceinfo: deviceinfo,
  87. cgInfo: cgInfo
  88. })
  89. this.getPerson()
  90. },
  91. bindDateChange: function (e) {
  92. var str = e.detail.value + " 00:00:00"
  93. this.data.datetimerange[0] = str
  94. this.setData({
  95. starttime: str,
  96. minDate1: str
  97. })
  98. },
  99. bindDateChange1: function (e) {
  100. var str = e.detail.value + " 23:59:59"
  101. this.data.datetimerange[1] = str
  102. this.setData({
  103. endtime: str
  104. })
  105. },
  106. // 点击人员
  107. clickOnPeople(e){
  108. var info=e.target.dataset.item
  109. info['content']=JSON.parse(info.info)
  110. this.setData({
  111. personInfo:info,
  112. show:true
  113. })
  114. },
  115. getPerson(){
  116. var data={
  117. q:JSON.stringify({}),
  118. pageSize:this.data.pageSize,
  119. pageNum:this.data.pageNum
  120. }
  121. WXAPI.getlistNum(data).then(res=>{
  122. if(res.success){
  123. if(res.data.list<this.data.pageSize){
  124. this.setData({
  125. pullUpStatus:false,
  126. personList: res.data.list
  127. })
  128. }else{
  129. this.setData({
  130. pullUpStatus:true,
  131. personList: res.data.list
  132. })
  133. }
  134. }else{
  135. wx.showToast({
  136. title: res.msg,
  137. duration:2000,
  138. mask:true,
  139. icon:'none'
  140. })
  141. }
  142. })
  143. },
  144. // 关闭添加人员弹窗
  145. onClose() {
  146. this.setData({
  147. show: false
  148. });
  149. },
  150. // 获取人员
  151. onChange(event) {
  152. this.setData({
  153. searchValue: event.detail
  154. })
  155. },
  156. pullUpMethods() {
  157. var data2 = {
  158. q: JSON.stringify({
  159. name: this.data.searchValue
  160. }),
  161. pageSize:this.data.pageSize,
  162. pageNum:this.data.pageNum
  163. }
  164. WXAPI.getlistNum(data2).then(res1 => {
  165. if (res1.data.list.length > 0) {
  166. var pullUpStatus=true
  167. if(res1.data.list<this.data.pageSize){
  168. pullUpStatus=false
  169. }else{
  170. pullUpStatus=false
  171. }
  172. var list=this.data.personList.concat(res1.data.list)
  173. this.setData({
  174. pullUpStatus:pullUpStatus,
  175. personList: list
  176. })
  177. } else {
  178. wx.showToast({
  179. title: "未发现该人员",
  180. duration: 2000,
  181. mask:true,
  182. icon: 'none'
  183. })
  184. }
  185. })
  186. },
  187. onClick() {
  188. var data2 = {
  189. q: JSON.stringify({
  190. name: this.data.searchValue
  191. }),
  192. pageSize:this.data.pageSize,
  193. pageNum:1
  194. }
  195. WXAPI.getlistNum(data2).then(res1 => {
  196. if (res1.data.list.length > 0) {
  197. var pullUpStatus=true
  198. if(res1.data.list<this.data.pageSize){
  199. pullUpStatus=false
  200. }else{
  201. pullUpStatus=false
  202. }
  203. this.setData({
  204. pullUpStatus:pullUpStatus,
  205. personList: res1.data.list
  206. })
  207. } else {
  208. wx.showToast({
  209. title: "未发现该人员",
  210. duration: 2000,
  211. mask:true,
  212. icon: 'none'
  213. })
  214. }
  215. })
  216. },
  217. addperson(e) {
  218. var deviceinfo = this.data.deviceinfo
  219. if (deviceinfo.manufactureId == 201||deviceinfo.manufactureId == 221) {
  220. if (app.globalData.zgBlueStatus) {
  221. this.adduser()
  222. } else {
  223. this.goBlue()
  224. }
  225. } else if (deviceinfo.manufactureId == 101) {
  226. if (app.globalData.bleStatus == '蓝牙已连接') {
  227. this.adduser()
  228. } else {
  229. this.startconnect()
  230. }
  231. }
  232. },
  233. goBlue() {
  234. var that = this
  235. var cgInfo = wx.getStorageSync('cgInfo')
  236. wx.showLoading({
  237. title: '连接中',
  238. mask:true
  239. })
  240. ICINLocker.scanDevices(this.data.deviceinfo.deviceNum)
  241. .then((res) => {
  242. console.log(res)
  243. if (res.status == 0) {
  244. var deviceId = res.deviceId
  245. //成功找到蓝牙情况下进行锁连接
  246. app.globalData.blueDeviceId = res.deviceId
  247. ICINLocker.connect(res.deviceId) // 此处 deviceId 存起来,当断开连接时使用
  248. .then((res) => {
  249. if (res.status == 0) {
  250. clearInterval(that.data.timer)
  251. this.setData({
  252. deviceId: deviceId
  253. })
  254. app.globalData.zgBlueStatus = true
  255. wx.hideLoading()
  256. wx.showToast({
  257. title: '连接成功',
  258. icon: "none",
  259. mask:true,
  260. duration: 2000
  261. })
  262. this.adduser()
  263. } else {
  264. wx.hideLoading()
  265. var msg = ''
  266. switch (res.errCode) {
  267. case 10001:
  268. msg = '调用失败,请打开蓝牙!';
  269. break;
  270. }
  271. wx.showToast({
  272. title: msg,
  273. duration: 2000,
  274. mask:true,
  275. icon: 'none'
  276. })
  277. //此处处理返回值不为成功(0)的情况
  278. }
  279. }).catch((res) => {
  280. wx.hideLoading()
  281. console.log(res, '失败')
  282. // 此处处理未能连接成功问题
  283. })
  284. }
  285. })
  286. .catch((res) => {
  287. //此处处理未能连接到蓝牙的问题
  288. if (res.status == 1) {
  289. if (res.statusReason.errCode == 10001) {
  290. wx.hideLoading()
  291. wx.showModal({
  292. title: "提示",
  293. content: "请检查手机蓝牙和定位功能是否打开?",
  294. showCancel: false,
  295. confirmText: "确定",
  296. success(res) {}
  297. });
  298. } else {
  299. if (!app.globalData.zgBlueStatus) {
  300. that.goBlue()
  301. } else {
  302. wx.hideLoading()
  303. }
  304. }
  305. }
  306. })
  307. },
  308. startconnect() {
  309. //if(checkWechatVersion())
  310. // {
  311. wx.showLoading({
  312. title: "连接中",
  313. mask:true
  314. })
  315. var deviceinfo = this.data.deviceinfo
  316. const ble = new BLE(this.data.deviceinfo.deviceNum, emitter)
  317. app.globalData.ble = ble
  318. this.watchBLE()
  319. app.globalData.ble.init()
  320. // }
  321. },
  322. watchBLE() {
  323. var that = this
  324. if (app.globalData.ble) {
  325. app.globalData.ble.listen(res => {
  326. if (res.type == 'connect') {
  327. if (res.data == "未打开适配器") {
  328. wx.hideLoading()
  329. wx.showModal({
  330. title: "提示",
  331. content: "请检查手机蓝牙和定位功能是否打开?",
  332. showCancel: false,
  333. confirmText: "确定",
  334. success(res) {}
  335. });
  336. } else {
  337. if (res.data == '蓝牙已连接') {
  338. app.globalData.bleStatus = res.data
  339. this.adduser()
  340. }
  341. }
  342. }
  343. })
  344. }
  345. },
  346. adduser() {
  347. var _this = this
  348. wx.showLoading({
  349. title: '正在添加',
  350. mask:true
  351. })
  352. var time1 = moment(this.data.datetimerange[0]).format("YYYYMMDDHHmmss")
  353. var time2 = moment(this.data.datetimerange[1]).format("YYYYMMDDHHmmss")
  354. var time3 = new Date(this.data.datetimerange[0].replace(/-/g, '/'))
  355. var time4 = new Date(this.data.datetimerange[1].replace(/-/g, '/'))
  356. var passwordStartTime = time3.getTime() / 1000
  357. var passwordEndTime = time4.getTime() / 1000
  358. if(passwordStartTime>passwordEndTime){
  359. wx.showToast({
  360. title: '开始时间不能大于结束时间',
  361. duration: 2000,
  362. icon: 'none',
  363. mask:true,
  364. })
  365. return
  366. }
  367. if (this.data.personInfo.content.userTelephone == '' || this.data.personInfo.name == '') {
  368. wx.showToast({
  369. title: '请填写完整信息',
  370. duration: 2000,
  371. mask:true,
  372. icon: 'none'
  373. })
  374. }else if(this.data.personInfo.content.userTelephone.length !=11){
  375. wx.showToast({
  376. title: '手机号格式错误',
  377. duration: 2000,
  378. mask:true,
  379. icon: 'none'
  380. })
  381. } else {
  382. var userInfo = wx.getStorageSync('userInfo')
  383. var deviceinfo = this.data.deviceinfo
  384. if (deviceinfo.manufactureId == 101) {
  385. var userPassword = this.data.personInfo.content.userPassword
  386. var data = {
  387. managerTelephone: "18270949468",
  388. managerPassword: "202108",
  389. userTelephone: this.data.personInfo.content.userTelephone,
  390. password: userPassword,
  391. userPassword: userPassword,
  392. startTime: time1,
  393. endTime: time2
  394. }
  395. var timestamp= moment().format("x")
  396. console.log(data,'-----')
  397. bleall.startFp1_63(data.managerTelephone, data.managerPassword, data.userTelephone,
  398. data.userPassword, data.password, data.startTime, data.endTime, 1,timestamp,
  399. function (res) {
  400. console.log(res)
  401. if (res.success) {
  402. bleall.startFp1_63(data.managerTelephone, data.managerPassword, data.userTelephone,
  403. data.userPassword, data.password,
  404. data.startTime, data.endTime, 4,timestamp,
  405. function (res1) {
  406. if (res1.success) {
  407. var data2 = [{
  408. cardId: _this.data.personInfo.id,
  409. siteId: _this.data.deviceinfo.roomId,
  410. startTime: time3,
  411. endTime: time4
  412. }]
  413. WXAPI.bindCard(data2).then(res2 => {
  414. var data3 = {
  415. code: "ble/operateUserInfo",
  416. data: {
  417. deviceNum: _this.data.deviceinfo.deviceNum,
  418. imei: _this.data.deviceinfo.imei,
  419. userTelephone: _this.data.personInfo.content.userTelephone,
  420. userPassword: userPassword,
  421. userStartTime: time1,
  422. userEndTime: time2,
  423. password: userPassword,
  424. passwordStartTime: time1,
  425. passwordEndTime: time2,
  426. operateMode: 1,
  427. order: 6226,
  428. roomName:_this.data.deviceinfo.roomName,
  429. cardName:'蓝牙'+_this.data.personInfo.name,
  430. timestamp: timestamp,
  431. managerTelephone: '18270949468',
  432. managerPassword: '202108',
  433. passwordLength: 10,
  434. permission: 4294967295,
  435. times: 255,
  436. recycleTime: "65535",
  437. }
  438. }
  439. wx.hideLoading()
  440. wx.showToast({
  441. title: '添加成功',
  442. icon: 'none',
  443. mask:true,
  444. duration: 2000
  445. })
  446. WXAPI.sendCommand(data3).then(res6 => {
  447. if(res6.success){
  448. var data4 = {
  449. result: 1,
  450. serial: res6.data.serialNumber
  451. }
  452. setTimeout(()=>{
  453. WXAPI.updatePush(data4).then(res4 => {
  454. })
  455. },1000)
  456. }
  457. })
  458. _this.setData({
  459. show: false
  460. })
  461. wx.navigateBack({
  462. delta: 1
  463. })
  464. })
  465. }
  466. })
  467. }
  468. if (res.data == "蓝牙功能暂不支持") {
  469. wx.showLoading({
  470. title: '重连中',
  471. mask:true
  472. })
  473. _this.startconnect()
  474. }
  475. })
  476. } else if (deviceinfo.manufactureId == 201||deviceinfo.manufactureId == 221) {
  477. var time1 = new Date(this.data.datetimerange[0].replace(/-/g, '/'))
  478. var time2 = new Date(this.data.datetimerange[1].replace(/-/g, '/'))
  479. var passwordStartTime = time1.getTime() / 1000
  480. var passwordEndTime = time2.getTime() / 1000
  481. var startTime = passwordStartTime.toString(16).toUpperCase()
  482. var endTime = passwordEndTime.toString(16).toUpperCase()
  483. var usrID = this.data.personInfo.content.userId
  484. wx.showLoading({
  485. title: '正在添加',
  486. mask:true
  487. })
  488. let info={
  489. usrID:usrID,
  490. startTime:startTime,
  491. endTime:endTime
  492. }
  493. methods.bluetoothOperation_ZH(this.data.cgInfo,info,1).then(result=>{
  494. if (result.status == '00') {
  495. var dataPasswod = {
  496. lockerId: this.data.deviceinfo.deviceNum,
  497. imei: this.data.deviceinfo.imei,
  498. lockerUserId: usrID,
  499. keyId: usrID,
  500. role: 0,
  501. order: 6226,
  502. action: 1,
  503. roomName:this.data.deviceinfo.roomName,
  504. cardName:'蓝牙'+this.data.personInfo.name,
  505. type: 1,
  506. result: 1
  507. }
  508. var data2 = [{
  509. cardId: this.data.personInfo.id,
  510. siteId: this.data.deviceinfo.roomId,
  511. startTime: time1,
  512. endTime: time2
  513. }]
  514. WXAPI.bindCard(data2).then(res2 => {
  515. if (res2.success) {
  516. WXAPI.ZGduserOperation(dataPasswod).then(res6 => {
  517. console.log(res6)
  518. if (res6.success) {
  519. var data3 = {
  520. result: 1,
  521. serial: res6.data.serialNumber
  522. }
  523. setTimeout(()=>{
  524. WXAPI.updatePush(data3).then(res4 => {
  525. })
  526. },1000)
  527. wx.hideLoading()
  528. wx.showToast({
  529. title: '添加成功',
  530. icon: 'none',
  531. mask:true,
  532. duration: 2000
  533. })
  534. } else {
  535. wx.hideLoading()
  536. wx.showToast({
  537. title: '添加失败',
  538. icon: 'none',
  539. mask:true,
  540. duration: 2000
  541. })
  542. }
  543. })
  544. this.setData({
  545. show: false
  546. })
  547. wx.showToast({
  548. title: '绑定成功',
  549. icon: 'none',
  550. mask:true,
  551. })
  552. wx.navigateBack({
  553. delta: 1
  554. })
  555. } else {
  556. wx.hideLoading()
  557. wx.showToast({
  558. title: '添加失败',
  559. mask:true,
  560. icon: 'none'
  561. })
  562. }
  563. })
  564. } else {
  565. wx.hideLoading()
  566. wx.showToast({
  567. title: '添加失败',
  568. duration: 2000,
  569. mask:true,
  570. icon: 'none'
  571. })
  572. }
  573. })
  574. }
  575. }
  576. },
  577. /**
  578. * 生命周期函数--监听页面初次渲染完成
  579. */
  580. onReady: function () {
  581. },
  582. /**
  583. * 生命周期函数--监听页面显示
  584. */
  585. onShow: function () {
  586. },
  587. /**
  588. * 生命周期函数--监听页面隐藏
  589. */
  590. onHide: function () {
  591. },
  592. /**
  593. * 生命周期函数--监听页面卸载
  594. */
  595. onUnload: function () {
  596. },
  597. /**
  598. * 页面相关事件处理函数--监听用户下拉动作
  599. */
  600. onPullDownRefresh: function () {
  601. },
  602. /**
  603. * 页面上拉触底事件的处理函数
  604. */
  605. onReachBottom: function () {
  606. if(this.data.pullUpStatus){
  607. var pageNum=this.data.pageNum+1
  608. this.setData({
  609. pageNum:pageNum
  610. })
  611. this.pullUpMethods()
  612. }
  613. },
  614. /**
  615. * 用户点击右上角分享
  616. */
  617. onShareAppMessage: function () {
  618. }
  619. })