提交 062514f7 编写于 作者: DCloud_iOS_XHY's avatar DCloud_iOS_XHY

提交 iOS 端 uts 示例

上级 e12715ff
import { UIDevice } from "UIKit";
type GetBatteryInfoOptions = {
success?: (res: UTSJSONObject) => void;
fail?: (res: UTSJSONObject) => void;
complete?: (res: UTSJSONObject) => void;
};
export default function getBatteryInfo(options: GetBatteryInfoOptions) {
UIDevice.current.isBatteryMonitoringEnabled = true
const res = {
errMsg: "getBatteryInfo:ok",
level: UIDevice.current.batteryLevel * 100,
isCharging: UIDevice.current.batteryState == UIDevice.BatteryState.charging,
};
if (options.success != null) {
options.success!(res);
}
if (options.complete != null) {
options.complete!(res);
}
}
export function addViewToDecorView() { }
export function removeViewToDecorView() { }
export function initAppLifecycle() { }
export function getLogoPath() {}
export function playAssetAudio(){}
/**
* 定时任务参数封装
*/
type TimerOptions = {
/**
* 定时任务开始的回调
* @res 回调参数
*/
start: (res: string) => void;
/**
* 定时任务执行的回调
* @res 回调参数
*/
work: (res: string) => void;
};
/**
* 执行延时任务
*/
export function doTimerTask(opts:TimerOptions) {
opts.start('doTimerTask start');
setTimeout(function() {
opts.work("doTimerTask work");
}, 2000);
return { name: "doTimerTask" };
}
/**
* 执行周期任务
*/
export function doIntervalTask(opts:TimerOptions) {
let taskRet = setInterval(function() {
opts.work("doIntervalTask work");
}, 2000);
opts.start('doIntervalTask start');
return { name: "doIntervalTask",taskId:taskRet};
}
/**
* 清除周期任务
*/
export function clearIntervalTask(taskId:number) {
clearInterval(taskId);
return { name: "clearIntervalTask"};
}
\ No newline at end of file
/**
* json参数格式定义
*/
type inputJSON = {
inputText: string,
errCode: number
}
/**
* json入参格式
*/
type JsonParamOptions = {
input: inputJSON;
success: (res: string) => void;
fail: (res: string) => void;
complete: (res: string) => void;
};
/**
* 导出无参的UTS函数
* @param opts
*/
export function callWithoutParam(success: () => void) {
success();
return { name: "doSthWithCallback" };
}
/**
* 导出一个字符串入参的UTS函数
*/
export function callWithStringParam(input: string, success: (res: string) => void) {
success(input);
return { name: "doSthWithCallback" };
}
/**
* 导出一个JSON入参的UTS函数
*/
export function callWithJSONParam(opts: JsonParamOptions) {
opts.input.errCode = 10;
opts.success(opts.input);
return { name: "doSthWithCallback" };
}
import { log } from "./utils.uts";
type AsyncOptions = {
type: string;
success: (res: string) => void;
fail: (res: string) => void;
complete: (res: string) => void;
};
/**
* 导出一个属性
*/
export const MAX = 100;
/**
* 导出一个同步方法
* @returns
*/
export function testSync(msg: string) {
console.log("log test");
log("log test1");
return {
msg: `hello ${msg}`,
};
}
/**
* 导出一个同步方法(触发了数组越界异常)
*/
export function testSyncError() {
const arr: string[] = [];
console.log(arr[1]);
}
/**
* 导出一个带callback的同步方法
* @param opts
*/
export function testSyncWithCallback(opts: AsyncOptions) {
if (opts.type == "success") {
opts.success("success");
} else {
opts.fail("fail");
}
opts.complete("complete");
return { name: "testSyncWithCallback" };
}
/**
* 导出一个异步方法
* @returns
*/
export async function testAsync(opts: AsyncOptions) {
if (opts.type == "success") {
opts.success("success");
} else {
opts.fail("fail");
}
opts.complete("complete");
return { name: "testAsync" };
}
type TestOptions = {
name: string;
callback: (res: string) => void;
};
export class Test {
id: number;
name: string;
static type: string = "Test";
constructor(id: number, options: TestOptions) {
this.id = id;
this.name = options.name;
options.callback("Test.constructor");
}
static testClassStaticSyncWithCallback(opts: AsyncOptions): UTSJSONObject {
return testSyncWithCallback(opts);
}
static async testClassStaticAsync(opts: AsyncOptions): Promise<UTSJSONObject> {
const res = await testAsync(opts);
return res;
}
testClassSyncWithCallback(opts: AsyncOptions): UTSJSONObject {
return testSyncWithCallback(opts);
}
async testClassAsync(opts: AsyncOptions): Promise<UTSJSONObject> {
const res = await testAsync(opts);
return res;
}
}
import { log } from "./utils.uts";
type AsyncOptions = {
type: string;
success: (res: string) => void;
fail: (res: string) => void;
complete: (res: string) => void;
};
/**
* 导出一个属性
*/
export const MAX = 100;
/**
* 导出一个同步方法
* @returns
*/
export function testSync(msg: string) {
console.log("log test");
log("log test1");
return {
msg: `hello ${msg}`,
};
}
/**
* 导出一个同步方法(触发了数组越界异常)
*/
export function testSyncError() {
const arr: string[] = [];
console.log(arr[1]);
}
/**
* 导出一个带callback的同步方法
* @param opts
*/
export function testSyncWithCallback(opts: AsyncOptions) {
if (opts.type == "success") {
opts.success("success");
} else {
opts.fail("fail");
}
opts.complete("complete");
return { name: "testSyncWithCallback" };
}
/**
* 导出一个异步方法
* @returns
*/
export async function testAsync(opts: AsyncOptions) {
if (opts.type == "success") {
opts.success("success");
} else {
opts.fail("fail");
}
opts.complete("complete");
return { name: "testAsync" };
}
type TestOptions = {
name: string;
callback: (res: string) => void;
};
export class Test {
id: number;
name: string;
static type: string = "Test";
constructor(id: number, options: TestOptions) {
this.id = id;
this.name = options.name;
options.callback("Test.constructor");
}
static testClassStaticSyncWithCallback(opts: AsyncOptions): UTSJSONObject {
return testSyncWithCallback(opts);
}
static async testClassStaticAsync(opts: AsyncOptions): Promise<UTSJSONObject> {
const res = await testAsync(opts);
return res;
}
testClassSyncWithCallback(opts: AsyncOptions): UTSJSONObject {
return testSyncWithCallback(opts);
}
async testClassAsync(opts: AsyncOptions): Promise<UTSJSONObject> {
const res = await testAsync(opts);
return res;
}
}
export function log(msg: string) {
console.log(msg);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册