index.uts 4.7 KB
Newer Older
1 2
import { UIImageView , UIImage , UIScreen } from 'UIKit';
import { DispatchQueue } from 'Dispatch';
杜庆泉's avatar
杜庆泉 已提交
3

lizhongyi_'s avatar
lizhongyi_ 已提交
4 5
import { UTSiOS } from "DCloudUTSFoundation";

DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
6 7 8 9 10 11 12 13 14
export function addViewToDecorView() { }
export function removeViewToDecorView() { }
export function initAppLifecycle() { }
export function getLogoPath() {}
export function playAssetAudio(){}

/**
 * 定时任务参数封装
 */
Y
yurj26 已提交
15
export type TimerOptions = {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
16 17 18 19 20 21 22 23 24 25 26 27
	/**
	 * 定时任务开始的回调
	 * @res 回调参数
	 */
	start: (res: string) => void;
	/**
	* 定时任务执行的回调
	* @res 回调参数
	*/
	work: (res: string) => void;
};

Y
yurj26 已提交
28 29 30 31 32
export type TimerResult = {
	name : string;
	taskId ?: number;
};

DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
33 34 35 36

/**
 * 执行延时任务
 */
Y
yurj26 已提交
37
export function doTimerTask(opts:TimerOptions): TimerResult {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
38 39 40 41 42
	opts.start('doTimerTask start');
	setTimeout(function() {
		opts.work("doTimerTask work");
	}, 2000);
  
Y
yurj26 已提交
43 44 45 46 47
  const result: TimerResult = { 
    name: "doTimerTask" 
  }

	return result
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
48 49 50 51 52
}

/**
 * 执行周期任务
 */
Y
yurj26 已提交
53
export function doIntervalTask(opts:TimerOptions): TimerResult {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
54 55 56 57 58 59
	
	let taskRet = setInterval(function() {
		opts.work("doIntervalTask work");
	}, 2000);
	opts.start('doIntervalTask start');
  
Y
yurj26 已提交
60 61 62 63 64 65
  const result: TimerResult = { 
    name: "doIntervalTask",
    taskId: taskRet
  }

	return result
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
66 67 68 69 70
}

/**
 * 清除周期任务
 */
Y
yurj26 已提交
71
export function clearIntervalTask(taskId:number):TimerResult {
DCloud_iOS_XHY's avatar
DCloud_iOS_XHY 已提交
72 73
	
	clearInterval(taskId);
Y
yurj26 已提交
74 75 76 77 78
  const result: TimerResult = { 
    name: "clearIntervalTask" 
  }

	return result
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
}

/* 
 * 保存全局数据信息
 */
class AdvanceModuleGloabInfo {
	static imageView?: UIImageView = null
}


/**
 * 将h5资源路径转成app本地资源路径 
 */
export function getResourcePath(path: string) {
	const imagePath = UTSiOS.getResourcePath(path)
	console.log(imagePath)
	
	if (AdvanceModuleGloabInfo.imageView == null) { 
		let vc = UTSiOS.getCurrentViewController()
		// uts方法默认会在子线程中执行,涉及 UI 操作必须在主线程中运行,通过 DispatchQueue.main.async 方法可将代码在主线程中运行
		DispatchQueue.main.async(execute=():void => {
			// 创建imageView
			let imageView = new UIImageView()
			let image = new UIImage(contentsOfFile = imagePath)
			imageView.image = image
			
			// 添加imageView并设置frame
			vc.view.addSubview(imageView)
lizhongyi_'s avatar
lizhongyi_ 已提交
107
			let imageSize: CGFloat = 80.0
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
			let midx = (UIScreen.main.bounds.size.width - imageSize) / 2
			let midy = (UIScreen.main.bounds.size.height - imageSize) / 2
			imageView.frame = CGRect(x = midx, y = midy, width = imageSize, height = imageSize)
			AdvanceModuleGloabInfo.imageView = imageView
		})
	}

}


export function removeExampleImageView() {
	DispatchQueue.main.async(execute=():void => {
		if (AdvanceModuleGloabInfo.imageView != null) {
			AdvanceModuleGloabInfo.imageView!.removeFromSuperview()
			AdvanceModuleGloabInfo.imageView = null
		}
	})
lizhongyi_'s avatar
lizhongyi_ 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
}

/**
 * add since 2023-06-19 
 * 新增传参测试用例
 */
export function inputArray(input : Array<string>) : boolean {

	let inputStr = JSON.stringify(input)

	if ('["a","b","c"]' == inputStr) {
		return true
	}
	return false

}

export type ParamOptions = {
	title : string,
	array : Array<string>
}


export function inputParam(option : ParamOptions) : boolean {
	console.log(option, "传入的参数")
	let inputStr = JSON.stringify(option)
	console.log(inputStr, 'stringify option')
	if ('{"array":[1,2,3],"title":"hello"}' == inputStr) {
		return true
	}
	return false
}

export function returnArray() : Array<string> {
	return ['1', '2', '3']
}

export function returnParam() : ParamOptions {

	let ret : ParamOptions = {
		title: "returnParam",
		array: ['1', '2', '3']
	}
	return ret

}

export type ParamCallback = (res : ParamOptions) => void
export type ArrayCallback = (res : Array<string>) => void

export function callbackArray(callback : ArrayCallback) {
	callback(['8', '8', '8'])
}


export function callbackParam(callback : ParamCallback) {
	let ret : ParamOptions = {
		title: "callbackParam",
		array: ['4', '5', '6']
	}
	callback(ret)   
}
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217

/**
 * 打印测试方法
 */
export function logStrTest() {
	console.log("logStrTest 字符串打印测试")
}

export function logFloatTest() {
	console.log(3.1415926)
}
export function logIntTest() {
	console.log(2023)
}

export function logObjectTest(){
	let ret : ParamOptions = {
		title: "logObjectTest",
		array: ['1', '2', '3']
	}
	console.log(ret)
}


export function logFunctionTest(){
	
	let testFun = function(){
		console.log("testFun")
	}
	console.log(testFun)
}
lizhongyi_'s avatar
lizhongyi_ 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234


export class User {  

    private name : string;  
    private age : Int;  

    constructor(hostP : string, port : Int) {  
        this.name = hostP;  
        this.age = port;  
    }  
	
	describeSelf():string{
		let text = "name = " + this.name + ";age = " + `${this.age}`
		return text
	}
}