Metric.tsx 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. import { View, Text, ScrollView } from "@tarojs/components";
  2. import './Metric.scss'
  3. import { setAuth } from "../hooks/werun";
  4. import { usePullDownRefresh, useReady } from "@tarojs/taro";
  5. import { useSelector } from "react-redux";
  6. import { useEffect, useRef, useState } from "react";
  7. import Taro from "@tarojs/taro";
  8. import { metricCards, uploadMetric, uploadSteps } from "@/services/trackSomething";
  9. import { TimeFormatter } from "@/utils/time_format";
  10. import MetricItem from "./MetricItem";
  11. import { get } from "http";
  12. import Modal from "@/components/layout/Modal";
  13. import PickerViews from "@/components/input/PickerViews";
  14. import LimitPickers from "@/components/input/LimitPickers";
  15. import SlidngScale from "@/components/input/SlidngScale";
  16. import NoData from "@/components/view/NoData";
  17. import { alphaToHex } from "@/utils/tools";
  18. import Layout from "@/components/layout/layout";
  19. import { NaviBarTitleShowType, TemplateType } from "@/utils/types";
  20. import { useTranslation } from "react-i18next";
  21. export default function Component(props: any) {
  22. const { t } = useTranslation()
  23. const user = useSelector((state: any) => state.user);
  24. const [list, setList] = useState([])
  25. const [isModalOpen, setIsModalOpen] = useState(false);
  26. const [isTimePickerOpen, setIsTimePickerOpen] = useState(false);
  27. const [pickerValue, setPickerValue] = useState([]);
  28. const [pickerItems, setPickerItems] = useState([]);
  29. const [isPoint, setIsPoint] = useState(false)
  30. const [metricItem, setMetricItem] = useState({})
  31. const [strTime, setStrTime] = useState('')
  32. const [time, setTime] = useState(0)
  33. const [showErrorPage, setErrorPage] = useState(false)
  34. const [setupTime, setSetupTime] = useState(0)
  35. const [triggered, setTriggered] = useState(true)
  36. const limitPickerRef = useRef<any>(null);
  37. //未登录<->已登录 状态切换时,执行一次授权检查
  38. useEffect(() => {
  39. getCards();
  40. }, [user.isLogin])
  41. const openModal = () => {
  42. setSetupTime(new Date().getTime())
  43. setIsModalOpen(true);
  44. };
  45. const closeModal = () => {
  46. setIsModalOpen(false);
  47. };
  48. global.refreshMetric = () => {
  49. getCards()
  50. }
  51. function getCards() {
  52. setTriggered(true)
  53. console.log('get Cards')
  54. metricCards().then(res => {
  55. setErrorPage(false)
  56. setList((res as any).cards)
  57. setTriggered(false)
  58. }).catch(e => {
  59. if (list.length == 0) {
  60. setErrorPage(true)
  61. }
  62. setTriggered(false)
  63. // console.log('请求超时')
  64. })
  65. }
  66. //ts 把数组items: [{code: "_walk", value: 2180},{code: "_walk", value: 4444}]中的value取出来,/分割,组成字符串,如2180/4444
  67. function getValues(items) {
  68. var values = ''
  69. items.map((item, index) => {
  70. if (index == 0) {
  71. values = item.value
  72. }
  73. else {
  74. values = values + '/' + item.value
  75. }
  76. })
  77. return values
  78. }
  79. function goDetail(item) {
  80. if (user.isLogin) {
  81. if (!item.latest_record) {
  82. return
  83. }
  84. Taro.navigateTo({
  85. url: '/pages/common/RecordsHistory?type=metric&refreshList=getCards&code=' + item.code+`&title=${item.name}`
  86. })
  87. }
  88. else {
  89. Taro.navigateTo({
  90. url: '/pages/account/ChooseAuth'
  91. })
  92. }
  93. }
  94. function record(item: any) {
  95. if (user.isLogin) {
  96. var now = new Date();
  97. var t = (now.getHours() < 10 ? '0' + now.getHours() : now.getHours()) + ":" + (now.getMinutes() < 10 ? '0' + now.getMinutes() : now.getMinutes());
  98. setStrTime(t)
  99. setTime(now.getTime())
  100. setMetricItem(item)
  101. openModal()
  102. console.log(item)
  103. }
  104. else {
  105. Taro.navigateTo({
  106. url: '/pages/account/ChooseAuth'
  107. })
  108. }
  109. }
  110. function pointPicker(item: any) {
  111. var min = item.min
  112. var max = item.max
  113. var step = item.step
  114. var value = item.default_value
  115. console.log(value)
  116. var items: number[] = []
  117. var items2: number[] = []
  118. var value0Index = 0
  119. var value1Index = 0
  120. for (var i = min; i <= max; i++) {
  121. if (i == Math.floor(value)) {
  122. value0Index = i - min
  123. }
  124. items.push(i)
  125. // items2.push(i)
  126. }
  127. for (var i = 0; i <= 9; i++) {
  128. items2.push(i)
  129. }
  130. value1Index = Math.round(10 * (value - Math.floor(value)));
  131. console.log(value1Index)
  132. setPickerValue([value0Index, value1Index] as any)
  133. setPickerItems([items, items2] as any)
  134. setIsPoint(true)
  135. openModal()
  136. }
  137. function singlePicker(item: any) {
  138. var min = item.min
  139. var max = item.max
  140. var step = 1//item.step
  141. var value = item.default_value
  142. var items: number[] = []
  143. for (var i = min; i <= max; i += step) {
  144. if (i == value) {
  145. setPickerValue([i - min] as any)
  146. }
  147. items.push(i)
  148. }
  149. setPickerItems([items] as any)
  150. setIsPoint(false)
  151. openModal()
  152. }
  153. function pickerChanged(e) {
  154. closeModal();
  155. var params = {}
  156. var date = new Date(time)
  157. var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
  158. if ((metricItem as any).schemas[0].type == 'DECIMAL') {
  159. var array = pickerItems;
  160. var value = array[0][e[0]] + '.' + array[1][e[1]]
  161. console.log(value)
  162. params = {
  163. code: (metricItem as any).code,
  164. timestamp: time,
  165. date: strDate,
  166. items: [{ code: (metricItem as any).schemas[0].code, value: value }]
  167. }
  168. }
  169. else if ((metricItem as any).schemas.length > 1) {
  170. }
  171. else {
  172. var array = pickerItems;
  173. var value = array[0][e[0]] + ''
  174. console.log(value)
  175. params = {
  176. code: (metricItem as any).code,
  177. timestamp: time,
  178. date: strDate,
  179. items: [{ code: (metricItem as any).schemas[0].code, value: value }]
  180. }
  181. }
  182. uploadMetric(params).then(res => {
  183. list.map((item, index) => {
  184. if ((item as any).code == (res as any).code) {
  185. (item as any).latest_record = (res as any).latest_record
  186. }
  187. })
  188. debugger
  189. setList(list)
  190. // getCards();
  191. })
  192. }
  193. function showTimePicker() {
  194. setIsTimePickerOpen(true)
  195. }
  196. function chooseTime(e) {
  197. setTime(e);
  198. setStrTime(TimeFormatter.formatTimestamp(e))
  199. setIsTimePickerOpen(false)
  200. }
  201. function cancelModal() {
  202. (metricItem as any).schemas.map((item, index) => {
  203. item.tempValue = ''
  204. })
  205. closeModal()
  206. }
  207. function confirmModal() {
  208. closeModal()
  209. var date = new Date(time)
  210. var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
  211. var array: any[] = [];
  212. (metricItem as any).schemas.map((item, index) => {
  213. array.push({
  214. code: item.code,
  215. value: item.tempValue && item.tempValue.length > 0 ? item.tempValue : item.default_value
  216. })
  217. })
  218. var params = {
  219. code: (metricItem as any).code,
  220. timestamp: time,
  221. date: strDate,
  222. items: array,
  223. extra: {
  224. set_time: setupTime,
  225. confirm_time: new Date().getTime()
  226. }
  227. }
  228. uploadMetric(params).then(res => {
  229. // getCards();
  230. (metricItem as any).schemas.map((item, index) => {
  231. item.tempValue = ''
  232. })
  233. list.map((item, index) => {
  234. if ((item as any).code == (res as any).code) {
  235. (item as any).latest_record = (res as any).latest_record
  236. }
  237. })
  238. setList(JSON.parse(JSON.stringify(list)))
  239. })
  240. }
  241. const limit = new Date().getTime() - 180 * 3600 * 1000 * 24;
  242. function detail() {
  243. return <View className="metric_container">
  244. {
  245. list.map((item: any, index: number) => {
  246. var unit = ''
  247. var value = '无记录'
  248. var desc = '记录解锁趋势'
  249. if (item.latest_record) {
  250. unit = item.schemas[0].default_unit
  251. value = getValues(item.latest_record.items)
  252. desc = TimeFormatter.formatTimestamp(item.latest_record.timestamp)
  253. }
  254. return <MetricItem title={item.name}
  255. value={value}
  256. unit={unit}
  257. desc={desc}
  258. btnText='记录'
  259. isDisabled={false}
  260. themeColor={item.theme_color}
  261. onClickDetail={() => { goDetail(item) }}
  262. onClick={() => { record(item) }}
  263. />
  264. })
  265. }
  266. <View className="space_width" />
  267. </View>
  268. }
  269. // return showErrorPage ? <NoData refresh={() => { getCards() }} /> : detail()
  270. return <View style={{ display: 'flex', flex: 1, height: '100%' }}>
  271. <Layout children={showErrorPage ? <NoData refresh={() => { getCards() }} /> : detail()}
  272. title={t('page.metric.title')}
  273. type={TemplateType.grid}
  274. refresh={() => { getCards() }}
  275. triggered={triggered}
  276. more={() => { getCards() }}
  277. titleShowStyle={NaviBarTitleShowType.scrollToShow}
  278. />
  279. {
  280. isModalOpen && <Modal dismiss={closeModal} title={(metricItem as any).name}
  281. themeColor={(metricItem as any).theme_color}
  282. confirm={confirmModal}>
  283. <View style={{
  284. display: 'flex', flexDirection: 'column',
  285. width: '100%', color: '#000'
  286. }}>
  287. <Text className='modal_title' style={{ color: (metricItem as any).theme_color }}>{props.title ? props.title : '测试标题 '}</Text>
  288. <View style={{ position: 'relative' }}>
  289. {
  290. (metricItem as any).schemas.map((item, index) => {
  291. return <View key={index}>
  292. {
  293. (metricItem as any).schemas.length > 1 && <Text style={{
  294. textAlign: 'center', width: '100%', display: 'flex',
  295. justifyContent: 'center', color: (metricItem as any).theme_color
  296. }}>{item.name}</Text>
  297. }
  298. <SlidngScale step={item.step} min={item.min} max={item.max} default_value={item.default_value}
  299. unit={item.default_unit}
  300. themeColor={(metricItem as any).theme_color}
  301. changed={(e) => { item.tempValue = e }} />
  302. </View>
  303. })
  304. }
  305. </View>
  306. <View className="change_time_bg ">
  307. <View className="gray1 time_bg" onClick={showTimePicker}>
  308. <Text className="time" >{strTime}</Text>
  309. </View>
  310. </View>
  311. <View className='modal_operate'>
  312. <View className='modal_btn' style={{ backgroundColor: (metricItem as any).theme_color + alphaToHex(0.4) }} onClick={cancelModal}>
  313. <Text className='modal_cancel_text' style={{ color: (metricItem as any).theme_color }}>取消</Text>
  314. </View>
  315. <View className='btn_space' />
  316. <View className='modal_btn' style={{ backgroundColor: (metricItem as any).theme_color }} onClick={confirmModal}>
  317. <Text className='modal_confirm_text' style={{ color: '#000' }}>确定</Text>
  318. </View>
  319. </View>
  320. {/* <View style={{ marginBottom: 20, marginTop: 20, display: 'flex', flexDirection: 'row', width: '100%' }}>
  321. <Text style={{ flex: 1, textAlign: 'center', height: 50 }} onClick={cancelModal}>取消</Text>
  322. <Text style={{ flex: 1, textAlign: 'center', height: 50 }} onClick={confirmModal}>确认</Text>
  323. </View> */}
  324. </View>
  325. </Modal>
  326. }
  327. {
  328. isTimePickerOpen && <Modal
  329. themeColor={(metricItem as any).theme_color}
  330. dismiss={() => setIsTimePickerOpen(false)} confirm={() => {
  331. var picker = limitPickerRef.current;
  332. chooseTime((picker as any).getConfirmData());
  333. setIsTimePickerOpen(false);
  334. }}>
  335. <LimitPickers ref={limitPickerRef} isRealTime={true} time={time} limit={limit} limitDay={180} onCancel={() => { setIsTimePickerOpen(false) }} onChange={(e) => {
  336. chooseTime(e)
  337. // pickerConfirm(e)
  338. // hidePicker()
  339. }} />
  340. </Modal>
  341. }
  342. </View>
  343. }