Login.js 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 CheckBox from '@react-native-community/checkbox';
  8. import { BackButton } from '../../components/Toolbar';
  9. import apiUser from '../../api/apiUser';
  10. import { setAccessToken } from '../../api/http';
  11. import { getStorageSync, setStorage, getStorageJsonSync, setStorageJson, getStorage } from '../../utils/storage';
  12. import Button from '../../components/Button';
  13. import Dialog from '../../components/Dialog';
  14. import { PageList } from '../Router';
  15. import utils from '../../utils/utils';
  16. import CheckBoxText from '../../components/CheckBoxText';
  17. export const AutoLogin = async (back) => {
  18. const data = await getStorageJsonSync('loginData')
  19. if (data && data.email && data.password) {
  20. apiUser.login(data).then(res => {
  21. if (res.data.accessToken) {
  22. setAccessToken(res.data.accessToken);
  23. if (back) back();
  24. }
  25. }).catch((err) => {
  26. console.warn('AutoLogin', err);
  27. toastShort('Sign in failed')
  28. });
  29. }
  30. }
  31. export default class Login extends React.Component {
  32. constructor(props) {
  33. super(props);
  34. this.state = {
  35. email: '',
  36. password: '',
  37. rememberMe: true
  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('Please enter email address');
  85. return;
  86. }
  87. if (this.state.password == '') {
  88. toastShort('Please enter password');
  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. render() {
  117. return (
  118. <View style={ui.flex1}>
  119. <View style={[styles.backBtn, {top: this.getBackTopPosition()}]}>
  120. <BackButton/>
  121. </View>
  122. <ScrollView
  123. style={styles.container}
  124. keyboardShouldPersistTaps={'handled'}>
  125. <View style={styles.header}>
  126. <Image
  127. style={styles.headerImg}
  128. resizeMode='contain'
  129. source={require('../../images/app-logo.png')} />
  130. </View>
  131. <View style={styles.loginView}>
  132. <View style={ui.center}>
  133. {/* <Image
  134. style={styles.logoImg}
  135. resizeMode='contain'
  136. source={require('../../images/app-logo.png')} /> */}
  137. {/* <Text style={styles.loginTitle}>Please Login</Text> */}
  138. </View>
  139. <View style={styles.loginForm}>
  140. <View style={styles.inputView}>
  141. {/* <Zocial name='email' size={28} color='#999999' /> */}
  142. <Image
  143. style={styles.inputIcon}
  144. source={require('../../images/user/sign-email.png')}
  145. />
  146. <TextInput
  147. style={styles.inputText}
  148. placeholder={"E-mail"}
  149. placeholderTextColor={textPlacehoder}
  150. keyboardType="email-address"
  151. textContentType='emailAddress'
  152. defaultValue={this.state.email}
  153. maxLength={50}
  154. clearButtonMode='while-editing'
  155. autoCapitalize="none"
  156. autoComplete="off"
  157. autoCorrect={false}
  158. onChangeText={(v) => {
  159. this.setState({
  160. email: v
  161. })
  162. }}/>
  163. </View>
  164. <View style={styles.inputView}>
  165. {/* <Fontisto name='locked' size={28} color='#999999' style={{marginLeft: 3, marginRight: 2}} /> */}
  166. <Image
  167. style={styles.inputIcon}
  168. source={require('../../images/user/sign-password.png')}
  169. />
  170. <TextInput
  171. style={styles.inputText}
  172. placeholder="Password"
  173. placeholderTextColor={textPlacehoder}
  174. textContentType='password'
  175. secureTextEntry={true}
  176. maxLength={20}
  177. onChangeText={(v) => {
  178. this.setState({
  179. password: v
  180. })
  181. }}
  182. onSubmitEditing={() => {
  183. this.onLogin();
  184. }}/>
  185. </View>
  186. <View style={ui.flexcw}>
  187. <View style={$padding(12, 8)}>
  188. <CheckBoxText
  189. value={this.state.rememberMe}
  190. onValueChange={(newValue) => {
  191. this.setState({ rememberMe: newValue });
  192. }}
  193. text={'Remember me'}
  194. />
  195. </View>
  196. <Text
  197. style={styles.linksText}
  198. onPress={() => startPage(PageList.forgotPassword)}>Forgot Password</Text>
  199. </View>
  200. <Button
  201. style={styles.loginButton}
  202. text='LOGIN'
  203. elevation={1.5}
  204. onClick={() => {
  205. this.onLogin()
  206. }}
  207. />
  208. </View>
  209. </View>
  210. <View style={styles.signView}>
  211. <Text style={{color: textPrimary}}>New User?</Text>
  212. {/* <Text
  213. style={styles.linksText}
  214. onPress={() => {
  215. startPage(PageList.register);
  216. }}
  217. >Click here to sign up</Text> */}
  218. <Text
  219. style={styles.linksText}
  220. onPress={() => startPage(PageList.register)}>Register as Public User</Text>
  221. {/* <Text
  222. style={styles.linksText}
  223. onPress={() => startPage(PageList.register, {isFleetUser: true})}>Register as Fleet / PHV Driver</Text> */}
  224. </View>
  225. </ScrollView>
  226. </View>
  227. );
  228. }
  229. };
  230. const styles = StyleSheet.create({
  231. container: {
  232. flex: 1,
  233. backgroundColor: colorLight
  234. },
  235. backBtn:{
  236. top: 4,
  237. left: 2,
  238. zIndex: 1,
  239. position: 'absolute'
  240. },
  241. header: {
  242. paddingTop: 22,
  243. paddingBottom: 20,
  244. paddingLeft: 32,
  245. paddingRight: 32,
  246. alignItems: 'center',
  247. //backgroundColor: colorPrimary
  248. },
  249. headerImg: {
  250. width: $vw(65),
  251. height: $vw(56.118)
  252. },
  253. loginView: {
  254. padding: 16,
  255. marginTop: -24,
  256. borderTopLeftRadius: 24,
  257. borderTopRightRadius: 24,
  258. backgroundColor: colorLight
  259. },
  260. logoImg: {
  261. width:136.19,
  262. height: 42.85,
  263. marginTop: 18
  264. },
  265. loginForm: {
  266. paddingLeft: 16,
  267. paddingRight: 16,
  268. },
  269. loginTitle: {
  270. fontSize: 22,
  271. fontWeight: 'bold',
  272. color: textPrimary
  273. },
  274. inputIcon: {
  275. width: 24,
  276. height: 24
  277. },
  278. inputView: {
  279. marginTop: 30,
  280. borderRadius: 0,
  281. paddingTop: 6,
  282. paddingLeft: 16,
  283. paddingRight: 16,
  284. paddingBottom: 6,
  285. alignItems: 'center',
  286. flexDirection: 'row',
  287. borderBottomWidth: 1,
  288. borderBottomColor: '#F5F5F5'
  289. //backgroundColor: '#F5F5F5'
  290. },
  291. inputText: {
  292. flex: 1,
  293. color: textPrimary,
  294. fontSize: 15,
  295. paddingTop: 6,
  296. paddingLeft: 16,
  297. paddingBottom: 6
  298. },
  299. loginButton: {
  300. marginTop: 32,
  301. borderRadius: 40
  302. },
  303. signView: {
  304. paddingTop: 32,
  305. paddingBottom: 16,
  306. alignItems: 'center',
  307. },
  308. checkText: {
  309. color: textPrimary,
  310. fontSize: 14,
  311. paddingLeft: 8
  312. },
  313. linksText: {
  314. ...ui.link,
  315. fontSize: 14,
  316. padding: 4,
  317. marginTop: 5,
  318. fontWeight: 'bold',
  319. textDecorationLine: 'underline'
  320. }
  321. });