35 lines
1.6 KiB
TypeScript
35 lines
1.6 KiB
TypeScript
import Taro from '@tarojs/taro'
|
|
import { requestPlatformPrivacyAuthorization } from '../consent'
|
|
|
|
export function imageErrorMessage (error: unknown, fallback = '图片上传失败,请稍后重试') {
|
|
const reason = error as { message?: string, errMsg?: string } | null
|
|
const message = String(reason?.message || reason?.errMsg || '').trim()
|
|
if (/cancel/i.test(message)) return ''
|
|
if (/api scope is not declared in the privacy agreement/i.test(message)) return '小程序尚未配置照片隐私声明,请联系客服处理'
|
|
if (/url not in domain list|domain.*not.*configured/i.test(message)) return '上传域名未配置,请联系客服处理'
|
|
if (/ssl|certificate|https/i.test(message)) return '图片上传安全连接失败,请联系客服处理'
|
|
if (/timeout/i.test(message)) return '图片上传超时,请重试'
|
|
return message || fallback
|
|
}
|
|
|
|
export async function chooseA5Image (): Promise<string> {
|
|
try {
|
|
if (Taro.getEnv() === Taro.ENV_TYPE.WEAPP) {
|
|
if (!await requestPlatformPrivacyAuthorization()) return ''
|
|
const result = await Taro.chooseMedia({
|
|
count: 1,
|
|
mediaType: ['image'],
|
|
sizeType: ['compressed'],
|
|
sourceType: ['album', 'camera'],
|
|
})
|
|
return result.tempFiles?.[0]?.tempFilePath || ''
|
|
}
|
|
const result = await Taro.chooseImage({ count: 1, sizeType: ['compressed'], sourceType: ['album', 'camera'] })
|
|
return result.tempFilePaths?.[0] || ''
|
|
} catch (error) {
|
|
const message = imageErrorMessage(error, '选择图片失败,请重试')
|
|
if (!message) return ''
|
|
throw new Error(message)
|
|
}
|
|
}
|