FoodConsole.tsx 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import { View, Image, Text, Switch } from '@tarojs/components'
  2. import './FoodConsole.scss'
  3. import { rpxToPx } from '@/utils/tools'
  4. import Taro from '@tarojs/taro'
  5. import Slider from '@/components/input/Slider'
  6. import { useEffect, useState } from 'react'
  7. import { baseUrl } from '@/services/http/api'
  8. import { createFoodJournal } from '@/services/foodJournal'
  9. import { useSelector } from 'react-redux'
  10. import { jumpPage } from '../trackTimeDuration/hooks/Common'
  11. import { IconShare } from '@/components/basic/Icons'
  12. import { useTranslation } from 'react-i18next'
  13. import { ColorType } from '@/context/themes/color'
  14. let useNavigation;
  15. if (process.env.TARO_ENV == 'rn') {
  16. useNavigation = require("@react-navigation/native").useNavigation
  17. }
  18. export default function Component(props: { addItem: Function, firstItem: any }) {
  19. const user = useSelector((state: any) => state.user);
  20. const [modeOn, setModeOn] = useState(false)
  21. const [firstData, setFirstData] = useState(props.firstItem)
  22. const [lastUnFinished, setLastUnFinished] = useState(false)
  23. const { t } = useTranslation()
  24. const [imgUrl, setImgUrl] = useState('')
  25. let navigation;
  26. if (useNavigation) {
  27. navigation = useNavigation()
  28. }
  29. useEffect(() => {
  30. setFirstData(props.firstItem)
  31. }, [props.firstItem, props.firstItem.feel.post_meal])
  32. useEffect(() => {
  33. if (firstData &&
  34. firstData.mindful_mode == 'AWARE' &&
  35. firstData.status != 'ABANDONED' &&
  36. (!firstData.feel.post_meal || !firstData.feel.pre_meal)) {
  37. setLastUnFinished(true)
  38. setModeOn(true)
  39. }
  40. else {
  41. setLastUnFinished(false)
  42. var isOn = Taro.getStorageSync('food_switch')
  43. setModeOn(isOn)
  44. // Taro.setStorageSync('food_switch',e.detail.value)
  45. }
  46. }, [firstData])
  47. useEffect(() => {
  48. // var str = Taro.getStorageSync('pic')
  49. // if (str && str.length > 0) {
  50. // setImgUrl(str)
  51. // }
  52. }, [])
  53. function choose(isAlbum = true) {
  54. if (!user.isLogin) {
  55. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  56. return;
  57. }
  58. clearFile()
  59. Taro.chooseMedia({
  60. count: 1,
  61. sizeType: ['compressed'],
  62. mediaType: ['image'],
  63. sourceType: [isAlbum ? 'album' : 'camera'],
  64. success: function (res) {
  65. var tempFilePath = res.tempFiles[0].tempFilePath
  66. // Taro.editImage({
  67. // src:tempFilePath,
  68. // success:function(res){
  69. // console.log(res)
  70. // }
  71. // })
  72. Taro.getFileSystemManager().saveFile({
  73. tempFilePath: tempFilePath,
  74. success: function (res) {
  75. var savedFilePath = res.savedFilePath
  76. Taro.setStorageSync('pic', savedFilePath)
  77. console.log(savedFilePath)
  78. setImgUrl(savedFilePath as any)
  79. uploadFile(savedFilePath, isAlbum ? 'album' : 'camera')
  80. }
  81. })
  82. }
  83. })
  84. }
  85. function clearFile() {
  86. Taro.getFileSystemManager().getSavedFileList({
  87. success: function (res) {
  88. if (res.fileList.length > 0) {
  89. Taro.removeSavedFile({
  90. filePath: res.fileList[0].filePath,
  91. complete: function (res) {
  92. console.log(res)
  93. console.log('remove success')
  94. }
  95. })
  96. }
  97. }
  98. })
  99. }
  100. function getCache(key: string) {
  101. var value = Taro.getStorageSync(key)
  102. if (value) {
  103. return JSON.parse(value)
  104. }
  105. return null;
  106. }
  107. function saveCache(key: string) {
  108. var strList = Taro.getStorageSync('food_operate')
  109. var list: any = []
  110. if (strList) {
  111. list = JSON.parse(strList)
  112. }
  113. list.add({
  114. key,
  115. timestamp: new Date().getTime()
  116. })
  117. Taro.setStorageSync('food_operate', JSON.stringify(list)
  118. )
  119. }
  120. function uploadFile(path, source) {
  121. Taro.showLoading({
  122. title: '加载中'
  123. })
  124. var dot = path.lastIndexOf('.')
  125. var fileExt = dot > 0 ? path.substring(dot) : ''
  126. // console.log(avatarUrl)
  127. Taro.request({
  128. method: 'GET',
  129. url: `${baseUrl}/api/thirdparty/aliyun/oss-form-upload`,
  130. header: {
  131. 'Authorization': 'bearer ' + global.token
  132. },
  133. data: {
  134. type: 'FOOD_JOURNAL',
  135. file_ext: fileExt
  136. },
  137. success: (rsp) => {
  138. if (rsp.statusCode != 200) {
  139. Taro.showToast({
  140. title: '操作失败,请检查网络',
  141. icon: 'none'
  142. })
  143. return
  144. }
  145. Taro.uploadFile({
  146. url: rsp.data.upload_url,
  147. filePath: path,
  148. name: 'file',
  149. formData: rsp.data.fields,
  150. success: rlt => {
  151. createData(rsp.data.view_url, source)
  152. // uploadAvatar(rsp.data.view_url)
  153. // _this.changeAvatar(rsp.data.view_url);
  154. },
  155. fail: rlt => {
  156. Taro.hideLoading()
  157. Taro.showModal({
  158. content: rlt.errMsg
  159. })
  160. }
  161. })
  162. }
  163. })
  164. }
  165. function createData(url, source) {
  166. var date = new Date();
  167. var time = date.getTime()
  168. var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
  169. createFoodJournal({
  170. media: [{
  171. url,
  172. type: url.indexOf('mp4') != -1 ? 'video' : 'image',
  173. source: source
  174. }],
  175. start: {
  176. timestamp: time,
  177. date: strDate
  178. },
  179. mindful_mode: modeOn ? 'AWARE' : 'NORMAL'
  180. }).then(res => {
  181. props.addItem(res)
  182. clearFile()
  183. setImgUrl('')
  184. Taro.removeStorageSync('pic')
  185. Taro.hideLoading()
  186. setFirstData(res)
  187. // if (modeOn) {
  188. // setSwitchDisable(true)
  189. // }
  190. }).catch(e => {
  191. Taro.hideLoading()
  192. })
  193. }
  194. function modeChange(e) {
  195. setModeOn(e.detail.value)
  196. Taro.setStorageSync('food_switch', e.detail.value)
  197. }
  198. return <View style={{ marginBottom: rpxToPx(60) }}>
  199. <View className='food_console_box'>
  200. <View style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
  201. <Text className='food_console_title'>感知模式</Text>
  202. <Switch disabled={lastUnFinished} checked={modeOn} color={ColorType.food} style={{ marginRight: rpxToPx(40), opacity: lastUnFinished ? 0.4 : 1 }} onChange={modeChange} />
  203. </View>
  204. <View className='food_console_desc'>
  205. <View className='food_console_desc_point' style={{ backgroundColor: ColorType.food }} />
  206. <Text>{t('feature.food.sence_desc')}</Text>
  207. </View>
  208. </View>
  209. {
  210. !lastUnFinished && <View style={{ display: 'flex', width: rpxToPx(750), alignItems: 'center', justifyContent: 'center', flexDirection: 'column' }}>
  211. <View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
  212. <View className='camera_bg' onClick={() => choose(false)}>
  213. <Image src={require('@assets/images/camera2.png')} className='camera_icon' />
  214. <Text className='camera_text'>{t('feature.food.camera')}</Text>
  215. <Text className='album_text'>{t('feature.food.album')}</Text>
  216. <View className='album_bottom' onClick={(e) => { choose(true); e.stopPropagation() }} />
  217. </View>
  218. {
  219. modeOn && <View style={{ height: rpxToPx(60) }} />
  220. }
  221. {modeOn && <Slider edit={true}/>}
  222. </View>
  223. </View>
  224. }
  225. </View>
  226. }