Login.js 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /**
  2. * 登录页面
  3. * @邠心vbe on 2021/03/18
  4. */
  5. import React from 'react';
  6. import {View, Text, StyleSheet, Image, TextInput, ScrollView, Platform, Pressable} from 'react-native';
  7. import { BackButton } from '../../components/Toolbar';
  8. import apiUser from '../../api/apiUser';
  9. import { setAccessToken } from '../../api/http';
  10. import { getStorageJsonSync, setStorageJson } from '../../utils/storage';
  11. import Button from '../../components/Button';
  12. import Dialog from '../../components/Dialog';
  13. import { PageList } from '../Router';
  14. import CheckBoxText from '../../components/CheckBoxText';
  15. import TextView from '../../components/TextView';
  16. export const AutoLogin = async (back) => {
  17. const data = await getStorageJsonSync('loginData')
  18. if (data && data.email && data.password) {
  19. apiUser.login(data).then(res => {
  20. if (res.data.accessToken) {
  21. setAccessToken(res.data.accessToken);
  22. if (back) back();
  23. }
  24. }).catch(err => {
  25. console.warn('AutoLogin', err);
  26. toastShort('Sign in failed')
  27. });
  28. }
  29. }
  30. export default class Login extends React.Component {
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. email: '',
  35. password: '',
  36. rememberMe: true,
  37. showPassword: false
  38. }
  39. }
  40. componentDidMount() {
  41. /*if (this.props.route.params.action) {
  42. this.props.navigation.addListener('beforeRemove', (e) => {
  43. if (this.props.route.params.action == '401') {
  44. //dispatchPage(StackActions.push('home'));
  45. dispatchPage(state => {
  46. console.log('routes', state);
  47. const r = [];
  48. var index = 0;
  49. var homekey = '';
  50. for (let i = 0; i < state.routes.length; i++) {
  51. const item = state.routes[i];
  52. if (item.name == 'home') {
  53. r.push(item);
  54. index = i;
  55. homekey = item.key;
  56. break;
  57. } else {
  58. r.push(item);
  59. }
  60. }
  61. console.log('new routes', r);
  62. return {
  63. ...CommonActions.goBack(),
  64. source: this.props.route.key,
  65. target: homekey,
  66. };
  67. });
  68. }
  69. });
  70. }*/
  71. this.getEmail();
  72. }
  73. async getEmail() {
  74. const data = await getStorageJsonSync('loginData');
  75. if (data && data.email) {
  76. this.setState({
  77. email: data.email
  78. });
  79. }
  80. }
  81. onLogin() {
  82. //console.log(this.state);
  83. if (this.state.email == '') {
  84. toastShort($t('sign.plsInputEmail'));
  85. return;
  86. }
  87. if (this.state.password == '') {
  88. toastShort($t('sign.plsInputPassword'));
  89. return;
  90. }
  91. Dialog.showProgressDialog();
  92. apiUser.login(this.state).then(res => {
  93. console.log('res.data', res);
  94. if (res.data.accessToken) {
  95. setAccessToken(res.data.accessToken);
  96. Dialog.dismissLoading();
  97. if (this.state.rememberMe) {
  98. setStorageJson('loginData', this.state);
  99. } else {
  100. setStorageJson('loginData', {});
  101. }
  102. startPage(PageList.home);
  103. //this.props.navigation.goBack();
  104. } else {
  105. toastShort(res.msg);
  106. Dialog.dismissLoading();
  107. }
  108. }).catch(err => {
  109. toastShort(err);
  110. Dialog.dismissLoading();
  111. });
  112. }
  113. getBackTopPosition() {
  114. return isIOS ? statusHeight : 4;
  115. }
  116. togglePassword() {
  117. this.setState({
  118. showPassword: !this.state.showPassword
  119. })
  120. }
  121. render() {
  122. return (
  123. <View style={ui.flex1}>
  124. <View style={[styles.backBtn, {top: this.getBackTopPosition()}]}>
  125. <BackButton/>
  126. </View>
  127. <ScrollView
  128. style={styles.container}
  129. keyboardShouldPersistTaps={'handled'}>
  130. <View style={styles.header}>
  131. <Image
  132. style={styles.headerImg}
  133. resizeMode='contain'
  134. source={require('../../images/app-logo.png')} />
  135. </View>
  136. <View style={styles.loginView}>
  137. <View style={ui.center}>
  138. {/* <Image
  139. style={styles.logoImg}
  140. resizeMode='contain'
  141. source={require('../../images/app-logo.png')} /> */}
  142. <TextView style={styles.loginTitle}>{$t('sign.plsLoginTitle')}</TextView>
  143. </View>
  144. <View style={styles.loginForm}>
  145. <View style={styles.inputView}>
  146. {/* <Zocial name='email' size={28} color='#999999' /> */}
  147. <Image
  148. style={styles.inputIcon}
  149. source={require('../../images/user/sign-email.png')}
  150. />
  151. <TextInput
  152. style={styles.inputText}
  153. placeholder={$t('sign.labelE_mail')}
  154. placeholderTextColor={textPlacehoder}
  155. keyboardType="email-address"
  156. textContentType='emailAddress'
  157. defaultValue={this.state.email}
  158. maxLength={50}
  159. clearButtonMode='while-editing'
  160. autoCapitalize="none"
  161. autoComplete="off"
  162. autoCorrect={false}
  163. onChangeText={(v) => {
  164. this.setState({
  165. email: v
  166. })
  167. }}/>
  168. </View>
  169. <View style={styles.inputView}>
  170. {/* <Fontisto name='locked' size={28} color='#999999' style={{marginLeft: 3, marginRight: 2}} /> */}
  171. <Image
  172. style={styles.inputIcon}
  173. source={require('../../images/user/sign-password.png')}
  174. />
  175. <TextInput
  176. style={styles.inputText}
  177. placeholder={$t('sign.labelPassword')}
  178. placeholderTextColor={textPlacehoder}
  179. textContentType='password'
  180. secureTextEntry={!this.state.showPassword}
  181. maxLength={20}
  182. onChangeText={(v) => {
  183. this.setState({
  184. password: v
  185. })
  186. }}
  187. onSubmitEditing={() => {
  188. this.onLogin();
  189. }}/>
  190. <MaterialCommunityIcons
  191. name={this.state.showPassword ? 'eye' : 'eye-off' }
  192. size={20}
  193. color={textCancel}
  194. onPress={() => this.togglePassword()}/>
  195. </View>
  196. <View style={ui.flexcw}>
  197. <View style={$padding(12, 8)}>
  198. <CheckBoxText
  199. value={this.state.rememberMe}
  200. onValueChange={(newValue) => {
  201. this.setState({ rememberMe: newValue });
  202. }}
  203. text={$t('sign.rememberMe')}
  204. />
  205. </View>
  206. <TextView
  207. style={styles.linksText}
  208. onPress={() => startPage(PageList.forgotPassword)}>{$t('sign.forgotPassword')}</TextView>
  209. </View>
  210. <Button
  211. style={styles.loginButton}
  212. text={$t('sign.btnLogin')}
  213. elevation={1.5}
  214. onClick={() => {
  215. this.onLogin()
  216. }}
  217. />
  218. </View>
  219. </View>
  220. <View style={styles.signView}>
  221. <TextView style={{color: textPrimary}}>{$t('sign.tipNewUser')}</TextView>
  222. {/* <Text
  223. style={styles.linksText}
  224. onPress={() => {
  225. startPage(PageList.register);
  226. }}
  227. >Click here to sign up</Text> */}
  228. <TextView
  229. style={styles.linksText}
  230. onPress={() => startPage(PageList.register)}>{$t('sign.registerPublicUser')}</TextView>
  231. {/* <Text
  232. style={styles.linksText}
  233. onPress={() => startPage(PageList.register, {isFleetUser: true})}>{$t('sign.registerDriverUser')}</Text> */}
  234. </View>
  235. </ScrollView>
  236. </View>
  237. );
  238. }
  239. };
  240. const styles = StyleSheet.create({
  241. container: {
  242. flex: 1,
  243. backgroundColor: colorLight
  244. },
  245. backBtn:{
  246. top: 4,
  247. left: 2,
  248. zIndex: 1,
  249. position: 'absolute'
  250. },
  251. header: {
  252. paddingTop: 22,
  253. paddingBottom: 20,
  254. paddingLeft: 32,
  255. paddingRight: 32,
  256. alignItems: 'center',
  257. //backgroundColor: colorPrimary
  258. },
  259. headerImg: {
  260. width: $vw(65),
  261. height: $vw(56.118)
  262. },
  263. loginView: {
  264. padding: 16,
  265. marginTop: -24,
  266. borderTopLeftRadius: 24,
  267. borderTopRightRadius: 24,
  268. backgroundColor: colorLight
  269. },
  270. logoImg: {
  271. width:136.19,
  272. height: 42.85,
  273. marginTop: 18
  274. },
  275. loginForm: {
  276. paddingLeft: 16,
  277. paddingRight: 16,
  278. },
  279. loginTitle: {
  280. fontSize: 22,
  281. fontWeight: 'bold',
  282. color: textPrimary
  283. },
  284. inputIcon: {
  285. width: 24,
  286. height: 24
  287. },
  288. inputView: {
  289. marginTop: 30,
  290. borderRadius: 0,
  291. paddingTop: 6,
  292. paddingLeft: 16,
  293. paddingRight: 16,
  294. paddingBottom: 6,
  295. alignItems: 'center',
  296. flexDirection: 'row',
  297. borderBottomWidth: 1,
  298. borderBottomColor: '#F5F5F5'
  299. //backgroundColor: '#F5F5F5'
  300. },
  301. inputText: {
  302. flex: 1,
  303. color: textPrimary,
  304. fontSize: 15,
  305. paddingTop: 6,
  306. paddingLeft: 16,
  307. paddingBottom: 6
  308. },
  309. loginButton: {
  310. marginTop: 32,
  311. borderRadius: 40
  312. },
  313. signView: {
  314. paddingTop: 32,
  315. paddingBottom: 16,
  316. alignItems: 'center',
  317. },
  318. checkText: {
  319. color: textPrimary,
  320. fontSize: 14,
  321. paddingLeft: 8
  322. },
  323. linksText: {
  324. ...ui.link,
  325. fontSize: 14,
  326. padding: 4,
  327. marginTop: 5,
  328. fontWeight: 'bold',
  329. textDecorationLine: 'underline'
  330. }
  331. });