login.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import Inputs from "@/components/input/Inputs";
  2. import { View, Text, Image } from "@tarojs/components";
  3. import { useEffect, useState } from "react";
  4. import { useTranslation } from "react-i18next";
  5. import check from '@/assets/svg/check.svg'
  6. import Texts from "@/components/basic/Texts";
  7. import Box from "@/components/layout/Box";
  8. import { ComponentStatus, TextType } from "@/utils/types";
  9. import Buttons from "@/components/basic/Buttons";
  10. import './Auth.scss'
  11. import { clientId, login } from "@/services/user";
  12. import { useDispatch, useSelector } from "react-redux";
  13. import Taro from "@tarojs/taro";
  14. import { ColorType } from "@/context/themes/color";
  15. import { loginSuccess } from "@/store/user";
  16. import { IconCheck, IconRadio, IconRadioCheck } from "@/components/basic/Icons";
  17. import { jumpPage } from "@/features/trackTimeDuration/hooks/Common";
  18. let useNavigation;
  19. if (process.env.TARO_ENV == 'rn') {
  20. useNavigation = require("@react-navigation/native").useNavigation
  21. }
  22. export default function Login(props: { register: () => void }) {
  23. const { t } = useTranslation()
  24. const dispatch = useDispatch();
  25. const [name, setName] = useState('');
  26. const [password, setPassword] = useState('');
  27. const user = useSelector((state: any) => state.user);
  28. const common = useSelector((state: any) => state.common);
  29. const [checked, setChecked] = useState(true)
  30. let navigation;
  31. if (useNavigation) {
  32. navigation = useNavigation()
  33. }
  34. useEffect(() => {
  35. if (user.isLogin) {
  36. //上报用户登录完成
  37. if (process.env.TARO_ENV == 'rn') {
  38. navigation.pop(2)
  39. return
  40. }
  41. if (user.is_new_user) {
  42. Taro.navigateBack({
  43. delta: 1
  44. })
  45. debugger
  46. Taro.redirectTo({
  47. url: '/pages/clock/ChooseScenario?trigger_event=SETUP_UPON_ACCOUNT_CREATION'
  48. })
  49. }
  50. else {
  51. Taro.navigateBack({
  52. delta: 2
  53. })
  54. }
  55. // if (user.scenario_select_count > 0 || user.ongoing) {
  56. // Taro.navigateBack({
  57. // delta: 2
  58. // })
  59. // }
  60. // else {
  61. // Taro.navigateBack({
  62. // delta: 1
  63. // })
  64. // Taro.redirectTo({
  65. // url: '/pages/clock/ChooseScenario?trigger_event=SETUP_UPON_ACCOUNT_CREATION'
  66. // })
  67. // }
  68. }
  69. }, [user.isLogin])
  70. const handleNameChange = (value: string) => {
  71. setName(value);
  72. };
  73. const handlePasswordChange = (value: string) => {
  74. setPassword(value);
  75. }
  76. const isLoginButtonDisabled = name === '' || password === '';
  77. function loginComponent() {
  78. return <View>
  79. <Inputs autoFocus={true} value={name} onChange={handleNameChange} onConfirm={(e) => { }} placeholder={t('feature.auth.login.input_account_placeholder')}></Inputs>
  80. <View style={{ height: 20 }} />
  81. <Inputs value={password} isSecure={true} onChange={handlePasswordChange} onConfirm={(e) => { }} placeholder={t('feature.auth.login.input_password_placeholder')}></Inputs>
  82. </View>
  83. }
  84. function LoginF() {
  85. if (isLoginButtonDisabled) {
  86. return;
  87. }
  88. login(name, password).then(res => {
  89. dispatch(loginSuccess(res))
  90. clientId()
  91. // navigation.goBack(3)
  92. }).catch(e => {
  93. // alert(e.data.error_message)
  94. // alert(''+JSON.stringify(e))
  95. Taro.showToast({
  96. icon: 'none',
  97. title: e.error_message
  98. })
  99. })
  100. // dispatch(login(name, password) as any);
  101. }
  102. function tapCheck() {
  103. setChecked(!checked)
  104. }
  105. function agreement() {
  106. const resource = common.resources.filter((item: any) => {
  107. return item.code == 'user_agreement'
  108. })
  109. if (!resource || resource.length == 0) {
  110. Taro.showToast({
  111. title: 'Not found.',
  112. icon: 'none'
  113. })
  114. return
  115. }
  116. jumpPage('/pages/common/H5?title=User Agreement&url=' + resource[0].url, 'H5', navigation, {
  117. title: 'User Agreement',
  118. url: resource[0].url
  119. })
  120. }
  121. function privacy() {
  122. const resource = common.resources.filter((item: any) => {
  123. return item.code == 'privacy'
  124. })
  125. debugger
  126. if (!resource|| resource.length == 0) {
  127. Taro.showToast({
  128. title: 'Not found.',
  129. icon: 'none'
  130. })
  131. return
  132. }
  133. jumpPage('/pages/common/H5?title=Privacy Policy&url=' + resource[0].url, 'H5', navigation, {
  134. title: 'Privacy Policy',
  135. url: resource[0].url
  136. })
  137. }
  138. return <View style={{ width: '100%', height: '100%' }}>
  139. <Texts text={'fast16cc'} type={TextType.primary}></Texts>
  140. {
  141. loginComponent()
  142. }
  143. <Buttons
  144. onClick={LoginF}
  145. title={t('feature.auth.create_account.login')}
  146. btnStyle={{ marginLeft: 23, marginRight: 23, marginTop: 20, marginBottom: 20, height: 44, borderRadius: 22, backgroundColor: ColorType.fast }}
  147. disabled={isLoginButtonDisabled || !checked}></Buttons>
  148. <View className="agree_view">
  149. {/* <Image style='width:12px;height:12px;' src={check} /> */}
  150. <View onClick={tapCheck}>
  151. {
  152. checked ? <IconRadioCheck width={20} color={ColorType.fast} /> :
  153. <IconRadio width={20} color={ColorType.fast} />
  154. }
  155. </View>
  156. <View style={{ width: 10 }} />
  157. <Text className='agree_text' style={{ color: '#ffffff66' }}>I agree to <Text onClick={privacy} style={{ color: '#ffffff' }}> Privacy Policy.</Text></Text>
  158. </View>
  159. <View style={{ flex: 1 }} />
  160. <View className="footer">
  161. <Text className="have_account">{t('feature.auth.login.footer_desc')}</Text>
  162. <Text className="login2" onClick={() => props.register()}> {t('feature.auth.login.footer_sign_up')}</Text>
  163. </View>
  164. </View>
  165. return <View style={{ backgroundColor: '#000', flex: 1, flexDirection: 'column', display: 'flex', width: '100%', height: '100%' }}>
  166. <Texts text={'fast16cc'} type={TextType.primary}></Texts>
  167. {/* <Texts text={t('feature.auth.create_account.sub_title')} type={TextType.secondary}></Texts> */}
  168. <Box children={loginComponent()}></Box>
  169. {/* <Buttons
  170. onClick={LoginF}
  171. title={t('feature.auth.create_account.btn_next')}
  172. style={{ marginLeft: 23, marginRight: 23, marginTop: 20, marginBottom: 20 }}
  173. status={isLoginButtonDisabled ? ComponentStatus.disable : ComponentStatus.enable}></Buttons>
  174. <View className="agree_view">
  175. <Image style='width:12px;height:12px;' src={check} />
  176. <Text className='agree_text'>{t('page.auth.agreement')}</Text>
  177. </View> */}
  178. <View style={{ flex: 1 }} />
  179. <View className="footer">
  180. <Text className="have_account">{t('feature.auth.login.footer_desc')}</Text>
  181. <Text className="login2" onClick={() => props.register()}>{t('feature.auth.login.footer_sign_up')}</Text>
  182. </View>
  183. </View>
  184. }