login.tsx 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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. let useNavigation;
  17. if (process.env.TARO_ENV == 'rn') {
  18. useNavigation = require("@react-navigation/native").useNavigation
  19. }
  20. export default function Login(props: { register: () => void }) {
  21. const { t } = useTranslation()
  22. const dispatch = useDispatch();
  23. const [name, setName] = useState('');
  24. const [password, setPassword] = useState('');
  25. const user = useSelector((state: any) => state.user);
  26. let navigation;
  27. if (useNavigation) {
  28. navigation = useNavigation()
  29. }
  30. useEffect(() => {
  31. if (user.isLogin) {
  32. if (process.env.TARO_ENV=='rn'){
  33. navigation.pop(2)
  34. return
  35. }
  36. if (user.scenario_select_count > 0) {
  37. Taro.navigateBack({
  38. delta: 2
  39. })
  40. }
  41. else {
  42. Taro.navigateBack({
  43. delta: 1
  44. })
  45. Taro.redirectTo({
  46. url: '/pages/clock/ChooseScenario'
  47. })
  48. }
  49. }
  50. }, [user.isLogin])
  51. const handleNameChange = (value: string) => {
  52. setName(value);
  53. };
  54. const handlePasswordChange = (value: string) => {
  55. setPassword(value);
  56. }
  57. const isLoginButtonDisabled = name === '' || password === '';
  58. function loginComponent() {
  59. return <View>
  60. <Inputs autoFocus={true} value={name} onChange={handleNameChange} onConfirm={(e) => { }} placeholder={t('feature.auth.login.input_account_placeholder')}></Inputs>
  61. <View style={{ height: 20 }} />
  62. <Inputs value={password} isSecure={true} onChange={handlePasswordChange} onConfirm={(e) => { }} placeholder={t('feature.auth.login.input_password_placeholder')}></Inputs>
  63. </View>
  64. }
  65. function LoginF() {
  66. if (isLoginButtonDisabled) {
  67. return;
  68. }
  69. login(name, password).then(res => {
  70. dispatch(loginSuccess(res))
  71. clientId()
  72. // navigation.goBack(3)
  73. }).catch(e => {
  74. // alert(e.data.error_message)
  75. // alert(''+JSON.stringify(e))
  76. Taro.showToast({
  77. icon:'none',
  78. title:e.error_message
  79. })
  80. console.log('error',e)
  81. })
  82. // dispatch(login(name, password) as any);
  83. }
  84. return <View style={{ width: '100%', height: '100%' }}>
  85. <Texts text={'App Name'} type={TextType.primary}></Texts>
  86. {
  87. loginComponent()
  88. }
  89. <Buttons
  90. onClick={LoginF}
  91. title={t('feature.auth.create_account.btn_next')}
  92. btnStyle={{ marginLeft: 23, marginRight: 23, marginTop: 20, marginBottom: 20, height: 44, borderRadius: 22, backgroundColor: ColorType.fast }}
  93. disabled={isLoginButtonDisabled}></Buttons>
  94. <View className="agree_view">
  95. <Image style='width:12px;height:12px;' src={check} />
  96. <Text className='agree_text'>{t('page.auth.agreement')}</Text>
  97. </View>
  98. <View style={{ flex: 1 }} />
  99. <View className="footer">
  100. <Text className="have_account">{t('feature.auth.login.footer_desc')}</Text>
  101. <Text className="login2" onClick={() => props.register()}>{t('feature.auth.login.footer_sign_up')}</Text>
  102. </View>
  103. </View>
  104. return <View style={{ backgroundColor: '#000', flex: 1, flexDirection: 'column', display: 'flex', width: '100%', height: '100%' }}>
  105. <Texts text={'App Name'} type={TextType.primary}></Texts>
  106. {/* <Texts text={t('feature.auth.create_account.sub_title')} type={TextType.secondary}></Texts> */}
  107. <Box children={loginComponent()}></Box>
  108. {/* <Buttons
  109. onClick={LoginF}
  110. title={t('feature.auth.create_account.btn_next')}
  111. style={{ marginLeft: 23, marginRight: 23, marginTop: 20, marginBottom: 20 }}
  112. status={isLoginButtonDisabled ? ComponentStatus.disable : ComponentStatus.enable}></Buttons>
  113. <View className="agree_view">
  114. <Image style='width:12px;height:12px;' src={check} />
  115. <Text className='agree_text'>{t('page.auth.agreement')}</Text>
  116. </View> */}
  117. <View style={{ flex: 1 }} />
  118. <View className="footer">
  119. <Text className="have_account">{t('feature.auth.login.footer_desc')}</Text>
  120. <Text className="login2" onClick={() => props.register()}>{t('feature.auth.login.footer_sign_up')}</Text>
  121. </View>
  122. </View>
  123. }