LoginV2.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. /**
  2. * 登录页面V2
  3. * @邠心vbe on 2022/12/23
  4. */
  5. import React from 'react';
  6. import {View, Text, StyleSheet, Image, TextInput, ScrollView, Platform} 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 utils from '../../utils/utils';
  15. import CheckBoxText from '../../components/CheckBoxText';
  16. export const AutoLogin = async (back) => {
  17. const data = await getStorageJsonSync('loginData')
  18. if (data && data.email && data.password) {
  19. apiUser.login(data).then(res => {
  20. if (res.data.accessToken) {
  21. setAccessToken(res.data.accessToken);
  22. if (back) back();
  23. }
  24. }).catch((err) => {
  25. console.warn('AutoLogin', err);
  26. toastShort('Sign in failed')
  27. });
  28. }
  29. }
  30. export default class Login extends React.Component {
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. email: '',
  35. password: '',
  36. rememberMe: true
  37. }
  38. }
  39. componentDidMount() {
  40. /*if (this.props.route.params.action) {
  41. this.props.navigation.addListener('beforeRemove', (e) => {
  42. if (this.props.route.params.action == '401') {
  43. //dispatchPage(StackActions.push('home'));
  44. dispatchPage(state => {
  45. console.log('routes', state);
  46. const r = [];
  47. var index = 0;
  48. var homekey = '';
  49. for (let i = 0; i < state.routes.length; i++) {
  50. const item = state.routes[i];
  51. if (item.name == 'home') {
  52. r.push(item);
  53. index = i;
  54. homekey = item.key;
  55. break;
  56. } else {
  57. r.push(item);
  58. }
  59. }
  60. console.log('new routes', r);
  61. return {
  62. ...CommonActions.goBack(),
  63. source: this.props.route.key,
  64. target: homekey,
  65. };
  66. });
  67. }
  68. });
  69. }*/
  70. this.getEmail();
  71. }
  72. async getEmail() {
  73. const data = await getStorageJsonSync('loginData');
  74. if (data && data.email) {
  75. this.setState({
  76. email: data.email
  77. });
  78. }
  79. }
  80. onLogin() {
  81. //console.log(this.state);
  82. if (this.state.email == '') {
  83. toastShort('Please enter email address');
  84. return;
  85. }
  86. if (this.state.password == '') {
  87. toastShort('Please enter password');
  88. return;
  89. }
  90. Dialog.showProgressDialog();
  91. apiUser.login(this.state).then(res => {
  92. console.log('res.data', res);
  93. if (res.data.accessToken) {
  94. setAccessToken(res.data.accessToken);
  95. Dialog.dismissLoading();
  96. if (this.state.rememberMe) {
  97. setStorageJson('loginData', this.state);
  98. } else {
  99. setStorageJson('loginData', {});
  100. }
  101. startPage(PageList.home);
  102. //this.props.navigation.goBack();
  103. } else {
  104. toastShort(res.msg);
  105. Dialog.dismissLoading();
  106. }
  107. }).catch((err) => {
  108. toastShort(err);
  109. Dialog.dismissLoading();
  110. });
  111. }
  112. getBackTopPosition() {
  113. return isIOS ? statusHeight : 4;
  114. }
  115. render() {
  116. return (
  117. <View style={ui.container}>
  118. <View style={[styles.backBtn, {top: this.getBackTopPosition()}]}>
  119. <BackButton/>
  120. </View>
  121. <ScrollView
  122. style={styles.container}
  123. keyboardShouldPersistTaps={'handled'}>
  124. <View style={styles.header}>
  125. <Image
  126. style={styles.headerImg}
  127. resizeMode='contain'
  128. source={require('../../images/app-logo.png')} />
  129. </View>
  130. <View style={styles.loginView}>
  131. <View style={styles.loginForm}>
  132. <View style={styles.inputView}>
  133. <Zocial name='email' size={28} color='#999999' />
  134. <TextInput
  135. style={styles.inputText}
  136. placeholder={"E-mail"}
  137. placeholderTextColor={textPlacehoder}
  138. textContentType='emailAddress'
  139. defaultValue={this.state.email}
  140. maxLength={50}
  141. onChangeText={(v) => {
  142. this.setState({
  143. email: v
  144. })
  145. }}/>
  146. </View>
  147. <View style={styles.inputView}>
  148. <Fontisto name='locked' size={28} color='#999999' style={{marginLeft: 3, marginRight: 2}} />
  149. <TextInput
  150. style={styles.inputText}
  151. placeholder="Password"
  152. placeholderTextColor={textPlacehoder}
  153. textContentType='password'
  154. secureTextEntry={true}
  155. maxLength={20}
  156. onChangeText={(v) => {
  157. this.setState({
  158. password: v
  159. })
  160. }}
  161. onSubmitEditing={() => {
  162. this.onLogin();
  163. }}/>
  164. </View>
  165. <View style={ui.flexcw}>
  166. <View style={$padding(12, 8)}>
  167. <CheckBoxText
  168. value={this.state.rememberMe}
  169. onValueChange={(newValue) => {
  170. this.setState({ rememberMe: newValue });
  171. }}
  172. text={'Remember me'}
  173. />
  174. </View>
  175. <Text
  176. style={styles.linksText}
  177. onPress={() => startPage(PageList.forgotPassword)}>Forgot Password</Text>
  178. </View>
  179. <Button
  180. style={styles.loginButton}
  181. text='SIGN IN'
  182. elevation={1.5}
  183. onClick={() => {
  184. this.onLogin()
  185. }}
  186. />
  187. </View>
  188. </View>
  189. <View style={styles.signView}>
  190. <Text style={{color: '#333'}}>New User?</Text>
  191. <Text
  192. style={styles.linksText}
  193. onPress={() => {
  194. startPage(PageList.register);
  195. }}
  196. >Click here to sign up</Text>
  197. </View>
  198. </ScrollView>
  199. </View>
  200. );
  201. }
  202. };
  203. const styles = StyleSheet.create({
  204. container: {
  205. flex: 1,
  206. backgroundColor: colorLight
  207. },
  208. backBtn:{
  209. top: 4,
  210. left: 2,
  211. zIndex: 1,
  212. position: 'absolute'
  213. },
  214. header: {
  215. height: 260,
  216. paddingTop: 56,
  217. paddingBottom: 20,
  218. paddingLeft: 32,
  219. paddingRight: 32,
  220. alignItems: 'center'
  221. },
  222. headerImg: {
  223. width: $vw(63),
  224. height: 100
  225. },
  226. loginView: {
  227. padding: 16,
  228. marginTop: -24,
  229. borderTopLeftRadius: 24,
  230. borderTopRightRadius: 24,
  231. backgroundColor: 'white'
  232. },
  233. logoImg: {
  234. width:136.19,
  235. height: 42.85,
  236. marginTop: 18
  237. },
  238. loginForm: {
  239. paddingLeft: 16,
  240. paddingRight: 16,
  241. },
  242. inputView: {
  243. marginTop: 30,
  244. borderRadius: 8,
  245. paddingTop: 6,
  246. paddingLeft: 16,
  247. paddingRight: 16,
  248. paddingBottom: 6,
  249. alignItems: 'center',
  250. flexDirection: 'row',
  251. backgroundColor: '#F5F5F5'
  252. },
  253. inputText: {
  254. flex: 1,
  255. color: '#333',
  256. fontSize: 15,
  257. paddingTop: 6,
  258. paddingLeft: 16,
  259. paddingBottom: 6
  260. },
  261. loginButton: {
  262. marginTop: 32,
  263. borderRadius: 4
  264. },
  265. signView: {
  266. paddingTop: 32,
  267. paddingBottom: 16,
  268. alignItems: 'center',
  269. },
  270. checkText: {
  271. color: '#333',
  272. fontSize: 14,
  273. paddingLeft: 8
  274. },
  275. linksText: {
  276. ...ui.link,
  277. padding: 8,
  278. fontSize: 14,
  279. textDecorationLine: 'underline'
  280. }
  281. });