Leon vor 2 Jahren
Ursprung
Commit
7b6e15a145

+ 13 - 10
src/features/food/FoodConsole.tsx

@@ -28,7 +28,7 @@ export default function Component(props: { addItem: Function }) {
         }
     }, [])
 
-    function choose(isAlbum=true) {
+    function choose(isAlbum = true) {
         if (!user.isLogin) {
             jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
             return;
@@ -37,7 +37,7 @@ export default function Component(props: { addItem: Function }) {
         Taro.chooseImage({
             count: 1,
             sizeType: ['compressed'],
-            sourceType: [isAlbum?'album':'camera'],
+            sourceType: [isAlbum ? 'album' : 'camera'],
             success: function (res) {
                 console.log(res)
                 var tempFilePaths = res.tempFilePaths
@@ -97,7 +97,7 @@ export default function Component(props: { addItem: Function }) {
 
     function uploadFile(path) {
         Taro.showLoading({
-            title:'加载中'
+            title: '加载中'
         })
         var dot = path.lastIndexOf('.')
         var fileExt = dot > 0 ? path.substring(dot) : ''
@@ -122,7 +122,7 @@ export default function Component(props: { addItem: Function }) {
                     success: rlt => {
                         console.log(rlt)
                         createData(rsp.data.view_url)
-                        
+
                         // uploadAvatar(rsp.data.view_url)
                         // _this.changeAvatar(rsp.data.view_url);
                     },
@@ -144,8 +144,11 @@ export default function Component(props: { addItem: Function }) {
 
         createFoodJournal({
             cover: url,
-            timestamp: time,
-            date: strDate
+            start: {
+                timestamp: time,
+                date: strDate
+            }
+
         }).then(res => {
             console.log(res)
             props.addItem(res)
@@ -166,15 +169,15 @@ export default function Component(props: { addItem: Function }) {
                     imgUrl && <Image style={{ width: '100%', height: '100%' }} src={imgUrl} mode="aspectFill" />
                 }
                 </View> */}
-                <View className='camera_bg' onClick={()=>choose(false)}>
-                    <Image src={require('@assets/images/camera2.png')} className='camera_icon'/>
+                <View className='camera_bg' onClick={() => choose(false)}>
+                    <Image src={require('@assets/images/camera2.png')} className='camera_icon' />
                     <Text className='camera_text'>拍摄食物</Text>
                     <Text className='album_text'>从相册选择</Text>
-                    <View className='album_bottom' onClick={(e)=>{choose(true);e.stopPropagation()}}/>
+                    <View className='album_bottom' onClick={(e) => { choose(true); e.stopPropagation() }} />
                 </View>
                 <Slider />
             </View>
-            <View className='demo1'/>
+            <View className='demo1' />
             {/* <Text style={{ color: '#fff',marginBottom:20 }} onClick={()=>choose(false)}>拍照</Text>
             <Text style={{ color: '#fff' }} onClick={uploadFile}>上传</Text> */}
 

+ 16 - 3
src/features/food/FoodJournal.tsx

@@ -6,13 +6,15 @@ import FoodTimeline from "./FoodTimeline"
 import Layout from "@/components/layout/layout"
 import { NaviBarTitleShowType, TemplateType } from "@/utils/types"
 import { useEffect, useState } from "react"
-import { getFoodJournals } from "@/services/foodJournal"
+import { getFoodJournals, getFoodTags } from "@/services/foodJournal"
 import { usePullDownRefresh, useReachBottom } from "@tarojs/taro"
 import Taro from "@tarojs/taro"
-import { useSelector } from "react-redux"
+import { useDispatch, useSelector } from "react-redux"
+import { setMealTags } from "@/store/common"
 
 export default function Component() {
     const { t } = useTranslation()
+    const dispatch = useDispatch();
     const user = useSelector((state: any) => state.user);
     const [pageIndex, setPageIndex] = useState(1)
     const [list, setList] = useState<any[]>([])
@@ -22,6 +24,7 @@ export default function Component() {
     const [loaded, setLoaded] = useState(false)
 
     useEffect(() => {
+        getTags()
         if (user.isLogin) {
             getList(1)
         }
@@ -38,6 +41,16 @@ export default function Component() {
         // getHistory()
     })
 
+    function getTags() {
+        getFoodTags({
+            type: 'meal_tag'
+        }).then(res => {
+            dispatch(setMealTags((res as any).data))
+        }).catch(e => {
+
+        })
+    }
+
     function more() {
         if (total <= list.length || isLoading) {
             return;
@@ -90,7 +103,7 @@ export default function Component() {
             {
                 user.isLogin && list && <FoodTimeline array={list} />
             }
-            <View style={{height:60}}/>
+            <View style={{ height: 60 }} />
         </View>
     }
 

+ 4 - 0
src/features/food/FoodTimelineItem.scss

@@ -27,4 +27,8 @@
     bottom: 0;
     height: 60px;
     backdrop-filter: blur(20px);
+}
+
+.food_item_date{
+    color: #fff;
 }

+ 59 - 10
src/features/food/FoodTimelineItem.tsx

@@ -7,9 +7,12 @@ import { useEffect, useState } from "react";
 import LimitPickers from "@/components/input/LimitPickers";
 import Modal from "@/components/layout/Modal";
 import { ColorType } from "@/context/themes/color";
+import { useSelector } from "react-redux";
 
 export default function Component(props: { data: any, delete: Function, preview: Function }) {
+    const common = useSelector((state: any) => state.common);
     const [detail, setDetail] = useState(props.data)
+    const [count, setCount] = useState(0)
     const [showModal, setShowModal] = useState(false)
     const [isStart, setIsStart] = useState(false)
     const { t } = useTranslation()
@@ -63,37 +66,83 @@ export default function Component(props: { data: any, delete: Function, preview:
     }
 
     function updateTag(index) {
-        var tags = ['早餐', '午餐', '晚餐', '宵夜']
-        var tag = tags[index]
+        var tag = common.meal_tags[index]
         editFoodJournal({
-            tags: [tag]
+            meal_tag: {
+                code: tag.code
+            }
         }, detail.id).then(res => {
             var obj = detail
-            obj.tags = [tag]
+            // obj.tags = [tag]
+            obj.meal_tag = {
+                code: tag.code,
+                label: tag.label
+            }
             setDetail(obj)
+            setCount(count + 1)
         }).catch(e => {
 
         })
     }
 
     function updateTime(e) {
-        console.log(e)
+        var date = new Date(e)
+        var year = date.getFullYear()
+        var month = date.getMonth() + 1
+        var day = date.getDate()
+        var obj: any;
+        obj = {
+            start: {
+                date: (year + '') + (month < 10 ? '0' + month : month) + (day < 10 ? '0' + day : day),
+                timestamp: e
+            }
+        }
+        if (!isStart) {
+            obj = {
+                end: {
+                    date: (year + '') + (month < 10 ? '0' + month : month) + (day < 10 ? '0' + day : day),
+                    timestamp: e
+                }
+            }
+        }
+
+        editFoodJournal({
+            ...obj
+        }, detail.id).then(res => {
+            setDetail(res)
+            setCount(count + 1)
+        }).catch(e => {
+
+        })
+    }
+
+    function getTime(t) {
+        var date = new Date(t)
+        var hour = date.getHours()
+        var minute = date.getMinutes()
+        return (hour < 10 ? '0' + hour : hour) + ':' + (minute < 10 ? '0' + minute : minute)
     }
 
     return <View className="food_timeline_item" onLongPress={showActionSheet}>
         {/* <View className="thumb" style={{backgroundColor:'pink'}}/> */}
         <View className="tags">
-            <Text onClick={() => updateTag(0)}>早餐</Text>
-            <Text onClick={() => updateTag(1)}>午餐</Text>
-            <Text onClick={() => updateTag(2)}>晚餐</Text>
-            <Text onClick={() => updateTag(3)}>宵夜</Text>
+            {
+                common.meal_tags.map((item, index) => {
+                    return <Text onClick={() => updateTag(index)}>{item.label}</Text>
+                })
+            }
         </View>
         <View className="thumb_bg">
             <Image className="thumb" src={detail.cover} mode="aspectFill" onClick={preview} />
             <View className="food_desc">
-                <Text>{detail.tags && detail.tags.length > 0 && detail.tags[0]}</Text>
+                <Text>{detail.meal_tag && detail.meal_tag.label}</Text>
+                <Text>{detail.start && getTime(detail.start.timestamp)} </Text>
+                {
+                    detail.end.timestamp && <Text>{'-' + getTime(detail.end.timestamp)}</Text>
+                }
             </View>
         </View>
+        <Text className="food_item_date">{detail.start.date}</Text>
         {
             showModal && <Modal dismiss={() => { setShowModal(false) }} confirm={() => { setShowModal(false) }}>
                 <View className='modal_content'>

+ 14 - 1
src/services/foodJournal.tsx

@@ -1,6 +1,19 @@
-import { API_FOOD } from './http/api';
+import { API_FOOD, API_FOOD_TAGS } from './http/api';
 import { request } from './http/request';
 
+export const getFoodTags = (params)=>{
+    return new Promise((resolve, reject) => {
+        request({
+            url: API_FOOD_TAGS, method: 'GET', data: {...params}
+        }).then(res => {
+            resolve(res);
+            console.log(res);
+            // dispatch(loginSuccess(res));
+        }).catch(e => {
+            reject(e)
+        })
+    })
+}
 export const getFoodJournals = (params) => {
     return new Promise((resolve, reject) => {
         request({

+ 1 - 0
src/services/http/api.js

@@ -44,6 +44,7 @@ export const API_METRIC_FOLLOWS = `${baseUrl}/api/metric/follows`
 
 //food
 export const API_FOOD = `${baseUrl}/api/food/journals`
+export const API_FOOD_TAGS = `${baseUrl}/api/system/tags`
 
 //permissions
 export const API_WX_PUB_FOLLOWED = `${baseUrl}/api/fast/user/wx-pub-followed`

+ 9 - 4
src/store/common.tsx

@@ -2,6 +2,7 @@ import { createSlice } from "@reduxjs/toolkit";
 
 interface CommonState {
     resources: [] | null;
+    meal_tags: [] | null;
     configs: any | null;
     duration: {
         min: number;
@@ -9,15 +10,16 @@ interface CommonState {
         step: number;
     } | null;
     showTabbar: boolean | true;
-    pageIndex:number|0;
+    pageIndex: number | 0;
 }
 
 const initialState: CommonState = {
     resources: [],
+    meal_tags: [],
     configs: null,
     duration: null,
     showTabbar: true,
-    pageIndex:0
+    pageIndex: 0
 }
 
 const commonSlice = createSlice({
@@ -28,6 +30,9 @@ const commonSlice = createSlice({
         setResources(state, action) {
             state.resources = action.payload;
         },
+        setMealTags(state, action) {
+            state.meal_tags = action.payload
+        },
         //是否disable并隐藏tabbar
         setTabbarStatus(state, action) {
             state.showTabbar = action.payload;
@@ -46,11 +51,11 @@ const commonSlice = createSlice({
 
             }
         },
-        changeTabbar(state,action){
+        changeTabbar(state, action) {
             state.pageIndex = action.payload;
         }
     }
 });
 
 export default commonSlice.reducer;
-export const { setResources, setConfigs,setTabbarStatus,changeTabbar} = commonSlice.actions;
+export const { setResources, setMealTags, setConfigs, setTabbarStatus, changeTabbar } = commonSlice.actions;