Login.js 9.7 KB

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