鸿蒙远场通信的封装

##鸿蒙核心技术##HarmonyOS SDK应用服务##RCP(远场通信)#

在阅读本文章前,请先阅读

鸿蒙中的网络请求之RCP远场通信-CSDN博客

RCP远场通信之拦截器-CSDN博客

现在我们对RCP进行封装

以get请求为例
 

新建一个rcpUtil对象
 

import { rcp } from "@kit.RemoteCommunicationKit";
import { RequestInterceptor } from "../Interceptor/RequestInterceptor";
import { rcpBaseUrl } from "../rcpBaseUrl/rcpBaseUrl";
import { uri } from "@kit.ArkTS";
import { queryModel } from "../Model/queryModel";

export class rcpUtil {
  private static rcpSession = rcp.createSession(RequestInterceptor)

  static getMessage<V>(url?: string, params?: queryModel[]) {

    let urlObject: uri.URI = new uri.URI(url ? rcpBaseUrl + url : rcpBaseUrl)
    params?.forEach((item) => {
      console.log("item", JSON.stringify(item))
      const map: Map<string, ESObject> = new Map(Object.entries(item));
      map.forEach((value:ESObject,key:string)=>{
        console.log(`key=${key} value=${value}`)
        urlObject=urlObject.addQueryValue(key,value.toString())
      })
    })
      console.log(urlObject.toString())
      return rcpUtil.rcpSession.get(urlObject.toString())
    }

    static closeConnect() {
      rcpUtil.rcpSession.close()
    }
  }

在这个对象中定义了get方法和close方法

其中查询的数据类型为

export interface queryModel {
  id:number
}

然后在主页引用

  onPageShow(): void {
    rcpUtil.getMessage<string | number>("", [{ id: 5 }] as queryModel[]).then((res: rcp.Response) => {
      console.log("onPageShow", JSON.stringify(res))
      rcpUtil.closeConnect()
    })
  }

之后运行

输出结果也符合预期

项目结构如下:

D:.
│  .clang-format
│  .gitignore
│  build-profile.json5
│  code-linter.json5
│  hvigorfile.ts
│  local.properties
│  oh-package-lock.json5
│  oh-package.json5

├─.hvigor
│  ├─cache
│  │      file-cache.json
│  │      meta.json
│  │      task-cache.json
│  │
│  ├─dependencyMap
│  │  │  dependencyMap.json5
│  │  │  oh-package.json5
│  │  │
│  │  └─entry
│  │          oh-package.json5
│  │
│  ├─outputs
│  │  ├─build-logs
│  │  │      build.log
│  │  │
│  │  ├─logs
│  │  │  └─details
│  │  │          details.json
│  │  │
│  │  └─sync
│  │          fileCache.json
│  │          output.json
│  │
│  └─report
│          report-202506140302571580.json
│          report-202506140303396740.json
│          report-202506140305542180.json
│          report-202506140310453690.json
│          report-202506140311599870.json
│          report-202506140312352210.json
│          report-202506140314198850.json
│          report-202506140314421260.json
│          report-202506140316046600.json
│          report-202506140318021680.json

├─.idea
│  │  .gitignore
│  │  modules.xml
│  │  workspace.xml
│  │
│  ├─.deveco
│  │  │  project.cache.json
│  │  │
│  │  └─module
│  │          entry.cache.json
│  │
│  └─modules
│      │  RCPMessage.iml
│      │
│      └─entry
│              entry.iml

├─AppScope
│  │  app.json5
│  │
│  └─resources
│      └─base
│          ├─element
│          │      string.json
│          │
│          └─media
│                  background.png
│                  foreground.png
│                  layered_image.json

