fix: 优化多个组件中的代码逻辑和类型声明,提升代码一致性

This commit is contained in:
wangzhongjie 2025-04-09 10:34:39 +08:00
parent 63f4248e9f
commit a4cb423722
4 changed files with 111 additions and 111 deletions

View File

@ -9,6 +9,7 @@ import { BusinessError } from '@ohos.base';
import { voiceService } from '../utils/Voice'; import { voiceService } from '../utils/Voice';
import dayTs from '../utils/Date'; import dayTs from '../utils/Date';
import { ObtainUdpBusinessInstance } from '../utils/business/ObtainUdpBusiness'; import { ObtainUdpBusinessInstance } from '../utils/business/ObtainUdpBusiness';
import common from '@ohos.app.ability.common';
@Entry @Entry
@Component @Component
@ -41,27 +42,28 @@ struct Index {
@State stopFlag: boolean = false @State stopFlag: boolean = false
@State fromIndex: boolean = false @State fromIndex: boolean = false
@State carInfo: CarInfoType = {} @State carInfo: CarInfoType = {}
private vocObj: voiceService; private vocObj?: voiceService;
private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext
// private AccountTable = new AccountTable(()=>{},CommonConstants); // private AccountTable = new AccountTable(()=>{},CommonConstants);
aboutToAppear() { aboutToAppear() {
const that = this const that = this
this.carInfo = AppStorage.get<CarInfoType>('carInfo') this.carInfo = AppStorage.get<CarInfoType>('carInfo')!
this.vocObj = new voiceService(async (status: string, val: string) => { this.vocObj = new voiceService(async (status: string, val?: string) => {
if (!that.breakFlag) { if (!that.breakFlag || !val) {
return return;
} }
if (status == 'idle') { if (status == 'idle') {
if (val == 'kszj.wav') { if (val == 'kszj.wav') {
console.log('finsh') console.log('finsh');
this.initfackCheck(0) this.initfackCheck(0);
// this.getCheckList(true) // this.getCheckList(true)
} else { } else {
this.subFlag = false this.subFlag = false;
this.getNextNode() this.getNextNode();
} }
} }
}); }, this.context);
let routParam: RouteParamsType = router.getParams() as RouteParamsType; let routParam: RouteParamsType = router.getParams() as RouteParamsType;
this.fromIndex = routParam?.fromIndex || false this.fromIndex = routParam?.fromIndex || false
this.carConfigurationInfoFn() this.carConfigurationInfoFn()
@ -107,8 +109,8 @@ struct Index {
} }
async xhxs() { async xhxs() {
this.vocObj.avPlayerStop() this.vocObj!.avPlayerStop()
this.vocObj.releasePlayer() this.vocObj!.releasePlayer()
router.pushUrl({ router.pushUrl({
url: 'pages/SignDisplay', url: 'pages/SignDisplay',
}, router.RouterMode.Single); }, router.RouterMode.Single);
@ -141,8 +143,8 @@ struct Index {
//模拟真实数据 //模拟真实数据
const param: CarConfigurationParams = { const param: CarConfigurationParams = {
body: { body: {
"carIdString": this.carInfo.carId, //考车ID "carIdString": this.carInfo.carId || "", //考车ID
"deviceNo": baseInfo.deviceNo "deviceNo": baseInfo.deviceNo || ""
} }
} }
@ -172,7 +174,7 @@ struct Index {
this.warnFlag = JSON.parse(JSON.stringify(WarnFlagData)) this.warnFlag = JSON.parse(JSON.stringify(WarnFlagData))
this.passArray = JSON.parse(JSON.stringify(PassData)) this.passArray = JSON.parse(JSON.stringify(PassData))
this.index = this.checkListCopy[0].key this.index = this.checkListCopy[0].key
this.vocObj.playAudio({ this.vocObj!.playAudio({
type: 1, type: 1,
name: 'kszj.wav' name: 'kszj.wav'
}) })
@ -272,7 +274,7 @@ struct Index {
flag = true; flag = true;
} }
}); });
this.vocObj.playAudio({ this.vocObj!.playAudio({
type: 1, type: 1,
name: flag ? 'zjwtg.wav' : 'zjtg.wav' name: flag ? 'zjwtg.wav' : 'zjtg.wav'
}) })
@ -325,7 +327,7 @@ struct Index {
// if (!flag) { // if (!flag) {
// that.warnFlag[index] = '2'; // that.warnFlag[index] = '2';
// } // }
const spzd: SpzdType = AppStorage.get('spzd'); const spzd: SpzdType = AppStorage.get('spzd')!;
const hasTruthyValue = Object.values(spzd).some((value: string) => value); const hasTruthyValue = Object.values(spzd).some((value: string) => value);
that.warnFlag[index] = hasTruthyValue ? '1' : '2'; that.warnFlag[index] = hasTruthyValue ? '1' : '2';
} else { } else {
@ -358,7 +360,7 @@ struct Index {
name, name,
}) })
} else { } else {
this.vocObj.playAudio({ this.vocObj!.playAudio({
type: 1, type: 1,
name, name,
}) })

View File

@ -312,7 +312,7 @@ export default struct FaceCompare {
this.showFaceCompareFlag = !this.showFaceCompareFlag this.showFaceCompareFlag = !this.showFaceCompareFlag
} }
} }
}); }, this.context);
const data = await this.fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/config3.txt'); const data = await this.fileUtil.readFile(GlobalConfig.comoonfileWriteAddress + '/config/config3.txt');
console.log('faceEnterIn,data', data) console.log('faceEnterIn,data', data)

