import { Textarea, View, Image, Input, ScrollView } from "@tarojs/components"; import './log_publish.scss' import { rpxToPx } from "@/utils/tools"; import { useEffect, useState } from "react"; import { useSelector } from "react-redux"; import Taro from "@tarojs/taro"; import { IconArrow, IconClose } from "@/components/basic/Icons"; import NewButton, { NewButtonType } from "@/_health/base/new_button"; import showAlert from "@/components/basic/Alert"; import { IconCamera, IconClock } from "@/_record/components/record_icon"; import { useTranslation } from "react-i18next"; import { MainColorType } from "@/context/themes/color"; import dayjs from "dayjs"; import { addEvents } from "@/services/health"; import { TimeFormatter } from "@/utils/time_format"; import { BASE_IMG_URL, baseUrl } from "@/services/http/api"; import LogTags from "./log_tags"; import PickerCard from "@/_record/components/picker_card"; import showActionSheet from "@/components/basic/ActionSheet"; let useRoute; let useNavigation; let LinearGradient; if (process.env.TARO_ENV == 'rn') { useRoute = require("@react-navigation/native").useRoute useNavigation = require("@react-navigation/native").useNavigation LinearGradient = require('react-native-linear-gradient').default } export default function LogPublish(props: { onClose: any, scenario: string, tag?: any, tags: any, addTag: boolean }) { const scale = '?x-oss-process=image/format,jpg/resize,w_400' const systemInfo: any = Taro.getWindowInfo ? Taro.getWindowInfo() : Taro.getSystemInfoSync(); const navigationBarHeight = systemInfo.statusBarHeight + 44; const screenHeight = systemInfo.screenHeight const [showChoose, setShowChoose] = useState(false) const record = useSelector((state: any) => state.record); const [pics, setPics] = useState([]) const [desc, setDesc] = useState('') const [focus, setFocus] = useState(false) const [time, setTime] = useState(dayjs().format('HH:mm')) const [selDate, setSelDate] = useState(dayjs().format('YYYY-MM-DD')) const [enterTimestmap] = useState(new Date().getTime()) const [scenario, setScenarios] = useState(props.scenario) const [posting, setPosting] = useState(false) const [title, setTitle] = useState(props.tag ? props.tag.title : '') const [step, setStep] = useState(props.addTag ? 1 : 2) const [inputFocus, setInputFocus] = useState(false) const [showTimePicker, setShowTimePicker] = useState(false) const [addEnter, setAddEnter] = useState(props.addTag ?? false) const { t } = useTranslation() useEffect(() => { if (step == 1) { setTimeout(() => { setInputFocus(true) }, 300) } else { setAddEnter(false) } }, [step]) function addImage(isCamera) { var source: any = isCamera ? ['camera'] : ['album'] Taro.chooseImage({ count: 9 - pics.length, sizeType: ['compressed'], // mediaType: ['image'], sourceType: source, success: async function (res) { const results = await Promise.all(res.tempFiles.map(getImageInfo)); chooseSuccess(results, true) // checkAuthorized() }, fail: function (res) { } }) } async function chooseSuccess(res, isAlbum) { // 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]) 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', timeout: 30000, 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({ timeout: 30000, 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' }), }); }); }; function tapPic() { var list = process.env.TARO_ENV == 'weapp' ? [t('health.add_photos'), t('health.camera2'), t('health.import_chat')] : [t('health.add_photos'), t('health.camera2')] showActionSheet({ title: '', // showActionSheetWithOptions: showActionSheetWithOptions, itemList: list, success: function (res) { switch (res) { case 0: addImage(false) break; case 1: addImage(true) break; case 2: Taro.chooseMessageFile({ count: 9 - pics.length, type: 'image', success: async function (res) { const results = await Promise.all(res.tempFiles.map(getImageInfo)); chooseSuccess(results, true) }, fail(res) { }, }) break; case 3: // setImgUrl('') break; } } }) } function save() { var date = TimeFormatter.stringToDate(selDate, time) date.setMilliseconds(new Date(enterTimestmap).getMilliseconds()) var params: any = { scenario: scenario, //ACTIVITY type: 'POINT', sub_type: scenario, title: title, time: { start_timestamp: date.getTime() } } // if (router.params.join_id) { // params.join_key = router.params.join_id // } var moment: any = { description: desc, } if (pics.length > 0) { var picList: any = [] pics.map((item) => { picList.push({ url: item.url, type: item.url.indexOf('mp4') != -1 ? 'video' : 'image', source: 'album', width: item.width, height: item.height, format: item.format }) }) moment.media = picList } params.moment = moment // if (event_id && event_id != 'undefined') { // params.event_id = event_id // } // if (is_temp) { // params.event = window == 'EAT' ? 'EAT_CUSTOM' : 'ACTIVE_CUSTOM' // } // params.op_page = window == 'EAT' ? 'HOME_EAT' : 'HOME_ACTIVE' params.extra = { set_time: global.set_time ? global.set_time : new Date().getTime(), confirm_time: new Date().getTime() } if (posting) return setPosting(true) // Taro.showLoading({ // title: t('health.uploading') // }) addEvents(params).then(res => { // setShowResult(true) // setResult(res) setPosting(false) // Taro.hideLoading() Taro.reLaunch({ url: '/pages/moment/moment' }) props.onClose() }).catch(e => { setPosting(false) // Taro.hideLoading() }) } function getDate() { var sel = dayjs(selDate) var now = dayjs().format('YYYY-MM-DD') const yesterday = dayjs().subtract(1, 'day'); if (sel.format('YYYY-MM-DD') == now) { return '' } if (yesterday.format('YYYY-MM-DD') == sel.format('YYYY-MM-DD')) { return global.language == 'en' ? 'Yesterday ' : '昨天 ' } else { return global.language == 'en' ? sel.format('MMM D ') : sel.format('MMMD日 ') } } function getBackground() { var time = record.time if (!time) return '#fff' const { background_colors } = time if (!background_colors) { return '#fff' } else if (background_colors.length == 1) { return background_colors[0] } return `linear-gradient(to bottom, ${background_colors[0]}, ${background_colors[1]})` } function bgView() { if (process.env.TARO_ENV == 'weapp') { return } var time = record.time if (!time) return const { background_colors } = time if (!background_colors) { return } else if (background_colors.length == 1) { return } return } function publishCard() { return { // setStep(0) // setFocus(false) setStep(0) }}> {/* {selPostCount} */} {title} {/* */}