Leon 2 vuotta sitten
vanhempi
commit
cb3de1b26e

+ 8 - 7
src/components/input/SlidngScale.tsx

@@ -103,15 +103,16 @@ export default function Component(props: {
     }, [isEnd, isDraging])
 
     function scrollContent(e) {
-        lastValue = e.detail.scrollLeft;
-        if (timerA)
-            clearTimeout(timerA);
+        update(e)
+        // lastValue = e.detail.scrollLeft;
+        // if (timerA)
+        //     clearTimeout(timerA);
 
-        timerA = setTimeout(() => {
+        // timerA = setTimeout(() => {
 
-            update(e)
-            setIsDraging(false)
-        }, 250) as any
+        //     update(e)
+        //     setIsDraging(false)
+        // }, 250) as any
     }
 
     const handleScroll = throttle((e) => {

+ 84 - 9
src/features/food/FoodConsole.tsx

@@ -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>

+ 0 - 0
src/features/food/FoodJournal.scss


+ 106 - 0
src/features/food/FoodJournal.tsx

@@ -0,0 +1,106 @@
+import { useTranslation } from "react-i18next"
+import TitleView from "../trackTimeDuration/components/TitleView"
+import { View } from "@tarojs/components"
+import FoodConsole from "./FoodConsole"
+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 { usePullDownRefresh, useReachBottom } from "@tarojs/taro"
+import Taro from "@tarojs/taro"
+import { useSelector } from "react-redux"
+
+export default function Component() {
+    const { t } = useTranslation()
+    const user = useSelector((state: any) => state.user);
+    const [pageIndex, setPageIndex] = useState(1)
+    const [list, setList] = useState<any[]>([])
+    const [count, setCount] = useState(0)
+    const [total, setTotal] = useState(0)
+    const [isLoading, setIsLoading] = useState(false)
+    const [loaded, setLoaded] = useState(false)
+
+    useEffect(() => {
+        if (user.isLogin) {
+            getList(1)
+        }
+    }, [user.isLogin])
+
+    usePullDownRefresh(() => {
+        getList(1)
+    })
+
+    useReachBottom(() => {
+        console.log('bottom')
+        more()
+        // setPageIndex(pageIndex+1)
+        // getHistory()
+    })
+
+    function more() {
+        if (total <= list.length || isLoading) {
+            return;
+        }
+        setIsLoading(true)
+        var page = pageIndex + 1
+        setPageIndex(page)
+    }
+
+
+    function getList(index) {
+        getFoodJournals({
+            page: index,
+            limit: 10
+        }).then(res => {
+            Taro.stopPullDownRefresh()
+
+            setLoaded(true)
+            setTotal((res as any).total)
+            if (index == 1) {
+                setList((res as any).data)
+            } else {
+                setList(list.concat((res as any).data))
+            }
+            setIsLoading(false)
+        }).catch(e => {
+            Taro.stopPullDownRefresh()
+        })
+    }
+
+    function addItem(item) {
+        setCount(count + 1)
+        var temps: any = list
+        if (!temps) {
+            temps = []
+        }
+        temps.unshift(item)
+        setList(temps)
+    }
+
+    function headerView() {
+        return <TitleView title={t('page.food.title')} subTitle="hello world" showAddBtn={false}>
+        </TitleView>
+    }
+    function detail() {
+        return <View>
+            {
+                <FoodConsole addItem={addItem} />
+            }
+            {
+                user.isLogin && list && <FoodTimeline array={list} />
+            }
+        </View>
+    }
+
+    return <View>
+        <Layout children={detail()}
+            title={t('page.food.title')}
+            type={TemplateType.customHeader}
+            header={headerView()}
+            refresh={() => { }}
+            triggered={false}
+            titleShowStyle={NaviBarTitleShowType.scrollToShow}
+        />
+    </View>
+}

+ 25 - 3
src/features/food/FoodTimeline.tsx

@@ -1,8 +1,30 @@
 import { View } from '@tarojs/components'
 import './FoodTimeline.scss'
+import FoodTimelineItem from './FoodTimelineItem'
+import { useEffect, useState } from 'react'
+import { delFoodJournal } from '@/services/foodJournal'
 
-export default function Component(){
-    return <View>
-        
+export default function Component(props: { array: any }) {
+    const [list, setList] = useState(props.array)
+
+    useEffect(() => {
+        console.log('hhhh')
+        setList(props.array)
+    }, [props.array.length])
+
+    function del(index) {
+        delFoodJournal(list[index].id).then(res => {
+            var temps = list.splice(index, 1)
+            setList(temps)
+        }).catch(e => {
+
+        })
+    }
+    return <View style={{ flexDirection: 'column', display: 'flex' }}>
+        {
+            list.map((item, index) => {
+                return <FoodTimelineItem data={item} key={index} delete={() => del(index)} />
+            })
+        }
     </View>
 }

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

@@ -0,0 +1,7 @@
+.food_timeline_item{
+    margin-bottom: 40px;
+}
+.thumb{
+    width: 200px;
+    height: 200px;
+}

+ 47 - 0
src/features/food/FoodTimelineItem.tsx

@@ -0,0 +1,47 @@
+import { View,Image } from "@tarojs/components";
+import './FoodTimelineItem.scss'
+import Taro from "@tarojs/taro";
+import { useTranslation } from "react-i18next";
+
+export default function Component(props:{data:any,delete:Function}){
+    const {t} = useTranslation()
+
+    function preview(){
+        Taro.previewImage({
+            current:props.data.cover,
+            urls:[props.data.cover]
+        })
+    }
+
+    function showActionSheet(){
+        Taro.showActionSheet({
+            itemList: [t('feature.common.action_sheet.delete')]
+        })
+            .then(res => {
+                switch (res.tapIndex) {
+                    case 0:
+                        Taro.showModal({
+                            title: t('feature.common.modal.delete_item_title'),
+                            content: t('feature.common.modal.delete_item_content'),
+                            success: function (res) {
+                                if (res.confirm) {
+                                    props.delete();
+                                }
+                            }
+                        })
+                        break;
+                    case 1:
+
+                        break;
+                }
+            })
+            .catch(err => {
+                console.log(err.errMsg)
+            })
+    }
+
+    return <View  className="food_timeline_item" onLongPress={showActionSheet}>
+        {/* <View className="thumb" style={{backgroundColor:'pink'}}/> */}
+        <Image className="thumb" src={props.data.cover} mode="aspectFill" onClick={preview}/>
+    </View>
+}

+ 13 - 0
src/pages/food/Food.scss

@@ -0,0 +1,13 @@
+.food_bg{
+    width: 100vw;
+    height: 100vh;
+    overflow: hidden;
+    margin: auto;
+}
+
+.food_detail{
+    height: 100%;
+    width: calc(100vw + 6px);
+    overflow-y: scroll;
+    overflow-x: hidden;
+}

+ 11 - 29
src/pages/food/Food.tsx

@@ -1,38 +1,20 @@
 import { View } from "@tarojs/components";
 import Tabbar from "@/components/navigation/TabBar";
 import './Food.scss'
-import Layout from "@/components/layout/layout";
-import { useTranslation } from "react-i18next";
-import { NaviBarTitleShowType, TemplateType } from "@/utils/types";
-import TitleView from "@/features/trackTimeDuration/components/TitleView";
-import FoodConsole from '@/features/food/FoodConsole';
-import FoodTimeline from "@/features/food/FoodTimeline";
+import FoodJournal from "@/features/food/FoodJournal";
 
 export default function Page() {
-    const { t } = useTranslation()
-    function headerView() {
-        return <TitleView title={t('page.food.title')} subTitle="hello world" showAddBtn={false}>
-        </TitleView>
-    }
-    function detail() {
-        return <View>
-            {
-                <FoodConsole />
-            }
-            {
-                <FoodTimeline />
-            }
-        </View>
-    }
+
     return <View>
-        <Layout children={detail()}
-            title={t('page.food.title')}
-            type={TemplateType.customHeader}
-            header={headerView()}
-            refresh={() => { }}
-            triggered={false}
-            titleShowStyle={NaviBarTitleShowType.scrollToShow}
-        />
+        <FoodJournal />
         <Tabbar index={4} />
     </View>
+
+    // return <View className="food_bg">
+    //     <View className="food_detail">
+    //         <FoodJournal />
+    //     </View>
+
+    //     <Tabbar index={4} />
+    // </View>
 }

+ 58 - 0
src/services/foodJournal.tsx

@@ -0,0 +1,58 @@
+import { API_FOOD } from './http/api';
+import { request } from './http/request';
+
+export const getFoodJournals = (params) => {
+    return new Promise((resolve, reject) => {
+        request({
+            url: API_FOOD, method: 'GET', data: { ...params }
+        }).then(res => {
+            resolve(res);
+            console.log(res);
+            // dispatch(loginSuccess(res));
+        }).catch(e => {
+            reject(e)
+        })
+    })
+}
+
+export const createFoodJournal = (params) => {
+    return new Promise((resolve, reject) => {
+        request({
+            url: API_FOOD, method: 'POST', data: { ...params }
+        }).then(res => {
+            resolve(res);
+            console.log(res);
+            // dispatch(loginSuccess(res));
+        }).catch(e => {
+            reject(e)
+        })
+    })
+}
+
+export const editFoodJournal = (params) => {
+    return new Promise((resolve, reject) => {
+        request({
+            url: API_FOOD, method: 'POST', data: { ...params }
+        }).then(res => {
+            resolve(res);
+            console.log(res);
+            // dispatch(loginSuccess(res));
+        }).catch(e => {
+            reject(e)
+        })
+    })
+}
+
+export const delFoodJournal = (id) => {
+    return new Promise((resolve, reject) => {
+        request({
+            url: API_FOOD + `/${id}`, method: 'DELETE', data: {}
+        }).then(res => {
+            resolve(res);
+            console.log(res);
+            // dispatch(loginSuccess(res));
+        }).catch(e => {
+            reject(e)
+        })
+    })
+}

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

@@ -42,7 +42,8 @@ export const API_METRIC_RECORDS = `${baseUrl}/api/metric/records`
 export const API_METRIC_GROUPS = `${baseUrl}/api/metric/groups`
 export const API_METRIC_FOLLOWS = `${baseUrl}/api/metric/follows`
 
-//journal
+//food
+export const API_FOOD = `${baseUrl}/api/food/journals`
 
 //permissions
 export const API_WX_PUB_FOLLOWED = `${baseUrl}/api/fast/user/wx-pub-followed`

+ 0 - 0
src/store/food_journal.tsx