View File

@ -14,8 +14,10 @@ export class voiceService {
private endFlag: Boolean = false; private endFlag: Boolean = false;
private mediaArray: Array<string> = []; private mediaArray: Array<string> = [];
private callBack: AVPlayerCallback | null = null; private callBack: AVPlayerCallback | null = null;
private context: common.UIAbilityContext
constructor(callBack: AVPlayerCallback) { constructor(callBack: AVPlayerCallback, context: common.UIAbilityContext) {
this.context = context
// 创建avPlayer实例对象 // 创建avPlayer实例对象
media.createAVPlayer().then(video => { media.createAVPlayer().then(video => {
this.avPlayer = video this.avPlayer = video
@ -96,11 +98,7 @@ export class voiceService {
// 以下为使用资源管理接口获取打包在HAP内的媒体资源文件并通过fdSrc属性进行播放示例 // 以下为使用资源管理接口获取打包在HAP内的媒体资源文件并通过fdSrc属性进行播放示例
avPlayerFdSrc(name: string) { avPlayerFdSrc(name: string) {
const context: common.UIAbilityContext = AppStorage.get('context') const context = this.context
if (!context) {
console.log(VoiceTag, ' context is null');
return
}
context.resourceManager.getRawFd(name, async (error, value) => { context.resourceManager.getRawFd(name, async (error, value) => {
if (error != null) { if (error != null) {
console.log(VoiceTag, ` callback getRawFd failed error code: ${error.code}, message: ${error.message}.`); console.log(VoiceTag, ` callback getRawFd failed error code: ${error.code}, message: ${error.message}.`);
@ -141,16 +139,16 @@ export class voiceService {
playAudio(param: playParams) { playAudio(param: playParams) {
this.endFlag = false this.endFlag = false
this.mediaArray = [] this.mediaArray = []
this.type = param.type this.type = param.type ?? 1;
if (param.type == 1) { if (param.type == 1) {
this.playerName = param.name this.playerName = param.name || ""
// this.nextPlayerName=param.nextName // this.nextPlayerName=param.nextName
this.avPlayerFdSrc(param.name); this.avPlayerFdSrc(param.name || "");
} else if (param.type == 2) { } else if (param.type == 2) {
this.avPlayerLive(param.url); this.avPlayerLive(param.url!);
} else if (param.type == 3) { } else if (param.type == 3) {
if (param.value.length) { if (param.value && param.value.length) {
this.mediaArray = param.value this.mediaArray = param.value;
this.avPlayerFdSrc(param.value[0]); this.avPlayerFdSrc(param.value[0]);
} }
} }

View File

@ -5,89 +5,12 @@ import UdpClient from '../UdpUtils'
// 获取PLC GPS信号 // 获取PLC GPS信号
class ObtainUdpBusiness { class ObtainUdpBusiness {
// 三代机UDP // 三代机UDP
private thirdGenerationMachineUdp: UdpClient private thirdGenerationMachineUdp?: UdpClient
// 几代机 // 几代机
private modelNo: string = "3" private modelNo: string = "3"
constructor() { constructor() {
this.modelNo = GlobalConfig.modelNo this.modelNo = GlobalConfig.modelNo || ""
}
private initSecondaryBoard() {
}
private initFirstGeneration() {
}
private initSecondGeneration() {
}
private initThirdGeneration() {
let config: EnvironmentConfigurationType =
AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")
this.thirdGenerationMachineUdp = new UdpClient()
this.thirdGenerationMachineUdp.create(config.udplocalIp, config.udplocalIpPort, config.udpOppositeIp,
config.udpOppositeIpPort)
this.thirdGenerationMachineUdp.setDealMethod(this.dealThirdGenerationMsg)
}
private dealSecondaryBoardMsg() {
}
private dealFirstGenerationMsg() {
}
private dealSecondGenerationMsg() {
}
private dealThirdGenerationMsg(data: ArrayBuffer) {
const dataView = new DataView(data);
let str = ""
for (let i = 0; i < dataView?.byteLength; ++i) {
let c = String.fromCharCode(dataView?.getUint8(i))
if (c !== "\n") {
str += c
}
}
return str
}
private onSecondaryBoardMsg(cb: Function) {
}
private onFirstGenerationMsg(cb: Function) {
}
private onSecondGenerationMsg(cb: Function) {
}
private onThirdGenerationMsg(cb: Function) {
this.thirdGenerationMachineUdp.onMsg(cb)
}
private sendSecondaryBoardMsg(data: string) {
}
private sendFirstGenerationMsg(data: string) {
}
private sendSecondGenerationMsg(data: string) {
}
private sendThirdGenerationMsg(data: string) {
this.thirdGenerationMachineUdp.sendMsg(data)
} }
init() { init() {
@ -138,7 +61,7 @@ class ObtainUdpBusiness {
case "2": case "2":
break break
case "3": case "3":
this.thirdGenerationMachineUdp.offMsg(cb) this.thirdGenerationMachineUdp!.offMsg(cb)
break break
default: default:
break break
@ -163,6 +86,83 @@ class ObtainUdpBusiness {
break break
} }
} }
private initSecondaryBoard() {
}
private initFirstGeneration() {
}
private initSecondGeneration() {
}
private initThirdGeneration() {
let config: EnvironmentConfigurationType =
AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")!
this.thirdGenerationMachineUdp = new UdpClient()
this.thirdGenerationMachineUdp.create(config.udplocalIp!, config.udplocalIpPort!, config.udpOppositeIp!,
config.udpOppositeIpPort!)
this.thirdGenerationMachineUdp.setDealMethod(this.dealThirdGenerationMsg)
}
private dealSecondaryBoardMsg() {
}
private dealFirstGenerationMsg() {
}
private dealSecondGenerationMsg() {
}
private dealThirdGenerationMsg(data: ArrayBuffer) {
const dataView = new DataView(data);
let str = ""
for (let i = 0; i < dataView?.byteLength; ++i) {
let c = String.fromCharCode(dataView?.getUint8(i))
if (c !== "\n") {
str += c
}
}
return str
}
private onSecondaryBoardMsg(cb: Function) {
}
private onFirstGenerationMsg(cb: Function) {
}
private onSecondGenerationMsg(cb: Function) {
}
private onThirdGenerationMsg(cb: Function) {
this.thirdGenerationMachineUdp!.onMsg(cb)
}
private sendSecondaryBoardMsg(data: string) {
}
private sendFirstGenerationMsg(data: string) {
}
private sendSecondGenerationMsg(data: string) {
}
private sendThirdGenerationMsg(data: string) {
this.thirdGenerationMachineUdp!.sendMsg(data)
}
} }
export const ObtainUdpBusinessInstance = new ObtainUdpBusiness() export const ObtainUdpBusinessInstance = new ObtainUdpBusiness()