ChooseImageUtils.uts 2.3 KB
Newer Older
DCloud-yyl's avatar
DCloud-yyl 已提交
1
import { ChooseImageOptions, ChooseVideoOptions } from "../interface.uts"
2
import {
DCloud-yyl's avatar
DCloud-yyl 已提交
3
	UniError_ChooseImage, UniError_ChooseVideo, MediaErrorImpl
DCloud-yyl's avatar
DCloud-yyl 已提交
4
} from "../unierror.uts"
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
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
				options.fail?.(error)
				options.complete?.(error)
			}
		})
	}
}

export function uniChooseVideo(options : ChooseVideoOptions, onSourceTypeSelect : (count : number, compressed : boolean, index : number) => void) {
	let count = 1

	/* 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 已提交
50
			onSourceTypeSelect(count, options.compressed, 1)
51
		} else if (sourceType.indexOf("camera") >= 0) {
DCloud-yyl's avatar
DCloud-yyl 已提交
52
			onSourceTypeSelect(count, options.compressed, 0)
53 54 55 56 57 58 59 60
		}
		return
	}

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