FoodConsole.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  1. import { View, Image, Text, Switch } from '@tarojs/components'
  2. import './FoodConsole.scss'
  3. import { rpxToPx, vibrate } 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. import { clearFoodCache, getFoodCache, saveFoodCache } from './hooks/ExtraData'
  15. import { async } from 'q'
  16. import { checkAuthorized } from '@/utils/check_authorized'
  17. let useNavigation;
  18. if (process.env.TARO_ENV == 'rn') {
  19. useNavigation = require("@react-navigation/native").useNavigation
  20. }
  21. export default function Component(props: { addItem: Function, firstItem: any }) {
  22. const user = useSelector((state: any) => state.user);
  23. const [modeOn, setModeOn] = useState(false)
  24. const [firstData, setFirstData] = useState(props.firstItem)
  25. const [lastUnFinished, setLastUnFinished] = useState(false)
  26. const common = useSelector((state: any) => state.common);
  27. const { t } = useTranslation()
  28. const [imgUrl, setImgUrl] = useState('')
  29. let navigation;
  30. if (useNavigation) {
  31. navigation = useNavigation()
  32. }
  33. // useEffect(() => {
  34. // setFirstData(props.firstItem)
  35. // }, [props.firstItem, props.firstItem.feel.post_meal])
  36. useEffect(() => {
  37. if (props.firstItem) {
  38. setFirstData(props.firstItem)
  39. }
  40. }, [props.firstItem])
  41. useEffect(() => {
  42. if (firstData &&
  43. firstData.mindful_mode == 'AWARE' &&
  44. (firstData.status != 'PART_COMPLETED' && firstData.status != 'COMPLETED')
  45. ) {
  46. setLastUnFinished(true)
  47. setModeOn(true)
  48. }
  49. else {
  50. setLastUnFinished(false)
  51. var isOn = Taro.getStorageSync('food_switch')
  52. setModeOn(isOn)
  53. }
  54. // if (firstData &&
  55. // firstData.mindful_mode == 'AWARE' &&
  56. // firstData.status != 'PART_COMPLETED' &&
  57. // (!firstData.feel.post_meal || !firstData.feel.pre_meal)) {
  58. // setLastUnFinished(true)
  59. // setModeOn(true)
  60. // }
  61. // else {
  62. // setLastUnFinished(false)
  63. // var isOn = Taro.getStorageSync('food_switch')
  64. // setModeOn(isOn)
  65. // }
  66. }, [firstData])
  67. useEffect(() => {
  68. // var str = Taro.getStorageSync('pic')
  69. // if (str && str.length > 0) {
  70. // setImgUrl(str)
  71. // }
  72. }, [])
  73. async function choose(isAlbum = true) {
  74. if (!user.isLogin) {
  75. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  76. return;
  77. }
  78. var params = {
  79. event: 'add_a_picture',
  80. value: isAlbum ? 'choose_from_album' : 'use_camera',
  81. }
  82. saveFoodCache('create', params)
  83. if (process.env.TARO_ENV == 'rn') {
  84. Taro.chooseImage({
  85. count: 1,
  86. sizeType: ['compressed'],
  87. sourceType: [isAlbum ? 'album' : 'camera'],
  88. success: function (res) {
  89. chooseSuccess(res, isAlbum)
  90. checkAuthorized()
  91. },
  92. fail: function (res) {
  93. chooseFailed(res, isAlbum)
  94. checkAuthorized()
  95. }
  96. })
  97. }
  98. else {
  99. Taro.chooseMedia({
  100. count: 1,
  101. sizeType: ['compressed'],
  102. mediaType: ['image'],
  103. sourceType: [isAlbum ? 'album' : 'camera'],
  104. success: function (res) {
  105. chooseSuccess(res, isAlbum)
  106. checkAuthorized()
  107. },
  108. fail: function (res) {
  109. chooseFailed(res, isAlbum)
  110. checkAuthorized()
  111. }
  112. })
  113. }
  114. }
  115. async function chooseSuccess(res, isAlbum) {
  116. var params = {
  117. event: 'add_a_picture',
  118. value: isAlbum ? 'choose_from_album_confirm' : 'use_camera_confirm',
  119. }
  120. saveFoodCache('create', params)
  121. var savedFilePath = process.env.TARO_ENV=='rn'?res.tempFiles[0].path:res.tempFiles[0].tempFilePath
  122. // var savedFilePath = res.savedFilePath
  123. await Taro.setStorage({key:'pic', data:savedFilePath})
  124. setImgUrl(savedFilePath as any)
  125. uploadFile(savedFilePath, isAlbum ? 'album' : 'camera')
  126. }
  127. function chooseFailed(res, isAlbum) {
  128. var params = {
  129. event: 'add_a_picture',
  130. value: isAlbum ? 'choose_from_album_cancel' : 'use_camera_cancel',
  131. }
  132. saveFoodCache('create', params)
  133. }
  134. function uploadFile(path, source) {
  135. Taro.showLoading({
  136. title: '加载中'
  137. })
  138. var dot = path.lastIndexOf('.')
  139. var fileExt = dot > 0 ? path.substring(dot) : ''
  140. Taro.request({
  141. method: 'GET',
  142. url: `${baseUrl}/api/thirdparty/aliyun/oss-form-upload`,
  143. header: {
  144. 'Authorization': 'bearer ' + global.token
  145. },
  146. data: {
  147. type: 'FOOD_JOURNAL',
  148. file_ext: fileExt
  149. },
  150. success: (rsp) => {
  151. if (rsp.statusCode != 200) {
  152. Taro.showToast({
  153. title: '操作失败,请检查网络',
  154. icon: 'none'
  155. })
  156. return
  157. }
  158. Taro.uploadFile({
  159. url: rsp.data.upload_url,
  160. filePath: path,
  161. name: 'file',
  162. formData: rsp.data.fields,
  163. success: rlt => {
  164. createData(rsp.data.view_url, source)
  165. // uploadAvatar(rsp.data.view_url)
  166. // _this.changeAvatar(rsp.data.view_url);
  167. },
  168. fail: rlt => {
  169. Taro.hideLoading()
  170. Taro.showModal({
  171. content: rlt.errMsg
  172. })
  173. }
  174. })
  175. }
  176. })
  177. }
  178. async function createData(url, source) {
  179. var date = new Date();
  180. var time = date.getTime()
  181. var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
  182. var event = await getFoodCache('create')
  183. debugger
  184. createFoodJournal({
  185. media: [{
  186. url,
  187. type: url.indexOf('mp4') != -1 ? 'video' : 'image',
  188. source: source
  189. }],
  190. start: {
  191. timestamp: time,
  192. date: strDate
  193. },
  194. mindful_mode: modeOn ? 'AWARE' : 'NORMAL',
  195. extra: {
  196. event: event
  197. }
  198. }).then(res => {
  199. props.addItem(res)
  200. setImgUrl('')
  201. Taro.removeStorage({key:'pic'})
  202. Taro.hideLoading()
  203. setFirstData(res)
  204. clearFoodCache('create')
  205. }).catch(e => {
  206. Taro.hideLoading()
  207. })
  208. }
  209. async function modeChange(e) {
  210. setModeOn(e.detail.value)
  211. await Taro.setStorage({key:'food_switch', data:e.detail.value})
  212. var params = {
  213. event: 'switch_toggle',
  214. value: e.detail.value ? 'on' : 'off',
  215. }
  216. saveFoodCache('create', params)
  217. }
  218. function more() {
  219. const resource = common.resources.filter((item: any) => {
  220. return item.code == 'about_mindful_eating'
  221. })
  222. jumpPage('/pages/common/H5?title=' + '' + '&url=' + resource[0].url)
  223. }
  224. function getLineBottom() {
  225. if (user.isLogin) {
  226. return props.firstItem ? -rpxToPx(60) : 0
  227. }
  228. else {
  229. return props.firstItem ? 0 : rpxToPx(60)
  230. }
  231. }
  232. function clickSwitch() {
  233. if (lastUnFinished) {
  234. vibrate('heavy')
  235. Taro.showModal({
  236. title: t('feature.common.prompt'),
  237. content: t('feature.food.disable_switch_modal_title'),
  238. confirmText: t('feature.food.disable_switch_modal_btn'),
  239. showCancel: false
  240. })
  241. }
  242. }
  243. return <View style={{ marginBottom: rpxToPx(60) }}>
  244. <View className='food_console_box'>
  245. <View style={{ display: 'flex', flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center' }}>
  246. <Text className='food_console_title'>感知模式</Text>
  247. <Switch className='myswitch'
  248. onClick={clickSwitch}
  249. disabled={lastUnFinished} checked={modeOn}
  250. color={props.firstItem && props.firstItem.status == 'WAIT_FOR_POSTMEAL' ? ColorType.food : ColorType.fast}
  251. style={{ marginRight: rpxToPx(40), opacity: lastUnFinished ? 0.4 : 1 }}
  252. onChange={modeChange} />
  253. </View>
  254. {
  255. modeOn && <View className='food_console_desc'>
  256. <View className='food_console_desc_point'
  257. style={{
  258. backgroundColor: props.firstItem && props.firstItem.status == 'WAIT_FOR_POSTMEAL' ? ColorType.food : ColorType.fast,
  259. marginTop: props.firstItem && props.firstItem.status == 'WAIT_FOR_POSTMEAL' ? rpxToPx(46 + 18) : rpxToPx(18)
  260. }} />
  261. <Text style={{ color: '#ffffff66' }}>{t('feature.food.sence_desc')}</Text>
  262. </View>
  263. }
  264. {
  265. !modeOn && <View className='food_console_desc_off'>
  266. <Text style={{ color: '#ffffff66' }}>{t('feature.food.sence_desc_off')}</Text>
  267. </View>
  268. }
  269. </View>
  270. {
  271. !lastUnFinished && <View style={{ display: 'flex', position: 'relative', width: rpxToPx(750), alignItems: 'center', justifyContent: 'center', flexDirection: 'column' }}>
  272. <View style={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
  273. <View className='camera_bg' onClick={() => choose(false)}>
  274. <Image src={require('@assets/images/camera2.png')} className='camera_icon' />
  275. <Text className='camera_text'>{t('feature.food.camera')}</Text>
  276. <Text className='album_text'>{t('feature.food.album')}</Text>
  277. <View className='album_bottom' onClick={(e) => {
  278. choose(true);
  279. if (process.env.TARO_ENV == 'weapp') {
  280. e.stopPropagation()
  281. }
  282. }} />
  283. </View>
  284. {
  285. modeOn && <View style={{ height: rpxToPx(60) }} />
  286. }
  287. {modeOn && <Slider edit={true} />}
  288. </View>
  289. <View className='center_line2' style={{ bottom: getLineBottom() }} />
  290. </View>
  291. }
  292. </View>
  293. }