LoginV2.js 8.2 KB

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