├─entry
│  │  .gitignore
│  │  build-profile.json5
│  │  hvigorfile.ts
│  │  obfuscation-rules.txt
│  │  oh-package.json5
│  │
│  ├─build
│  │  ├─config
│  │  │      buildConfig.json
│  │  │
│  │  └─default
│  │      ├─cache
│  │      │  └─default
│  │      │      └─default@CompileArkTS
│  │      │          └─esmodule
│  │      │              │  .tsbuildinfo
│  │      │              │  .tsbuildinfo.linter
│  │      │              │  .ts_checker_cache
│  │      │              │  compileInfo.json
│  │      │              │
│  │      │              └─debug
│  │      │                  │  compileContextInfo.json
│  │      │                  │  dep_info.json
│  │      │                  │  filesInfo.txt
│  │      │                  │  modules.cache
│  │      │                  │  npmEntries.protoBin
│  │      │                  │  npmEntries.txt
│  │      │                  │  sourceMaps.json
│  │      │                  │
│  │      │                  ├─compiler.cache
│  │      │                  │  ├─modules
│  │      │                  │  │      EntryAbility.ets-791e95bd1f5061c7205bfda893f1b35d.msgpack
│  │      │                  │  │      EntryBackupAbility.ets-da360888ca4a30e94ec4f8c85bd64b1f.msgpack
│  │      │                  │  │      Index.ets-b8a6c75b005ebf1b450cb6197e690982.msgpack
│  │      │                  │  │      rcpBaseUrl.ets-bc99b8a28c58889f07f3b6df8e55f309.msgpack
│  │      │                  │  │      RCPPage.ets-9547274f52c274ae6430421615c3c6e4.msgpack
│  │      │                  │  │      rcpUtil.ets-47e0ea87209ec8632a664258794b4ddc.msgpack
│  │      │                  │  │      RequestInterceptor.ets-167f95dbf37d22fdc0d356fb673cf21c.msgpack
│  │      │                  │  │
│  │      │                  │  ├─other
│  │      │                  │  │      pkgName2SourceRoots.msgpack
│  │      │                  │  │
│  │      │                  │  └─plugins
│  │      │                  │          plugins.msgpack
│  │      │                  │
│  │      │                  ├─entry
│  │      │                  │  └─src
│  │      │                  │      └─main
│  │      │                  │          └─ets
│  │      │                  │              ├─entryability
│  │      │                  │              │      EntryAbility.protoBin
│  │      │                  │              │      EntryAbility.ts
│  │      │                  │              │
│  │      │                  │              ├─entrybackupability
│  │      │                  │              │      EntryBackupAbility.protoBin
│  │      │                  │              │      EntryBackupAbility.ts
│  │      │                  │              │
│  │      │                  │              ├─Interceptor
│  │      │                  │              │      RequestInterceptor.protoBin
│  │      │                  │              │      RequestInterceptor.ts
│  │      │                  │              │
│  │      │                  │              ├─pages
│  │      │                  │              │      Index.protoBin
│  │      │                  │              │      Index.ts
│  │      │                  │              │      RCPPage.protoBin
│  │      │                  │              │      RCPPage.ts
│  │      │                  │              │
│  │      │                  │              ├─rcpBaseUrl
│  │      │                  │              │      rcpBaseUrl.protoBin
│  │      │                  │              │      rcpBaseUrl.ts
│  │      │                  │              │
│  │      │                  │              └─rcpUtil
│  │      │                  │                      rcpUtil.protoBin
│  │      │                  │                      rcpUtil.ts
│  │      │                  │
│  │      │                  └─temporary
│  │      │                      └─ets
│  │      ├─generated
│  │      │  ├─profile
│  │      │  │  └─default
│  │      │  │          BuildProfile.ets
│  │      │  │
│  │      │  └─r
│  │      │      └─default
│  │      │              ResourceTable.h
│  │      │
│  │      ├─intermediates
│  │      │  ├─cmake
│  │      │  │  └─default
│  │      │  │      └─obj
│  │      │  ├─hap_metadata
│  │      │  │  └─default
│  │      │  │          output_metadata.json
│  │      │  │
│  │      │  ├─libs
│  │      │  │  └─default
│  │      │  ├─loader
│  │      │  │  └─default
│  │      │  │          loader.json
│  │      │  │          pkgContextInfo.json
│  │      │  │
│  │      │  ├─loader_out
│  │      │  │  └─default
│  │      │  │      └─ets
│  │      │  │              modules.abc
│  │      │  │              sourceMaps.map
│  │      │  │
│  │      │  ├─merge_profile
│  │      │  │  └─default
│  │      │  │          module.json
│  │      │  │
│  │      │  ├─package
│  │      │  │  └─default
│  │      │  │          module.json
│  │      │  │
│  │      │  ├─patch
│  │      │  │  └─default
│  │      │  │          base_native_libs.json
│  │      │  │
│  │      │  ├─process_profile
│  │      │  │  └─default
│  │      │  │          module.json
│  │      │  │
│  │      │  ├─res
│  │      │  │  └─default
│  │      │  │      │  ark_module.json
│  │      │  │      │  module.json
│  │      │  │      │  opt-compression.json
│  │      │  │      │  resConfig.json
│  │      │  │      │  resources.index
│  │      │  │      │  ResourceTable.txt
│  │      │  │      │
│  │      │  │      ├─.caches
│  │      │  │      │  └─base
│  │      │  │      │      └─media
│  │      │  │      │              background.png
│  │      │  │      │              foreground.png
│  │      │  │      │
│  │      │  │      ├─ids_map
│  │      │  │      │      id_defined.json
│  │      │  │      │
│  │      │  │      └─resources
│  │      │  │          ├─base
│  │      │  │          │  ├─media
│  │      │  │          │  │      background.png
│  │      │  │          │  │      foreground.png
│  │      │  │          │  │      layered_image.json
│  │      │  │          │  │      startIcon.png
│  │      │  │          │  │
│  │      │  │          │  └─profile
│  │      │  │          │          backup_config.json
│  │      │  │          │          main_pages.json
│  │      │  │          │
│  │      │  │          └─rawfile
│  │      │  ├─router_map
│  │      │  │  └─default
│  │      │  │          loader-router-map.json
│  │      │  │          temp-router-map.json
│  │      │  │
│  │      │  ├─source_map
│  │      │  │  └─default
│  │      │  │          sourceMaps.map
│  │      │  │
│  │      │  └─stripped_native_libs
│  │      │      └─default
│  │      └─outputs
│  │          └─default
│  │              │  entry-default-unsigned.hap
│  │              │  pack.info
│  │              │
│  │              └─mapping
│  │                      sourceMaps.map
│  │
│  └─src
│      ├─main
│      │  │  module.json5
│      │  │
│      │  ├─ets
│      │  │  ├─entryability
│      │  │  │      EntryAbility.ets
│      │  │  │
│      │  │  ├─entrybackupability
│      │  │  │      EntryBackupAbility.ets
│      │  │  │
│      │  │  ├─Interceptor
│      │  │  │      RequestInterceptor.ets
│      │  │  │
│      │  │  ├─Model
│      │  │  │      deleteBodyModel.ets
│      │  │  │      getReturnModel.ets
│      │  │  │      postBodyModel.ets
│      │  │  │      putBodyModel.ets
│      │  │  │      queryModel.ets
│      │  │  │
│      │  │  ├─pages
│      │  │  │      Index.ets
│      │  │  │      RCPPage.ets
│      │  │  │
│      │  │  ├─rcpBaseUrl
│      │  │  │      rcpBaseUrl.ets
│      │  │  │
│      │  │  └─rcpUtil
│      │  │          rcpUtil.ets
│      │  │
│      │  └─resources
│      │      ├─base
│      │      │  ├─element
│      │      │  │      color.json
│      │      │  │      float.json
│      │      │  │      string.json
│      │      │  │
│      │      │  ├─media
│      │      │  │      background.png
│      │      │  │      foreground.png
│      │      │  │      layered_image.json
│      │      │  │      startIcon.png
│      │      │  │
│      │      │  └─profile
│      │      │          backup_config.json
│      │      │          main_pages.json
│      │      │
│      │      ├─dark
│      │      │  └─element
│      │      │          color.json
│      │      │
│      │      └─rawfile
│      ├─mock
│      │      mock-config.json5
│      │
│      ├─ohosTest
│      │  │  module.json5
│      │  │
│      │  └─ets
│      │      └─test
│      │              Ability.test.ets
│      │              List.test.ets
│      │
│      └─test
│              List.test.ets
│              LocalUnit.test.ets

