fix:基础请求url

This commit is contained in:
wangzhongjie 2025-04-14 10:59:37 +08:00
parent 70951df9bb
commit 6e4f0c282b
9 changed files with 52 additions and 37 deletions

View File

@ -4,13 +4,13 @@
{ {
"name": "default", "name": "default",
"material": { "material": {
"certpath": "C:/Users/93218/.ohos/config/openharmony/default_harmony_vehicle_terminal_Ja3OiX9fkBwGKa0NNRbcXszAt35lPCdUakY4mTtjFwU=.cer", "certpath": "/Users/wangzhongjie/.ohos/config/openharmony/default_car_next_xIuD6UMCLxZgyeiH-w2XdDck6DewIfdHAvOk_FUbNZo=.cer",
"storePassword": "0000001B8DAD42641E5F7218D3617E25253F6E8B9D3E209D83F6E7D7A50EEA33CC9F3672C35C8893FE7C05", "storePassword": "0000001B69B6F927C1A8A9C2E65B309DCE5BAA4B1498D997D8BE4F04A3E9A739B4C9FADB2811D9CB2EAA93",
"keyAlias": "debugKey", "keyAlias": "debugKey",
"keyPassword": "0000001B8689E02F5E83BE0C395F8A4DC367C1B2C222600352DC5D93C9B8251DAA9B059371E119BEA7DFB1", "keyPassword": "0000001BE240E3D5E0916E6FB737EBE3B7C7CED5870FCD2660699C961E1A4EC3969F036EB5EE138DC4B7FD",
"profile": "C:/Users/93218/.ohos/config/openharmony/default_harmony_vehicle_terminal_Ja3OiX9fkBwGKa0NNRbcXszAt35lPCdUakY4mTtjFwU=.p7b", "profile": "/Users/wangzhongjie/.ohos/config/openharmony/default_car_next_xIuD6UMCLxZgyeiH-w2XdDck6DewIfdHAvOk_FUbNZo=.p7b",
"signAlg": "SHA256withECDSA", "signAlg": "SHA256withECDSA",
"storeFile": "C:/Users/93218/.ohos/config/openharmony/default_harmony_vehicle_terminal_Ja3OiX9fkBwGKa0NNRbcXszAt35lPCdUakY4mTtjFwU=.p12" "storeFile": "/Users/wangzhongjie/.ohos/config/openharmony/default_car_next_xIuD6UMCLxZgyeiH-w2XdDck6DewIfdHAvOk_FUbNZo=.p12"
} }
} }
], ],

View File

