LoginV2.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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 CheckBoxText from '../../components/CheckBoxText';
  15. import TextView from '../../components/TextView';
  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={styles.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. clearButtonMode='while-editing'
  141. autoCapitalize="none"
  142. autoComplete="off"
  143. autoCorrect={false}
  144. maxLength={50}
  145. onChangeText={(v) => {
  146. this.setState({
  147. email: v
  148. })
  149. }}/>
  150. </View>
  151. <View style={styles.inputView}>
  152. <Fontisto name='locked' size={28} color='#999999' style={{marginLeft: 3, marginRight: 2}} />
  153. <TextInput
  154. style={styles.inputText}
  155. placeholder="Password"
  156. placeholderTextColor={textPlacehoder}
  157. textContentType='password'
  158. secureTextEntry={true}
  159. maxLength={20}
  160. onChangeText={(v) => {
  161. this.setState({
  162. password: v
  163. })
  164. }}
  165. onSubmitEditing={() => {
  166. this.onLogin();
  167. }}/>
  168. </View>
  169. <View style={ui.flexcw}>
  170. <View style={$padding(12, 8)}>
  171. <CheckBoxText
  172. value={this.state.rememberMe}
  173. onValueChange={(newValue) => {
  174. this.setState({ rememberMe: newValue });
  175. }}
  176. text={'Remember me'}
  177. />
  178. </View>
  179. <TextView
  180. style={styles.linksText}
  181. onPress={() => startPage(PageList.forgotPassword)}>Forgot Password</TextView>
  182. </View>
  183. <Button
  184. style={styles.loginButton}
  185. text='SIGN IN'
  186. elevation={1.5}
  187. onClick={() => {
  188. this.onLogin()
  189. }}
  190. />
  191. </View>
  192. </View>
  193. <View style={styles.signView}>
  194. <TextView style={{color: textPrimary}}>New User?</TextView>
  195. <TextView
  196. style={styles.linksText}
  197. onPress={() => {
  198. startPage(PageList.register);
  199. }}
  200. >Click here to sign up</TextView>
  201. </View>
  202. </ScrollView>
  203. </View>
  204. );
  205. }
  206. };
  207. const styles = StyleSheet.create({
  208. container: {
  209. flex: 1,
  210. backgroundColor: colorThemes
  211. },
  212. backBtn:{
  213. top: 4,
  214. left: 2,
  215. zIndex: 1,
  216. position: 'absolute'
  217. },
  218. header: {
  219. height: 260,
  220. paddingTop: 56,
  221. paddingBottom: 20,
  222. paddingLeft: 32,
  223. paddingRight: 32,
  224. alignItems: 'center'
  225. },
  226. headerImg: {
  227. width: $vw(63),
  228. height: 60,
  229. marginTop: 60
  230. },
  231. loginView: {
  232. padding: 16,
  233. marginTop: -24,
  234. borderTopLeftRadius: 24,
  235. borderTopRightRadius: 24
  236. },
  237. logoImg: {
  238. width:136.19,
  239. height: 40,
  240. marginTop: 18
  241. },
  242. loginForm: {
  243. paddingLeft: 16,
  244. paddingRight: 16,
  245. },
  246. inputView: {
  247. marginTop: 30,
  248. borderRadius: 8,
  249. paddingTop: 6,
  250. paddingLeft: 16,
  251. paddingRight: 16,
  252. paddingBottom: 6,
  253. alignItems: 'center',
  254. flexDirection: 'row',
  255. backgroundColor: '#F5F5F5'
  256. },
  257. inputText: {
  258. flex: 1,
  259. color: textPrimary,
  260. fontSize: 15,
  261. paddingTop: 6,
  262. paddingLeft: 16,
  263. paddingBottom: 6
  264. },
  265. loginButton: {
  266. marginTop: 32
  267. },
  268. signView: {
  269. paddingTop: 32,
  270. paddingBottom: 16,
  271. alignItems: 'center',
  272. },
  273. checkText: {
  274. color: textPrimary,
  275. fontSize: 14,
  276. paddingLeft: 8
  277. },
  278. linksText: {
  279. ...ui.link,
  280. padding: 8,
  281. fontSize: 14,
  282. textDecorationLine: 'underline'
  283. }
  284. });