├─hvigor
│      hvigor-config.json5

└─oh_modules
    ├─.ohpm
    │  │  lock.json5
    │  │
    │  ├─@ohos+hamock@1.0.0
    │  │  └─oh_modules
    │  │      └─@ohos
    │  │          └─hamock
    │  │              │  build-profile.json5
    │  │              │  CHANGELOG.md
    │  │              │  hvigorfile.js
    │  │              │  hvigorfile.ts
    │  │              │  index.d.ts
    │  │              │  index.ets
    │  │              │  index.js
    │  │              │  index.ts
    │  │              │  LICENSE
    │  │              │  oh-package.json5
    │  │              │  README.md
    │  │              │
    │  │              ├─libs
    │  │              └─src
    │  │                  ├─main
    │  │                  │  │  module.json
    │  │                  │  │
    │  │                  │  └─mock
    │  │                  │          ArgumentMatchers.js
    │  │                  │          ArgumentMatchers.ts
    │  │                  │          ExtendInterface.js
    │  │                  │          ExtendInterface.ts
    │  │                  │          MockKit.js
    │  │                  │          MockKit.ts
    │  │                  │          VerificationMode.js
    │  │                  │          VerificationMode.ts
    │  │                  │
    │  │                  └─res
    │  │                      └─schemas
    │  │                              mock-config-json5-schema.json
    │  │
    │  ├─@ohos+hypium@1.0.21
    │  │  └─oh_modules
    │  │      └─@ohos
    │  │          └─hypium
    │  │              │  build-profile.json5
    │  │              │  BuildProfile.ets
    │  │              │  CHANGELOG.md
    │  │              │  consumer-rules.txt
    │  │              │  hvigorfile.ts
    │  │              │  index.d.ts
    │  │              │  index.ets
    │  │              │  index.js
    │  │              │  index.ts
    │  │              │  LICENSE
    │  │              │  obfuscation-rules.txt
    │  │              │  oh-package.json5
    │  │              │  README.md
    │  │              │  ResourceTable.txt
    │  │              │
    │  │              ├─example
    │  │              ├─libs
    │  │              └─src
    │  │                  └─main
    │  │                      │  Constant.js
    │  │                      │  core.js
    │  │                      │  event.js
    │  │                      │  interface.js
    │  │                      │  module.json
    │  │                      │  service.js
    │  │                      │
    │  │                      ├─module
    │  │                      │  ├─assert
    │  │                      │  │  │  assertClose.js
    │  │                      │  │  │  assertContain.js
    │  │                      │  │  │  assertFail.js
    │  │                      │  │  │  assertFalse.js
    │  │                      │  │  │  assertInstanceOf.js
    │  │                      │  │  │  assertLarger.js
    │  │                      │  │  │  assertLargerOrEqual.js
    │  │                      │  │  │  assertLess.js
    │  │                      │  │  │  assertLessOrEqual.js
    │  │                      │  │  │  assertNaN.js
    │  │                      │  │  │  assertNegUnlimited.js
    │  │                      │  │  │  assertNull.js
    │  │                      │  │  │  assertPosUnlimited.js
    │  │                      │  │  │  assertPromiseIsPending.js
    │  │                      │  │  │  assertPromiseIsRejected.js
    │  │                      │  │  │  assertPromiseIsRejectedWith.js
    │  │                      │  │  │  assertPromiseIsRejectedWithError.js
    │  │                      │  │  │  assertPromiseIsResolved.js
    │  │                      │  │  │  assertPromiseIsResolvedWith.js
    │  │                      │  │  │  assertThrowError.js
    │  │                      │  │  │  assertUndefined.js
    │  │                      │  │  │  ExpectExtend.js
    │  │                      │  │  │  isPromiseLike.js
    │  │                      │  │  │
    │  │                      │  │  └─deepEquals
    │  │                      │  │          assertDeepEquals.js
    │  │                      │  │          DeepTypeUtils.js
    │  │                      │  │
    │  │                      │  ├─config
    │  │                      │  │      configService.js
    │  │                      │  │      DataDriver.js
    │  │                      │  │      Filter.js
    │  │                      │  │
    │  │                      │  ├─coverage
    │  │                      │  │      coverageCollect.js
    │  │                      │  │
    │  │                      │  ├─kit
    │  │                      │  │      SysTestKit.js
    │  │                      │  │
    │  │                      │  ├─mock
    │  │                      │  │      ArgumentMatchers.js
    │  │                      │  │      ExtendInterface.js
    │  │                      │  │      MockKit.js
    │  │                      │  │      VerificationMode.js
    │  │                      │  │
    │  │                      │  └─report
    │  │                      │          LogExpectError.js
    │  │                      │          OhReport.js
    │  │                      │          ReportExtend.js
    │  │                      │
    │  │                      └─testrunner
    │  │                              OpenHarmonyTestRunner.ts
    │  │
    │  └─oh_modules
    │      └─@ohos
    │          ├─hamock
    │          │  │  build-profile.json5
    │          │  │  CHANGELOG.md
    │          │  │  hvigorfile.js
    │          │  │  hvigorfile.ts
    │          │  │  index.d.ts
    │          │  │  index.ets
    │          │  │  index.js
    │          │  │  index.ts
    │          │  │  LICENSE
    │          │  │  oh-package.json5
    │          │  │  README.md
    │          │  │
    │          │  ├─libs
    │          │  └─src
    │          │      ├─main
    │          │      │  │  module.json
    │          │      │  │
    │          │      │  └─mock
    │          │      │          ArgumentMatchers.js
    │          │      │          ArgumentMatchers.ts
    │          │      │          ExtendInterface.js
    │          │      │          ExtendInterface.ts
    │          │      │          MockKit.js
    │          │      │          MockKit.ts
    │          │      │          VerificationMode.js
    │          │      │          VerificationMode.ts
    │          │      │
    │          │      └─res
    │          │          └─schemas
    │          │                  mock-config-json5-schema.json
    │          │
    │          └─hypium
    │              │  build-profile.json5
    │              │  BuildProfile.ets
    │              │  CHANGELOG.md
    │              │  consumer-rules.txt
    │              │  hvigorfile.ts
    │              │  index.d.ts
    │              │  index.ets
    │              │  index.js
    │              │  index.ts
    │              │  LICENSE
    │              │  obfuscation-rules.txt
    │              │  oh-package.json5
    │              │  README.md
    │              │  ResourceTable.txt
    │              │
    │              ├─example
    │              ├─libs
    │              └─src
    │                  └─main
    │                      │  Constant.js
    │                      │  core.js
    │                      │  event.js
    │                      │  interface.js
    │                      │  module.json
    │                      │  service.js
    │                      │
    │                      ├─module
    │                      │  ├─assert
    │                      │  │  │  assertClose.js
    │                      │  │  │  assertContain.js
    │                      │  │  │  assertFail.js
    │                      │  │  │  assertFalse.js
    │                      │  │  │  assertInstanceOf.js
    │                      │  │  │  assertLarger.js
    │                      │  │  │  assertLargerOrEqual.js
    │                      │  │  │  assertLess.js
    │                      │  │  │  assertLessOrEqual.js
    │                      │  │  │  assertNaN.js
    │                      │  │  │  assertNegUnlimited.js
    │                      │  │  │  assertNull.js
    │                      │  │  │  assertPosUnlimited.js
    │                      │  │  │  assertPromiseIsPending.js
    │                      │  │  │  assertPromiseIsRejected.js
    │                      │  │  │  assertPromiseIsRejectedWith.js
    │                      │  │  │  assertPromiseIsRejectedWithError.js
    │                      │  │  │  assertPromiseIsResolved.js
    │                      │  │  │  assertPromiseIsResolvedWith.js
    │                      │  │  │  assertThrowError.js
    │                      │  │  │  assertUndefined.js
    │                      │  │  │  ExpectExtend.js
    │                      │  │  │  isPromiseLike.js
    │                      │  │  │
    │                      │  │  └─deepEquals
    │                      │  │          assertDeepEquals.js
    │                      │  │          DeepTypeUtils.js
    │                      │  │
    │                      │  ├─config
    │                      │  │      configService.js
    │                      │  │      DataDriver.js
    │                      │  │      Filter.js
    │                      │  │
    │                      │  ├─coverage
    │                      │  │      coverageCollect.js
    │                      │  │
    │                      │  ├─kit
    │                      │  │      SysTestKit.js
    │                      │  │
    │                      │  ├─mock
    │                      │  │      ArgumentMatchers.js
    │                      │  │      ExtendInterface.js
    │                      │  │      MockKit.js
    │                      │  │      VerificationMode.js
    │                      │  │
    │                      │  └─report
    │                      │          LogExpectError.js
    │                      │          OhReport.js
    │                      │          ReportExtend.js
    │                      │
    │                      └─testrunner
    │                              OpenHarmonyTestRunner.ts
    │
    └─@ohos
        ├─hamock
        │  │  build-profile.json5
        │  │  CHANGELOG.md
        │  │  hvigorfile.js
        │  │  hvigorfile.ts
        │  │  index.d.ts
        │  │  index.ets
        │  │  index.js
        │  │  index.ts
        │  │  LICENSE
        │  │  oh-package.json5
        │  │  README.md
        │  │
        │  ├─libs
        │  └─src
        │      ├─main
        │      │  │  module.json
        │      │  │
        │      │  └─mock
        │      │          ArgumentMatchers.js
        │      │          ArgumentMatchers.ts
        │      │          ExtendInterface.js
        │      │          ExtendInterface.ts
        │      │          MockKit.js
        │      │          MockKit.ts
        │      │          VerificationMode.js
        │      │          VerificationMode.ts
        │      │
        │      └─res
        │          └─schemas
        │                  mock-config-json5-schema.json
        │
        └─hypium
            │  build-profile.json5
            │  BuildProfile.ets
            │  CHANGELOG.md
            │  consumer-rules.txt
            │  hvigorfile.ts
            │  index.d.ts
            │  index.ets
            │  index.js
            │  index.ts
            │  LICENSE
            │  obfuscation-rules.txt
            │  oh-package.json5
            │  README.md
            │  ResourceTable.txt
            │
            ├─example
            ├─libs
            └─src
                └─main
                    │  Constant.js
                    │  core.js
                    │  event.js
                    │  interface.js
                    │  module.json
                    │  service.js
                    │
                    ├─module
                    │  ├─assert
                    │  │  │  assertClose.js
                    │  │  │  assertContain.js
                    │  │  │  assertFail.js
                    │  │  │  assertFalse.js
                    │  │  │  assertInstanceOf.js
                    │  │  │  assertLarger.js
                    │  │  │  assertLargerOrEqual.js
                    │  │  │  assertLess.js
                    │  │  │  assertLessOrEqual.js
                    │  │  │  assertNaN.js
                    │  │  │  assertNegUnlimited.js
                    │  │  │  assertNull.js
                    │  │  │  assertPosUnlimited.js
                    │  │  │  assertPromiseIsPending.js
                    │  │  │  assertPromiseIsRejected.js
                    │  │  │  assertPromiseIsRejectedWith.js
                    │  │  │  assertPromiseIsRejectedWithError.js
                    │  │  │  assertPromiseIsResolved.js
                    │  │  │  assertPromiseIsResolvedWith.js
                    │  │  │  assertThrowError.js
                    │  │  │  assertUndefined.js
                    │  │  │  ExpectExtend.js
                    │  │  │  isPromiseLike.js
                    │  │  │
                    │  │  └─deepEquals
                    │  │          assertDeepEquals.js
                    │  │          DeepTypeUtils.js
                    │  │
                    │  ├─config
                    │  │      configService.js
                    │  │      DataDriver.js
                    │  │      Filter.js
                    │  │
                    │  ├─coverage
                    │  │      coverageCollect.js
                    │  │
                    │  ├─kit
                    │  │      SysTestKit.js
                    │  │
                    │  ├─mock
                    │  │      ArgumentMatchers.js
                    │  │      ExtendInterface.js
                    │  │      MockKit.js
                    │  │      VerificationMode.js
                    │  │
                    │  └─report
                    │          LogExpectError.js
                    │          OhReport.js
                    │          ReportExtend.js
                    │
                    └─testrunner
                            OpenHarmonyTestRunner.ts

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值