ChooseAuth.tsx 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. import { View, Text } from "@tarojs/components";
  2. import Buttons from '@/components/basic/Buttons'
  3. import Texts from '@/components/basic/Texts'
  4. import './ChooseAuth.scss'
  5. import { ButtonType, ComponentStatus, TextType } from "../../utils/types";
  6. import Taro, { useReady } from "@tarojs/taro";
  7. import { useDispatch, useSelector } from "react-redux";
  8. import { useEffect, useState } from "react";
  9. import { wxLogin } from "@/services/user";
  10. import { ColorType } from "@/context/themes/color";
  11. import { ChooseScenarioBtn } from "@/features/common/SpecBtns";
  12. import { useTranslation } from "react-i18next";
  13. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  14. import { loginSuccess } from "@/store/user";
  15. import RequestType, { thirdPartRequest } from "@/services/thirdPartRequest";
  16. let useNavigation;
  17. if (process.env.TARO_ENV == 'rn') {
  18. useNavigation = require("@react-navigation/native").useNavigation
  19. }
  20. export default function Page() {
  21. const [btnDisable, setBtnDisable] = useState(false)
  22. const [code, setCode] = useState('')
  23. const dispatch = useDispatch();
  24. const { t } = useTranslation()
  25. const user = useSelector((state: any) => state.user);
  26. let navigation;
  27. if (useNavigation) {
  28. navigation = useNavigation()
  29. }
  30. useEffect(() => {
  31. if (navigation) {
  32. navigation.setOptions({
  33. headerTitle: '',
  34. });
  35. }
  36. if (user.isLogin) {
  37. if (user.is_new_user) {
  38. console.log('new user')
  39. if (user.scenario_select_count == 0) {
  40. console.log('new')
  41. Taro.redirectTo({
  42. url: '/pages/clock/ChooseScenario?trigger_event=SETUP_UPON_ACCOUNT_CREATION'
  43. })
  44. setTimeout(() => {
  45. Taro.navigateTo({
  46. url: '/pages/account/ProfileSetting?newuser=1'
  47. })
  48. }, 100)
  49. }
  50. else {
  51. console.log('replace')
  52. Taro.redirectTo({
  53. url: '/pages/account/ProfileSetting?newuser=1'
  54. })
  55. }
  56. }
  57. else {
  58. console.log('old user')
  59. // if (user.scenario_select_count > 0) {
  60. // Taro.navigateBack({
  61. // delta: 1
  62. // })
  63. // }
  64. // else {
  65. // Taro.redirectTo({
  66. // url: '/pages/clock/ChooseScenario'
  67. // })
  68. // }
  69. }
  70. }
  71. }, [user.isLogin])
  72. useReady(() => {
  73. // getCode()
  74. })
  75. function getCode() {
  76. setBtnDisable(true)
  77. if (process.env.TARO_ENV === 'weapp') {
  78. thirdPartRequest(RequestType.RequestTypeWXLogin).then(res => {
  79. login((res as any).code)
  80. }).catch(err => {
  81. Taro.showToast({
  82. title: '登录失败,请稍等重试',
  83. icon: 'none'
  84. })
  85. setBtnDisable(false)
  86. })
  87. // Taro.login().then(res => {
  88. // setCode(res.code)
  89. // login(res.code)
  90. // // setBtnDisable(false)
  91. // }).catch(e => {
  92. // console.log(e)
  93. // // getCode()
  94. // setBtnDisable(false)
  95. // })
  96. }
  97. }
  98. async function login(strCode) {
  99. setBtnDisable(true)
  100. wxLogin(strCode).then(res => {
  101. console.log(res)
  102. dispatch(loginSuccess(res));
  103. setBtnDisable(false)
  104. if (!(res as any).is_new_user){
  105. Taro.navigateBack({
  106. delta: 1
  107. })
  108. }
  109. // if (!(res as any).is_new_user){
  110. // if ((res as any).scenario_select_count > 0) {
  111. // console.log('choose auth navi back')
  112. // Taro.navigateBack({
  113. // delta: 1
  114. // })
  115. // }
  116. // else {
  117. // debugger
  118. // Taro.redirectTo({
  119. // url: '/pages/clock/ChooseScenario?trigger_event=SETUP_UPON_ACCOUNT_CREATION'
  120. // })
  121. // }
  122. // }
  123. }).catch(e => {
  124. setBtnDisable(false)
  125. })
  126. // dispatch(wxLogin(code) as any);
  127. // try {
  128. // const { encryptedData, iv } = await Taro.getUserProfile({
  129. // desc: '获取用户信息',
  130. // });
  131. // dispatch(wxLogin(code, encryptedData, iv) as any);
  132. // // 在这里处理用户信息
  133. // } catch (error) {
  134. // console.log('获取用户信息失败:', error);
  135. // }
  136. }
  137. function createAccount1() {
  138. jumpPage('/pages/account/Auth', 'Auth', navigation)
  139. }
  140. function login1() {
  141. jumpPage('/pages/account/Auth', 'Auth', navigation, { isLogin: true })
  142. }
  143. return <View className="container choose_container">
  144. <Texts text='fast16cc' type={TextType.big} />
  145. <Text className="slogan">{t('page.choose_auth.slogan')}</Text>
  146. {
  147. process.env.TARO_ENV == 'weapp' ? <ChooseScenarioBtn disable={btnDisable} title={t('page.choose_auth.btn_wechat')} onClick={getCode} background={ColorType.fast} /> :
  148. <ChooseScenarioBtn disable={btnDisable} title={t('page.choose_auth.btn_login')} onClick={login1} background={ColorType.fast} />
  149. // <Text style={{color:'red'}} onClick={createAccount1}>{t('page.choose_auth.btn_signup')}</Text>
  150. }
  151. <View style={{ height: 30 }} />
  152. {
  153. process.env.TARO_ENV == 'rn' && <ChooseScenarioBtn disable={btnDisable} title={t('page.choose_auth.btn_signup')} onClick={createAccount1} background={ColorType.fast} />
  154. }
  155. {/* <ChooseScenarioBtn title={t('page.choose_auth.btn_wechat')} onClick={login} background={ColorType.fast} /> */}
  156. {/* <View style={{ height: 100 }} /> */}
  157. {/* <Buttons title='微信登录' onClick={login} btnStyle={{ width: 289, marginBottom: 30,backgroundColor:ColorType.fast }} /> */}
  158. {/* <Buttons title='Create account' type={ButtonType.outline} onClick={createAccount} btnStyle={{ width: 289, marginBottom: 30 }} /> */}
  159. </View>;
  160. }