Login.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. onChangeText={(v) => {
  156. this.setState({
  157. email: v
  158. })
  159. }}/>
  160. </View>
  161. <View style={styles.inputView}>
  162. {/* <Fontisto name='locked' size={28} color='#999999' style={{marginLeft: 3, marginRight: 2}} /> */}
  163. <Image
  164. style={styles.inputIcon}
  165. source={require('../../images/user/sign-password.png')}
  166. />
  167. <TextInput
  168. style={styles.inputText}
  169. placeholder="Password"
  170. placeholderTextColor={textPlacehoder}
  171. textContentType='password'
  172. secureTextEntry={true}
  173. maxLength={20}
  174. onChangeText={(v) => {
  175. this.setState({
  176. password: v
  177. })
  178. }}
  179. onSubmitEditing={() => {
  180. this.onLogin();
  181. }}/>
  182. </View>
  183. <View style={ui.flexcw}>
  184. <View style={$padding(12, 8)}>
  185. <CheckBoxText
  186. value={this.state.rememberMe}
  187. onValueChange={(newValue) => {
  188. this.setState({ rememberMe: newValue });
  189. }}
  190. text={'Remember me'}
  191. />
  192. </View>
  193. <Text
  194. style={styles.linksText}
  195. onPress={() => startPage(PageList.forgotPassword)}>Forgot Password</Text>
  196. </View>
  197. <Button
  198. style={styles.loginButton}
  199. text='LOGIN'
  200. elevation={1.5}
  201. onClick={() => {
  202. this.onLogin()
  203. }}
  204. />
  205. </View>
  206. </View>
  207. <View style={styles.signView}>
  208. <Text style={{color: textPrimary}}>New User?</Text>
  209. {/* <Text
  210. style={styles.linksText}
  211. onPress={() => {
  212. startPage(PageList.register);
  213. }}
  214. >Click here to sign up</Text> */}
  215. <Text
  216. style={styles.linksText}
  217. onPress={() => startPage(PageList.register)}>Register as Public User</Text>
  218. {/* <Text
  219. style={styles.linksText}
  220. onPress={() => startPage(PageList.register, {isFleetUser: true})}>Register as Fleet / PHV Driver</Text> */}
  221. </View>
  222. </ScrollView>
  223. </View>
  224. );
  225. }
  226. };
  227. const styles = StyleSheet.create({
  228. container: {
  229. flex: 1,
  230. backgroundColor: colorLight
  231. },
  232. backBtn:{
  233. top: 4,
  234. left: 2,
  235. zIndex: 1,
  236. position: 'absolute'
  237. },
  238. header: {
  239. paddingTop: 22,
  240. paddingBottom: 20,
  241. paddingLeft: 32,
  242. paddingRight: 32,
  243. alignItems: 'center',
  244. //backgroundColor: colorPrimary
  245. },
  246. headerImg: {
  247. width: $vw(65),
  248. height: $vw(56.118)
  249. },
  250. loginView: {
  251. padding: 16,
  252. marginTop: -24,
  253. borderTopLeftRadius: 24,
  254. borderTopRightRadius: 24,
  255. backgroundColor: colorLight
  256. },
  257. logoImg: {
  258. width:136.19,
  259. height: 42.85,
  260. marginTop: 18
  261. },
  262. loginForm: {
  263. paddingLeft: 16,
  264. paddingRight: 16,
  265. },
  266. loginTitle: {
  267. fontSize: 22,
  268. fontWeight: 'bold',
  269. color: textPrimary
  270. },
  271. inputIcon: {
  272. width: 24,
  273. height: 24
  274. },
  275. inputView: {
  276. marginTop: 30,
  277. borderRadius: 0,
  278. paddingTop: 6,
  279. paddingLeft: 16,
  280. paddingRight: 16,
  281. paddingBottom: 6,
  282. alignItems: 'center',
  283. flexDirection: 'row',
  284. borderBottomWidth: 1,
  285. borderBottomColor: '#F5F5F5'
  286. //backgroundColor: '#F5F5F5'
  287. },
  288. inputText: {
  289. flex: 1,
  290. color: textPrimary,
  291. fontSize: 15,
  292. paddingTop: 6,
  293. paddingLeft: 16,
  294. paddingBottom: 6
  295. },
  296. loginButton: {
  297. marginTop: 32,
  298. borderRadius: 40
  299. },
  300. signView: {
  301. paddingTop: 32,
  302. paddingBottom: 16,
  303. alignItems: 'center',
  304. },
  305. checkText: {
  306. color: textPrimary,
  307. fontSize: 14,
  308. paddingLeft: 8
  309. },
  310. linksText: {
  311. ...ui.link,
  312. fontSize: 14,
  313. padding: 4,
  314. marginTop: 5,
  315. fontWeight: 'bold',
  316. textDecorationLine: 'underline'
  317. }
  318. });