import api from "@/api";

export default {
	uploadObj: api.system.asset.upload,
	fileListObj: api.system.asset.list,
	moveFileObj: api.system.asset.move,
	delFileObj: api.system.asset.del,
	menuListObj: api.system.asset_category.list,
	menuAddObj: api.system.asset_category.add,
	menuEditObj: api.system.asset_category.edit,
	menuDelObj: api.system.asset_category.del,
	successCode: 200,
	maxSize: 20,
	max: 99,
	uploadParseData: function (res) {
		return Object.assign({
			fileName: res.data.fileName,
			url: res.data.filePath
		}, res.data)
	},
	listParseData: function (res) {
		return {
			rows: res.data,
			total: res.count,
			msg: res.msg,
			code: res.code
		}
	},
	request: {
		page: 'page',
		pageSize: 'limit',
		keyword: 'file_name',
		menuKey: 'category_id'
	},
	menuProps: {
		key: 'category_id',
		label: 'category_name',
		children: 'children'
	},
	fileProps: {
		id: 'asset_id',
		fileName: 'ori_name',
		url: 'url'
	},
	fileType(type: string) {
		switch (type) {
			case 'word':
				return {
					"accept": "application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document",
					"ext": ['doc', 'docx']
				}
			case 'excel':
				return {
					"accept": "application/vnd.ms-excel, application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
					"ext": ['xls', 'xlsx']
				}
			case 'ppt':
				return {
					"accept": "application/vnd.ms-powerpoint, application/vnd.openxmlformats-officedocument.presentationml.presentation",
					"ext": ['ppt', 'pptx']
				}
			case 'image':
				return {
					"accept": "image/x-ms-bmp, image/jpeg, image/gif, image/png",
					"ext": ['png', 'jpg', 'bmp', 'jpeg', 'gif']
				}
			case 'video':
				return {
					"accept": "video/mp4",
					"ext": ['mp4']
				}
			case 'audio':
				return {
					"accept": "audio/mpeg, audio/wav",
					"ext": ['mp3', 'wav']
				}
			default:
				return {
					"accept": "",
					"ext": []
				}
		}
	},
	// 图片
	isImg(fileUrl: string) {
		const fileExt = fileUrl.substring(fileUrl.lastIndexOf(".") + 1)
		return this.fileType('image')['ext'].indexOf(fileExt) != -1
	},
	// 视频
	isVideo(fileUrl: string) {
		const fileExt = fileUrl.substring(fileUrl.lastIndexOf(".") + 1)
		return this.fileType('video')['ext'].indexOf(fileExt) != -1
	},
	getExt(fileUrl: string) {
		return fileUrl.substring(fileUrl.lastIndexOf(".") + 1)
	},
	getType(name: string) {
		switch (this.getExt(name)) {
			case 'mp3':
			case 'wav':
				return 'audio';
			case 'mp4':
				return 'video';
			case 'png':
			case 'jpg':
			case 'jpeg':
			case 'bmp':
			case 'gif':
				return 'image';
			case 'ppt':
			case 'pptx':
				return 'ppt';
			case 'xls':
			case 'xlsx':
				return 'excel';
			case 'doc':
			case 'docx':
				return 'word';
			default:
				return 'unknown'
		}
	}
}
