ChooseImageUtils.uts 2.4 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
import { ChooseImageOptions, ChooseVideoOptions } from "./interface.uts"
import {
	UniError_ChooseImage, UniError_ChooseVideo, MediaErrorImpl
} from "./unierror.uts"
export const CODE_CAMERA_ERROR = 11;
export const CODE_GALLERY_ERROR = 12;
export const CODE_GET_IMAGE_INFO_CODE = 13

export function uniChooseImage(options : ChooseImageOptions, onSourceTypeSelect : (count : number, compressed : boolean, index : number) => void) {
	let count = options.count != null ? (options.count! <= 0 ? 9 : options.count) : 9
	// 默认为 false
	let compressed = false;
	if ((options.sizeType) != null) {
		compressed = options.sizeType!.indexOf("original") < 0
	}
	/* source type 乱传如何处理 */
	let sourceType : Array<string> = (options.sourceType != null && options.sourceType!.length > 0) ? options.sourceType! : ["album", "camera"];
	let itemList = ["拍摄", "从相册选择"]
	if (sourceType.length == 1) {
		if (sourceType.indexOf("album") >= 0) {
			onSourceTypeSelect(count!, compressed, 1)
		} else if (sourceType.indexOf("camera") >= 0) {
			onSourceTypeSelect(count!, compressed, 0)
		}
		return
	}
	if (sourceType.length == 2) {
		uni.showActionSheet({
			itemList: itemList,
			success: (e) => {
				onSourceTypeSelect(count!, compressed, e.tapIndex!)
			},
			fail: (e) => {
				let error = new MediaErrorImpl(1101001, UniError_ChooseImage);
				options.fail?.(error)
				options.complete?.(error)
			}
		})
	}
}

export function uniChooseVideo(options : ChooseVideoOptions, onSourceTypeSelect : (count : number, compressed : boolean, index : number) => void) {
	let count = 1
	// 默认为 false
	let compressed = options.compressed != null ? options.compressed! : true;

	/* source type 乱传如何处理 */
	let sourceType : Array<string> = (options.sourceType != null && options.sourceType!.length > 0) ? options.sourceType! : ["album", "camera"];
	let itemList = ["拍摄", "从相册选择"]
	if (sourceType.length == 1) {
		if (sourceType.indexOf("album") >= 0) {
			onSourceTypeSelect(count!, compressed, 1)
		} else if (sourceType.indexOf("camera") >= 0) {
			onSourceTypeSelect(count!, compressed, 0)
		}
		return
	}

	if (sourceType.length == 2) {
		uni.showActionSheet({
			itemList: itemList,
			success: (e) => {
				onSourceTypeSelect(count!, compressed, e.tapIndex!)
			},
			fail: (e) => {
				let error = new MediaErrorImpl(1101001, UniError_ChooseVideo);
				options.fail?.(error)
				options.complete?.(error)
			}
		})
	}
}