ChooseAuth.tsx 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. import { View, Text, Image } 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, MainColorType } 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. import NewButton, { NewButtonType } from "@/_health/base/new_button";
  17. import { rpxToPx } from "@/utils/tools";
  18. let useNavigation;
  19. if (process.env.TARO_ENV == 'rn') {
  20. useNavigation = require("@react-navigation/native").useNavigation
  21. }
  22. export default function Page() {
  23. const [btnDisable, setBtnDisable] = useState(false)
  24. const [code, setCode] = useState('')
  25. const dispatch = useDispatch();
  26. const { t } = useTranslation()
  27. const user = useSelector((state: any) => state.user);
  28. let navigation;
  29. if (useNavigation) {
  30. navigation = useNavigation()
  31. }
  32. useEffect(() => {
  33. if (navigation) {
  34. navigation.setOptions({
  35. headerTitle: '',
  36. });
  37. }
  38. if (user.isLogin) {
  39. if (user.is_new_user) {
  40. console.log('new user')
  41. if (user.scenario_select_count == 0) {
  42. console.log('new')
  43. // Taro.redirectTo({
  44. // url: '/pages/clock/ChooseScenario?trigger_event=SETUP_UPON_ACCOUNT_CREATION'
  45. // })
  46. setTimeout(() => {
  47. Taro.redirectTo({
  48. url: '/_account/pages/ProfileSetting?newuser=1'
  49. })
  50. }, 100)
  51. }
  52. else {
  53. console.log('replace')
  54. Taro.redirectTo({
  55. url: '/_account/pages/ProfileSetting?newuser=1'
  56. })
  57. }
  58. }
  59. else {
  60. console.log('old user')
  61. // if (user.scenario_select_count > 0) {
  62. // Taro.navigateBack({
  63. // delta: 1
  64. // })
  65. // }
  66. // else {
  67. // Taro.redirectTo({
  68. // url: '/pages/clock/ChooseScenario'
  69. // })
  70. // }
  71. }
  72. }
  73. }, [user.isLogin])
  74. useReady(() => {
  75. // getCode()
  76. })
  77. function getCode() {
  78. setBtnDisable(true)
  79. if (process.env.TARO_ENV === 'weapp') {
  80. thirdPartRequest(RequestType.RequestTypeWXLogin).then(res => {
  81. login((res as any).code)
  82. }).catch(err => {
  83. Taro.showToast({
  84. title: '登录失败,请稍等重试',
  85. icon: 'none'
  86. })
  87. setBtnDisable(false)
  88. })
  89. // Taro.login().then(res => {
  90. // setCode(res.code)
  91. // login(res.code)
  92. // // setBtnDisable(false)
  93. // }).catch(e => {
  94. // console.log(e)
  95. // // getCode()
  96. // setBtnDisable(false)
  97. // })
  98. }
  99. }
  100. async function login(strCode) {
  101. setBtnDisable(true)
  102. var params: any = {}
  103. var share_uid = Taro.getStorageSync('share_uid')
  104. var share_info = Taro.getStorageSync('share_info')
  105. if (share_uid) {
  106. params.follow_origin = share_info ? 'WECHAT_GROUP_CHAT' : 'WECHAT_PRIVATE_CHAT'
  107. if (share_info) {
  108. var groupInfo = JSON.parse(share_info)
  109. params.wechat = groupInfo
  110. // {
  111. // encryptedData: groupInfo.encryptedData,
  112. // iv: groupInfo.iv
  113. // }
  114. }
  115. }
  116. console.log(share_uid, share_info, params)
  117. wxLogin(strCode, params).then(res => {
  118. console.log(res)
  119. dispatch(loginSuccess(res));
  120. setBtnDisable(false)
  121. if (!(res as any).is_new_user) {
  122. Taro.navigateBack({
  123. delta: 1
  124. })
  125. } else {
  126. Taro.setStorage({ key: '149alert', data: true })
  127. }
  128. // if (!(res as any).is_new_user){
  129. // if ((res as any).scenario_select_count > 0) {
  130. // console.log('choose auth navi back')
  131. // Taro.navigateBack({
  132. // delta: 1
  133. // })
  134. // }
  135. // else {
  136. // debugger
  137. // Taro.redirectTo({
  138. // url: '/pages/clock/ChooseScenario?trigger_event=SETUP_UPON_ACCOUNT_CREATION'
  139. // })
  140. // }
  141. // }
  142. }).catch(e => {
  143. setBtnDisable(false)
  144. })
  145. // dispatch(wxLogin(code) as any);
  146. // try {
  147. // const { encryptedData, iv } = await Taro.getUserProfile({
  148. // desc: '获取用户信息',
  149. // });
  150. // dispatch(wxLogin(code, encryptedData, iv) as any);
  151. // // 在这里处理用户信息
  152. // } catch (error) {
  153. // console.log('获取用户信息失败:', error);
  154. // }
  155. }
  156. function createAccount1() {
  157. jumpPage('/_account/pages/Auth', 'Auth', navigation)
  158. }
  159. function login1() {
  160. jumpPage('/_account/pages/Auth', 'Auth', navigation, { isLogin: true })
  161. }
  162. return <View className="container choose_container">
  163. <Image src={require('@assets/_health/main_logo.png')} style={{ width: rpxToPx(160), height: rpxToPx(160) }} />
  164. <View className="h50 bold" style={{ marginTop: rpxToPx(24), marginBottom: rpxToPx(12) }}>fast16cc</View>
  165. <Text className="h30" style={{ marginBottom: rpxToPx(678) }}>{t('page.choose_auth.slogan')}</Text>
  166. <View className="main_footer">
  167. {
  168. process.env.TARO_ENV == 'weapp' ? <NewButton
  169. type={NewButtonType.fill}
  170. color={MainColorType.success}
  171. title={t('page.choose_auth.btn_wechat')}
  172. width={rpxToPx(646)}
  173. height={rpxToPx(96)}
  174. onClick={getCode}
  175. /> :
  176. <NewButton
  177. type={NewButtonType.fill}
  178. color={MainColorType.success}
  179. title={t('page.choose_auth.btn_login')}
  180. width={rpxToPx(646)}
  181. height={rpxToPx(96)}
  182. onClick={login1}
  183. />
  184. }
  185. {
  186. process.env.TARO_ENV == 'rn' && <View style={{ height: 30 }} />
  187. }
  188. {
  189. process.env.TARO_ENV == 'rn' && <NewButton
  190. type={NewButtonType.fill}
  191. color={MainColorType.success}
  192. title={t('page.choose_auth.btn_signup')}
  193. width={rpxToPx(646)}
  194. height={rpxToPx(96)}
  195. onClick={createAccount1}
  196. />
  197. // <ChooseScenarioBtn disable={btnDisable} title={t('page.choose_auth.btn_signup')} onClick={createAccount1} background={ColorType.fast} />
  198. }
  199. </View>
  200. </View>;
  201. }