LoginV2.js 8.1 KB

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