move.tsx 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import { View, Text } from "@tarojs/components";
  2. import './move.scss'
  3. import { useDispatch, useSelector } from "react-redux";
  4. import { getScenario, getThemeColor } from "@/features/health/hooks/health_hooks";
  5. import { useEffect, useState } from "react";
  6. import Taro from "@tarojs/taro";
  7. import { checkStart, setResult } from "@/store/action_results";
  8. import RequestType, { thirdPartRequest } from "@/services/thirdPartRequest";
  9. import { setAuth } from "@/features/trackSomething/hooks/werun";
  10. import { useTranslation } from "react-i18next";
  11. import { uploadActiveMoves } from "@/services/health";
  12. import dayjs from "dayjs";
  13. export default function Move() {
  14. const health = useSelector((state: any) => state.health);
  15. const [allowRun, setAllowRun] = useState(false)
  16. const { t } = useTranslation()
  17. const dispatch = useDispatch()
  18. useEffect(() => {
  19. Taro.getSetting({
  20. success: res => {
  21. //第一步,检测是否有授权 - 没有授权
  22. if (!res.authSetting['scope.werun']) {
  23. setAllowRun(false)
  24. Taro.setStorage({ key: 'auth', data: false })
  25. }
  26. else {
  27. setAllowRun(true)
  28. Taro.setStorage({ key: 'auth', data: true })
  29. }
  30. }
  31. })
  32. }, [])
  33. function tapLog() {
  34. if (allowRun) {
  35. checkout()
  36. }
  37. else {
  38. setAuth(successAuth, refuseAuth, t)
  39. }
  40. }
  41. function successAuth() {
  42. Taro.setStorage({ key: 'auth', data: true })
  43. setAllowRun(true)
  44. }
  45. function refuseAuth() {
  46. // setTitle('开启');
  47. Taro.setStorage({ key: 'auth', data: false })
  48. setAllowRun(false)
  49. }
  50. function checkout() {
  51. dispatch(checkStart());
  52. getWeRunData(false)
  53. }
  54. function getWeRunData(autoCheck = false) {
  55. if (autoCheck) {
  56. return
  57. }
  58. else {
  59. dispatch(checkStart());
  60. }
  61. // setTitle('打卡');
  62. setAllowRun(true)
  63. var date = new Date();
  64. var time = date.getTime()
  65. var strDate = (date.getFullYear() + '') + (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : (date.getMonth() + 1)) + (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
  66. thirdPartRequest(RequestType.RequestTypeWXRunData).then(res => {
  67. var params = {
  68. is_manual: autoCheck ? 0 : 1,
  69. timestamp: time,
  70. encryptedData: (res as any).encryptedData,
  71. iv: (res as any).iv,
  72. date: strDate,
  73. cloudID: (res as any).cloudID,
  74. }
  75. uploadActiveMoves({
  76. wechat_run:{
  77. encryptedData:(res as any).encryptedData,
  78. iv:(res as any).iv,
  79. },
  80. date:dayjs().format('YYYYMMDD'),
  81. timestamp:new Date().getTime(),
  82. schedule_id:getScenario(health.windows,health.mode).window_id
  83. }).then(res=>{
  84. Taro.showToast({
  85. title:'上报成功',
  86. icon:'none'
  87. })
  88. })
  89. // uploadSteps(params).then(res => {
  90. // if ((res as any).error_code == 'WX_STEP_PARSE_FAIL') {
  91. // retry(params, autoCheck, time)
  92. // dispatch(checkRetry())
  93. // }
  94. // else {
  95. // setShowErrorBadge(false)
  96. // uploadSuccess(res, autoCheck, time)
  97. // setTimeout(() => {
  98. // setCount(count + 1)
  99. // }, 31000)
  100. // }
  101. // }).catch(e => {
  102. // retry(params, autoCheck, time)
  103. // });
  104. }).catch(_ => {
  105. dispatch(setResult({ isSuccess: false }) as any)
  106. })
  107. }
  108. return <View>
  109. <Text>Move Every Hour</Text>
  110. <View className="log_btn" style={{ backgroundColor: getThemeColor(health.mode) }} onClick={tapLog}>
  111. Log
  112. </View>
  113. </View>
  114. }