MainConsole.tsx 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988
  1. import { WindowStatusType, WindowType } from "@/utils/types";
  2. import { View, Text, Image, Swiper, SwiperItem } from "@tarojs/components";
  3. import dayjs from "dayjs";
  4. import { useEffect, useRef, useState } from "react";
  5. import { useDispatch, useSelector } from "react-redux";
  6. import './MainConsole.scss'
  7. import { jumpPage } from "../trackTimeDuration/hooks/Common";
  8. import Modal from "@/components/layout/Modal.weapp";
  9. import { MainColorType } from "@/context/themes/color";
  10. import ConsolePicker from "../trackTimeDuration/components/ConsolePicker";
  11. import { clockTimes, delRecord, makeDone, updateEventDuration, updateFast, updateSchedule, updateTarget } from "@/services/health";
  12. import TimePicker from "../common/TimePicker";
  13. import showActionSheet from "@/components/basic/ActionSheet";
  14. import { rpxToPx } from "@/utils/tools";
  15. import { setMode, setShowActionTip } from "@/store/health";
  16. import { getCountownTime, getDuration, getScenario, getThemeColor, getWindowStatus } from "./hooks/health_hooks";
  17. import { IconActive, IconArrow, IconCircle, IconClose, IconMiss, IconMore, IconNotification, IconNotificationOff, IconSwitch } from "@/components/basic/Icons";
  18. import DurationPicker from "@/_health/components/duration_picker";
  19. import Taro from "@tarojs/taro";
  20. import { systemLocation } from "@/services/common";
  21. import { TimeFormatter } from "@/utils/time_format";
  22. import { clearLocation } from "@/services/user";
  23. import OnBoard from "@/_health/components/onboard";
  24. import NewButton, { NewButtonType } from "@/_health/base/new_button";
  25. import showAlert from "@/components/basic/Alert";
  26. import { useTranslation } from "react-i18next";
  27. import Cell from "@/_health/base/cell";
  28. import ConsoleCell from "@/_health/components/console_cell";
  29. import IconTitleCell from "@/_health/components/icon_title_cell";
  30. import StatusIndicator, { StatusType } from "@/_health/base/status_indicator";
  31. let useNavigation;
  32. let min = 0
  33. let max = 0
  34. let defaultTimestamp = 0
  35. let lastFastSleepStatus = ''
  36. if (process.env.TARO_ENV == 'rn') {
  37. useNavigation = require("@react-navigation/native").useNavigation
  38. }
  39. export default function MainConsole(props: { type: WindowType }) {
  40. const health = useSelector((state: any) => state.health);
  41. const user = useSelector((state: any) => state.user);
  42. const [durationPicker, setDurationPicker] = useState(false)
  43. const [btnDisable, setBtnDisable] = useState(false)
  44. const [selItem, setSelItem] = useState<any>(null)
  45. const [hideGuideTip, setHideGuideTip] = useState(false)
  46. const [hideErrorTip, setHideErrorTip] = useState(false)
  47. const { t } = useTranslation()
  48. const dispatch = useDispatch()
  49. let navigation, showActionSheetWithOptions;
  50. if (useNavigation) {
  51. navigation = useNavigation()
  52. }
  53. useEffect(() => {
  54. }, [props.type])
  55. //
  56. useEffect(() => {
  57. if (health.fast_with_sleep.status == 'OG2_MISALIGNED' || health.fast_with_sleep.status == 'OG2_NO1') {
  58. if (lastFastSleepStatus != health.fast_with_sleep.status) {
  59. setHideErrorTip(false)
  60. }
  61. }
  62. lastFastSleepStatus = health.fast_with_sleep.status
  63. }, [health.fast_with_sleep.status])
  64. global.chooseLocation = () => {
  65. chooseLocation()
  66. }
  67. function edit(item) {
  68. if (item.scenario != 'FAST' && item.scenario != 'SLEEP') {
  69. return
  70. }
  71. if (item.action == 'NA' || item.action == 'POST_MOMENT' || 'SLEEP_WAKE_UP' == item.action) {
  72. return;
  73. }
  74. if (!user.isLogin) {
  75. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  76. return
  77. }
  78. console.log(item)
  79. setSelItem(item)
  80. }
  81. function record(item) {
  82. if (!user.isLogin) {
  83. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  84. return
  85. }
  86. if (item.event == 'FAST_START' && health.long_fast.status == 'OG') {
  87. showAlert({
  88. title: '长断食进行中',
  89. content: '当前有一个正在进行的长断食记录,要转换为间歇性断食吗?',
  90. showCancel: true,
  91. cancelText: '取消',
  92. confirmText: '确定',
  93. confirm: () => {
  94. changeToIF();
  95. }
  96. })
  97. return
  98. }
  99. setSelItem(item)
  100. switch (item.action) {
  101. case 'START':
  102. {
  103. jumpPage(`/_health/pages/log_time?index=${health.mode == 'FAST' ? 0 : 1}&single=1&is_start=1&window=${health.mode}`)
  104. }
  105. return;
  106. case 'END':
  107. {
  108. var sceniaro = getScenario(health.windows, health.mode)
  109. jumpPage(`/_health/pages/log_time?type=home&index=${health.mode == 'FAST' ? 3 : 2}&single=${sceniaro.status == 'OG' ? 1 : 0}&is_start=0&window=${health.mode}`)
  110. }
  111. return;
  112. case 'MARK_DONE':
  113. {
  114. setBtnDisable(true)
  115. clockTimes({
  116. check_items: [{
  117. schedule_id: item.schedule_id,
  118. date: dayjs().format('YYYYMMDD'),
  119. timestamp: new Date().getTime()
  120. }]
  121. }).then(res => {
  122. Taro.showToast({
  123. title: '操作成功',
  124. icon: 'success'
  125. })
  126. global.refreshWindow()
  127. global.refreshHistory()
  128. }).catch(e => {
  129. })
  130. }
  131. return;
  132. }
  133. jumpPage(`/_health/pages/add_moment?moment=${JSON.stringify(item)}&title=${item.title}&schedule_id=${item.schedule_id}&event_id=${item.event_id}`)
  134. }
  135. function operateTitle(item) {
  136. return t('health.log')
  137. // switch (item.action) {
  138. // case 'START':
  139. // case 'END':
  140. // return '打卡'
  141. // case 'MARK_DONE':
  142. // return 'Action'
  143. // }
  144. // return '记录'
  145. }
  146. function itemTitle(item: any) {
  147. // if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  148. // return item.title
  149. // }
  150. if (item.real) {
  151. return TimeFormatter.dayjsFormat(item.real.timestamp)
  152. // return dayjs(item.real.timestamp).format('MM-DD HH:mm')
  153. }
  154. if (!item.target || !item.target.timestamp) {
  155. return item.time_label
  156. }
  157. return TimeFormatter.dayjsFormat(item.target.timestamp)
  158. // return dayjs(item.target.timestamp).format('MM-DD HH:mm')
  159. }
  160. function itemValue(item: any) {
  161. let themeColor: any = getThemeColor(health.mode)
  162. // const scenario = getScenario(health.windows, health.mode)
  163. // if (item.action == 'END' && !scenario.real) {
  164. // themeColor = MainColorType.g01
  165. // }
  166. if (item.target && item.target.timestamp >= new Date().getTime()) {
  167. themeColor = MainColorType.g02
  168. }
  169. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  170. return null
  171. }
  172. if (item.action && item.action != 'NA') {
  173. if (health.mode == 'FAST' || health.mode == 'SLEEP') {
  174. if (item.action == 'POST_MOMENT') {
  175. return <IconArrow width={rpxToPx(34)} color={MainColorType.g03} />
  176. }
  177. }
  178. // else if (health.mode == 'ACTIVE' && item.action == 'POST_MOMENT') {
  179. // return <NewButton
  180. // color={themeColor}
  181. // type={NewButtonType.border}
  182. // title={operateTitle(item)}
  183. // width={rpxToPx(128)}
  184. // height={rpxToPx(72)}
  185. // bold={true}
  186. // onClick={() => record(item)} />
  187. // }
  188. return <NewButton
  189. color={themeColor}
  190. type={item.target && item.target.timestamp >= new Date().getTime() ? NewButtonType.alpha2 : NewButtonType.alpha}
  191. title={operateTitle(item)}
  192. width={rpxToPx(128)}
  193. height={rpxToPx(72)}
  194. bold={true}
  195. onClick={() => record(item)} />
  196. }
  197. if (item.action && item.action == 'NA') {
  198. // if (health.mode == 'FAST' || health.mode == 'SLEEP') {
  199. if (item.moment && item.moment.media && item.moment.media.length > 0) {
  200. return <Image
  201. src={item.moment.media[0].url}
  202. mode="aspectFill"
  203. className="console_item_img" />
  204. }
  205. else {
  206. return <IconArrow width={rpxToPx(34)} color={MainColorType.g03} />
  207. }
  208. // }
  209. }
  210. return <View />
  211. }
  212. function canTap(item) {
  213. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  214. return false;
  215. }
  216. if (!user.isLogin) {
  217. return false
  218. }
  219. if (!item.event_id) {
  220. return false
  221. }
  222. return true
  223. }
  224. function tapTimeline(item, inex) {
  225. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  226. return;
  227. }
  228. if (!user.isLogin) {
  229. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  230. return
  231. }
  232. if (!item.event_id) {
  233. return
  234. }
  235. jumpPage(`/_health/pages/timeline_detail?event_id=${item.event_id}&schedule_id=${item.schedule_id}`)
  236. }
  237. function getIconColor(index) {
  238. var color = getThemeColor(health.mode)
  239. const time = new Date().getTime()
  240. const scenario = getScenario(health.windows, health.mode)
  241. const timeline = scenario.timeline[index]
  242. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  243. if (time > timeline.target.timestamp) {
  244. return MainColorType.g03
  245. }
  246. }
  247. if (timeline.real) {
  248. return MainColorType.g03
  249. }
  250. if (timeline.target.timestamp < new Date().getTime()) {
  251. return MainColorType.error
  252. }
  253. return color
  254. }
  255. function processIcon(index) {
  256. const time = new Date().getTime()
  257. const scenario = getScenario(health.windows, health.mode)
  258. const timeline = scenario.timeline[index]
  259. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  260. if (time > timeline.target.timestamp) {
  261. return <Image style={{ width: rpxToPx(24), height: rpxToPx(24) }} src={require('@assets/_health/checked.png')} />
  262. }
  263. return <IconCircle width={rpxToPx(32)} color={MainColorType.g02} />
  264. // return <View style={{width:rpxToPx(26),height:rpxToPx(26),borderRadius:rpxToPx(13),backgroundColor:'#fff'}}/>
  265. }
  266. // if (timeline.target.timestamp < new Date().getTime()) {
  267. // return <IconMiss color="#fff" width={rpxToPx(24)} />
  268. // }
  269. if (timeline.real) {
  270. return <Image style={{ width: rpxToPx(24), height: rpxToPx(24) }} src={require('@assets/_health/checked.png')} />
  271. }
  272. return <View style={{ width: rpxToPx(26), height: rpxToPx(26), borderRadius: rpxToPx(13), backgroundColor: '#fff' }} />
  273. // return timeline.reminder ? <IconNotification color="#fff" width={rpxToPx(24)} /> : <IconNotificationOff color="#fff" width={rpxToPx(24)} />
  274. }
  275. function timelineItem(item: any, index: number, count: number) {
  276. var strColor = ''
  277. if (item.real) {
  278. strColor = getThemeColor(health.mode)
  279. }
  280. else if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  281. const time = new Date().getTime()
  282. const scenario = getScenario(health.windows, health.mode)
  283. const timeline = scenario.timeline[index]
  284. if (time > timeline.target.timestamp) {
  285. strColor = getThemeColor(health.mode)
  286. }
  287. else {
  288. strColor = 'transparent'
  289. }
  290. }
  291. else {
  292. strColor = 'transparent'
  293. }
  294. function statusView() {
  295. return <StatusIndicator type={StatusType.console}
  296. color={strColor}
  297. fontColor={item.real ? getThemeColor(health.mode) : MainColorType.g01}
  298. bold={item.real ? true : false}
  299. // color={new Date().getTime()<item.target.timestamp?MainColorType.g02:getThemeColor(health.mode)}
  300. text={itemTitle(item)}
  301. fontSize={rpxToPx(24)}
  302. >{
  303. (item.real || (health.mode == 'DAY' || health.mode == 'NIGHT')) ? processIcon(index) : <IconCircle width={rpxToPx(32)} color={MainColorType.g02} />
  304. }</StatusIndicator>
  305. }
  306. return <ConsoleCell
  307. status={statusView()}
  308. title={(item.moment && item.moment.title) ? item.moment.title : item.title}
  309. description={item.moment ? item.moment.description : null}
  310. right={itemValue(item)}
  311. disable={!canTap(item)}
  312. onClick={() => tapTimeline(item, index)}
  313. showLine={true}
  314. fullLine={count - 1 == index}
  315. />
  316. }
  317. function changeToIF() {
  318. updateFast({ fast_type: 'IF' }).then(res => {
  319. global.refreshWindow()
  320. })
  321. }
  322. function actionList() {
  323. var list: any = []
  324. switch (health.mode) {
  325. case 'ACTIVE':
  326. {
  327. if (!health.finish_setup) {
  328. list.push(t('health.finish_setup'))
  329. }
  330. list.push(t('health.log_extra_activity'))
  331. var scenario = getScenario(health.windows, 'ACTIVE')
  332. if (scenario.status != 'WFS') {
  333. list.push(t('health.mark_done'))
  334. }
  335. if (health.finish_setup) {
  336. list.push(t('health.edit_schedule'))
  337. }
  338. }
  339. break;
  340. case 'EAT':
  341. {
  342. if (!health.finish_setup) {
  343. list.push(t('health.finish_setup'))
  344. }
  345. list.push(t('health.log_extra_meal'))
  346. var scenario = getScenario(health.windows, 'EAT')
  347. if (scenario.status != 'WFS') {
  348. list.push(t('health.mark_done'))
  349. }
  350. if (health.finish_setup) {
  351. list.push(t('health.edit_schedule'))
  352. }
  353. }
  354. break;
  355. case 'FAST':
  356. case 'SLEEP':
  357. {
  358. if (!health.finish_setup) {
  359. list.push(t('health.finish_setup'))
  360. }
  361. const obj = getScenario(health.windows, health.mode)
  362. if (obj.window_id) {
  363. list.push(t('health.delete_current_record'))
  364. }
  365. if (health.finish_setup) {
  366. list.push(t('health.edit_schedule'))
  367. }
  368. }
  369. break;
  370. case 'DAY':
  371. case 'NIGHT':
  372. {
  373. list = [t('health.change_location'), t('health.clear_location')]
  374. }
  375. break;
  376. }
  377. return list;
  378. }
  379. function more() {
  380. if (!user.isLogin) {
  381. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  382. return
  383. }
  384. var strTitle = ''
  385. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  386. strTitle = getLocation()
  387. }
  388. else if (health.mode == 'EAT' || health.mode == 'ACTIVE') {
  389. const scenario = getScenario(health.windows, health.mode)
  390. if (scenario.archive_timestamp) {
  391. /*"Today's logs will be archived at 23:59."
  392. "Today's logs will be archived at 03:10 tomorrow."
  393. "今日记录将于 23:59 归档"
  394. "今日记录将于明天 23:59 归档"*/
  395. var today = new Date().getDate()
  396. var date = new Date(scenario.archive_timestamp).getDate()
  397. strTitle = `今日记录将于${today == date ? '' : '明天'} ${dayjs(scenario.archive_timestamp).format('HH:mm')} 归档`
  398. }
  399. else {
  400. strTitle = t('health.more_actions')
  401. }
  402. }
  403. else {
  404. strTitle = t('health.more_actions')
  405. }
  406. showActionSheet({
  407. showActionSheetWithOptions: showActionSheetWithOptions,
  408. title: strTitle,
  409. itemList: actionList(),
  410. success: (res) => {
  411. tapActionSheet(res)
  412. }
  413. });
  414. }
  415. function getLocation() {
  416. var scenario = getScenario(health.windows, health.mode)
  417. if (!scenario.extra.choose_location) {
  418. return t('health.more_actions')
  419. }
  420. return `${getCity()} | ${Math.abs(parseInt(scenario.extra.lat))}°${parseInt(scenario.extra.lat) < 0 ? 'S' : 'N'} ${Math.abs(parseInt(scenario.extra.lng))}°${parseInt(scenario.extra.lng) < 0 ? 'W' : 'E'}`
  421. }
  422. function getCity() {
  423. var city = ''
  424. var scenario = getScenario(health.windows, health.mode)
  425. if (scenario.extra.address) {
  426. if (scenario.extra.address.city.length > 0) {
  427. city = scenario.extra.address.city
  428. }
  429. else if (scenario.extra.address.province.length > 0) {
  430. city = scenario.extra.address.province
  431. }
  432. else if (scenario.extra.address.country.length > 0) {
  433. city = scenario.extra.address.country
  434. }
  435. else {
  436. city = t('feature.track_time_duration.third_ring.unknown')
  437. }
  438. }
  439. else {
  440. city = t('feature.track_time_duration.third_ring.unknown')
  441. }
  442. return city
  443. }
  444. function tapActionSheet(index) {
  445. var title = actionList()[index]
  446. switch (title) {
  447. case t('health.finish_setup'):
  448. tapGuide();
  449. break;
  450. case t('health.log_extra_activity'):
  451. jumpPage(`/_health/pages/add_moment?title=&is_temp=${true}`)
  452. break;
  453. case t('health.log_extra_meal'):
  454. jumpPage(`/_health/pages/add_moment?title=加餐&is_temp=${true}`)
  455. break;
  456. case t('health.mark_done'):
  457. tapMakeDone()
  458. break
  459. case t('health.edit_schedule'):
  460. jumpPage('/_health/pages/schedules?mode=' + health.mode)
  461. break
  462. case t('health.delete_current_record'):
  463. {
  464. const obj = getScenario(health.windows, health.mode)
  465. showAlert({
  466. title: 'del',
  467. content: '确认删除此记录?',
  468. showCancel: true,
  469. confirm: () => {
  470. delRecord(obj.window_id).then(res => {
  471. global.refreshWindow()
  472. })
  473. }
  474. })
  475. }
  476. break;
  477. case t('health.change_location'):
  478. chooseLocation()
  479. break
  480. case t('health.clear_location'):
  481. tapClearLocation()
  482. break
  483. }
  484. }
  485. function tapGuide() {
  486. jumpPage('/_health/pages/guide_begin')
  487. // showAlert({
  488. // title:'设置引导弹窗',
  489. // content:'设置引导描述',
  490. // showCancel:true,
  491. // cancelText:'稍后',
  492. // confirmText:'设置',
  493. // cancel:()=>{
  494. // },
  495. // confirm:()=>{
  496. // jumpPage('/_health/pages/guide_fast')
  497. // }
  498. // })
  499. }
  500. function tapMakeDone() {
  501. makeDone(getScenario(health.windows, health.mode).window_id).then(res => {
  502. global.refreshWindow()
  503. global.refreshHistory()
  504. })
  505. }
  506. function detail() {
  507. const { day, night } = health.windows.night_day
  508. const { fast, eat } = health.windows.fast_eat
  509. const { sleep, active } = health.windows.sleep_active
  510. let list: any = []
  511. switch (health.mode) {
  512. case 'DAY':
  513. list = day.timeline
  514. if (day.extra.choose_location == false) {
  515. return <OnBoard title={t('health.sunrise_to_sunset')}
  516. desc={t('health.rise_to_set_desc')}
  517. btnTitle={t('health.choose_location')}
  518. onClick={chooseLocation}
  519. />
  520. }
  521. break;
  522. case 'NIGHT':
  523. list = night.timeline
  524. if (night.extra.choose_location == false) {
  525. return <OnBoard title={t('health.sunset_to_sunrise')}
  526. desc={t('health.set_to_rise_desc')}
  527. btnTitle={t('health.choose_location')}
  528. onClick={chooseLocation}
  529. />
  530. }
  531. break;
  532. case 'FAST':
  533. list = fast.timeline
  534. break;
  535. case 'EAT':
  536. list = eat.timeline
  537. break;
  538. case 'SLEEP':
  539. list = sleep.timeline
  540. break;
  541. case 'ACTIVE':
  542. list = active.timeline
  543. if (active.onboard == false) {
  544. var seconds = new Date().getSeconds()
  545. // return <Swiper autoplay>
  546. // {
  547. // list.map((item, index) => {
  548. // return <SwiperItem key={index}>
  549. // <OnBoard title={item.title}
  550. // desc={item.time_label}
  551. // btnTitle="Action"
  552. // onClick={() => {
  553. // if (!user.isLogin) {
  554. // jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  555. // return
  556. // }
  557. // jumpPage('/_health/pages/active_plan?schedule=' + JSON.stringify(item))
  558. // }}
  559. // />
  560. // </SwiperItem>
  561. // })
  562. // }
  563. // </Swiper>
  564. return <OnBoard title={list[0].title}
  565. desc={list[0].time_label}
  566. btnTitle="Action"
  567. onClick={() => {
  568. if (!user.isLogin) {
  569. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  570. return
  571. }
  572. jumpPage('/_health/pages/active_plan?schedule=' + JSON.stringify(list.length > 0 ? list[0] : '{}'))
  573. }}
  574. />
  575. }
  576. break;
  577. }
  578. return <View>
  579. {
  580. list.map((item, index) => {
  581. return timelineItem(item, index, list.length)
  582. })
  583. }
  584. </View>
  585. }
  586. function switchText() {
  587. switch (health.mode) {
  588. case 'FAST':
  589. return t('health.switch_to', { scenario: t('health.eat') })
  590. case 'EAT':
  591. return t('health.switch_to', { scenario: t('health.fast') })
  592. case 'DAY':
  593. return t('health.switch_to', { scenario: t('health.night') })
  594. case 'NIGHT':
  595. return t('health.switch_to', { scenario: global.language == 'en' ? 'Daylight' : '白天' })//'Switch to Daylight'
  596. case 'SLEEP':
  597. return t('health.switch_to', { scenario: t('health.active') })
  598. case 'ACTIVE':
  599. return t('health.switch_to', { scenario: t('health.sleep') })
  600. }
  601. return ''
  602. }
  603. function tapSwitchBtn() {
  604. switch (health.mode) {
  605. case 'FAST':
  606. dispatch(setMode('EAT'));
  607. break
  608. case 'EAT':
  609. dispatch(setMode('FAST'));
  610. break
  611. case 'DAY':
  612. dispatch(setMode('NIGHT'));
  613. break
  614. case 'NIGHT':
  615. dispatch(setMode('DAY'));
  616. break
  617. case 'SLEEP':
  618. dispatch(setMode('ACTIVE'));
  619. break
  620. case 'ACTIVE':
  621. dispatch(setMode('SLEEP'));
  622. break
  623. }
  624. }
  625. function tapClearLocation() {
  626. Taro.showModal({
  627. title: '提示',
  628. content: '确认清除位置数据?',
  629. success: function (res) {
  630. if (res.confirm) {
  631. clearLocation().then(res => {
  632. global.refreshWindow()
  633. })
  634. } else if (res.cancel) {
  635. console.log('用户点击取消')
  636. }
  637. }
  638. })
  639. }
  640. function chooseLocation() {
  641. if (!user.isLogin) {
  642. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  643. return
  644. }
  645. var scenario = getScenario(health.windows, health.mode)
  646. Taro.chooseLocation({
  647. latitude: scenario.extra.choose_location ? parseFloat(scenario.extra.lat + '') : undefined,
  648. longitude: scenario.extra.choose_location ? parseFloat(scenario.extra.lng + '') : undefined,
  649. success: function (res) {
  650. uploadLocation(res)
  651. },
  652. fail(res) {
  653. Taro.showToast({
  654. title: '位置修改失败!\n请重新选择就近位置',
  655. icon: 'none'
  656. })
  657. },
  658. complete(res) {
  659. }
  660. })
  661. }
  662. function uploadLocation(res) {
  663. var today = new Date()
  664. var yesterday = new Date(today.getTime() - 24 * 3600 * 1000)
  665. var tomorrow = new Date(today.getTime() + 24 * 3600 * 1000 * 5)
  666. var strYesterday = `${yesterday.getFullYear()}-${TimeFormatter.padZero(yesterday.getMonth() + 1)}-${TimeFormatter.padZero(yesterday.getDate())}`
  667. var strTomorrow = `${tomorrow.getFullYear()}-${TimeFormatter.padZero(tomorrow.getMonth() + 1)}-${TimeFormatter.padZero(tomorrow.getDate())}`
  668. systemLocation({
  669. lat: res.latitude,
  670. lng: res.longitude,
  671. name: res.name,
  672. address: res.address,
  673. date_start: strYesterday,
  674. date_end: strTomorrow,
  675. coordinate_system_standard: process.env.TARO_ENV == 'weapp' ? 'GCJ-02' : 'WGS-84'
  676. }).then(data => {
  677. global.refreshWindow()
  678. })
  679. }
  680. function updateDuration(duration) {
  681. setDurationPicker(false)
  682. const scenario = getScenario(health.windows, health.mode)
  683. updateEventDuration(scenario.timeline[0].event_id, duration).then(res => {
  684. global.refreshWindow()
  685. })
  686. }
  687. function timePointClass() {
  688. if (getWindowStatus(health.windows, health.mode) == WindowStatusType.process) {
  689. return 'time_point time_point_animation'
  690. }
  691. return 'time_point'
  692. }
  693. function fastWithSleepStatus() {
  694. const { status } = health.fast_with_sleep
  695. var str = ''
  696. switch (status) {
  697. case 'OG1':
  698. str = t('health.fast_before_bed')
  699. break;
  700. case 'OG2_NO1':
  701. str = t('health.fast_missing')
  702. break;
  703. case 'OG2':
  704. str = t('health.fast_while_sleep')
  705. break;
  706. case 'OG2_MISALIGNED':
  707. str = t('health.misaligned')
  708. break;
  709. case 'OG3':
  710. str = t('health.fast_after_sleep')
  711. break
  712. }
  713. return <View className="h26" style={{ color: MainColorType.g01 }}>{str}</View>
  714. }
  715. function longFastStatus() {
  716. const { status } = health.long_fast
  717. return <View className="h26" style={{ color: MainColorType.g01 }}>{status == 'OG' ? 'In progress' : ''}</View>
  718. }
  719. function showMoreBtn() {
  720. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  721. if (!getScenario(health.windows, health.mode).extra.choose_location) {
  722. return false
  723. }
  724. }
  725. return true;
  726. }
  727. function getTitleColor() {
  728. if (getWindowStatus(health.windows, health.mode) == WindowStatusType.upcoming) {
  729. return MainColorType.g01
  730. }
  731. const scenario = getScenario(health.windows, health.mode)
  732. if (scenario.status == 'OG') {
  733. return getThemeColor(health.mode)
  734. }
  735. return '#000'
  736. }
  737. function polarCheck() {
  738. const scenario = getScenario(health.windows, health.mode)
  739. if (health.mode == 'DAY' && scenario.extra && scenario.extra.is_polar_day) {
  740. return <View className="console_tag_bg" style={{ backgroundColor: getThemeColor(health.mode) }}>
  741. <Image src={global.language == 'en' ? require('@assets/_health/tag_polar.png') : require('@assets/_health/tag_polar_day.png')} className="console_tag_img" />
  742. </View>
  743. }
  744. if (health.mode == 'NIGHT' && scenario.extra && scenario.extra.is_polar_night) {
  745. return <View className="console_tag_bg" style={{ backgroundColor: getThemeColor(health.mode) }}>
  746. <Image src={global.language == 'en' ? require('@assets/_health/tag_polar.png') : require('@assets/_health/tag_polar_night.png')} className="console_tag_img" />
  747. </View>
  748. }
  749. }
  750. function ogTime() {
  751. return <View className="time_count h30 bold" style={{
  752. backgroundColor: getThemeColor(health.mode)
  753. }}>
  754. {/* <View className="console_tag_bg" style={{backgroundColor:getThemeColor(health.mode)}}>
  755. <Image src={global.language=='en'?require('@assets/_health/tag_log.png'):require('@assets/_health/tag_record.png')} className="console_tag_img"/>
  756. </View> */}
  757. {
  758. getCountownTime(health.windows, health.mode)
  759. }
  760. </View>
  761. }
  762. return <View className="main-console-bg">
  763. <Image className="main_arrow" src={require('@assets/images/center_arrow.png')} />
  764. <View className="main_summary">
  765. <View className="main_summary_time h30 bold" style={{
  766. marginTop: rpxToPx(14),
  767. }} >
  768. {polarCheck()}
  769. {getScenario(health.windows, health.mode).status == 'OG' ? ogTime() : getDuration(health.windows, health.mode)}
  770. </View>
  771. <Text className="main_summary_duration g01"
  772. style={{
  773. marginBottom: rpxToPx(30)
  774. }}>{getScenario(health.windows, health.mode).status == 'OG' ? getDuration(health.windows, health.mode) : getCountownTime(health.windows, health.mode)}
  775. </Text>
  776. <View className="border_footer_line" />
  777. </View>
  778. <View style={{ backgroundColor: '#fff', width: rpxToPx(750) }}>
  779. {
  780. detail()
  781. }
  782. </View>
  783. <View className="main_footer2">
  784. <View style={{ width: rpxToPx(316), height: rpxToPx(128), display: 'flex', alignItems: 'center', justifyContent: 'center' }}>
  785. <NewButton
  786. type={NewButtonType.link}
  787. title={switchText()}
  788. onClick={tapSwitchBtn}
  789. >
  790. </NewButton>
  791. </View>
  792. {
  793. showMoreBtn() && <NewButton
  794. btnStyle={{
  795. position: 'absolute',
  796. top: rpxToPx(42),
  797. right: rpxToPx(40)
  798. }}
  799. type={NewButtonType.more}
  800. onClick={more}
  801. />
  802. }
  803. </View>
  804. {
  805. user.isLogin && !hideGuideTip && !health.finish_setup && <View className="guide_tip h26" onClick={() => {
  806. tapGuide();
  807. setHideGuideTip(true)
  808. }}>{t('health.console_guide_tip')}
  809. <NewButton type={NewButtonType.img} btnStyle={{
  810. position: 'absolute',
  811. right: 0,
  812. top: 0,
  813. bottom: 0,
  814. width: rpxToPx(92)
  815. }} onClick={() => {
  816. setHideGuideTip(true)
  817. }}>
  818. <IconClose color={MainColorType.g01} width={rpxToPx(32)} height={rpxToPx(32)} />
  819. </NewButton>
  820. </View>
  821. }
  822. {
  823. health.mode == 'ACTIVE' && <View>
  824. <View className="main_column_space" /><IconTitleCell
  825. icon={<IconActive width={rpxToPx(32)} color={MainColorType.active} />}
  826. title={t('health.move_more')}
  827. onClick={() => {
  828. jumpPage('/_health/pages/move')
  829. }}
  830. /></View>
  831. }
  832. {
  833. (health.mode == 'FAST' || health.mode == 'SLEEP') && <View >
  834. <View className="main_column_space" />
  835. <IconTitleCell
  836. onClick={() => {
  837. if (health.long_fast.status == 'OG') {
  838. showAlert({
  839. title: '长断食进行中',
  840. content: '当前有一个正在进行的长断食记录,要转换为间歇性断食吗?',
  841. showCancel: true,
  842. cancelText: '取消',
  843. confirmText: '确定',
  844. confirm: () => {
  845. updateFast({ fast_type: 'IF' }).then(res => {
  846. global.refreshWindow()
  847. jumpPage('/_health/pages/fast_sleep')
  848. })
  849. }
  850. })
  851. return
  852. }
  853. setTimeout(() => {
  854. setHideErrorTip(true)
  855. }, 1000)
  856. jumpPage('/_health/pages/fast_sleep')
  857. }}
  858. showLine={health.mode == 'FAST'}
  859. icon={<Image className="active_icon" src={require('@assets/_health/fast.png')} />}
  860. title={t('health.fast_with_sleep')}
  861. desc={<View style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
  862. {
  863. fastWithSleepStatus()
  864. }
  865. {
  866. !hideErrorTip && (health.fast_with_sleep.status == 'OG2_MISALIGNED' || health.fast_with_sleep.status == 'OG2_NO1') && <View style={{
  867. backgroundColor: MainColorType.error,
  868. width: rpxToPx(12),
  869. height: rpxToPx(12),
  870. borderRadius: rpxToPx(6),
  871. marginLeft: rpxToPx(12),
  872. marginRight: rpxToPx(12)
  873. }} />
  874. }
  875. </View>}
  876. />
  877. </View>
  878. }
  879. {
  880. health.mode == 'FAST' && <IconTitleCell
  881. onClick={() => {
  882. jumpPage('/_health/pages/long_fast')
  883. }}
  884. icon={<Image className="active_icon" src={require('@assets/_health/fast.png')} />}
  885. title={t('health.long_fast')}
  886. desc={
  887. longFastStatus()
  888. }
  889. />
  890. }
  891. <View className="circle" />
  892. {
  893. durationPicker && <DurationPicker
  894. done={(time) => {
  895. updateDuration(time)
  896. }}
  897. dismiss={() => {
  898. setDurationPicker(false)
  899. }} time={getScenario(health.windows, health.mode).target.duration} />
  900. }
  901. </View >
  902. }