ChooseImageUtils.uts 2.4 KB
Newer Older
1 2
import { ChooseImageOptions, ChooseVideoOptions } from "./interface.uts"
import {
DCloud-yyl's avatar
DCloud-yyl 已提交
3
	UniError_ChooseImage, UniError_ChooseVideo, MediaErrorImpl
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
} 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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
21
			onSourceTypeSelect(count!, compressed, 1)
22
		} else if (sourceType.indexOf("camera") >= 0) {
DCloud-yyl's avatar
DCloud-yyl 已提交
23
			onSourceTypeSelect(count!, compressed, 0)
24 25 26 27 28 29 30
		}
		return
	}
	if (sourceType.length == 2) {
		uni.showActionSheet({
			itemList: itemList,
			success: (e) => {
DCloud-yyl's avatar
DCloud-yyl 已提交
31
				onSourceTypeSelect(count!, compressed, e.tapIndex!)
32 33
			},
			fail: (e) => {
DCloud-yyl's avatar
DCloud-yyl 已提交
34
				let error = new MediaErrorImpl(1101001, UniError_ChooseImage);
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
				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) {
DCloud-yyl's avatar
DCloud-yyl 已提交
52
			onSourceTypeSelect(count!, compressed, 1)
53
		} else if (sourceType.indexOf("camera") >= 0) {
DCloud-yyl's avatar
DCloud-yyl 已提交
54
			onSourceTypeSelect(count!, compressed, 0)
55 56 57 58 59 60 61 62
		}
		return
	}

	if (sourceType.length == 2) {
		uni.showActionSheet({
			itemList: itemList,
			success: (e) => {
DCloud-yyl's avatar
DCloud-yyl 已提交
63
				onSourceTypeSelect(count!, compressed, e.tapIndex!)
64 65
			},
			fail: (e) => {
DCloud-yyl's avatar
DCloud-yyl 已提交
66
				let error = new MediaErrorImpl(1101001, UniError_ChooseVideo);
67 68 69 70 71 72
				options.fail?.(error)
				options.complete?.(error)
			}
		})
	}
}