|
|
@@ -1,21 +1,38 @@
|
|
|
-import { View, Image } from '@tarojs/components'
|
|
|
+import { View, Image, Text } from '@tarojs/components'
|
|
|
import './FoodConsole.scss'
|
|
|
import { rpxToPx } from '@/utils/tools'
|
|
|
import Taro from '@tarojs/taro'
|
|
|
import Slider from '@/components/input/Slider'
|
|
|
import { useEffect, useState } from 'react'
|
|
|
+import { baseUrl } from '@/services/http/api'
|
|
|
+import { createFoodJournal } from '@/services/foodJournal'
|
|
|
+import { useSelector } from 'react-redux'
|
|
|
+import { jumpPage } from '../trackTimeDuration/hooks/Common'
|
|
|
+let useNavigation;
|
|
|
+if (process.env.TARO_ENV == 'rn') {
|
|
|
+ useNavigation = require("@react-navigation/native").useNavigation
|
|
|
+}
|
|
|
|
|
|
-export default function Component() {
|
|
|
- const [imgUrl, setImgUrl] = useState(null)
|
|
|
+export default function Component(props: { addItem: Function }) {
|
|
|
+ const user = useSelector((state: any) => state.user);
|
|
|
+ const [imgUrl, setImgUrl] = useState('')
|
|
|
+ let navigation;
|
|
|
+ if (useNavigation) {
|
|
|
+ navigation = useNavigation()
|
|
|
+ }
|
|
|
|
|
|
- useEffect(()=>{
|
|
|
- var str = Taro.getStorageSync('pic')
|
|
|
- if (str && str.length>0){
|
|
|
+ useEffect(() => {
|
|
|
+ var str = Taro.getStorageSync('pic')
|
|
|
+ if (str && str.length > 0) {
|
|
|
setImgUrl(str)
|
|
|
}
|
|
|
- },[])
|
|
|
+ }, [])
|
|
|
|
|
|
function choose() {
|
|
|
+ if (!user.isLogin) {
|
|
|
+ jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
|
|
|
+ return;
|
|
|
+ }
|
|
|
clearFile()
|
|
|
Taro.chooseImage({
|
|
|
count: 1,
|
|
|
@@ -77,16 +94,74 @@ export default function Component() {
|
|
|
)
|
|
|
}
|
|
|
|
|
|
+ function uploadFile(e) {
|
|
|
+ var dot = imgUrl.lastIndexOf('.')
|
|
|
+ var fileExt = dot > 0 ? imgUrl.substring(dot) : ''
|
|
|
+ // console.log(avatarUrl)
|
|
|
+ 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) => {
|
|
|
+ console.log(rsp)
|
|
|
+ Taro.uploadFile({
|
|
|
+ url: rsp.data.upload_url,
|
|
|
+ filePath: imgUrl,
|
|
|
+ name: 'file',
|
|
|
+ formData: rsp.data.fields,
|
|
|
+ success: rlt => {
|
|
|
+ console.log(rlt)
|
|
|
+ createData(rsp.data.view_url)
|
|
|
+ // uploadAvatar(rsp.data.view_url)
|
|
|
+ // _this.changeAvatar(rsp.data.view_url);
|
|
|
+ },
|
|
|
+ fail: rlt => {
|
|
|
+ Taro.showModal({
|
|
|
+ content: rlt.errMsg
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
+ function createData(url) {
|
|
|
+ var date = new Date();
|
|
|
+ var time = date.getTime()
|
|
|
+ var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
|
|
|
+
|
|
|
+ createFoodJournal({
|
|
|
+ cover: url,
|
|
|
+ timestamp: time,
|
|
|
+ date: strDate
|
|
|
+ }).then(res => {
|
|
|
+ console.log(res)
|
|
|
+ props.addItem(res)
|
|
|
+ clearFile()
|
|
|
+ setImgUrl('')
|
|
|
+ Taro.removeStorageSync('pic')
|
|
|
+ }).catch(e => {
|
|
|
+
|
|
|
+ })
|
|
|
+ }
|
|
|
+
|
|
|
return <View >
|
|
|
- <View style={{ display: 'flex', width: rpxToPx(750), alignItems: 'center', justifyContent: 'center' }}>
|
|
|
+ <View style={{ display: 'flex', width: rpxToPx(750), alignItems: 'center', justifyContent: 'center', flexDirection: 'column' }}>
|
|
|
<View style={{ display: 'flex', flexDirection: 'row' }}>
|
|
|
<Slider />
|
|
|
<View className='box11' onClick={choose}>{
|
|
|
- imgUrl && <Image style={{width:'100%',height:'100%'}} src={imgUrl} mode="aspectFill"/>
|
|
|
+ imgUrl && <Image style={{ width: '100%', height: '100%' }} src={imgUrl} mode="aspectFill" />
|
|
|
}
|
|
|
</View>
|
|
|
<Slider />
|
|
|
</View>
|
|
|
+ <Text style={{ color: '#fff' }} onClick={uploadFile}>上传</Text>
|
|
|
|
|
|
</View>
|
|
|
</View>
|