@ -1,5 +1,5 @@
{ {
"lockfileVersion": 1, "lockfileVersion": 2,
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
"specifiers": {}, "specifiers": {},
"packages": {} "packages": {}

View File

@ -17,9 +17,14 @@ export default class EntryAbility extends UIAbility {
await InitTable() await InitTable()
let fileUtil = new FileUtils(this.context) let fileUtil = new FileUtils(this.context)
const data = await fileUtil.readFile(GlobalConfig.commonFileWriteAddress + '/config/ipConfig.txt'); const data = await fileUtil.readFile(GlobalConfig.commonFileWriteAddress + '/config/ipConfig.txt');
console.log("读取配置", data)
if (data !== '' && data !== undefined) { if (data !== '' && data !== undefined) {
console.log("配置设置")
const result: EnvironmentConfigurationType = JSON.parse(data) const result: EnvironmentConfigurationType = JSON.parse(data)
AppStorage.setOrCreate<EnvironmentConfigurationType>("EnvironmentConfiguration", result) AppStorage.setOrCreate<EnvironmentConfigurationType>("EnvironmentConfiguration", result)
const host=`http://${result.centerIp}:${result.centerPort}`
console.log("host",host)
AppStorage.setOrCreate<string>("host",host)
} }
} catch (e) { } catch (e) {
console.error('sql first error', e) console.error('sql first error', e)

View File

@ -221,6 +221,7 @@ struct Index {
async initParams() { async initParams() {
console.log("test1111") console.log("test1111")
ObtainUdpBusinessInstance.init(); ObtainUdpBusinessInstance.init();
await GetCarInfo()
CenterUDPBusinessInstance.init(); CenterUDPBusinessInstance.init();
CenterUDPBusinessInstance.onMsg((data: centerCallBackMsgType) => { CenterUDPBusinessInstance.onMsg((data: centerCallBackMsgType) => {
if (data.id == 32) { if (data.id == 32) {
@ -228,7 +229,6 @@ struct Index {
} }
},) },)
await GetDeviceInfo(this.context) await GetDeviceInfo(this.context)
await GetCarInfo()
// getTCP() // getTCP()
this.carInfo = AppStorage.get<CarInfoType>('carInfo')! this.carInfo = AppStorage.get<CarInfoType>('carInfo')!
this.deviceId = this.carInfo.carNo || "" this.deviceId = this.carInfo.carNo || ""

View File

@ -14,7 +14,6 @@ interface RequestOption {
timeout?: number timeout?: number
} }
const base: string = AppStorage.get("host") || "http://127.0.0.1"
function xmlToJSON(target: string): object { function xmlToJSON(target: string): object {
let conv = new convertxml.ConvertXML() let conv = new convertxml.ConvertXML()
@ -120,9 +119,10 @@ type RequestResult = Object | object | string | CenterCodeResult
export default function Request<T extends RequestResult>(options: RequestOption): Promise<T> { export default function Request<T extends RequestResult>(options: RequestOption): Promise<T> {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
const base: string = AppStorage.get<string>("host") || "http://127.0.0.1"
const instance = http.createHttp() const instance = http.createHttp()
const baseURL = options.host || base const baseURL = options.host || base
console.log(RequestTag, "基础url:", baseURL)
instance.request(baseURL + options.url, { instance.request(baseURL + options.url, {
method: options.method, method: options.method,
header: { header: {

View File

@ -61,6 +61,8 @@ export default class UdpClient {
bindUdp(): Promise<void> | undefined { bindUdp(): Promise<void> | undefined {
return this.udp?.bind({ return this.udp?.bind({
address: this.localIp, port: parseInt(this.localIpPort), family: 1 address: this.localIp, port: parseInt(this.localIpPort), family: 1
}).then(res=>{
console.log(UDPTag,"udp 绑定成功!",JSON.stringify(res))
}) })
} }
@ -90,6 +92,8 @@ export default class UdpClient {
address: { address: {
address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1 address: this.oppositeIp, port: parseInt(this.oppositeIpPort), family: 1
} }
}).then(res=>{
console.log(UDPTag,"udp发送成功",JSON.stringify(res))
}) })
} }
@ -109,14 +113,19 @@ export default class UdpClient {
} }
async create(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) { async create(udpLocalIp: string, udpLocalIpPort: string, udpOppositeIp: string, udpOppositeIpPort: string) {
console.log(UDPTag,"udp 初始化")
await this.close() await this.close()
this.localIp = udpLocalIp this.localIp = udpLocalIp
this.oppositeIp = udpOppositeIp this.oppositeIp = udpOppositeIp
this.localIpPort = udpLocalIpPort this.localIpPort = udpLocalIpPort
this.oppositeIpPort = udpOppositeIpPort this.oppositeIpPort = udpOppositeIpPort
this.udp = socket.constructUDPSocketInstance(); this.udp = socket.constructUDPSocketInstance();
this.bindEvent() try{
return this.bindUdp() this.bindUdp()
this.bindEvent()
} catch (e) {
console.log(UDPTag,"udp错误",JSON.stringify(e))
}
} }
onMsg(callback: Function) { onMsg(callback: Function) {

View File

@ -124,22 +124,16 @@ class CenterUDPBusiness {
this.udp.offMsg(cb) this.udp.offMsg(cb)
} }
async init(): Promise<void> { async init() {
return new Promise((resolve, reject) => { try {
try { let result: EnvironmentConfigurationType =
let result: EnvironmentConfigurationType = AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")!
AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")! const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo')!
const carInfo: CarInfoType = AppStorage.get<CarInfoType>('carInfo')! this.udp.create(result.udplocalIp!, '8800', carInfo.udpAddress!, carInfo.messagePort!)
AppStorage.setOrCreate("host", `http://${result.centerIp}:${result.centerPort}`) this.udp.setDealMethod(this.dealMsg)
this.udp.create(result.udplocalIp!, '8800', carInfo.udpAddress!, carInfo.messagePort!) } catch (e) {
.then(resolve) console.error(UDPTag, "初始化中心 udp失败")
.catch(reject) }
this.udp.setDealMethod(this.dealMsg)
} catch (e) {
reject(e)
console.error(UDPTag, "初始化中心 udp失败")
}
})
} }
// 关闭所有动作 // 关闭所有动作

View File

@ -1,4 +1,4 @@
import { GlobalConfig } from '../../config' import { GlobalConfig, UDPTag } from '../../config'
import { EnvironmentConfigurationType } from '../../model' import { EnvironmentConfigurationType } from '../../model'
import UdpClient from '../UdpUtils' import UdpClient from '../UdpUtils'
@ -100,12 +100,17 @@ class ObtainUdpBusiness {
} }
private initThirdGeneration() { private initThirdGeneration() {
let config: EnvironmentConfigurationType = try {
AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")! let config: EnvironmentConfigurationType =
this.thirdGenerationMachineUdp = new UdpClient() AppStorage.get<EnvironmentConfigurationType>("EnvironmentConfiguration")!
this.thirdGenerationMachineUdp.create(config.udplocalIp!, config.udplocalIpPort!, config.udpOppositeIp!, console.log("获取udp设置", JSON.stringify(config))
config.udpOppositeIpPort!) this.thirdGenerationMachineUdp = new UdpClient()
this.thirdGenerationMachineUdp.setDealMethod(this.dealThirdGenerationMsg) this.thirdGenerationMachineUdp.create(config.udplocalIp!, config.udplocalIpPort!, config.udpOppositeIp!,
config.udpOppositeIpPort!)
this.thirdGenerationMachineUdp.setDealMethod(this.dealThirdGenerationMsg)
} catch (e) {
console.error(UDPTag, "三代机 udp失败")
}
} }
private dealSecondaryBoardMsg() { private dealSecondaryBoardMsg() {

View File

@ -1,5 +1,5 @@
{ {
"lockfileVersion": 1, "lockfileVersion": 2,
"ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.", "ATTENTION": "THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.",
"specifiers": { "specifiers": {
"@ohos/hypium@1.0.19": "@ohos/hypium@1.0.19", "@ohos/hypium@1.0.19": "@ohos/hypium@1.0.19",
@ -8,11 +8,13 @@
"packages": { "packages": {
"@ohos/hypium@1.0.19": { "@ohos/hypium@1.0.19": {
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.19.har", "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/hypium/-/hypium-1.0.19.har",
"integrity": "sha512-cEjDgLFCm3cWZDeRXk7agBUkPqjWxUo6AQeiu0gEkb3J8ESqlduQLSIXeo3cCsm8U/asL7iKjF85ZyOuufAGSQ==" "integrity": "sha512-cEjDgLFCm3cWZDeRXk7agBUkPqjWxUo6AQeiu0gEkb3J8ESqlduQLSIXeo3cCsm8U/asL7iKjF85ZyOuufAGSQ==",
"registryType": "ohpm"
}, },
"@ohos/crypto-js@2.0.3": { "@ohos/crypto-js@2.0.3": {
"resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/crypto-js/-/crypto-js-2.0.3.har", "resolved": "https://ohpm.openharmony.cn/ohpm/@ohos/crypto-js/-/crypto-js-2.0.3.har",
"integrity": "sha512-LuHaR1kD5PxnOXnuR1fWvPwGtbed9Q/QGzk6JOh8y5Wdzvi8brPesODZiaWf9scOVRHsbTPOtZw91vWB35p1vQ==" "integrity": "sha512-LuHaR1kD5PxnOXnuR1fWvPwGtbed9Q/QGzk6JOh8y5Wdzvi8brPesODZiaWf9scOVRHsbTPOtZw91vWB35p1vQ==",
"registryType": "ohpm"
} }
} }
} }