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.

551 lines
15 KiB

1 year ago
  1. // pages/Bluetooth1/Bluetooth1.js
  2. var util = require('../../utils/util');
  3. var md5 = require('../../utils/md5.js');
  4. import {
  5. BLE
  6. } from "../../utils/btls/ble";
  7. const app = getApp();
  8. const WXAPI = require('../../utils/request')
  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. var bleall = require('../../utils/bleall');
  21. import moment from "../../utils/moment"
  22. Page({
  23. /**
  24. * 页面的初始数据
  25. */
  26. data: {
  27. deviceInfo: {
  28. roomId: '',
  29. },
  30. dataTree: [],
  31. selectKey: '', //选中的节点id
  32. dataList: [],
  33. array: [],
  34. show: false,
  35. deviceNum: '',
  36. type: '1',
  37. cgInfo: '',
  38. lockStatus: "",
  39. userInfo: '',
  40. show1: false,
  41. actions: [{
  42. name: '静音',
  43. color: '#ee0a24'
  44. },
  45. {
  46. name: '低音',
  47. color: '#333'
  48. },
  49. {
  50. name: '高音',
  51. color: '#333'
  52. },
  53. ],
  54. volume: '',
  55. adminuserinfo: {
  56. enterpriseId: ''
  57. }
  58. },
  59. /**
  60. * 生命周期函数--监听页面加载
  61. */
  62. onLoad: function (options) {
  63. var adminuserinfo = wx.getStorageSync('userInfo')
  64. this.setData({
  65. deviceNum: options.deviceNum,
  66. selectindex: options.selectindex,
  67. adminuserinfo: adminuserinfo
  68. })
  69. var data = {
  70. q: JSON.stringify({
  71. deviceNum: options.deviceNum,
  72. // lockType:1,
  73. types:[1,21],
  74. })
  75. }
  76. WXAPI.getDeviceItems(data).then(res => {
  77. if (res.data.list.length == 0) {
  78. wx.showToast({
  79. title: '该智能锁未加入平台,请先加入平台',
  80. })
  81. wx.navigateBack({
  82. delta: 0,
  83. })
  84. this.setData({
  85. deviceInfo: ''
  86. })
  87. } else {
  88. app.globalData.deviceNum = res.data.list[0].deviceNum
  89. this.setData({
  90. deviceInfo: res.data.list[0]
  91. })
  92. }
  93. })
  94. this.getRoom()
  95. },
  96. unbindRoom() {
  97. wx.showModal({
  98. title: '是否确认重置锁',
  99. success: (result) => {
  100. if(result.confirm){
  101. var data = [{
  102. siteId: this.data.deviceInfo.roomId,
  103. deviceId: this.data.deviceInfo.id,
  104. }, ]
  105. WXAPI.unbindDeviceSite(data).then(res => {
  106. if (res.success) {
  107. var deviceInfo = this.data.deviceInfo
  108. deviceInfo['roomId'] = ''
  109. deviceInfo['roomName'] = ''
  110. this.setData({
  111. deviceInfo
  112. })
  113. wx.showToast({
  114. title: '解绑成功',
  115. duration: 2000,
  116. icon: 'none'
  117. })
  118. } else {
  119. wx.showToast({
  120. title: '解绑失败',
  121. duration: 2000,
  122. icon: 'none'
  123. })
  124. }
  125. })
  126. }
  127. }
  128. })
  129. },
  130. // 设置音量
  131. setVoice() {
  132. },
  133. // 蓝牙开锁
  134. openTheDoor(e) {
  135. if (e) {
  136. this.setData({
  137. current: e.currentTarget.dataset.current
  138. })
  139. }
  140. var that = this
  141. let userTelephone = '18270949468'
  142. let userPassword = '202108'
  143. let pwd = '2021110309'
  144. let openLockType = 2
  145. var timestamp = moment().format("x")
  146. bleall.openLock(userTelephone, userPassword, pwd, openLockType, timestamp, function (data) {
  147. if (data) {
  148. wx.showToast({
  149. title: '开锁成功',
  150. duration: 2000,
  151. icon: "none"
  152. })
  153. } else {
  154. wx.showToast({
  155. title: '开锁失败',
  156. duration: 2000,
  157. icon: "none"
  158. })
  159. }
  160. })
  161. },
  162. // 重置门锁
  163. resetLock(e) {
  164. wx.showModal({
  165. title: '是否确认重置锁',
  166. success: (result) => {
  167. if (result.confirm) {
  168. if (e) {
  169. this.setData({
  170. current: e.currentTarget.dataset.current
  171. })
  172. }
  173. var data = {
  174. managerTelephone: '18070949468',
  175. managerPassword: '202108'
  176. }
  177. var timestamp = moment().format("x")
  178. bleall.startFp_26(data.managerTelephone, data.managerPassword, timestamp, function (data) {
  179. if (data) {
  180. wx.showToast({
  181. title: '重置成功',
  182. duration: 2000,
  183. icon: "none"
  184. })
  185. var data2 = {
  186. q: JSON.stringify({
  187. id: this.data.deviceInfo.roomId
  188. })
  189. }
  190. WXAPI.getSiteTenant(data2).then(res2 => {
  191. console.log(res2, 'res22222')
  192. if (res2.success) {
  193. var ids = []
  194. res2.data.list.map(li => {
  195. var obj = {
  196. siteId: this.data.deviceInfo.roomId,
  197. cardId: li.cardId,
  198. }
  199. ids.push(obj)
  200. })
  201. WXAPI.unbindCard(ids).then(res3 => {
  202. if (res3.success) {
  203. wx.showToast({
  204. title: '退租成功',
  205. duration: 2000,
  206. icon: 'none'
  207. })
  208. } else {
  209. wx.showToast({
  210. title: '退租失败',
  211. duration: 2000,
  212. icon: 'none'
  213. })
  214. }
  215. })
  216. } else {
  217. wx.showToast({
  218. title: '退租失败',
  219. duration: 2000,
  220. icon: 'none'
  221. })
  222. }
  223. })
  224. } else {
  225. wx.showToast({
  226. title: '重置失败',
  227. duration: 2000,
  228. icon: "none"
  229. })
  230. }
  231. })
  232. }else{
  233. }
  234. },
  235. fail: (res) => {
  236. },
  237. })
  238. },
  239. // 获取房间
  240. getRoom() {
  241. var _this = this
  242. WXAPI.getTree('').then(res => {
  243. var newArr = util.uniqueArray(res.data, "id");
  244. this.list = newArr
  245. var setting = {
  246. data: {
  247. simpleData: {
  248. idKey: 'id',
  249. pIdKey: 'pid'
  250. },
  251. key: {
  252. children: 'children'
  253. }
  254. }
  255. }
  256. var dataList = this.transformTozTreeFormat(setting, newArr)
  257. this.setData({
  258. dataList: dataList
  259. })
  260. })
  261. },
  262. transformTozTreeFormat(setting, sNodes) {
  263. var i, l,
  264. key = setting.data.simpleData.idKey,
  265. parentKey = setting.data.simpleData.pIdKey,
  266. childKey = setting.data.key.children
  267. if (!key || key == "" || !sNodes) return [];
  268. var r = [];
  269. var tmpMap = [];
  270. for (i = 0, l = sNodes.length; i < l; i++) {
  271. tmpMap[sNodes[i][key]] = sNodes[i];
  272. }
  273. for (i = 0, l = sNodes.length; i < l; i++) {
  274. if (tmpMap[sNodes[i][parentKey]] && sNodes[i][key] != sNodes[i][parentKey]) {
  275. if (!tmpMap[sNodes[i][parentKey]][childKey])
  276. tmpMap[sNodes[i][parentKey]][childKey] = [];
  277. tmpMap[sNodes[i][parentKey]][childKey].push(sNodes[i]);
  278. } else {
  279. r.push(sNodes[i]);
  280. }
  281. }
  282. return r;
  283. },
  284. bindroom() {
  285. this.setData({
  286. show: true
  287. });
  288. },
  289. handleSelect(e) {
  290. var _this = this
  291. if (e.detail.tips) {
  292. wx.showToast({
  293. title: '请选择房间',
  294. icon: 'none'
  295. })
  296. } else {
  297. wx.showModal({
  298. title: '',
  299. content: '是否绑定',
  300. success(res) {
  301. if (res.confirm) {
  302. _this.setData({
  303. selectKey: e.detail.item.id,
  304. show: false
  305. })
  306. var data = {
  307. pageNum: 1,
  308. pageSize: 1,
  309. q: JSON.stringify({
  310. roomIds: [e.detail.item.id],
  311. // deviceNum:_this.data.deviceInfo.deviceNum,
  312. // lockType:1,
  313. types:[1,21],
  314. })
  315. }
  316. WXAPI.getDeviceItems(data).then(res => {
  317. if (res.data.list.length == 0) {
  318. var data2 = [{
  319. siteId: e.detail.item.id,
  320. deviceId: _this.data.deviceInfo.id,
  321. }]
  322. WXAPI.bindDeviceSite(data2).then(res2 => {
  323. if (res2.success) {
  324. _this.data.deviceInfo['roomId'] = e.detail.item.id
  325. var deviceInfo = _this.data.deviceInfo
  326. deviceInfo['roomId'] = e.detail.item.id
  327. deviceInfo['roomName'] = e.detail.item.name
  328. _this.setData({
  329. deviceInfo
  330. })
  331. wx.showToast({
  332. title: '绑定房间成功',
  333. icon: 'none'
  334. })
  335. // _this.addAdmin()
  336. } else {
  337. wx.showToast({
  338. title: '绑定房间失败',
  339. icon: 'none'
  340. })
  341. }
  342. })
  343. } else {
  344. wx.showToast({
  345. title: '该房间已绑定',
  346. icon: 'none'
  347. })
  348. }
  349. })
  350. } else if (res.cancel) {
  351. _this.setData({
  352. selectKey: e.detail.item.id,
  353. show: false
  354. })
  355. }
  356. }
  357. })
  358. }
  359. },
  360. addAdmin(e) {
  361. wx.showModal({
  362. title: '是否确认初始化管理员',
  363. success: (result) => {
  364. if (result.confirm) {
  365. if (e) {
  366. this.setData({
  367. current: e.currentTarget.dataset.current
  368. })
  369. }
  370. var _this = this
  371. var data = {
  372. deviceNum: this.data.deviceNum,
  373. managerTelephone: '18270949468',
  374. managerPassword: '680000',
  375. newPassword: '202108',
  376. key: '1234567890'
  377. }
  378. var timestamp = moment().format("x")
  379. bleall.startFp_23(data.deviceNum, data.managerTelephone, data.managerPassword, data.newPassword, data.key, timestamp, function (data) {
  380. console.log(data)
  381. if (data.success) {
  382. var data1 = {
  383. managerTelephone: "18270949468",
  384. managerPassword: '202108',
  385. userTelephone: '18270949468',
  386. userPassword: '202108',
  387. password: '2021110309',
  388. startTime: "20210813010101",
  389. endTime: "20680813010101"
  390. }
  391. bleall.startFp1_63(data1.managerTelephone, data1.managerPassword, data1.userTelephone, data1
  392. .userPassword, data1.password,
  393. data1.startTime, data1.endTime, 4, timestamp,
  394. function (res) {
  395. if (res.success) {
  396. bleall.startFp1_63(data1.managerTelephone, data1.managerPassword, data1.userTelephone, data1
  397. .userPassword, data1.password,
  398. data1.startTime, data1.endTime, 7, timestamp,
  399. function (res1) {
  400. console.log(res1)
  401. if (res1.success) {
  402. var data2 = {
  403. code: "ble/registerManager",
  404. data: {
  405. deviceNum: _this.data.deviceInfo.deviceNum,
  406. imei: _this.data.deviceInfo.imei,
  407. order: 6101,
  408. telephone: '18270949468',
  409. originPassword: "680000",
  410. newPassword: "202108",
  411. key: '1234567890',
  412. status:1,
  413. timestamp: timestamp
  414. }
  415. }
  416. WXAPI.sendCommand(data2).then(res2 => {
  417. console.log(res2)
  418. if (res2.success) {
  419. }
  420. })
  421. wx.showToast({
  422. title: '初始化管理员成功',
  423. duration: 2000,
  424. icon: "none"
  425. })
  426. }
  427. if (res1.data == "蓝牙功能暂不支持") {
  428. wx.showLoading({
  429. title: '重连中',
  430. mask: true
  431. })
  432. _this.startconnect()
  433. }
  434. })
  435. }
  436. })
  437. }
  438. if (data.data == "蓝牙功能暂不支持") {
  439. wx.showLoading({
  440. title: '重连中',
  441. mask: true
  442. })
  443. _this.startconnect()
  444. }
  445. })
  446. }
  447. }
  448. })
  449. },
  450. startconnect() {
  451. wx.showLoading({
  452. title: "连接中",
  453. mask: true
  454. })
  455. const ble = new BLE(this.data.deviceNum, emitter)
  456. app.globalData.ble = ble
  457. this.watchBLE()
  458. app.globalData.ble.init()
  459. // }
  460. },
  461. watchBLE() {
  462. var that = this
  463. if (app.globalData.ble) {
  464. app.globalData.ble.listen(res => {
  465. if (res.type == 'connect') {
  466. if (res.data == "未打开适配器") {
  467. wx.hideLoading()
  468. wx.showModal({
  469. title: "提示",
  470. content: "请检查手机蓝牙和定位功能是否打开?",
  471. showCancel: false,
  472. confirmText: "确定",
  473. success(res) {
  474. }
  475. });
  476. } else {
  477. if (res.data == '蓝牙已连接') {
  478. app.globalData.bleStatus = res.data
  479. if (this.data.current == "0") {
  480. this.openTheDoor()
  481. } else if (this.data.current == "1") {
  482. this.addAdmin()
  483. } else if (this.data.current == "2") {
  484. this.resetLock()
  485. }
  486. }
  487. }
  488. }
  489. })
  490. }
  491. },
  492. /**
  493. * 生命周期函数--监听页面初次渲染完成
  494. */
  495. onReady: function () {
  496. },
  497. /**
  498. * 生命周期函数--监听页面显示
  499. */
  500. onShow: function () {
  501. },
  502. /**
  503. * 生命周期函数--监听页面隐藏
  504. */
  505. onHide: function () {
  506. },
  507. /**
  508. * 生命周期函数--监听页面卸载
  509. */
  510. onUnload: function () {
  511. },
  512. /**
  513. * 页面相关事件处理函数--监听用户下拉动作
  514. */
  515. onPullDownRefresh: function () {
  516. },
  517. /**
  518. * 页面上拉触底事件的处理函数
  519. */
  520. onReachBottom: function () {
  521. },
  522. /**
  523. * 用户点击右上角分享
  524. */
  525. onShareAppMessage: function () {
  526. }
  527. })