提交 7dca62c8 编写于 作者: 杜庆泉's avatar 杜庆泉

add uts doc

上级 21bfe6e7
......@@ -37,15 +37,15 @@
});
},
testDoSthWithString: function () {
callWithStringParam({
input:this.stringParam,
success:function(response){
callWithStringParam(
this.stringParam,
function(response){
uni.showToast({
title:'uts插件uts-helloworld的callWithStringParam方法收到了你输入的字符串:'+response,
icon:'none'
});
},
});
);
},
testDoSthWithJSON: function () {
console.log(this.jsonParam);
......
......@@ -36,9 +36,12 @@ export function callWithoutParam(opts: NoParamOptions) {
return { name: "doSthWithCallback" };
}
export function callWithStringParam(opts: StringParamOptions) {
export function callWithStringParam(input:string,success: (res: string) => void) {
setTimeout(function() {
opts.success(opts.input);
success(input);
}, 500);
return { name: "doSthWithCallback" };
......
......@@ -4,70 +4,28 @@ import kotlinx.coroutines.CoroutineScope;
import kotlinx.coroutines.Deferred;
import kotlinx.coroutines.Dispatchers;
import io.dcloud.uts.*;
fun log(msg: String) {
console.log(msg, "at uni_modules/uts-advance/app-android/utils.uts:2");
}
open class AsyncOptions {
open lateinit var type: String;
open lateinit var success: UTSCallback;
open lateinit var fail: UTSCallback;
open lateinit var complete: UTSCallback;
}
val MAX = 100;
fun testSync(msg: String): UtsJSONObject {
console.log("log test", "at uni_modules/uts-advance/app-android/index.uts:18");
log("log test1");
open class TimerOptions {
open lateinit var start: UTSCallback;
open lateinit var work: UTSCallback;
}
fun doTimerTask(opts: TimerOptions): UtsJSONObject {
opts.start("doTimerTask start");
setTimeout(fun() {
opts.work("doTimerTask work");
}
, 2000);
return object : UtsJSONObject() {
var msg = """hello ${msg}"""
var name = "doTimerTask"
};
}
fun testSyncError() {
val arr: MutableList<String> = mutableListOf();
console.log(arr[1], "at uni_modules/uts-advance/app-android/index.uts:29");
}
fun testSyncWithCallback(opts: AsyncOptions): UtsJSONObject {
if (opts.type == "success") opts.success("success");
else opts.fail("fail");
opts.complete("complete");
fun doIntervalTask(opts: TimerOptions): UtsJSONObject {
var taskRet = setInterval(fun() {
opts.work("doIntervalTask work");
}
, 2000);
opts.start("doIntervalTask start");
return object : UtsJSONObject() {
var name = "testSyncWithCallback"
};
}
suspend fun testAsync(opts: AsyncOptions): Deferred<UtsJSONObject> = CoroutineScope(Dispatchers.Default).async {
if (opts.type == "success") opts.success("success");
else opts.fail("fail");
opts.complete("complete");
return@async object : UtsJSONObject() {
var name = "testAsync"
var name = "doIntervalTask"
var taskId = taskRet
};
}
open class TestOptions {
open lateinit var name: String;
open lateinit var callback: UTSCallback;
}
open class Test {
open var id: Number;
open var name: String;
constructor(id: Number, options: TestOptions){
this.id = id;
this.name = options.name;
options.callback("Test.constructor");
}
open fun testClassSyncWithCallback(opts: AsyncOptions): UtsJSONObject {
return testSyncWithCallback(opts);
}
open suspend fun testClassAsync(opts: AsyncOptions): Deferred<UtsJSONObject> = CoroutineScope(Dispatchers.Default).async {
val res = testAsync(opts).await();
return@async res;
}
companion object {
var type: String = "Test";
fun testClassStaticSyncWithCallback(opts: AsyncOptions): UtsJSONObject {
return testSyncWithCallback(opts);
}
suspend fun testClassStaticAsync(opts: AsyncOptions): Deferred<UtsJSONObject> = CoroutineScope(Dispatchers.Default).async {
val res = testAsync(opts).await();
return@async res;
}
}
}
......@@ -34,9 +34,9 @@ fun callWithoutParam(opts: NoParamOptions): UtsJSONObject {
var name = "doSthWithCallback"
};
}
fun callWithStringParam(opts: StringParamOptions): UtsJSONObject {
fun callWithStringParam(input: String, success: (res: String) -> Unit): UtsJSONObject {
setTimeout(fun() {
opts.success(opts.input);
success(input);
}
, 500);
return object : UtsJSONObject() {
......
......@@ -12,7 +12,7 @@ fun getBatteryCapacity(): String {
if (context != null) {
val manager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager;
val currentLevel: Number = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
return "" + currentLevel + "**456464%";
return "" + currentLevel + "%";
}
return "0%";
}
......@@ -199,14 +199,11 @@ if (uni.restoreGlobal) {
});
},
testDoSthWithString: function() {
callWithStringParam({
input: this.stringParam,
success: function(response) {
callWithStringParam(this.stringParam, function(response) {
uni.showToast({
title: "uts\u63D2\u4EF6uts-helloworld\u7684callWithStringParam\u65B9\u6CD5\u6536\u5230\u4E86\u4F60\u8F93\u5165\u7684\u5B57\u7B26\u4E32\uFF1A" + response,
icon: "none"
});
}
});
},
testDoSthWithJSON: function() {
......
......@@ -34,9 +34,9 @@ fun callWithoutParam(opts: NoParamOptions): UtsJSONObject {
var name = "doSthWithCallback"
};
}
fun callWithStringParam(opts: StringParamOptions): UtsJSONObject {
fun callWithStringParam(input: String, success: (res: String) -> Unit): UtsJSONObject {
setTimeout(fun() {
opts.success(opts.input);
success(input);
}
, 500);
return object : UtsJSONObject() {
......
{"version":3,"sources":["\\\\?\\D:\\Do\\Source\\hello-uts\\uni_modules\\uts-helloworld\\app-android\\index.uts"],"sourcesContent":["type NoParamOptions = {\n success: (res: string) => void;\n fail: (res: string) => void;\n complete: (res: string) => void;\n};\r\n\r\ntype StringParamOptions = {\r\n input:string;\n success: (res: string) => void;\n fail: (res: string) => void;\n complete: (res: string) => void;\n};\r\n\r\n\r\ntype inputJSON = {\r\n\tinputText:string,\r\n\terrCode:number\r\n}\r\ntype JsonParamOptions = {\r\n input:inputJSON;\n success: (res: string) => void;\n fail: (res: string) => void;\n complete: (res: string) => void;\n};\r\n\r\n\r\n/**\n * 导出一个带callback的同步方法\n * @param opts\n */\nexport function callWithoutParam(opts: NoParamOptions) {\r\n\tsetTimeout(function() {\r\n\t\topts.success();\r\n\t}, 500);\n \n return { name: \"doSthWithCallback\" };\n}\r\n\r\nexport function callWithStringParam(opts: StringParamOptions) {\r\n\tsetTimeout(function() {\r\n\t\topts.success(opts.input);\r\n\t}, 500);\n \n return { name: \"doSthWithCallback\" };\n}\r\n\r\n\r\nexport function callWithJSONParam(opts: JsonParamOptions) {\r\n\topts.input.errCode = 10;\r\n\tsetTimeout(function() {\r\n\t\topts.success(opts.input);\r\n\t}, 500);\n \n return { name: \"doSthWithCallback\" };\n}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;QACrB,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC;;IACzB,EAAE,GAAG,CAAC,CAAC;IAEP,OAAO;QAAE,IAAA,IAAI,GAAE,mBAAmB;KAAE,CAAC"}
\ No newline at end of file
{"version":3,"sources":["\\\\?\\D:\\Do\\Source\\hello-uts\\uni_modules\\uts-helloworld\\app-android\\index.uts"],"sourcesContent":["type NoParamOptions = {\n success: (res: string) => void;\n fail: (res: string) => void;\n complete: (res: string) => void;\n};\r\n\r\ntype StringParamOptions = {\r\n input:string;\n success: (res: string) => void;\n fail: (res: string) => void;\n complete: (res: string) => void;\n};\r\n\r\n\r\ntype inputJSON = {\r\n\tinputText:string,\r\n\terrCode:number\r\n}\r\ntype JsonParamOptions = {\r\n input:inputJSON;\n success: (res: string) => void;\n fail: (res: string) => void;\n complete: (res: string) => void;\n};\r\n\r\n\r\n/**\n * 导出一个带callback的同步方法\n * @param opts\n */\nexport function callWithoutParam(opts: NoParamOptions) {\r\n\tsetTimeout(function() {\r\n\t\topts.success();\r\n\t}, 500);\n \n return { name: \"doSthWithCallback\" };\n}\r\n\r\n\n \r\nexport function callWithStringParam(input:string,success: (res: string) => void) {\r\n\t\r\n\tsetTimeout(function() {\r\n\t\tsuccess(input);\r\n\t}, 500);\n \n return { name: \"doSthWithCallback\" };\n}\r\n\r\n\r\nexport function callWithJSONParam(opts: JsonParamOptions) {\r\n\topts.input.errCode = 10;\r\n\tsetTimeout(function() {\r\n\t\topts.success(opts.input);\r\n\t}, 500);\n \n return { name: \"doSthWithCallback\" };\n}\r\n\r\n\r\n"],"names":[],"mappings":";;;;;;AAAsB,WAAjB,cAAc;IACjB,kBAAA,OAAO,EAAA,WAAuB,CAAC;IAC/B,kBAAA,IAAI,EAAA,WAAuB,CAAC;IAC5B,kBAAA,QAAQ,EAAA,WAAuB,CAAC;CACjC,AAAC;AAEwB,WAArB,kBAAkB;IACrB,kBAAA,KAAK,EAAC,MAAM,CAAC;IACb,kBAAA,O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routines.Dispatchers;
import io.dcloud.uts.*;
fun log(msg: String) {
console.log(msg, "at uni_modules/uts-advance/app-android/utils.uts:2");
}
open class AsyncOptions {
open lateinit var type: String;
open lateinit var success: UTSCallback;
open lateinit var fail: UTSCallback;
open lateinit var complete: UTSCallback;
}
val MAX = 100;
fun testSync(msg: String): UtsJSONObject {
console.log("log test", "at uni_modules/uts-advance/app-android/index.uts:18");
log("log test1");
open class TimerOptions {
open lateinit var start: UTSCallback;
open lateinit var work: UTSCallback;
}
fun doTimerTask(opts: TimerOptions): UtsJSONObject {
opts.start("doTimerTask start");
setTimeout(fun() {
opts.work("doTimerTask work");
}
, 2000);
return object : UtsJSONObject() {
var msg = """hello ${msg}"""
var name = "doTimerTask"
};
}
fun testSyncError() {
val arr: MutableList<String> = mutableListOf();
console.log(arr[1], "at uni_modules/uts-advance/app-android/index.uts:29");
}
fun testSyncWithCallback(opts: AsyncOptions): UtsJSONObject {
if (opts.type == "success") opts.success("success");
else opts.fail("fail");
opts.complete("complete");
fun doIntervalTask(opts: TimerOptions): UtsJSONObject {
var taskRet = setInterval(fun() {
opts.work("doIntervalTask work");
}
, 2000);
opts.start("doIntervalTask start");
return object : UtsJSONObject() {
var name = "testSyncWithCallback"
};
}
suspend fun testAsync(opts: AsyncOptions): Deferred<UtsJSONObject> = CoroutineScope(Dispatchers.Default).async {
if (opts.type == "success") opts.success("success");
else opts.fail("fail");
opts.complete("complete");
return@async object : UtsJSONObject() {
var name = "testAsync"
var name = "doIntervalTask"
var taskId = taskRet
};
}
open class TestOptions {
open lateinit var name: String;
open lateinit var callback: UTSCallback;
}
open class Test {
open var id: Number;
open var name: String;
constructor(id: Number, options: TestOptions){
this.id = id;
this.name = options.name;
options.callback("Test.constructor");
}
open fun testClassSyncWithCallback(opts: AsyncOptions): UtsJSONObject {
return testSyncWithCallback(opts);
}
open suspend fun testClassAsync(opts: AsyncOptions): Deferred<UtsJSONObject> = CoroutineScope(Dispatchers.Default).async {
val res = testAsync(opts).await();
return@async res;
}
companion object {
var type: String = "Test";
fun testClassStaticSyncWithCallback(opts: AsyncOptions): UtsJSONObject {
return testSyncWithCallback(opts);
}
suspend fun testClassStaticAsync(opts: AsyncOptions): Deferred<UtsJSONObject> = CoroutineScope(Dispatchers.Default).async {
val res = testAsync(opts).await();
return@async res;
}
}
}
......@@ -34,9 +34,9 @@ fun callWithoutParam(opts: NoParamOptions): UtsJSONObject {
var name = "doSthWithCallback"
};
}
fun callWithStringParam(opts: StringParamOptions): UtsJSONObject {
fun callWithStringParam(input: String, success: (res: String) -> Unit): UtsJSONObject {
setTimeout(fun() {
opts.success(opts.input);
success(input);
}
, 500);
return object : UtsJSONObject() {
......
......@@ -12,7 +12,7 @@ fun getBatteryCapacity(): String {
if (context != null) {
val manager = context.getSystemService(Context.BATTERY_SERVICE) as BatteryManager;
val currentLevel: Number = manager.getIntProperty(BatteryManager.BATTERY_PROPERTY_CAPACITY);
return "" + currentLevel + "**456464%";
return "" + currentLevel + "%";
}
return "0%";
}
## 1 前置条件
+ 1 HX 3.5.6 之后版本
## 2 uts原生插件介绍
### 2.1 什么是uts原生插件
UTS= Uni Type Script.
UTS 插件是在uni原生插件的基础上,改用UTS作为插件开发语言。
进一步降低插件开发门槛,平台差异,执行效率的新型插件形式
![uts插件结构](./UTS结构示意图1.png)
### 2.2 uts原生插件与uni原生插件的区别
|-|传统原生插件|uts原生插件|
|-|-------|--------|
|开发语言|java/oc|uts|
|开发环境|Android studio/XCode|HBuilderX|
|打包方式|外挂aar 等产出物|编译时生成原生代码|
优点:
1 减少原生环境搭建环节,降低插件开发难度
2 进一步降低平台差异,一种语言开发两个平台插件
2 编译时生成原生代码,提高代码执行效率
## 3 UTS语法介绍
## 4 开发UTS插件
### 4.1 uni_modules
确保项目根目录存在uni_modules文件夹
![插件目录](./uni_modules.jpg)
`uni_modules`类似 NPM中的`node_modules`.是用来存放插件依赖的文件夹。
如果项目中已存在此目录,则跳过。如果不存在,在需要手动创建一个。
### 4.2 新建UTS插件
选中uni_modules文件夹 --> 右键新建插件
![新建插件1](./new_uts_plugin.jpg)
选择UTS原生插件
![新建插件2](./new_uts_plugin2.jpg)
插件目录结构
![新建插件3](./new_uts_plugin3.jpg)
### 4.3 清单文件package.json
package.json为插件的清单文件,里面说明了整个UTS插件的配置信息,下面是一个完整的示例
```
{
"id": "uts-helloworld",
"displayName": "uts-helloworld",
"version": "1.0.0",
"description": "uts-helloworld",
"keywords": [
"uts-helloworld"
],
"repository": "",
"engines": {
"HBuilderX": "^3.1.0"
},
"dcloudext": {
"type": "native-uts",
"sale": {
"regular": {
"price": "0.00"
},
"sourcecode": {
"price": "0.00"
}
},
"contact": {
"qq": ""
},
"declaration": {
"ads": "",
"data": "",
"permissions": ""
},
"npmurl": ""
},
"uni_modules": {
"dependencies": [],
"encrypt": [],
"platforms": {
"cloud": {
"tcb": "u",
"aliyun": "u"
},
"client": {
"Vue": {
"vue2": "u",
"vue3": "u"
},
"App": {
"app-vue": "u",
"app-nvue": "u"
},
"H5-mobile": {
"Safari": "u",
"Android Browser": "u",
"微信浏览器(Android)": "u",
"QQ浏览器(Android)": "u"
},
"H5-pc": {
"Chrome": "u",
"IE": "u",
"Edge": "u",
"Firefox": "u",
"Safari": "u"
},
"小程序": {
"微信": "u",
"阿里": "u",
"百度": "u",
"字节跳动": "u",
"QQ": "u",
"钉钉": "u",
"快手": "u",
"飞书": "u",
"京东": "u"
},
"快应用": {
"华为": "u",
"联盟": "u"
}
}
},
"type": "uts",
"uts": {
"android": {
"libs": [],
"dependencies": []
},
"ios": {
"libs": []
}
}
}
}
```
## 5 使用插件
### 5.1 引用UTS插件
uts插件import有两种方式
import {a,b} from 'xxx'
```
import {
callWithoutParam,
callWithStringParam,
callWithJSONParam
} from "../../../uni_modules/uts-helloworld";
```
import X from 'XXX'
### 5.2 用法
延迟操作
```
//执行延迟操作
var timer = setTimeout(function, 1000);
//取消延迟操作
clearTimeout(timer);
```
定时操作
```
//执行定时操作
var timer = setInterval(function, 1000);
//取消定时操作
clearInterval(timer);
```
## 6 测试
### 5.1 真机运行
直接运行
### 5.2 云端打包
### 5.3 示例项目
### 一个简单的示例
### 插件目录结构
### 插件使用方法
完整的示例项目地址:
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册