import { View, Image } from '@tarojs/components' import './choose_actions.scss' import { IconClose } from '@/components/basic/Icons' import { rpxToPx } from '@/utils/tools' import NewButton, { NewButtonType } from '@/_health/base/new_button' import Taro from '@tarojs/taro' import { BASE_IMG_URL, baseUrl } from '@/services/http/api' import { useTranslation } from 'react-i18next' import { jumpPage } from '@/features/trackTimeDuration/hooks/Common' import { MainColorType } from '@/context/themes/color' import { useEffect } from 'node_modules/@types/react' export default function ChooseActions(props: { close: any, quick: any, chooseText: any, chooseImg: any }) { const { t } = useTranslation() function camera() { addImage(true) } function album() { addImage(false) } function chat() { Taro.chooseMessageFile({ count: 9, type: 'image', success: async function (res) { const results = await Promise.all(res.tempFiles.map(getImageInfo)); chooseSuccess(results, true) }, fail(res) { }, }) } function text() { props.close() props.chooseText() // jumpPage(`/_record/pages/log_record?scenario=${props.scenario}&only_text=1`) } function checkin() { props.close() props.quick() // jumpPage(`/_record/pages/log_record?scenario=${props.scenario}&check_in=1`) } function addImage(isCamera) { var source: any = isCamera ? ['camera'] : ['album'] console.log(source) Taro.chooseMedia({ count: 9, sizeType: ['compressed'], mediaType: ['image'], sourceType: source, success: async function (res) { const results = await Promise.all(res.tempFiles.map(getImageInfo)); chooseSuccess(results, true) }, fail: function (res) { } }) } async function chooseSuccess(res, isAlbum) { console.log('选择图片', res) // const filePaths = res.map(file => file.path // // process.env.TARO_ENV === 'rn' || isFilePath ? file.path : file.tempFilePath // ) Taro.showLoading({ title: t('health.uploading') }) try { const uploadedUrls = await Promise.all(res.map(path => uploadFile2(path, isAlbum ? 'album' : 'camera'))) // setPics([...pics, ...uploadedUrls]) // jumpPage(`/_record/pages/log_record?imgs=${JSON.stringify(uploadedUrls)}&scenario=${props.scenario}`) props.chooseImg(uploadedUrls) props.close() Taro.hideLoading() } catch (error) { console.error('Error uploading files:', error) Taro.hideLoading() } } function uploadFile2(obj: any, source: string): Promise { return new Promise((resolve, reject) => { var path = obj.path const dot = path.lastIndexOf('.') const fileExt = dot > 0 ? path.substring(dot) : '' Taro.request({ method: 'GET', url: `${baseUrl}/api/thirdparty/aliyun/oss-form-upload`, header: { 'Authorization': 'bearer ' + global.token }, data: { type: 'FOOD_JOURNAL', file_ext: fileExt }, success: (rsp) => { if (rsp.statusCode !== 200) { reject(new Error(t('health.networkError'))) return } Taro.uploadFile({ url: rsp.data.upload_url, filePath: path, name: 'file', formData: rsp.data.fields, success: () => { var temp = JSON.parse(JSON.stringify(obj)) temp.url = rsp.data.view_url // resolve(rsp.data.view_url) resolve(temp) }, fail: (error) => { reject(error) } }) }, fail: reject }) }) } const getImageInfo = (src) => { const { tempFilePath, path } = src return new Promise((resolve) => { Taro.getImageInfo({ src: tempFilePath ? tempFilePath : path, success: (result) => resolve({ height: result.height, width: result.width, orientation: result.orientation, path: result.path, type: result.type }), fail: (error) => resolve({ height: 1024, width: 1024, orientation: 'up', path: tempFilePath, type: 'unknown' }), }); }); }; return Add Photos Add Text Camera Import from Chats OR Quick Check in }