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.

633 lines
16 KiB

1 year ago
  1. // index.js
  2. // 获取应用实例
  3. const app = getApp()
  4. var util = require('../../utils/util');
  5. import ICINLocker from "../../utils/ICINPakage/ICINLocker"
  6. const WXAPI = require('../../utils/request')
  7. // ArrayBuffer转16进度字符串示例
  8. function ab2hex(buffer) {
  9. var hexArr = Array.prototype.map.call(
  10. new Uint8Array(buffer),
  11. function (bit) {
  12. return ('00' + bit.toString(16)).slice(-2)
  13. }
  14. )
  15. return hexArr.join(',');
  16. }
  17. import {
  18. ztreeToElementTree,
  19. IntToBytesLittleEndian,
  20. BytesToIntBigEndian,
  21. uniqueArray
  22. } from "../../utils/util"
  23. import moment from "../../utils/moment"
  24. Page({
  25. data: {
  26. show: false,
  27. signKey: '',
  28. commonKey: '',
  29. list: [],
  30. searchValue: '',
  31. userInfo: {},
  32. pageNum: 1,
  33. pageSize: 20,
  34. stoppull: false,
  35. zNodes: [],
  36. index: 0,
  37. array: [],
  38. index1: 0,
  39. array1: [],
  40. ces: 0,
  41. ces1: '',
  42. ces2: '',
  43. dataList: [],
  44. selectKey: '', //选中的节点id
  45. selectRoomName: '选择房间',
  46. pid:'',
  47. treeList:[],
  48. allDevice:[],
  49. devices_list:[],
  50. searching:''
  51. },
  52. onLoad() {
  53. this.getAllDevice()
  54. var userInfo = wx.getStorageSync('userInfo')
  55. this.setData({
  56. userInfo,
  57. })
  58. wx.getStorage({
  59. key: "token",
  60. success: (res) => {
  61. // this.getIntegration()
  62. if (userInfo.type >= 3) {
  63. this.getRoomList()
  64. this.getRoom1()
  65. } else if (userInfo.type < 3) {
  66. this.getTree()
  67. }
  68. },
  69. fail: (res) => {
  70. wx.redirectTo({
  71. url: "/pages/login/login"
  72. })
  73. }
  74. })
  75. },
  76. getCgInfo(deviceNum){
  77. return new Promise((resolve, reject)=>{
  78. var data3 = {
  79. code: "zg/bleRegisterInfo",
  80. data: {
  81. lockerId: deviceNum
  82. }
  83. }
  84. WXAPI.sendCommand(data3).then(res1 => {
  85. if(res1.success){
  86. resolve(res1)
  87. }else{
  88. reject(res1)
  89. }
  90. })
  91. })
  92. },
  93. // 获取所有设备
  94. getAllDevice(){
  95. var data1={
  96. pageNum: 0,
  97. pageSize: 0,
  98. }
  99. WXAPI.getDeviceItems(data1).then(res => {
  100. if(res.success){
  101. this.setData({
  102. allDevice:res.data.list
  103. })
  104. }
  105. })
  106. },
  107. handleSelect(e) {
  108. wx.showToast({
  109. title: `已选择${e.detail.item.name}`,
  110. icon: 'none',
  111. duration: 1000
  112. })
  113. wx.setStorageSync('selectKey', e.detail.item.id)
  114. wx.setStorageSync('selectRoomName', e.detail.item.name)
  115. wx.setStorageSync('pid', e.detail.item.id)
  116. this.setData({
  117. selectKey: e.detail.item.id,
  118. selectRoomName: e.detail.item.name,
  119. pid:e.detail.item.id,
  120. show: false
  121. })
  122. this.getRoom2(e.detail.item.id)
  123. },
  124. bindroom() {
  125. this.setData({
  126. show: true
  127. });
  128. },
  129. onClose() {
  130. this.setData({
  131. show: false
  132. });
  133. },
  134. zh() {
  135. var time = moment().format("x")
  136. var str = this.toBin(time)
  137. console.log(str)
  138. var ces1 = IntToBytesLittleEndian(time, 6)
  139. var ces2 = BytesToIntBigEndian(ces1)
  140. this.setData({
  141. ces: time,
  142. ces1: ces1,
  143. ces2: ces2,
  144. })
  145. },
  146. toBin(str) {
  147. var arr = [];
  148. var remainder, i, str2, num, char;
  149. while (str.length > 0) {
  150. str2 = "";
  151. remainder = 0;
  152. for (i = 0; i < str.length; i++) { // str2 = str组成的十进制数 / 2
  153. num = str.charCodeAt(i) - 0x30; // num to String
  154. num = remainder * 10 + num;
  155. char = Math.floor(num / 2).toString();
  156. // 忽略最高为的0 , 即最高为如果是 0 则不放入 str2
  157. if (!(char === "0" && str2 === "")) {
  158. str2 += char;
  159. }
  160. remainder = num % 2;
  161. }
  162. str = str2;
  163. arr.push(remainder); // 保存余数
  164. }
  165. return arr.reverse().join('');
  166. },
  167. bindPickerChange: function (e) {
  168. console.log('picker发送选择改变,携带值为', e.detail.value)
  169. var array1 = [],
  170. children = []
  171. if (this.data.zNodes[e.detail.value].children) {
  172. children = this.data.zNodes[e.detail.value].children
  173. children.map(li => {
  174. array1.push(li.name)
  175. })
  176. }
  177. this.getRoom2(this.data.zNodes[e.detail.value].id)
  178. this.setData({
  179. index: e.detail.value,
  180. array1: array1
  181. })
  182. },
  183. bindPickerChange1: function (e) {
  184. console.log('picker发送选择改变,携带值为', e.detail.value)
  185. this.getRoom2(this.data.zNodes[this.data.index].children[e.detail.value].id)
  186. this.setData({
  187. index1: e.detail.value,
  188. })
  189. },
  190. getRoomList(){
  191. var data = {
  192. pageNum: 0,
  193. pageSize: 0
  194. };
  195. WXAPI.getSiteList(data).then(res => {
  196. var newArr = util.uniqueArray(res.data.list.list, "id");
  197. this.setData({
  198. treeList:newArr
  199. })
  200. })
  201. },
  202. getTree() {
  203. var _this = this
  204. WXAPI.getTree('').then(res => {
  205. var list=res.data.sort((pre, cur)=>pre.id - cur.id)
  206. var newArr = util.uniqueArray(list, "id");
  207. var setting = {
  208. data: {
  209. simpleData: {
  210. idKey: 'id',
  211. pIdKey: 'pid'
  212. },
  213. key: {
  214. children: 'children'
  215. }
  216. }
  217. }
  218. var dataList = this.transformTozTreeFormat(setting, newArr)
  219. var selectKey = wx.getStorageSync('selectKey') || ''
  220. var selectRoomName = wx.getStorageSync('selectRoomName') || dataList[0].name
  221. var pid = wx.getStorageSync('pid') || dataList[0].pid
  222. this.setData({
  223. dataList: dataList,
  224. // pid:dataList[0].pid,
  225. treeList:newArr,
  226. // selectRoomName:dataList[0].name,
  227. selectKey,selectRoomName,pid
  228. })
  229. this.getRoom()
  230. })
  231. },
  232. onChange(event) {
  233. this.setData({
  234. searchValue: event.detail
  235. })
  236. },
  237. godetail(e) {
  238. console.log(e)
  239. var IMEI = e.currentTarget.dataset.item
  240. // if (this.data.userInfo.type != 3) {
  241. if (IMEI.imei) {
  242. var q = {
  243. roomId: IMEI.id,
  244. // lockType:1,
  245. types:[1,21, 41],
  246. }
  247. var data1 = {
  248. q: JSON.stringify(q)
  249. }
  250. WXAPI.getDeviceItems(data1).then(res => {
  251. console.log(res);
  252. if (res.data.list.length > 0) {
  253. // wx.setStorage({
  254. // key: "deviceinfo",
  255. // data: res.data.list[0]
  256. // })
  257. wx.setStorageSync('deviceinfo', res.data.list[0])
  258. wx.navigateTo({
  259. url: "/pages/roomDetail/roomDetail"
  260. })
  261. } else {
  262. wx.showToast({
  263. title: "未发现此门锁信息",
  264. duration: 2000,
  265. icon: 'none'
  266. })
  267. }
  268. })
  269. } else {
  270. wx.showToast({
  271. title: "还未绑定门锁",
  272. duration: 2000,
  273. icon: 'none'
  274. })
  275. }
  276. // } else if (this.data.userInfo.type == 3) {
  277. // wx.setStorage({
  278. // key: "deviceinfo",
  279. // data: this.data.list[0]
  280. // })
  281. // wx.navigateTo({
  282. // url: "/pages/roomDetail/roomDetail"
  283. // })
  284. // }
  285. },
  286. // 搜索
  287. onClick(e) {
  288. var userInfo = this.data.userInfo
  289. this.setData({
  290. pageNum: 1,
  291. pid: '',
  292. list: []
  293. })
  294. wx.getStorage({
  295. key: "token",
  296. success: (res) => {
  297. if (userInfo.type != 3) {
  298. this.getRoom()
  299. } else if (userInfo.type == 3) {
  300. this.getRoom1()
  301. }
  302. },
  303. fail: (res) => {
  304. wx.redirectTo({
  305. url: "/pages/login/login"
  306. })
  307. }
  308. })
  309. },
  310. // 获取用户信息
  311. getuserInfo() {
  312. WXAPI.getuserInfo('').then(res => {
  313. this.setData({
  314. userInfo: res.data,
  315. });
  316. wx.setStorageSync("userInfo", res.data)
  317. })
  318. },
  319. findIndexArray(data,id,indexArray){
  320. let arr = Array.from(indexArray);
  321. for(let i=0,len=data.length;i<len;i++){
  322. arr.push(data[i].name);
  323. if(data[i].id===id){
  324. return arr
  325. }
  326. let children=data[i].children;
  327. if(children&&children.length){
  328. let result=this.findIndexArray(children,id,arr);
  329. if(result) return result
  330. }
  331. arr.pop()
  332. }
  333. return false
  334. },
  335. // 获取房间
  336. getRoom2(id) {
  337. var q = {
  338. ancestor: id,
  339. isRoom:1
  340. };
  341. console.log(q)
  342. var data = {
  343. q: JSON.stringify(q),
  344. pageNum: 1,
  345. pageSize: 20
  346. };
  347. WXAPI.getSiteList(data).then(res => {
  348. var stoppull = false
  349. if (res.data.list.list.length < 20) {
  350. stoppull = true
  351. }
  352. res.data.list.list.map(li=>{
  353. var name = this.findIndexArray(this.data.dataList, li.id, [])
  354. var path = ''
  355. if (name) {
  356. path = name.reverse().join('/')
  357. }
  358. li['path'] = path
  359. var device=this.data.allDevice.filter(item=>item.roomId==li.id)[0]
  360. if(device){
  361. li.deviceNum=device.deviceNum
  362. li['battery']=device.battery
  363. li['rssi']=device.rssi
  364. li['isOnline']=device.isOnline
  365. li['manufactureId']=device.manufactureId
  366. }else{
  367. li['battery']=''
  368. li['rssi']=''
  369. li['isOnline']=''
  370. li['manufactureId']=''
  371. }
  372. })
  373. var alllist = res.data.list.list
  374. this.setData({
  375. list: alllist,
  376. stoppull,
  377. pageNum: 1
  378. })
  379. })
  380. },
  381. // 获取房间
  382. getRoom() {
  383. var q = {
  384. ancestor: this.data.pid,
  385. isRoom:1,
  386. name:this.data.searchValue
  387. };
  388. var data = {
  389. q: JSON.stringify(q),
  390. pageNum: this.data.pageNum,
  391. pageSize: this.data.pageSize
  392. };
  393. WXAPI.getSiteList(data).then(res => {
  394. var stoppull = false
  395. if (res.data.list.list.length < 20) {
  396. stoppull = true
  397. }
  398. res.data.list.list.map(li=>{
  399. var name = this.findIndexArray(this.data.dataList, li.id, [])
  400. var path = ''
  401. if (name) {
  402. path = name.reverse().join('/')
  403. }
  404. li['path'] = path
  405. var device=this.data.allDevice.filter(item=>item.roomId==li.id)[0]
  406. if(device){
  407. li.deviceNum=device.deviceNum
  408. li['battery']=device.battery
  409. li['rssi']=device.rssi
  410. li['isOnline']=device.isOnline
  411. li['manufactureId']=device.manufactureId
  412. }else{
  413. li['battery']=''
  414. li['rssi']=''
  415. li['isOnline']=''
  416. li['manufactureId']=''
  417. }
  418. })
  419. var alllist = this.data.list.concat(res.data.list.list)
  420. this.setData({
  421. list: alllist,
  422. stoppull
  423. })
  424. })
  425. },
  426. transformTozTreeFormat(setting, sNodes) {
  427. var i, l,
  428. key = setting.data.simpleData.idKey,
  429. parentKey = setting.data.simpleData.pIdKey,
  430. childKey = setting.data.key.children
  431. if (!key || key == "" || !sNodes) return [];
  432. var r = [];
  433. var tmpMap = [];
  434. for (i = 0, l = sNodes.length; i < l; i++) {
  435. tmpMap[sNodes[i][key]] = sNodes[i];
  436. }
  437. for (i = 0, l = sNodes.length; i < l; i++) {
  438. if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
  439. if (!tmpMap[sNodes[i][parentKey]][childKey])
  440. tmpMap[sNodes[i][parentKey]][childKey] = [];
  441. tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
  442. } else {
  443. r.push(sNodes[i]);
  444. }
  445. }
  446. return r;
  447. },
  448. // 获取房间列表
  449. getRoom1() {
  450. var q = {
  451. number: this.data.userInfo.username,
  452. isRoom:1,
  453. name:this.data.searchValue
  454. };
  455. var data = {
  456. q: JSON.stringify(q),
  457. pageNum: 0,
  458. pageSize: 0
  459. };
  460. WXAPI.getlistNum(data).then(res => {
  461. if(res.success){
  462. var person=res.data.list[0]
  463. var query={}
  464. wx.setStorageSync('info', person)
  465. WXAPI.onlyTenant(query).then(res2=>{
  466. var dataList=res2.data.list.filter(item=> item.cardId==person.id)
  467. let list=[]
  468. dataList.map(li=>{
  469. var obj=this.data.treeList.filter(it=>it.id===li.roomId)[0]
  470. if(obj){
  471. list.push(obj)
  472. }
  473. var device=this.data.allDevice.filter(item=>item.roomId==li.id)[0]
  474. if(device){
  475. li.deviceNum=device.deviceNum
  476. li['battery']=device.battery
  477. li['rssi']=device.rssi
  478. li['isOnline']=device.isOnline
  479. li['manufactureId']=device.manufactureId
  480. }else{
  481. li['battery']=''
  482. li['rssi']=''
  483. li['isOnline']=''
  484. li['manufactureId']=''
  485. }
  486. })
  487. this.setData({
  488. list:list
  489. })
  490. })
  491. }else{
  492. wx.showToast({
  493. icon: "none",
  494. duration: 2000,
  495. title: "未找到该人员"
  496. })
  497. }
  498. })
  499. },
  500. qrcodefun() {
  501. var that = this
  502. wx.scanCode({
  503. success(res) {
  504. var deviceNum = ''
  505. if (res.result.indexOf('&') != -1) {
  506. deviceNum = res.result.split('&')[0]
  507. } else if(res.result.indexOf('WSL') != -1){
  508. deviceNum = res.result
  509. }else if (res.result.indexOf('_')) {
  510. deviceNum = "ICIN_" + res.result.split('_')[0]
  511. }
  512. console.log(res.result.indexOf('WSL'))
  513. var q = {
  514. deviceNum: deviceNum,
  515. // lockType:1,
  516. types:[1,21],
  517. };
  518. var data = {
  519. q: JSON.stringify(q),
  520. pageNum: 1,
  521. pageSize: 10,
  522. };
  523. WXAPI.getDeviceItems(data).then(res => {
  524. var len = res.data.list
  525. if (len.length > 0) {
  526. wx.setStorage({
  527. key: "deviceinfo",
  528. data: res.data.list[0]
  529. })
  530. wx.navigateTo({
  531. url: "/pages/roomDetail/roomDetail?open=true"
  532. })
  533. } else {
  534. wx.showToast({
  535. icon: "none",
  536. duration: 2000,
  537. title: "未在平台找到此设备!"
  538. })
  539. }
  540. })
  541. }
  542. })
  543. },
  544. onPullDownRefresh() {
  545. this.setData({
  546. list:[]
  547. })
  548. this.onLoad()
  549. setTimeout(()=>{
  550. wx.stopPullDownRefresh()
  551. },500)
  552. // this.setData({
  553. // pageNum: 1,
  554. // list: [],
  555. // stoppull: false
  556. // })
  557. // wx.getStorage({
  558. // key: "token",
  559. // success: (res) => {
  560. // wx.stopPullDownRefresh()
  561. // if (this.data.userInfo.type > 3) {
  562. // this.getRoom()
  563. // } else if (this.data.userInfo.type <= 3) {
  564. // this.getRoom1()
  565. // }
  566. // },
  567. // fail: (res) => {
  568. // wx.stopPullDownRefresh()
  569. // wx.redirectTo({
  570. // url: "/pages/login/login"
  571. // })
  572. // }
  573. // })
  574. },
  575. onReachBottom() {
  576. var userInfo = this.data.userInfo
  577. if (!this.data.stoppull) {
  578. wx.getStorage({
  579. key: "token",
  580. success: (res) => {
  581. var num = this.data.pageNum + 1
  582. this.setData({
  583. pageNum: num
  584. })
  585. if (userInfo.type != 3) {
  586. this.getRoom()
  587. } else if (userInfo.type == 3) {
  588. this.getRoom1()
  589. }
  590. },
  591. fail: (res) => {
  592. wx.redirectTo({
  593. url: "/pages/login/login"
  594. })
  595. }
  596. })
  597. } else {
  598. wx.showToast({
  599. title: '暂无更多数据',
  600. duration: 2000,
  601. icon: 'none'
  602. })
  603. }
  604. },
  605. closeblue() {
  606. wx.showLoading({
  607. title: "正在停止...",
  608. mask: true
  609. });
  610. app.globalData.ble.close()
  611. setTimeout(() => {
  612. wx.hideLoading()
  613. }, 1000);
  614. app.globalData.bleStatus = false
  615. },
  616. onShow: function () {
  617. wx.hideLoading()
  618. if (app.globalData.zgBlueStatus) {
  619. ICINLocker.disConnect(app.globalData.blueDeviceId);
  620. }
  621. if(app.globalData.bleStatus){
  622. this.closeblue()
  623. }
  624. },
  625. getUserProfile(e) {},
  626. getUserInfo(e) {
  627. // 不推荐使用getUserInfo获取用户信息,预计自2021年4月13日起,getUserInfo将不再弹出弹窗,并直接返回匿名的用户个人信息
  628. this.setData({
  629. userInfo: e.detail.userInfo,
  630. hasUserInfo: true
  631. })
  632. },
  633. onShareAppMessage() {}
  634. })