Login.js 9.4 KB

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