選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

device-probing.js 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. import bleapi from "../../bluetooth/bleUtil.js"
  2. import { facapi } from "../../bluetooth/facUtil.js"
  3. import Api from "../../api/index.js"
  4. import { requestFnc } from "../../utils/request.js"
  5. import {
  6. vehicleTypeMap,
  7. cpuCardTypeMap,
  8. userTypeMap,
  9. plateColorToColorMap,
  10. } from "../../utils/systemConstant.js"
  11. import { desensitization } from "../../utils/index.js"
  12. Page({
  13. data: {
  14. cpuCardInfo: {
  15. cpuCardNo: "",
  16. licenseColor: "",
  17. licensePlate: "",
  18. tollVehicleType: "",
  19. issueId: "",
  20. cpuCardType: "",
  21. signDate: "",
  22. expirationDate: "",
  23. },
  24. obuCardInfo: {
  25. obuCardNo: "",
  26. licenseColor: "",
  27. licensePlate: "",
  28. tollVehicleType: "",
  29. signDate: "",
  30. expirationDate: "",
  31. activateState: "",
  32. },
  33. isLoading: false,
  34. isDisabled: false,
  35. bluetoothName: "",
  36. bluethList: [],
  37. show: false,
  38. needShowList: false,
  39. connectLoaingText: "请稍后...",
  40. },
  41. onLoad: function () {
  42. this.closeBle()
  43. // wx.navigateTo({
  44. // url: 'plugin://issuer-plugin/monthly-bill-detail',
  45. // })
  46. },
  47. goNextProbingObu() {
  48. this.setData({
  49. obuCardInfo: {},
  50. cpuCardInfo: {},
  51. })
  52. this.scanDevice((isConnected) => {
  53. if (isConnected) {
  54. this.probingObu()
  55. }
  56. })
  57. },
  58. endLoading() {
  59. this.setData({
  60. isLoading: false,
  61. })
  62. },
  63. // 开启扫描蓝牙设备
  64. scanDevice(callback) {
  65. this.setData({
  66. isLoading: true,
  67. })
  68. if (this.needShowList) {
  69. this.setData({
  70. connectLoaingText: "设备搜索中...",
  71. })
  72. }
  73. bleapi.ScanDevice([], this.needShowList, (devResult, list) => {
  74. console.log("ScanDevice", devResult)
  75. if (devResult.code != 0) {
  76. console.log("搜索失败", devResult)
  77. this.endLoading()
  78. // 正常列表展示搜索结束不弹窗
  79. if (devResult.code === 99) {
  80. return
  81. }
  82. //搜索设备失败
  83. this.showModal({
  84. title: "错误",
  85. devResult: devResult,
  86. content: devResult.err_msg,
  87. showMyContent: true,
  88. })
  89. } else {
  90. if (this.needShowList) {
  91. this.setData({
  92. bluethList: list,
  93. show: true,
  94. isLoading: false,
  95. })
  96. } else {
  97. console.log(
  98. "搜索到设备:" +
  99. devResult +
  100. " " +
  101. devResult.data.device_name
  102. )
  103. this.connectDevice(devResult, callback)
  104. this.setData({
  105. bluetoothName: devResult.data.device_name,
  106. })
  107. }
  108. }
  109. })
  110. },
  111. // sdk连接蓝牙
  112. connectDevice(devResult, callback) {
  113. facapi.ConnectDevice(
  114. devResult.data,
  115. (onDisconnect) => {
  116. console.log("连接回调:" + onDisconnect)
  117. },
  118. (result) => {
  119. bleapi.StopScanDevice(function (code) {
  120. console.log("返回数据", code)
  121. })
  122. if (result.code == 0) {
  123. console.log("连接标签设备成功")
  124. callback(true)
  125. } else {
  126. this.showModal({
  127. title: "错误",
  128. devResult: result,
  129. content: "设备连接失败,请将手机靠近设备后重试。",
  130. })
  131. callback(false)
  132. }
  133. }
  134. )
  135. },
  136. probingCpu() {
  137. let cpuCardInfo = {}
  138. facapi.OpenCard((devResult) => {
  139. console.log("OpenCard-devResult=>", devResult)
  140. if (devResult.code == 0) {
  141. facapi.GetCardNo((devResult) => {
  142. if (devResult.code == 0) {
  143. console.log("|||=>>GetCardNo-devResult={}", devResult)
  144. cpuCardInfo.cpuCardNo = devResult.data || ""
  145. cpuCardInfo.encryptionCpuCardNo =
  146. desensitization(devResult.data, 4, -4) || ""
  147. this.setData({
  148. cpuCardInfo,
  149. })
  150. this.getCardFile0016()
  151. } else {
  152. this.showModal({
  153. title: "错误",
  154. devResult: devResult,
  155. content:
  156. "获取卡号失败:" +
  157. devResult.code +
  158. ":" +
  159. devResult.err_msg +
  160. (devResult.msg ? ":" + devResult.msg : ""),
  161. })
  162. }
  163. })
  164. } else {
  165. this.showModal({
  166. title: "错误",
  167. devResult: devResult,
  168. content:
  169. "打开卡失败:" +
  170. devResult.code +
  171. ":" +
  172. devResult.err_msg +
  173. (devResult.msg ? ":" + devResult.msg : ""),
  174. })
  175. }
  176. })
  177. },
  178. probingObu() {
  179. facapi.OpenChannel((devResult) => {
  180. console.log("sdk的OpenChannel调用")
  181. if (devResult.code == 0) {
  182. // 读取设备sn号
  183. facapi.GetSerialNo((devResult) => {
  184. console.log("getSerialNo==>>", JSON.stringify(devResult))
  185. this.getObuSystemInfo(devResult)
  186. })
  187. } else {
  188. this.showModal({
  189. title: "错误",
  190. devResult: devResult,
  191. content:
  192. "打开Obu失败:" +
  193. devResult.code +
  194. ":" +
  195. devResult.err_msg +
  196. (devResult.msg ? ":" + devResult.msg : ""),
  197. })
  198. }
  199. })
  200. },
  201. getCardFile0016() {
  202. facapi.GetCardFile16((devResult) => {
  203. console.log("GetCardFile0016-devResult=>", devResult)
  204. if (devResult.code == 0) {
  205. this.getCardFile0015()
  206. } else {
  207. this.endLoading()
  208. this.disConnect()
  209. }
  210. })
  211. },
  212. getCardFile0015() {
  213. let cpuCardInfo = {
  214. cpuCardNo: this.data.cpuCardInfo.cpuCardNo,
  215. encryptionCpuCardNo: this.data.cpuCardInfo.encryptionCpuCardNo,
  216. }
  217. facapi.GetCardFile15((devResult) => {
  218. console.log("GetCardFile0015-devResult=>", devResult)
  219. if (devResult.code == 0) {
  220. console.log(
  221. " getCardFile0015-this.cpuCardInfo = >",
  222. devResult.code
  223. )
  224. let data = {
  225. filename: Api.systemDecoder.filename,
  226. data: {
  227. type: "3",
  228. content: devResult.data,
  229. },
  230. }
  231. requestFnc(
  232. Api.systemDecoder.url,
  233. data,
  234. (res) => {
  235. console.log(
  236. "getCardFile0015-Api.systemDecoder-res=>",
  237. res
  238. )
  239. //车牌号脱敏
  240. var licensePlate = res.licensePlate
  241. let begain = 3 + (licensePlate.length - 7)
  242. cpuCardInfo.licensePlate =
  243. desensitization(res.licensePlate, begain, -2) || ""
  244. cpuCardInfo.licenseColor = plateColorToColorMap.get(
  245. `${res.licenseColor}`
  246. )
  247. cpuCardInfo.tollVehicleType = vehicleTypeMap.get(`${res.carType}`)
  248. cpuCardInfo.issueId = res.netWorkCode || ""
  249. cpuCardInfo.cpuCardType = cpuCardTypeMap.get(`${res.cardType}`)
  250. cpuCardInfo.signDate = res.signedDate || ""
  251. if (
  252. cpuCardInfo.signDate != "" &&
  253. cpuCardInfo.signDate.length == 8
  254. ) {
  255. cpuCardInfo.signDate =
  256. cpuCardInfo.signDate.substring(0, 4) +
  257. "-" +
  258. cpuCardInfo.signDate.substring(4, 6) +
  259. "-" +
  260. cpuCardInfo.signDate.substring(6, 8)
  261. }
  262. cpuCardInfo.expirationDate = res.expireDate || ""
  263. if (
  264. cpuCardInfo.expirationDate != "" &&
  265. cpuCardInfo.expirationDate.length == 8
  266. ) {
  267. cpuCardInfo.expirationDate =
  268. cpuCardInfo.expirationDate.substring(0, 4) +
  269. "-" +
  270. cpuCardInfo.expirationDate.substring(4, 6) +
  271. "-" +
  272. cpuCardInfo.expirationDate.substring(6, 8)
  273. }
  274. cpuCardInfo.userType = userTypeMap.get(
  275. `${res.userType}`
  276. )
  277. this.setData({
  278. cpuCardInfo,
  279. })
  280. this.endLoading()
  281. this.disConnect()
  282. },
  283. (error) => {
  284. console.log(
  285. "getCardFile0015-Api.systemDecoder-error=>",
  286. error
  287. )
  288. this.endLoading()
  289. this.disConnect()
  290. }
  291. )
  292. } else {
  293. this.endLoading()
  294. this.disConnect()
  295. }
  296. })
  297. },
  298. getObuSystemInfo(devResult) {
  299. let obuCardInfo = {}
  300. console.log("|||getSystemInfo==>", JSON.stringify(devResult))
  301. if (devResult.code == 0) {
  302. obuCardInfo.obuCardNo = devResult.data || ""
  303. facapi.GetSystemInfo((devResult) => {
  304. console.log("getSystemInfo=>", devResult)
  305. let data = {
  306. filename: Api.systemDecoder.filename,
  307. data: {
  308. type: "1",
  309. content: devResult.data,
  310. },
  311. }
  312. requestFnc(
  313. Api.systemDecoder.url,
  314. data,
  315. (res) => {
  316. console.log(
  317. "getObuSystemInfo-Api.systemDecoder-res=>>",
  318. res
  319. )
  320. obuCardInfo.obuCardNo = res.serialNumber || ""
  321. obuCardInfo.encryptionObuCardNo =
  322. desensitization(res.serialNumber, 4, -4) || ""
  323. obuCardInfo.signDate = res.signedDate || ""
  324. obuCardInfo.expirationDate = res.expiredDate || ""
  325. if (
  326. obuCardInfo.signDate != "" &&
  327. obuCardInfo.signDate.length == 8
  328. ) {
  329. obuCardInfo.signDate =
  330. obuCardInfo.signDate.substring(0, 4) +
  331. "-" +
  332. obuCardInfo.signDate.substring(4, 6) +
  333. "-" +
  334. obuCardInfo.signDate.substring(6, 8)
  335. }
  336. if (
  337. obuCardInfo.expirationDate != "" &&
  338. obuCardInfo.expirationDate.length == 8
  339. ) {
  340. obuCardInfo.expirationDate =
  341. obuCardInfo.expirationDate.substring(0, 4) +
  342. "-" +
  343. obuCardInfo.expirationDate.substring(4, 6) +
  344. "-" +
  345. obuCardInfo.expirationDate.substring(6, 8)
  346. }
  347. obuCardInfo.tearFlag = res.tearFlag
  348. this.setData({
  349. obuCardInfo,
  350. })
  351. facapi.OpenCard((devResult) => {
  352. this.getVehicleInfo(devResult)
  353. })
  354. },
  355. (error) => {
  356. console.log(
  357. "getObuSystemInfo-Api.systemDecoder-error=>",
  358. error
  359. )
  360. this.endLoading()
  361. this.disConnect()
  362. }
  363. )
  364. })
  365. } else {
  366. this.showModal({
  367. title: "错误",
  368. devResult: devResult,
  369. content:
  370. "获取Obu编号失败:" +
  371. devResult.code +
  372. ":" +
  373. devResult.err_msg +
  374. (devResult.msg ? ":" + devResult.msg : ""),
  375. })
  376. this.endLoading()
  377. this.disConnect()
  378. }
  379. },
  380. getVehicleInfo(devResult) {
  381. console.log("|||getVehicleInfo==>", JSON.stringify(devResult))
  382. if (devResult.code == 0) {
  383. facapi.GetVehicleInfo("1122334455667788", (devResult) => {
  384. console.log("getVehicleInfo=>", devResult)
  385. let params = {
  386. filename: Api.obuRead.filename,
  387. data: {
  388. obuId: this.data.obuCardInfo.obuCardNo,
  389. encryptedInfo: devResult.data,
  390. },
  391. }
  392. console.log("获取车辆信息入参", params)
  393. requestFnc(
  394. Api.obuRead.url,
  395. params,
  396. (res) => {
  397. console.log("obuRead", res)
  398. let vehicleInfo = JSON.parse(res.vehicleInfo)
  399. console.log(
  400. "GetVehicleInfo-res.issue_info=>",
  401. vehicleInfo
  402. )
  403. this.data.obuCardInfo.licenseColor =
  404. plateColorToColorMap.get(
  405. `${vehicleInfo.licenseColor}`
  406. )
  407. //车牌号脱敏
  408. var licensePlate = vehicleInfo.licensePlate
  409. let begain = 3 + (licensePlate.length - 7)
  410. this.data.obuCardInfo.licensePlate =
  411. desensitization(
  412. vehicleInfo.licensePlate,
  413. begain,
  414. -2
  415. ) || ""
  416. this.data.obuCardInfo.tollVehicleType =
  417. vehicleTypeMap.get(`${vehicleInfo.carType}`)
  418. this.data.obuCardInfo.userType = userTypeMap.get(
  419. `${vehicleInfo.userType}`
  420. )
  421. let typeObj = {
  422. "00": "未激活",
  423. "01": "已激活",
  424. }
  425. if (this.data.obuCardInfo.licensePlate) {
  426. typeObj["00"] = "标签异常"
  427. }
  428. this.data.obuCardInfo.activateState =
  429. typeObj[this.data.obuCardInfo.tearFlag] || "未知"
  430. this.setData({
  431. obuCardInfo: this.data.obuCardInfo,
  432. })
  433. this.probingCpu()
  434. },
  435. (error) => {
  436. console.log("GetVehicleInfo-Api.obuRead-error=>", error)
  437. this.endLoading()
  438. this.disConnect()
  439. }
  440. )
  441. })
  442. } else {
  443. this.showModal({
  444. title: "错误",
  445. devResult: devResult,
  446. content:
  447. "获取车辆失败:" +
  448. devResult.code +
  449. ":" +
  450. devResult.err_msg +
  451. (devResult.msg ? ":" + devResult.msg : ""),
  452. })
  453. this.endLoading()
  454. this.disConnect()
  455. }
  456. },
  457. disConnect() {
  458. if (facapi.facSdk && facapi.facSdk.DisconnectDevice) {
  459. facapi.facSdk.DisconnectDevice(function (code) {
  460. console.log("关闭连接结果", code)
  461. })
  462. }
  463. // 完成后关闭蓝牙模块
  464. bleapi.CloseBle((obj) => {
  465. console.log(obj)
  466. })
  467. },
  468. closeBle() {
  469. // 进入前关闭蓝牙,初始化
  470. bleapi.CheckBle((res) => {
  471. if (res.available) {
  472. this.disConnect()
  473. }
  474. })
  475. },
  476. //显示弹框
  477. showModal(data) {
  478. this.endLoading()
  479. this.disConnect()
  480. //显示弹框
  481. wx.showModal({
  482. title: "提示",
  483. content: data.content,
  484. showCancel: false,
  485. })
  486. },
  487. })