import bleapi from "../../bluetooth/bleUtil.js" import { facapi } from "../../bluetooth/facUtil.js" import Api from "../../api/index.js" import { requestFnc } from "../../utils/request.js" import { vehicleTypeMap, cpuCardTypeMap, userTypeMap, plateColorToColorMap, } from "../../utils/systemConstant.js" import { desensitization } from "../../utils/index.js" Page({ data: { cpuCardInfo: { cpuCardNo: "", licenseColor: "", licensePlate: "", tollVehicleType: "", issueId: "", cpuCardType: "", signDate: "", expirationDate: "", }, obuCardInfo: { obuCardNo: "", licenseColor: "", licensePlate: "", tollVehicleType: "", signDate: "", expirationDate: "", activateState: "", }, isLoading: false, isDisabled: false, bluetoothName: "", bluethList: [], show: false, needShowList: false, connectLoaingText: "请稍后...", }, onLoad: function () { this.closeBle() // wx.navigateTo({ // url: 'plugin://issuer-plugin/monthly-bill-detail', // }) }, goNextProbingObu() { this.setData({ obuCardInfo: {}, cpuCardInfo: {}, }) this.scanDevice((isConnected) => { if (isConnected) { this.probingObu() } }) }, endLoading() { this.setData({ isLoading: false, }) }, // 开启扫描蓝牙设备 scanDevice(callback) { this.setData({ isLoading: true, }) if (this.needShowList) { this.setData({ connectLoaingText: "设备搜索中...", }) } bleapi.ScanDevice([], this.needShowList, (devResult, list) => { console.log("ScanDevice", devResult) if (devResult.code != 0) { console.log("搜索失败", devResult) this.endLoading() // 正常列表展示搜索结束不弹窗 if (devResult.code === 99) { return } //搜索设备失败 this.showModal({ title: "错误", devResult: devResult, content: devResult.err_msg, showMyContent: true, }) } else { if (this.needShowList) { this.setData({ bluethList: list, show: true, isLoading: false, }) } else { console.log( "搜索到设备:" + devResult + " " + devResult.data.device_name ) this.connectDevice(devResult, callback) this.setData({ bluetoothName: devResult.data.device_name, }) } } }) }, // sdk连接蓝牙 connectDevice(devResult, callback) { facapi.ConnectDevice( devResult.data, (onDisconnect) => { console.log("连接回调:" + onDisconnect) }, (result) => { bleapi.StopScanDevice(function (code) { console.log("返回数据", code) }) if (result.code == 0) { console.log("连接标签设备成功") callback(true) } else { this.showModal({ title: "错误", devResult: result, content: "设备连接失败,请将手机靠近设备后重试。", }) callback(false) } } ) }, probingCpu() { let cpuCardInfo = {} facapi.OpenCard((devResult) => { console.log("OpenCard-devResult=>", devResult) if (devResult.code == 0) { facapi.GetCardNo((devResult) => { if (devResult.code == 0) { console.log("|||=>>GetCardNo-devResult={}", devResult) cpuCardInfo.cpuCardNo = devResult.data || "" cpuCardInfo.encryptionCpuCardNo = desensitization(devResult.data, 4, -4) || "" this.setData({ cpuCardInfo, }) this.getCardFile0016() } else { this.showModal({ title: "错误", devResult: devResult, content: "获取卡号失败:" + devResult.code + ":" + devResult.err_msg + (devResult.msg ? ":" + devResult.msg : ""), }) } }) } else { this.showModal({ title: "错误", devResult: devResult, content: "打开卡失败:" + devResult.code + ":" + devResult.err_msg + (devResult.msg ? ":" + devResult.msg : ""), }) } }) }, probingObu() { facapi.OpenChannel((devResult) => { console.log("sdk的OpenChannel调用") if (devResult.code == 0) { // 读取设备sn号 facapi.GetSerialNo((devResult) => { console.log("getSerialNo==>>", JSON.stringify(devResult)) this.getObuSystemInfo(devResult) }) } else { this.showModal({ title: "错误", devResult: devResult, content: "打开Obu失败:" + devResult.code + ":" + devResult.err_msg + (devResult.msg ? ":" + devResult.msg : ""), }) } }) }, getCardFile0016() { facapi.GetCardFile16((devResult) => { console.log("GetCardFile0016-devResult=>", devResult) if (devResult.code == 0) { this.getCardFile0015() } else { this.endLoading() this.disConnect() } }) }, getCardFile0015() { let cpuCardInfo = { cpuCardNo: this.data.cpuCardInfo.cpuCardNo, encryptionCpuCardNo: this.data.cpuCardInfo.encryptionCpuCardNo, } facapi.GetCardFile15((devResult) => { console.log("GetCardFile0015-devResult=>", devResult) if (devResult.code == 0) { console.log( " getCardFile0015-this.cpuCardInfo = >", devResult.code ) let data = { filename: Api.systemDecoder.filename, data: { type: "3", content: devResult.data, }, } requestFnc( Api.systemDecoder.url, data, (res) => { console.log( "getCardFile0015-Api.systemDecoder-res=>", res ) //车牌号脱敏 var licensePlate = res.licensePlate let begain = 3 + (licensePlate.length - 7) cpuCardInfo.licensePlate = desensitization(res.licensePlate, begain, -2) || "" cpuCardInfo.licenseColor = plateColorToColorMap.get( `${res.licenseColor}` ) cpuCardInfo.tollVehicleType = vehicleTypeMap.get(`${res.carType}`) cpuCardInfo.issueId = res.netWorkCode || "" cpuCardInfo.cpuCardType = cpuCardTypeMap.get(`${res.cardType}`) cpuCardInfo.signDate = res.signedDate || "" if ( cpuCardInfo.signDate != "" && cpuCardInfo.signDate.length == 8 ) { cpuCardInfo.signDate = cpuCardInfo.signDate.substring(0, 4) + "-" + cpuCardInfo.signDate.substring(4, 6) + "-" + cpuCardInfo.signDate.substring(6, 8) } cpuCardInfo.expirationDate = res.expireDate || "" if ( cpuCardInfo.expirationDate != "" && cpuCardInfo.expirationDate.length == 8 ) { cpuCardInfo.expirationDate = cpuCardInfo.expirationDate.substring(0, 4) + "-" + cpuCardInfo.expirationDate.substring(4, 6) + "-" + cpuCardInfo.expirationDate.substring(6, 8) } cpuCardInfo.userType = userTypeMap.get( `${res.userType}` ) this.setData({ cpuCardInfo, }) this.endLoading() this.disConnect() }, (error) => { console.log( "getCardFile0015-Api.systemDecoder-error=>", error ) this.endLoading() this.disConnect() } ) } else { this.endLoading() this.disConnect() } }) }, getObuSystemInfo(devResult) { let obuCardInfo = {} console.log("|||getSystemInfo==>", JSON.stringify(devResult)) if (devResult.code == 0) { obuCardInfo.obuCardNo = devResult.data || "" facapi.GetSystemInfo((devResult) => { console.log("getSystemInfo=>", devResult) let data = { filename: Api.systemDecoder.filename, data: { type: "1", content: devResult.data, }, } requestFnc( Api.systemDecoder.url, data, (res) => { console.log( "getObuSystemInfo-Api.systemDecoder-res=>>", res ) obuCardInfo.obuCardNo = res.serialNumber || "" obuCardInfo.encryptionObuCardNo = desensitization(res.serialNumber, 4, -4) || "" obuCardInfo.signDate = res.signedDate || "" obuCardInfo.expirationDate = res.expiredDate || "" if ( obuCardInfo.signDate != "" && obuCardInfo.signDate.length == 8 ) { obuCardInfo.signDate = obuCardInfo.signDate.substring(0, 4) + "-" + obuCardInfo.signDate.substring(4, 6) + "-" + obuCardInfo.signDate.substring(6, 8) } if ( obuCardInfo.expirationDate != "" && obuCardInfo.expirationDate.length == 8 ) { obuCardInfo.expirationDate = obuCardInfo.expirationDate.substring(0, 4) + "-" + obuCardInfo.expirationDate.substring(4, 6) + "-" + obuCardInfo.expirationDate.substring(6, 8) } obuCardInfo.tearFlag = res.tearFlag this.setData({ obuCardInfo, }) facapi.OpenCard((devResult) => { this.getVehicleInfo(devResult) }) }, (error) => { console.log( "getObuSystemInfo-Api.systemDecoder-error=>", error ) this.endLoading() this.disConnect() } ) }) } else { this.showModal({ title: "错误", devResult: devResult, content: "获取Obu编号失败:" + devResult.code + ":" + devResult.err_msg + (devResult.msg ? ":" + devResult.msg : ""), }) this.endLoading() this.disConnect() } }, getVehicleInfo(devResult) { console.log("|||getVehicleInfo==>", JSON.stringify(devResult)) if (devResult.code == 0) { facapi.GetVehicleInfo("1122334455667788", (devResult) => { console.log("getVehicleInfo=>", devResult) let params = { filename: Api.obuRead.filename, data: { obuId: this.data.obuCardInfo.obuCardNo, encryptedInfo: devResult.data, }, } console.log("获取车辆信息入参", params) requestFnc( Api.obuRead.url, params, (res) => { console.log("obuRead", res) let vehicleInfo = JSON.parse(res.vehicleInfo) console.log( "GetVehicleInfo-res.issue_info=>", vehicleInfo ) this.data.obuCardInfo.licenseColor = plateColorToColorMap.get( `${vehicleInfo.licenseColor}` ) //车牌号脱敏 var licensePlate = vehicleInfo.licensePlate let begain = 3 + (licensePlate.length - 7) this.data.obuCardInfo.licensePlate = desensitization( vehicleInfo.licensePlate, begain, -2 ) || "" this.data.obuCardInfo.tollVehicleType = vehicleTypeMap.get(`${vehicleInfo.carType}`) this.data.obuCardInfo.userType = userTypeMap.get( `${vehicleInfo.userType}` ) let typeObj = { "00": "未激活", "01": "已激活", } if (this.data.obuCardInfo.licensePlate) { typeObj["00"] = "标签异常" } this.data.obuCardInfo.activateState = typeObj[this.data.obuCardInfo.tearFlag] || "未知" this.setData({ obuCardInfo: this.data.obuCardInfo, }) this.probingCpu() }, (error) => { console.log("GetVehicleInfo-Api.obuRead-error=>", error) this.endLoading() this.disConnect() } ) }) } else { this.showModal({ title: "错误", devResult: devResult, content: "获取车辆失败:" + devResult.code + ":" + devResult.err_msg + (devResult.msg ? ":" + devResult.msg : ""), }) this.endLoading() this.disConnect() } }, disConnect() { if (facapi.facSdk && facapi.facSdk.DisconnectDevice) { facapi.facSdk.DisconnectDevice(function (code) { console.log("关闭连接结果", code) }) } // 完成后关闭蓝牙模块 bleapi.CloseBle((obj) => { console.log(obj) }) }, closeBle() { // 进入前关闭蓝牙,初始化 bleapi.CheckBle((res) => { if (res.available) { this.disConnect() } }) }, //显示弹框 showModal(data) { this.endLoading() this.disConnect() //显示弹框 wx.showModal({ title: "提示", content: data.content, showCancel: false, }) }, })