index.uts 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /**
  2. * 引用 Android 系统库,示例如下:
  3. * import { Context } from "android.content.Context";
  4. * [可选实现,按需引入]
  5. */
  6. /* 引入 interface.uts 文件中定义的变量 */
  7. import { MyApiOptions, MyApiResult, MyApi, MyApiSync } from '../interface.uts';
  8. /* 引入 unierror.uts 文件中定义的变量 */
  9. import { MyApiFailImpl } from '../unierror';
  10. /**
  11. * 引入三方库
  12. * [可选实现,按需引入]
  13. *
  14. * 在 Android 平台引入三方库有以下两种方式:
  15. * 1、[推荐] 通过 仓储 方式引入,将 三方库的依赖信息 配置到 config.json 文件下的 dependencies 字段下。详细配置方式[详见](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#dependencies)
  16. * 2、直接引入,将 三方库的aar或jar文件 放到libs目录下。更多信息[详见](https://uniapp.dcloud.net.cn/plugin/uts-plugin.html#android%E5%B9%B3%E5%8F%B0%E5%8E%9F%E7%94%9F%E9%85%8D%E7%BD%AE)
  17. *
  18. * 在通过上述任意方式依赖三方库后,使用时需要在文件中 import,如下示例:
  19. * import { LottieAnimationView } from 'com.airbnb.lottie.LottieAnimationView'
  20. */
  21. /**
  22. * UTSAndroid 为平台内置对象,不需要 import 可直接调用其API,[详见](https://uniapp.dcloud.net.cn/uts/utsandroid.html#utsandroid)
  23. */
  24. /**
  25. * 异步方法
  26. *
  27. * uni-app项目中(vue/nvue)调用示例:
  28. * 1、引入方法声明 import { myApi } from "@/uni_modules/uts-api"
  29. * 2、方法调用
  30. * myApi({
  31. * paramA: false,
  32. * complete: (res) => {
  33. * console.log(res)
  34. * }
  35. * });
  36. * uni-app x项目(uvue)中调用示例:
  37. * 1、引入方法及参数声明 import { myApi, MyApiOptions } from "@/uni_modules/uts-api";
  38. * 2、方法调用
  39. * let options = {
  40. * paramA: false,
  41. * complete: (res : any) => {
  42. * console.log(res)
  43. * }
  44. * } as MyApiOptions;
  45. * myApi(options);
  46. *
  47. */
  48. export const myApi : MyApi = function (options : MyApiOptions) {
  49. if (options.paramA == true) {
  50. // 返回数据
  51. const res : MyApiResult = {
  52. fieldA: 85,
  53. fieldB: true,
  54. fieldC: 'some message'
  55. };
  56. options.success?.(res);
  57. options.complete?.(res);
  58. } else {
  59. // 返回错误
  60. const err = new MyApiFailImpl(9010001);
  61. options.fail?.(err)
  62. options.complete?.(err)
  63. }
  64. }
  65. /**
  66. * 同步方法
  67. *
  68. * uni-app项目中(vue/nvue)调用示例:
  69. * 1、引入方法声明 import { myApiSync } from "@/uni_modules/uts-api"
  70. * 2、方法调用 myApiSync(true)
  71. *
  72. * uni-app x项目(uvue)中调用示例:
  73. * 1、引入方法及参数声明 import { myApiSync } from "@/uni_modules/uts-api";
  74. * 2、方法调用 myApiSync(true)
  75. */
  76. export const myApiSync : MyApiSync = function (paramA : boolean) : MyApiResult {
  77. // 返回数据,根据插件功能获取实际的返回值
  78. const res : MyApiResult = {
  79. fieldA: 85,
  80. fieldB: paramA,
  81. fieldC: 'some message'
  82. };
  83. return res;
  84. }
  85. /**
  86. * 更多插件开发的信息详见:https://uniapp.dcloud.net.cn/plugin/uts-plugin.html
  87. */