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.

348 lines
14 KiB

1 year ago
  1. import ICINLocker from "./ICINPakage/ICINLocker"
  2. const WXAPI = require('./request')
  3. import moment from "./moment"
  4. var bleall = require('./bleall');
  5. import {
  6. BLE
  7. } from "./btls/ble";
  8. const app = getApp();
  9. const emitter = app.globalData.emitter
  10. // 锁端连接蓝牙
  11. function bluetoothConnectivity(deviceNum,manufactureId){
  12. var that=this
  13. return new Promise((resolve, reject)=>{
  14. if(manufactureId==201||manufactureId==221){
  15. let data={
  16. status:'',
  17. msg:''
  18. }
  19. wx.showLoading({
  20. title: '连接蓝牙中',
  21. mask: true
  22. })
  23. ICINLocker.scanDevices(deviceNum)
  24. .then((res) => {
  25. if (res.status === 0) {
  26. app.globalData.blueDeviceId = res.deviceId
  27. ICINLocker.connect(res.deviceId,false) // 此处 deviceId 存起来,当断开连接时使用
  28. .then((res1) => {
  29. if (res1.status === 0) {
  30. data.status='00'
  31. data.msg='连接成功'
  32. wx.hideLoading()
  33. wx.showToast({
  34. title: '连接成功',
  35. icon: "none",
  36. duration: 2000
  37. })
  38. app.globalData.zgBlueStatus = true
  39. resolve(data)
  40. }else{
  41. data.status='01'
  42. data.msg='连接失败'
  43. wx.hideLoading()
  44. var msg = ''
  45. switch (res.errCode) {
  46. case 10001:
  47. msg = '调用失败,请打开蓝牙!';
  48. break;
  49. }
  50. wx.showToast({
  51. title: msg,
  52. duration: 2000,
  53. icon: 'none'
  54. })
  55. reject(data)
  56. }
  57. })
  58. }else{
  59. data.status='01'
  60. data.msg='搜索失败'
  61. wx.hideLoading()
  62. wx.showToast({
  63. title: '搜索失败',
  64. duration: 2000,
  65. icon: 'none'
  66. })
  67. reject(data)
  68. }
  69. }).catch((res) => {
  70. if (res.status == 1) {
  71. if (res.statusReason.errCode == 10001) {
  72. data.status='01'
  73. data.msg='请检查手机蓝牙和定位功能是否打开?'
  74. wx.hideLoading()
  75. wx.showToast({
  76. title: '请检查手机蓝牙和定位功能是否打开?',
  77. duration: 2000,
  78. icon: 'none'
  79. })
  80. reject(data)
  81. }else if(res.statusReason.errCode == 1000){
  82. data.status='01'
  83. data.msg='未找到该设备'
  84. wx.hideLoading()
  85. wx.showToast({
  86. title: '未找到该设备',
  87. duration: 2000,
  88. icon: 'none'
  89. })
  90. reject(data)
  91. }else {
  92. if (!app.globalData.zgBlueStatus && app.globalData.blueStatus) {
  93. that.bluetoothConnectivity()
  94. } else {
  95. data.status='01'
  96. data.msg=res.statusReason
  97. wx.hideLoading()
  98. wx.showToast({
  99. title: res.statusReason,
  100. duration: 2000,
  101. icon: 'none'
  102. })
  103. reject(data)
  104. }
  105. }
  106. }else{
  107. data.status='01'
  108. data.msg='连接失败'
  109. wx.hideLoading()
  110. reject(data)
  111. }
  112. })
  113. }else if(manufactureId==101){
  114. let data={
  115. status:'',
  116. msg:''
  117. }
  118. wx.showLoading({
  119. title: '连接蓝牙中',
  120. mask: true
  121. })
  122. const ble = new BLE(deviceNum, emitter)
  123. app.globalData.ble = ble
  124. app.globalData.ble.init()
  125. if (app.globalData.ble && app.globalData.bleStatus) {
  126. app.globalData.ble.listen(res => {
  127. if (res.type == 'connect') {
  128. if (res.data == "未打开适配器") {
  129. wx.hideLoading()
  130. wx.showToast({
  131. title: '请检查手机蓝牙和定位功能是否打开?',
  132. duration: 2000,
  133. icon: 'none'
  134. })
  135. data.status='01'
  136. data.msg='请检查手机蓝牙和定位功能是否打开?'
  137. reject(data)
  138. } else {
  139. if (res.data == '蓝牙已连接') {
  140. app.globalData.bleStatus = res.data
  141. data.status='00'
  142. data.msg='连接成功'
  143. resolve(data)
  144. }
  145. }
  146. }
  147. })
  148. }
  149. }
  150. })
  151. }
  152. // ZH,GX1000蓝牙开锁
  153. function bluetoothUnlocking(data,manufactureId){
  154. wx.showLoading({
  155. title: '开锁中',
  156. mask:true,
  157. })
  158. if(manufactureId==201||manufactureId==221){
  159. ICINLocker.getSignKey(data.lockerId, data.superId,data.superId, (callback) => {
  160. console.log(callback,'管理员获取密钥')
  161. if (callback.status == '00') {
  162. var userSignKey = callback.signKey?callback.signKey:callback.commonKey||''
  163. ICINLocker.getCommonKey(data.lockerId, data.superId, data.superId, userSignKey, (callback1) => {
  164. if(callback1.status=='00'){
  165. var commonKey = callback1.commonKey
  166. ICINLocker.openTheDoor(data.lockerId, data.superId, userSignKey, data.superId, '', '00', commonKey, (callback2) => {
  167. if(callback2.status=='00'){
  168. var rqTime=moment().format("X")
  169. var username=wx.getStorageSync('username'),password=wx.getStorageSync('password')
  170. var str=`${username}+${password}+${rqTime}`
  171. var signature=md5(str)
  172. var data = {
  173. code: "zg/bleUnlockRecord",
  174. data: {
  175. lockerId:data.lockerId,
  176. lockerUserId: data.superId,
  177. roomId:data.roomId,
  178. roomName:data.roomName,
  179. cardName:username,
  180. signature:signature
  181. }
  182. }
  183. console.log(data,'蓝牙开锁')
  184. wx.hideLoading()
  185. wx.showToast({
  186. title: '开锁成功',
  187. duration:2000,
  188. icon:'none'
  189. })
  190. WXAPI.sendCommand(data).then(res => {
  191. console.log(res)
  192. })
  193. }else{
  194. wx.hideLoading()
  195. wx.showToast({
  196. title: '开锁失败',
  197. duration:2000,
  198. icon:'none'
  199. })
  200. }
  201. })
  202. }else{
  203. wx.hideLoading()
  204. wx.showToast({
  205. title: '连接失败',
  206. duration:2000,
  207. icon:'none'
  208. })
  209. }
  210. })
  211. }else{
  212. wx.hideLoading()
  213. }
  214. });
  215. }else if(manufactureId==101){
  216. let userTelephone = "18270949468" //JSON.parse(this.data.deviceinfo.info).userTelephone
  217. let userPassword = "202108" //JSON.parse(this.data.deviceinfo.info).userPassword
  218. let pwd = "2021110309" //JSON.parse(this.data.deviceinfo.info).userPassword
  219. let openLockType = 2
  220. var userInfo=wx.getStorageSync('userInfo')
  221. var timestamp = moment().format("x")
  222. bleall.openLock(userTelephone, userPassword, pwd, openLockType, timestamp, function (res) {
  223. if (res.success) {
  224. var rqTime=moment().format("X")
  225. var username=wx.getStorageSync('username'),password=wx.getStorageSync('password')
  226. var str=`${username}+${password}+${rqTime}`
  227. var signature=md5(str)
  228. var data = {
  229. code: "ld/bleUnlockRecord",
  230. data: {
  231. deviceNum: data.deviceNum,
  232. userTelephone: userTelephone,
  233. password: userPassword,
  234. roomId:data.roomId,
  235. roomName:data.roomName,
  236. cardName:userInfo.username,
  237. signature:signature
  238. }
  239. }
  240. WXAPI.sendCommand(data).then(res => {
  241. })
  242. }
  243. })
  244. }
  245. }
  246. // ZH锁蓝牙操作
  247. /*
  248. 1--添加用户
  249. 2--删除用户
  250. 3--添加密码
  251. 4--删除密码
  252. 5--添加指纹
  253. 6--删除指纹
  254. 7--添加卡
  255. 8--删除卡
  256. */
  257. function bluetoothOperation_ZH(data,info,type){
  258. return new Promise((resolve, reject)=>{
  259. wx.showLoading({
  260. title: '下发中',
  261. mask:true,
  262. })
  263. if(type==1){
  264. ICINLocker.getSignKey(data.lockerId, data.superId, data.superId, (callback) => {
  265. if(callback.status=='00'){
  266. var signKey = callback.signKey
  267. ICINLocker.getCommonKey(data.lockerId, data.superId, data.superId, signKey, (callback1) => {
  268. if(callback1.status=='00'){
  269. var commonKey = callback1.commonKey
  270. ICINLocker.addUserToLock(data.lockerId, data.superId, info.usrID, data.superId, '00', signKey, '', commonKey, info.startTime, info.endTime, (callback2) => {
  271. if(callback2.status=='00'){
  272. resolve(callback2)
  273. }else{
  274. reject(callback2)
  275. }
  276. })
  277. }else{
  278. reject(callback1)
  279. }
  280. })
  281. }else{
  282. reject(callback)
  283. }
  284. })
  285. }else{
  286. ICINLocker.getUserSignKey(data.lockerId, data.superId, info.userPassword, data.superId, data.authKey, '01', (callback) => {
  287. if(callback.status=='00'){
  288. var userSignKey = callback.userSignKey
  289. ICINLocker.getCommonKey(data.lockerId, info.userPassword, info.userPassword, userSignKey, (callback1) => {
  290. if(callback1.status=='00'){
  291. var commonKey = callback1.commonKey
  292. if(type==3){
  293. console.log(data,info,commonKey)
  294. ICINLocker.handlePwd(data.lockerId, data.superId, info.userPassword, info.password, info.pwdNo, data.superId, data.authKey, '', info.startTime, info.endTime, 255, commonKey, (callback2) => {
  295. if(callback2.status=='00'){
  296. resolve(callback2)
  297. }else{
  298. reject(callback2)
  299. }
  300. })
  301. }else if(type==5){
  302. wx.showLoading({
  303. title: '请在锁上按压手指',
  304. mask:true,
  305. })
  306. ICINLocker.addFingerPrints(data.lockerId, data.superId, info.userPassword, data.superId, '', data.authKey, info.startTime, info.endTime, '00', commonKey, (callback2) => {
  307. if(callback2.status=='00' && callback2.completeFlag=='01'){
  308. resolve(callback2)
  309. }else if(callback2.status=='ff' && callback2.completeFlag=='00'){
  310. wx.showToast({
  311. title: '录入失败',
  312. icon:'none'
  313. })
  314. reject(callback2)
  315. }
  316. })
  317. }else if(type==7){
  318. wx.showLoading({
  319. title: '请在锁上按键区刷卡',
  320. mask:true,
  321. })
  322. ICINLocker.RfCardRegister(data.lockerId, data.superId, info.userPassword, data.superId, info.pwdNo, '',data.authKey, info.startTime, info.endTime, commonKey, (callback2) => {
  323. if(callback2.status=='00'){
  324. resolve(callback2)
  325. }else{
  326. reject(callback2)
  327. }
  328. })
  329. }
  330. }else{
  331. reject(callback1)
  332. }
  333. })
  334. }else{
  335. reject(callback)
  336. }
  337. })
  338. }
  339. })
  340. }
  341. module.exports = {
  342. bluetoothUnlocking,
  343. bluetoothConnectivity,
  344. bluetoothOperation_ZH,
  345. }