Login.tsx 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. Taro.redirectTo({
  46. url: '/pages/clock/ChooseScenario?trigger_event=SETUP_UPON_ACCOUNT_CREATION'
  47. })
  48. }
  49. else {
  50. Taro.navigateBack({
  51. delta: 2
  52. })
  53. }
  54. // if (user.scenario_select_count > 0 || user.ongoing) {
  55. // Taro.navigateBack({
  56. // delta: 2
  57. // })
  58. // }
  59. // else {
  60. // Taro.navigateBack({
  61. // delta: 1
  62. // })
  63. // Taro.redirectTo({
  64. // url: '/pages/clock/ChooseScenario?trigger_event=SETUP_UPON_ACCOUNT_CREATION'
  65. // })
  66. // }
  67. }
  68. }, [user.isLogin])
  69. const handleNameChange = (value: string) => {
  70. setName(value);
  71. };
  72. const handlePasswordChange = (value: string) => {
  73. setPassword(value);
  74. }
  75. const isLoginButtonDisabled = name === '' || password === '';
  76. function loginComponent() {
  77. return <View>
  78. <Inputs autoFocus={true} value={name} onChange={handleNameChange} onConfirm={(e) => { }} placeholder={t('feature.auth.login.input_account_placeholder')}></Inputs>
  79. <View style={{ height: 20 }} />
  80. <Inputs value={password} isSecure={true} onChange={handlePasswordChange} onConfirm={(e) => { }} placeholder={t('feature.auth.login.input_password_placeholder')}></Inputs>
  81. </View>
  82. }
  83. function LoginF() {
  84. if (isLoginButtonDisabled) {
  85. return;
  86. }
  87. login(name, password).then(res => {
  88. dispatch(loginSuccess(res))
  89. clientId()
  90. // navigation.goBack(3)
  91. }).catch(e => {
  92. // alert(e.data.error_message)
  93. // alert(''+JSON.stringify(e))
  94. Taro.showToast({
  95. icon: 'none',
  96. title: e.error_message
  97. })
  98. })
  99. // dispatch(login(name, password) as any);
  100. }
  101. function tapCheck() {
  102. setChecked(!checked)
  103. }
  104. function agreement() {
  105. const resource = common.resources.filter((item: any) => {
  106. return item.code == 'user_agreement'
  107. })
  108. if (!resource || resource.length == 0) {
  109. Taro.showToast({
  110. title: 'Not found.',
  111. icon: 'none'
  112. })
  113. return
  114. }
  115. jumpPage('/pages/common/H5?title=User Agreement&url=' + resource[0].url, 'H5', navigation, {
  116. title: 'User Agreement',
  117. url: resource[0].url
  118. })
  119. }
  120. function privacy() {
  121. const resource = common.resources.filter((item: any) => {
  122. return item.code == 'privacy'
  123. })
  124. if (!resource || resource.length == 0) {
  125. Taro.showToast({
  126. title: 'Not found.',
  127. icon: 'none'
  128. })
  129. return
  130. }
  131. jumpPage('/pages/common/H5?title=Privacy Policy&url=' + resource[0].url, 'H5', navigation, {
  132. title: 'Privacy Policy',
  133. url: resource[0].url
  134. })
  135. }
  136. return <View style={{ width: '100%', height: '100%' }}>
  137. <Texts text={'fast16cc'} type={TextType.primary}></Texts>
  138. {
  139. loginComponent()
  140. }
  141. <Buttons
  142. onClick={LoginF}
  143. title={t('feature.auth.create_account.login')}
  144. btnStyle={{
  145. marginLeft: 23, marginRight: 23, marginTop: 20,
  146. color: ColorType.black,
  147. marginBottom: 20, height: 44,
  148. borderRadius: 22, backgroundColor: ColorType.fast
  149. }}
  150. disabled={isLoginButtonDisabled || !checked}></Buttons>
  151. <View className="agree_view">
  152. {/* <Image style='width:12px;height:12px;' src={check} /> */}
  153. <View onClick={tapCheck}>
  154. {
  155. checked ? <IconRadioCheck width={20} color={ColorType.fast} /> :
  156. <IconRadio width={20} color={ColorType.fast} />
  157. }
  158. </View>
  159. <View style={{ width: 10 }} />
  160. <Text className='agree_text' style={{ color: '#ffffff66' }}>I agree to <Text onClick={privacy} style={{ color: '#ffffff' }}> Privacy Policy.</Text></Text>
  161. </View>
  162. <View style={{ flex: 1 }} />
  163. <View className="footer">
  164. <Text className="have_account">{t('feature.auth.login.footer_desc')}</Text>
  165. <Text className="login2" onClick={() => props.register()}> {t('feature.auth.login.footer_sign_up')}</Text>
  166. </View>
  167. </View>
  168. return <View style={{ backgroundColor: '#000', flex: 1, flexDirection: 'column', display: 'flex', width: '100%', height: '100%' }}>
  169. <Texts text={'fast16cc'} type={TextType.primary}></Texts>
  170. {/* <Texts text={t('feature.auth.create_account.sub_title')} type={TextType.secondary}></Texts> */}
  171. <Box children={loginComponent()}></Box>
  172. {/* <Buttons
  173. onClick={LoginF}
  174. title={t('feature.auth.create_account.btn_next')}
  175. style={{ marginLeft: 23, marginRight: 23, marginTop: 20, marginBottom: 20 }}
  176. status={isLoginButtonDisabled ? ComponentStatus.disable : ComponentStatus.enable}></Buttons>
  177. <View className="agree_view">
  178. <Image style='width:12px;height:12px;' src={check} />
  179. <Text className='agree_text'>{t('page.auth.agreement')}</Text>
  180. </View> */}
  181. <View style={{ flex: 1 }} />
  182. <View className="footer">
  183. <Text className="have_account">{t('feature.auth.login.footer_desc')}</Text>
  184. <Text className="login2" onClick={() => props.register()}>{t('feature.auth.login.footer_sign_up')}</Text>
  185. </View>
  186. </View>
  187. }