fix: rang drag problem & opt picture compress
This commit is contained in:
@@ -10,7 +10,10 @@ interface RangeProps {
|
|||||||
max?: number;
|
max?: number;
|
||||||
step?: number;
|
step?: number;
|
||||||
value?: [number, number];
|
value?: [number, number];
|
||||||
onChange?: (name: string, value: [number, number]) => void;
|
onChange?: {
|
||||||
|
(value: [number, number]): void
|
||||||
|
(name: string, value: [number, number]): void
|
||||||
|
};
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
className?: string;
|
className?: string;
|
||||||
name: string;
|
name: string;
|
||||||
@@ -41,7 +44,11 @@ const NtrpRange: React.FC<RangeProps> = ({
|
|||||||
|
|
||||||
const handleEndChange = (val: [number, number]) => {
|
const handleEndChange = (val: [number, number]) => {
|
||||||
setCurrentValue(val);
|
setCurrentValue(val);
|
||||||
|
if (name) {
|
||||||
onChange?.(name, val);
|
onChange?.(name, val);
|
||||||
|
} else {
|
||||||
|
onChange?.(val)
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const marks = useMemo(() => {
|
const marks = useMemo(() => {
|
||||||
|
|||||||
@@ -68,12 +68,12 @@ export default function UploadCover(props: UploadCoverProps) {
|
|||||||
setVisible(false)
|
setVisible(false)
|
||||||
}, [value])
|
}, [value])
|
||||||
|
|
||||||
const onWxAdd = useCallback((images: CoverImageValue[], onFileUploaded: Promise<{ id: string, data: uploadFileResponseData }[]>) => {
|
const onWxAdd = useCallback((images: CoverImageValue[], onFileUpdate: Promise<{ id: string, url: string }[]>) => {
|
||||||
onAdd(images)
|
onAdd(images)
|
||||||
onFileUploaded.then(res => {
|
onFileUpdate.then(res => {
|
||||||
onAdd(res.map(item => ({
|
onAdd(res.map(item => ({
|
||||||
id: item.id,
|
id: item.id,
|
||||||
url: item.data.file_path,
|
url: item.url,
|
||||||
})))
|
})))
|
||||||
})
|
})
|
||||||
}, [onAdd])
|
}, [onAdd])
|
||||||
|
|||||||
@@ -11,6 +11,80 @@ export interface UploadFromWxProps {
|
|||||||
maxCount: number
|
maxCount: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function convert_to_jpg_and_compress (src: string, { width, height }): Promise<string> {
|
||||||
|
console.log(width, height, '13123132')
|
||||||
|
const canvas = Taro.createOffscreenCanvas({ type: '2d', width, height })
|
||||||
|
console.log(canvas, canvas.width, canvas.height, 'canvas')
|
||||||
|
const ctx = canvas.getContext('2d') as CanvasRenderingContext2D
|
||||||
|
|
||||||
|
const image = canvas.createImage()
|
||||||
|
await new Promise(resolve => {
|
||||||
|
image.onload = resolve
|
||||||
|
image.src = src
|
||||||
|
})
|
||||||
|
ctx.clearRect(0, 0, width, height)
|
||||||
|
ctx.drawImage(image, 0, 0, width, height)
|
||||||
|
|
||||||
|
const imageData = ctx.getImageData(0, 0, width, height)
|
||||||
|
console.log(imageData, 'imageData')
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
Taro.canvasToTempFilePath({
|
||||||
|
canvas,
|
||||||
|
fileType: 'jpg',
|
||||||
|
quality: 0.7,
|
||||||
|
success: res => resolve(res.tempFilePath),
|
||||||
|
fail: reject
|
||||||
|
})
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
async function compressImage(files) {
|
||||||
|
const res: string[] = []
|
||||||
|
for (const file of files) {
|
||||||
|
console.log(file, file.width, file.height, 'file frfdfsfds')
|
||||||
|
const compressed_image = await convert_to_jpg_and_compress(file.path, { width: file.width, height: file.height })
|
||||||
|
res.push(compressed_image)
|
||||||
|
}
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
// 图片标准容器为 360 * 240 3:2
|
||||||
|
// 压缩后图片最大宽高
|
||||||
|
const IMAGE_MAX_SIZE = {
|
||||||
|
width: 1080,
|
||||||
|
height: 720,
|
||||||
|
}
|
||||||
|
|
||||||
|
// 标准长宽比,判断标准
|
||||||
|
const STANDARD_ASPECT_RATIO = IMAGE_MAX_SIZE.width / IMAGE_MAX_SIZE.height
|
||||||
|
|
||||||
|
// 根据图片标准重新设置图片尺寸
|
||||||
|
async function onChooseImageSuccess(tempFiles) {
|
||||||
|
console.log(tempFiles, 'tempFiles')
|
||||||
|
const result: { path: string, size: number, widht: number, height: number }[] = []
|
||||||
|
for (const tempFile of tempFiles) {
|
||||||
|
console.count('tempFile')
|
||||||
|
const { width, height } = await Taro.getImageInfo({ src: tempFile.path })
|
||||||
|
const image_aspect_ratio = width / height
|
||||||
|
let fileRes: { path: string, size: number } = { path: tempFile.path, size: tempFile.size }
|
||||||
|
// 如果图片长宽比小于标准长宽比,则依照图片高度以及图片最大高度来重新设置图片尺寸
|
||||||
|
if (image_aspect_ratio < STANDARD_ASPECT_RATIO) {
|
||||||
|
fileRes = {
|
||||||
|
...fileRes,
|
||||||
|
...(height > IMAGE_MAX_SIZE.height ? { width: Math.floor(IMAGE_MAX_SIZE.height * image_aspect_ratio), height: IMAGE_MAX_SIZE.height } : { width: Math.floor(height * image_aspect_ratio), height }),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fileRes = {
|
||||||
|
...fileRes,
|
||||||
|
...(width > IMAGE_MAX_SIZE.width ? { width: IMAGE_MAX_SIZE.width, height: Math.floor(IMAGE_MAX_SIZE.width / image_aspect_ratio) } : { width, height: Math.floor(width / image_aspect_ratio) }),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
result.push(fileRes)
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
export default function UploadFromWx(props: UploadFromWxProps) {
|
export default function UploadFromWx(props: UploadFromWxProps) {
|
||||||
const {
|
const {
|
||||||
onAdd = () => void 0,
|
onAdd = () => void 0,
|
||||||
@@ -22,21 +96,29 @@ export default function UploadFromWx(props: UploadFromWxProps) {
|
|||||||
sizeType: ['original', 'compressed'],
|
sizeType: ['original', 'compressed'],
|
||||||
sourceType: ['album', 'camera'],
|
sourceType: ['album', 'camera'],
|
||||||
}).then(async (res) => {
|
}).then(async (res) => {
|
||||||
// TODO: compress image
|
const analyzedFiles = await onChooseImageSuccess(res.tempFiles)
|
||||||
// TODO: cropping image to const size
|
// compress image
|
||||||
let count = 0
|
// cropping image to standard size
|
||||||
const files = res.tempFiles.map(item => ({
|
const compressedTempFiles = await compressImage(analyzedFiles)
|
||||||
filePath: item.path,
|
|
||||||
|
let start = Date.now()
|
||||||
|
const files = compressedTempFiles.map(path => ({
|
||||||
|
filePath: path,
|
||||||
description: '封面图',
|
description: '封面图',
|
||||||
tags: 'cover',
|
tags: 'cover',
|
||||||
is_public: 1 as unknown as 0 | 1,
|
is_public: 1 as unknown as 0 | 1,
|
||||||
id: (Date.now() + count++).toString(),
|
id: (start++).toString(),
|
||||||
}))
|
}))
|
||||||
const onFileUploaded = uploadApi.batchUpload(files)
|
const onFileUpdate = uploadApi.batchUpload(files).then(res => {
|
||||||
|
return res.map(item => ({
|
||||||
|
id: item.id,
|
||||||
|
url: item.data.file_url
|
||||||
|
}))
|
||||||
|
})
|
||||||
onAdd(files.map(item => ({
|
onAdd(files.map(item => ({
|
||||||
id: item.id,
|
id: item.id,
|
||||||
url: item.filePath,
|
url: item.filePath,
|
||||||
})), onFileUploaded) // TODO: add loading state
|
})), onFileUpdate)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return (
|
return (
|
||||||
|
|||||||
@@ -549,7 +549,7 @@ function Index() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const fetchDetail = async () => {
|
const fetchDetail = async () => {
|
||||||
const res = await DetailService.getDetail(243/* Number(id) */)
|
const res = await DetailService.getDetail(3387/* Number(id) */)
|
||||||
if (res.code === 0) {
|
if (res.code === 0) {
|
||||||
setDetail(res.data)
|
setDetail(res.data)
|
||||||
}
|
}
|
||||||
@@ -561,7 +561,7 @@ function Index() {
|
|||||||
|
|
||||||
const handleJoinGame = () => {
|
const handleJoinGame = () => {
|
||||||
Taro.navigateTo({
|
Taro.navigateTo({
|
||||||
url: `/pages/orderCheck/index?gameId=${243/* id */}`,
|
url: `/pages/orderCheck/index?gameId=${3387/* id */}`,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user