Login.js 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. /**
  2. * 登录页面
  3. * @邠心vbe on 2021/03/18
  4. */
  5. import React from 'react';
  6. import {View, Text, StyleSheet, Image, TextInput, ScrollView, Platform} 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={ui.container}
  144. keyboardShouldPersistTaps={'handled'}>
  145. <View style={styles.header}>
  146. <Image style={styles.headerImg} source={require('../../images/login-head.png')} />
  147. </View>
  148. <View style={styles.loginView}>
  149. <View style={ui.center}>
  150. <Image style={styles.logoImg} source={require('../../images/app-logo.png')} />
  151. </View>
  152. <View style={styles.loginForm}>
  153. <View style={styles.inputView}>
  154. <Zocial name='email' size={28} color='#999999' />
  155. <TextInput
  156. style={styles.inputText}
  157. placeholder={"E-mail"}
  158. textContentType='emailAddress'
  159. defaultValue={this.state.email}
  160. maxLength={50}
  161. onChangeText={(v) => {
  162. this.setState({
  163. email: v
  164. })
  165. }}/>
  166. </View>
  167. <View style={styles.inputView}>
  168. <Fontisto name='locked' size={28} color='#999999' style={{marginLeft: 3, marginRight: 2}} />
  169. <TextInput
  170. style={styles.inputText}
  171. placeholder="Password"
  172. textContentType='password'
  173. secureTextEntry={true}
  174. maxLength={20}
  175. onChangeText={(v) => {
  176. this.setState({
  177. password: v
  178. })
  179. }}
  180. onSubmitEditing={() => {
  181. this.onLogin();
  182. }}/>
  183. </View>
  184. <View style={ui.flexcw}>
  185. <View style={$padding(12, 8)}>
  186. <CheckBoxText
  187. value={this.state.rememberMe}
  188. onValueChange={(newValue) => {
  189. this.setState({ rememberMe: newValue });
  190. }}
  191. text={'Remember me'}
  192. />
  193. </View>
  194. <Text
  195. style={styles.linksText}
  196. onPress={() => startPage(PageList.forgotPassword)}>Forgot Password</Text>
  197. </View>
  198. <Button
  199. style={styles.loginButton}
  200. text='SIGN IN'
  201. elevation={1.5}
  202. onClick={() => {
  203. this.onLogin()
  204. }}
  205. />
  206. </View>
  207. </View>
  208. <View style={styles.signView}>
  209. <Text style={{color: '#333'}}>New User?</Text>
  210. <Text
  211. style={styles.linksText}
  212. onPress={() => {
  213. startPage(PageList.register);
  214. }}
  215. >Click here to sign up</Text>
  216. </View>
  217. </ScrollView>
  218. </View>
  219. );
  220. }
  221. };
  222. const styles = StyleSheet.create({
  223. backBtn:{
  224. top: 4,
  225. left: 2,
  226. zIndex: 1,
  227. position: 'absolute'
  228. },
  229. header: {
  230. paddingTop: 22,
  231. paddingBottom: 20,
  232. paddingLeft: 32,
  233. paddingRight: 32,
  234. alignItems: 'center',
  235. backgroundColor: colorAccent
  236. },
  237. headerImg: {
  238. width: $vw(65),
  239. height: $vw(56.118)
  240. },
  241. loginView: {
  242. padding: 16,
  243. marginTop: -24,
  244. borderTopLeftRadius: 24,
  245. borderTopRightRadius: 24,
  246. backgroundColor: 'white'
  247. },
  248. logoImg: {
  249. width:136.19,
  250. height: 42.85,
  251. marginTop: 18
  252. },
  253. loginForm: {
  254. paddingLeft: 16,
  255. paddingRight: 16,
  256. },
  257. inputView: {
  258. marginTop: 30,
  259. borderRadius: 8,
  260. paddingTop: 6,
  261. paddingLeft: 16,
  262. paddingRight: 16,
  263. paddingBottom: 6,
  264. alignItems: 'center',
  265. flexDirection: 'row',
  266. backgroundColor: '#F5F5F5'
  267. },
  268. inputText: {
  269. flex: 1,
  270. color: '#333',
  271. fontSize: 15,
  272. paddingTop: 6,
  273. paddingLeft: 16,
  274. paddingBottom: 6
  275. },
  276. loginButton: {
  277. marginTop: 32,
  278. borderRadius: 4
  279. },
  280. signView: {
  281. paddingTop: 32,
  282. paddingBottom: 16,
  283. alignItems: 'center',
  284. },
  285. checkText: {
  286. color: '#333',
  287. fontSize: 14,
  288. paddingLeft: 8
  289. },
  290. linksText: {
  291. ...ui.link,
  292. padding: 8,
  293. fontSize: 14,
  294. textDecorationLine: 'underline'
  295. }
  296. });