Activity.tsx 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { View, Text } from "@tarojs/components";
  2. import './Metric.scss'
  3. import { setAuth } from "../hooks/werun";
  4. import { useReady } from "@tarojs/taro";
  5. import { useDispatch, useSelector } from "react-redux";
  6. import { useEffect, useState } from "react";
  7. import Taro from "@tarojs/taro";
  8. import { activityCards, uploadSteps } from "@/services/trackSomething";
  9. import { TimeFormatter } from "@/utils/time_format";
  10. import MetricItem from "./MetricItem";
  11. import { checkFail, checkStart, checkSuccess, setResult } from "@/store/action_results";
  12. export default function Component(props: any) {
  13. const user = useSelector((state: any) => state.user);
  14. const checkResult = useSelector((state: any) => state.checkResult);
  15. const [allowRun, setAllowRun] = useState(false)
  16. const [stepInfo, setStepInfo] = useState(null)
  17. const [lastTime, setLastTime] = useState(new Date().getTime())
  18. const [isCheking, setIsChecking] = useState(false)
  19. const [strBtnTitle, setStrBtnTitle] = useState('')
  20. const [list, setList] = useState([])
  21. const dispatch = useDispatch();
  22. // const [title, setTitle] = useState('打卡')
  23. //未登录<->已登录 状态切换时,执行一次授权检查
  24. useEffect(() => {
  25. checkAuth()
  26. }, [user.isLogin])
  27. useEffect(() => {
  28. console.log('checkResult.type:' + checkResult.type)
  29. }, [
  30. checkResult.type
  31. ])
  32. //页面渲染完成后执行一次授权检查
  33. useReady(() => {
  34. getCards();
  35. })
  36. function getCards() {
  37. activityCards().then(res => {
  38. setList((res as any).cards)
  39. })
  40. }
  41. function checkAuth() {
  42. // Taro.checkSession
  43. if (user.isLogin) {
  44. // setAuth()
  45. Taro.getSetting({
  46. success: res => {
  47. //第一步,检测是否有授权 - 没有授权
  48. if (!res.authSetting['scope.werun']) {
  49. // setTitle('开启')
  50. setAllowRun(false)
  51. }
  52. else {
  53. setAllowRun(true)
  54. // setTitle('打卡')
  55. //自动打卡流程
  56. var time = Taro.getStorageSync('lastUploadStepsTime')
  57. if (time) {
  58. var now = new Date().getTime()
  59. if (now - time >= 30 * 24 * 3600 * 1000) {
  60. getWeRunData(true)
  61. }
  62. }
  63. else {
  64. getWeRunData(true)
  65. }
  66. }
  67. }
  68. })
  69. }
  70. else {
  71. // Taro.setStorageSync('lastUploadStepsTime', time)
  72. setAllowRun(false)
  73. // setTitle('开启');
  74. }
  75. }
  76. function checkout() {
  77. // console.log('开始获取步数的时间戳:' + new Date().getTime())
  78. if (allowRun) {
  79. setIsChecking(true)
  80. dispatch(checkStart());
  81. }
  82. if (user.isLogin) {
  83. setAuth(getWeRunData, refuseAuth)
  84. }
  85. else {
  86. Taro.navigateTo({
  87. url: '/pages/ChooseAuth'
  88. })
  89. }
  90. }
  91. function getWeRunData(autoCheck = false) {
  92. // setTitle('打卡');
  93. setAllowRun(true)
  94. var date = new Date();
  95. var time = date.getTime()
  96. var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
  97. Taro.getWeRunData({
  98. success: res => {
  99. // console.log('已获取步数的时间戳:' + new Date().getTime())
  100. uploadSteps({
  101. is_manual: autoCheck ? 0 : 1,
  102. timestamp: time,
  103. encryptedData: res.encryptedData,
  104. iv: res.iv,
  105. date: strDate,
  106. cloudID: res.cloudID,
  107. }).then(res => {
  108. Taro.setStorageSync('lastUploadStepsTime', time)
  109. dispatch(setResult({ isSuccess: true }) as any)
  110. setStepInfo((res as any)[(res as any).length - 1])
  111. setLastTime(time)
  112. setIsChecking(false)
  113. getCards();
  114. }).catch(e => {
  115. dispatch(setResult({ isSuccess: false }) as any);
  116. });
  117. }
  118. })
  119. }
  120. function refuseAuth() {
  121. // setTitle('开启');
  122. setIsChecking(false)
  123. setAllowRun(false)
  124. }
  125. function goDetail() {
  126. if (user.isLogin) {
  127. Taro.navigateTo({
  128. url: '/pages/RecordsHistory?type=activity'
  129. })
  130. }
  131. else {
  132. Taro.navigateTo({
  133. url: '/pages/ChooseAuth'
  134. })
  135. }
  136. }
  137. // checkResult.type == 'ing' && setStrBtnTitle('打卡中...')
  138. var btnTitle = '';
  139. var isEnable = true;
  140. if (allowRun) {
  141. if (checkResult.type == 'idle') {
  142. btnTitle = '打卡'
  143. isEnable = true
  144. }
  145. else if (checkResult.type == 'ing') {
  146. btnTitle = '打卡中...'
  147. isEnable = false;
  148. }
  149. else if (checkResult.type == 'success') {
  150. btnTitle = '打卡成功'
  151. isEnable = false
  152. }
  153. else if (checkResult.type == 'fail') {
  154. btnTitle = '打卡失败'
  155. isEnable = false
  156. }
  157. else if (checkResult.type == 'countdown') {
  158. btnTitle = checkResult.title + 's'
  159. isEnable = false;
  160. }
  161. }
  162. else {
  163. btnTitle = '开启';
  164. isEnable = true;
  165. }
  166. return <View>
  167. {
  168. list.map((item: any, index) => {
  169. var value = '0'
  170. var desc = ''
  171. if (item.latest_record) {
  172. value = item.latest_record.items[0].value
  173. if (item.latest_record.timestamp == 0) {
  174. desc = '今天待打卡'
  175. }
  176. else {
  177. desc = TimeFormatter.formatTimestamp(item.latest_record.timestamp)
  178. }
  179. }
  180. else {
  181. desc = '今天待打卡'
  182. }
  183. if (!allowRun) {
  184. value = '未开启'
  185. desc = '开启步数仅自己可见'
  186. }
  187. return <MetricItem title={item.name}
  188. // value={allowRun ? stepInfo ? (stepInfo as any).step : '' : '未开启'}
  189. value={value}
  190. unit={allowRun ? '步' : ''}
  191. desc={desc}
  192. btnText={btnTitle}
  193. isDisabled={!isEnable}
  194. // desc={allowRun ? stepInfo ? TimeFormatter.formatTimestamp(lastTime) : '' : '开启步数仅自己可见'}
  195. // btnText={allowRun ? isCheking ? '打卡中...' : '打卡' : '开启'}
  196. // isDisabled={isCheking}
  197. themeColor={item.theme_color}
  198. onClickDetail={goDetail}
  199. onClick={checkout}
  200. />
  201. })
  202. }
  203. </View>
  204. return <View className="metric_bg">
  205. <Text className="metric_title">行走</Text>
  206. {
  207. !allowRun && <Text className="metric_value">未开启</Text>
  208. }
  209. {
  210. !allowRun && <Text className="mteric_desc">开启步数仅自己可见</Text>
  211. }
  212. {
  213. allowRun && stepInfo && <Text className="metric_value">{(stepInfo as any).step}<Text className="metric_unit">步</Text></Text>
  214. }
  215. {
  216. allowRun && stepInfo && <Text className="mteric_desc">{TimeFormatter.formatTimestamp(lastTime)}</Text>
  217. }
  218. <View className="operate" onClick={checkout}>{allowRun ? '打卡' : '开启'}</View>
  219. </View>
  220. }