MainConsole.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. import { WindowType } from "@/utils/types";
  2. import { View, Text, Image } 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, 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 } from "@/store/health";
  16. import { getCountownTime, getDuration, getScenario, getWindowStatus } from "./hooks/health_hooks";
  17. let useNavigation;
  18. let min = 0
  19. let max = 0
  20. let defaultTimestamp = 0
  21. if (process.env.TARO_ENV == 'rn') {
  22. useNavigation = require("@react-navigation/native").useNavigation
  23. }
  24. export default function MainConsole(props: { type: WindowType }) {
  25. const health = useSelector((state: any) => state.health);
  26. const user = useSelector((state: any) => state.user);
  27. const [showPicker, setShowPicker] = useState(false)
  28. const [showTimePicker, setShowTimePicker] = useState(false)
  29. const [operateType, setOperateType] = useState('')
  30. const [btnDisable, setBtnDisable] = useState(false)
  31. const [selItem, setSelItem] = useState<any>(null)
  32. const limitPickerRef = useRef(null)
  33. const dispatch = useDispatch()
  34. let navigation, showActionSheetWithOptions;
  35. if (useNavigation) {
  36. navigation = useNavigation()
  37. }
  38. useEffect(() => {
  39. }, [props.type])
  40. function edit(item) {
  41. if (item.scenario != 'FAST' && item.scenario != 'SLEEP') {
  42. return
  43. }
  44. if (item.action == 'NA' || item.action == 'POST_MOMENT' || 'SLEEP_WAKE_UP' == item.action) {
  45. return;
  46. }
  47. if (!user.isLogin) {
  48. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  49. return
  50. }
  51. console.log(item)
  52. setSelItem(item)
  53. setShowPicker(true)
  54. }
  55. function record(item) {
  56. if (!user.isLogin) {
  57. jumpPage('/pages/account/ChooseAuth', 'ChooseAuth', navigation)
  58. return
  59. }
  60. setSelItem(item)
  61. switch (item.action) {
  62. case 'START':
  63. {
  64. defaultTimestamp = new Date().getTime()
  65. min = defaultTimestamp - 1 * 24 * 3600 * 1000
  66. max = defaultTimestamp
  67. setOperateType('startFast')
  68. setShowTimePicker(true)
  69. }
  70. return;
  71. case 'END':
  72. {
  73. defaultTimestamp = new Date().getTime()
  74. // defaultTimestamp = e ? new Date().getTime() : logEventTimestamp
  75. min = defaultTimestamp - 1 * 24 * 3600 * 1000
  76. max = defaultTimestamp
  77. setOperateType('endFast')
  78. setShowTimePicker(true)
  79. }
  80. return;
  81. }
  82. jumpPage('/pages/clock/AddMoment?moment=' + JSON.stringify(item))
  83. }
  84. function operateTitle(item) {
  85. switch (item.action) {
  86. case 'START':
  87. case 'END':
  88. return '打卡'
  89. }
  90. return '记录'
  91. }
  92. function itemTitle(item: any) {
  93. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  94. return item.title
  95. }
  96. return dayjs(item.target.timestamp).format('HH:mm')
  97. }
  98. function itemValue(item: any) {
  99. let themeColor: any = getThemeColor()
  100. const scenario = getScenario(health.windows,health.mode)
  101. if (item.action == 'END' && !scenario.real) {
  102. themeColor = '#B2B2B2'
  103. }
  104. if (health.mode == 'DAY' || health.mode == 'NIGHT') {
  105. return <Text className="timeline_time">{dayjs(item.target.timestamp).format('HH:mm')}</Text>
  106. }
  107. if (item.action && item.action != 'NA')
  108. return <View className="timeline_operate" style={{ color: themeColor, backgroundColor: themeColor + '1A' }} onClick={() => record(item)}>{operateTitle(item)}</View>
  109. return <View />
  110. }
  111. function timelineItem(item: any, index: number, count: number) {
  112. return <View key={index} className="timeline_item" >
  113. <View className="timeline_left">
  114. {
  115. health.mode != 'DAY' && health.mode != 'NIGHT' && <Text className="timeline_title">{item.title}</Text>
  116. }
  117. <View style={{ display: 'flex', flexDirection: 'row', alignItems: 'center' }}>
  118. <Text className="timeline_time" onClick={() => edit(item)}>{itemTitle(item)}</Text>
  119. </View>
  120. </View>
  121. {
  122. itemValue(item)
  123. }
  124. {/* {
  125. item.action && item.action != 'NA' &&
  126. } */}
  127. <View className="border_footer_line" style={{ left: count - 1 == index ? -rpxToPx(64) : 0 }} />
  128. </View>
  129. }
  130. function timeContent() {
  131. return <Modal
  132. testInfo={null}
  133. dismiss={() => {
  134. setShowPicker(false)
  135. }}
  136. confirm={() => { }}>
  137. {
  138. pickerContent()
  139. }
  140. </Modal>
  141. }
  142. function pickerContent() {
  143. const timestamp = selItem.target.timestamp
  144. const strTime = dayjs(timestamp).format('HH:mm')
  145. var title, color;
  146. switch (selItem.scenario) {
  147. case 'FAST':
  148. title = operateType == 'endFast' ? '结束断食' : '开始断食'
  149. color = MainColorType.fast
  150. break;
  151. case 'SLEEP':
  152. title = operateType == 'endFast' ? '结束睡眠' : '开始睡眠'
  153. color = MainColorType.sleep
  154. }
  155. return <TimePicker time={strTime}
  156. color={color}
  157. title={title}
  158. confirm={(e) => {
  159. confirmPickerTime(e)
  160. }}
  161. cancel={() => {
  162. setShowPicker(false)
  163. }} />
  164. }
  165. function confirmPickerTime(strTime) {
  166. if (selItem.event == 'FAST_END') {
  167. const obj = health.windows.fast_eat.fast.timeline[0]
  168. if (obj.action == 'POST_MOMENT' || obj.action == 'NA') {
  169. const format = dayjs(obj.target.timestamp).format(`YYYY-MM-DDT${strTime}:ss`)
  170. var t = new Date(format).getTime()
  171. if (t <= obj.target.timestamp) {
  172. t += 24 * 3600 * 1000
  173. }
  174. updateTarget(t, obj.event_id).then(res => {
  175. global.refreshWindow()
  176. setShowPicker(false)
  177. })
  178. return;
  179. }
  180. }
  181. else if (selItem.event == 'SLEEP_WAKE_UP') {
  182. const obj = health.windows.sleep_active.sleep.timeline[0]
  183. if (obj.action == 'POST_MOMENT' || obj.action == 'NA') {
  184. const format = dayjs(obj.target.timestamp).format(`YYYY-MM-DDT${strTime}:ss`)
  185. var t = new Date(format).getTime()
  186. if (t <= obj.target.timestamp) {
  187. t += 24 * 3600 * 1000
  188. }
  189. updateTarget(t, obj.event_id).then(res => {
  190. global.refreshWindow()
  191. setShowPicker(false)
  192. })
  193. return;
  194. }
  195. }
  196. console.log('zzz')
  197. updateSchedule({
  198. time: strTime
  199. }, selItem.schedule_id).then(res => {
  200. global.refreshWindow()
  201. setShowPicker(false)
  202. })
  203. }
  204. function modalContent() {
  205. global.set_time = new Date().getTime()
  206. return <Modal
  207. testInfo={null}
  208. dismiss={() => {
  209. setShowTimePicker(false)
  210. }}
  211. confirm={() => { }}>
  212. {
  213. timePickerContent()
  214. }
  215. </Modal>
  216. }
  217. function timePickerContent() {
  218. var title, color;
  219. switch (selItem.scenario) {
  220. case 'FAST':
  221. title = operateType == 'endFast' ? '结束断食' : '开始断食'
  222. color = MainColorType.fast
  223. break;
  224. case 'SLEEP':
  225. title = operateType == 'endFast' ? '结束睡眠' : '开始睡眠'
  226. color = MainColorType.sleep
  227. }
  228. var endTimestamp = 0
  229. if (operateType == 'endFast') {
  230. endTimestamp = new Date().getTime()//fastData.target.end_time
  231. }
  232. var duration = 24 * 3600 * 1000//fastData.target.duration
  233. return <View className="modal_content">
  234. <ConsolePicker ref={limitPickerRef}
  235. themeColor={color}
  236. title={title}
  237. onCancel={() => {
  238. setShowTimePicker(false)
  239. }}
  240. min={min}
  241. max={max}
  242. current={defaultTimestamp}
  243. duration={duration}
  244. endTimestamp={endTimestamp}
  245. isFast={true}
  246. isEnd={operateType == 'endFast'}
  247. isTimeout={false}
  248. isLoading={btnDisable}
  249. onChange={(e) => {
  250. pickerConfirm(e, null)
  251. global.pauseIndexTimer = false
  252. }}
  253. />
  254. </View>
  255. }
  256. function pickerConfirm(t1: number, event: any) {
  257. if (btnDisable) {
  258. return
  259. }
  260. global.scenario = 'FAST'
  261. setBtnDisable(true)
  262. clockTimes({
  263. check_items: [{
  264. schedule_id: selItem.schedule_id,
  265. date: dayjs(t1).format('YYYYMMDD'),
  266. timestamp: t1
  267. }]
  268. }).then(res => {
  269. setBtnDisable(false)
  270. setShowTimePicker(false)
  271. global.refreshWindow()
  272. }).catch(e => {
  273. setBtnDisable(false)
  274. })
  275. }
  276. function more() {
  277. showActionSheet({
  278. showActionSheetWithOptions: showActionSheetWithOptions,
  279. title: 'Oprate Title',
  280. itemList: [
  281. 'Add Snack',
  282. '自定义餐次列表',
  283. ],
  284. success: (res) => {
  285. switch (res) {
  286. case 0:
  287. break;
  288. case 1:
  289. jumpPage('/_eat/pages/meal_list')
  290. break;
  291. }
  292. }
  293. });
  294. }
  295. function getThemeColor() {
  296. switch (health.mode) {
  297. case 'DAY':
  298. return MainColorType.day;
  299. case 'NIGHT':
  300. return MainColorType.night;
  301. case 'FAST':
  302. return MainColorType.fast;
  303. case 'EAT':
  304. return MainColorType.eat;
  305. case 'SLEEP':
  306. return MainColorType.sleep;
  307. case 'ACTIVE':
  308. return MainColorType.active;
  309. }
  310. }
  311. function detail() {
  312. const { day, night } = health.windows.night_day
  313. const { fast, eat } = health.windows.fast_eat
  314. const { sleep, active } = health.windows.sleep_active
  315. let list: any = []
  316. switch (health.mode) {
  317. case 'DAY':
  318. list = day.timeline
  319. break;
  320. case 'NIGHT':
  321. list = night.timeline
  322. break;
  323. case 'FAST':
  324. list = fast.timeline
  325. break;
  326. case 'EAT':
  327. list = eat.timeline
  328. break;
  329. case 'SLEEP':
  330. list = sleep.timeline
  331. break;
  332. case 'ACTIVE':
  333. list = active.timeline
  334. break;
  335. }
  336. return <View>
  337. {
  338. list.map((item, index) => {
  339. return timelineItem(item, index, list.length)
  340. })
  341. }
  342. </View>
  343. }
  344. function switchText() {
  345. switch (health.mode) {
  346. case 'FAST':
  347. return 'Switch to Eat'
  348. case 'EAT':
  349. return 'Switch to Fast'
  350. case 'DAY':
  351. return 'Switch to Night'
  352. case 'NIGHT':
  353. return 'Switch to Daylight'
  354. case 'SLEEP':
  355. return 'Switch to Activity'
  356. case 'ACTIVE':
  357. return 'Switch to Sleep'
  358. }
  359. return ''
  360. }
  361. function tapSwitchBtn() {
  362. switch (health.mode) {
  363. case 'FAST':
  364. dispatch(setMode('EAT'));
  365. break
  366. case 'EAT':
  367. dispatch(setMode('FAST'));
  368. break
  369. case 'DAY':
  370. dispatch(setMode('NIGHT'));
  371. break
  372. case 'NIGHT':
  373. dispatch(setMode('DAY'));
  374. break
  375. case 'SLEEP':
  376. dispatch(setMode('ACTIVE'));
  377. break
  378. case 'ACTIVE':
  379. dispatch(setMode('SLEEP'));
  380. break
  381. }
  382. }
  383. return <View className="main-console-bg">
  384. <Image className="main_arrow" src={require('@assets/images/center_arrow.png')} />
  385. <View className="main_summary">
  386. <View className="main_summary_status" style={{ color: getThemeColor() }}>{getWindowStatus(health.windows, health.mode)}</View>
  387. <Text className="main_summary_time">{getCountownTime(health.windows, health.mode)}</Text>
  388. <Text className="main_summary_duration">Total {getDuration(health.windows, health.mode)}</Text>
  389. <View className="border_footer_line" />
  390. </View>
  391. <View style={{ backgroundColor: '#fff', width: rpxToPx(750) }}>
  392. {
  393. detail()
  394. }
  395. </View>
  396. <View className="main_footer">
  397. <Text className="main_footer_text" onClick={tapSwitchBtn}>{switchText()}</Text>
  398. </View>
  399. {
  400. (health.mode == 'EAT' || health.mode == 'ACTIVE') && <Text style={{ display: 'flex', alignItems: 'center', justifyContent: 'center' }} onClick={more}>更多</Text>
  401. }
  402. {
  403. showTimePicker && modalContent()
  404. }
  405. {
  406. showPicker && timeContent()
  407. }
  408. </View>
  409. }