login.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  1. import React, {Component} from 'react';
  2. import {
  3. View,
  4. Text,
  5. TextInput,
  6. TouchableOpacity,
  7. Image,
  8. SafeAreaView,
  9. DeviceEventEmitter,
  10. ScrollView,
  11. } from 'react-native';
  12. import {WhiteSpace, WingBlank} from '@ant-design/react-native';
  13. import {
  14. IndividualStorageData,
  15. RetrieveData,
  16. StorageData,
  17. } from '../../data/storage';
  18. import {RequestNetwork} from '../../data/encryption';
  19. import Spinner from 'react-native-loading-spinner-overlay';
  20. import public_css from '../../source/css/public_css';
  21. import loading_css from '../../source/css/loading_css';
  22. import login_css from './login_css';
  23. import {CheckPhoneNumber, CheckPassword} from '../../source/inspect/inspect';
  24. import {ToastShow} from '../../components/toast/toast';
  25. export default class login extends Component {
  26. constructor(props) {
  27. super(props);
  28. this.props.navigation.dangerouslyGetParent().setOptions({
  29. tabBarVisible: false,
  30. });
  31. this.afterEnd = this.afterEnd;
  32. this.state = {
  33. user_name: '',
  34. user_password: '',
  35. user_verification: '',
  36. user_status: 1,
  37. token: '',
  38. timeLeft: 60,
  39. begin: 0,
  40. isDisable: false,
  41. spinner: false,
  42. initRealmMark: false,
  43. initData: 'loading',
  44. };
  45. }
  46. render() {
  47. return (
  48. <SafeAreaView style={public_css.body}>
  49. <View style={public_css.body}>
  50. <View style={login_css.bg}>
  51. <View style={login_css.login_bg_img}>
  52. <Image
  53. source={require('../../source/img/login/login_bg.png')}
  54. style={login_css.bgImg}
  55. resizeMode="cover"
  56. />
  57. </View>
  58. <Spinner
  59. visible={this.state.spinner}
  60. textContent={this.state.initData}
  61. textStyle={loading_css.spinnerTextStyle}
  62. />
  63. <View style={login_css.full_box}>
  64. <ScrollView style={login_css.inner_con}>
  65. <WhiteSpace />
  66. <WingBlank>
  67. <View style={login_css.input_item}>
  68. <Text style={login_css.text_label}>手机号:</Text>
  69. <TextInput
  70. style={login_css.text_input}
  71. ref="mobileInput"
  72. placeholder="请输入您的手机号码"
  73. clearButtonMode="while-editing"
  74. secureTextEntry={false}
  75. keyboardType={'numeric'}
  76. onChangeText={(text) => {
  77. this.setState({
  78. user_name: text,
  79. });
  80. }}
  81. />
  82. </View>
  83. </WingBlank>
  84. <WingBlank>
  85. {this.state.user_status === 1 ? (
  86. <View style={login_css.input_item}>
  87. <Text style={login_css.text_label}>密码:</Text>
  88. <TextInput
  89. style={login_css.text_input}
  90. ref="passwordInput"
  91. placeholder="请输入您的密码"
  92. clearButtonMode="while-editing"
  93. secureTextEntry
  94. value={this.state.user_password}
  95. onChangeText={(text) => {
  96. this.setState({
  97. user_password: text,
  98. });
  99. }}
  100. />
  101. </View>
  102. ) : (
  103. <View style={login_css.input_item}>
  104. <Text style={login_css.text_label}>验证码:</Text>
  105. <TextInput
  106. style={login_css.text_input}
  107. placeholder="请输入您的短信验证码"
  108. clearButtonMode="while-editing"
  109. value={this.state.user_verification}
  110. onChangeText={(text) => {
  111. this.setState({
  112. user_verification: text,
  113. });
  114. }}
  115. />
  116. <TouchableOpacity
  117. style={login_css.vertify}
  118. disabled={this.state.isDisable}
  119. onPress={this.beginCountDown.bind(this)}>
  120. <Text style={login_css.texts}>
  121. {this.state.begin === 0
  122. ? '获取验证码'
  123. : this.state.timeLeft + '秒后重试'}
  124. </Text>
  125. </TouchableOpacity>
  126. </View>
  127. )}
  128. </WingBlank>
  129. <WhiteSpace />
  130. <WingBlank>
  131. <View
  132. style={{
  133. flexDirection: 'row',
  134. justifyContent: 'space-between',
  135. }}>
  136. <Text
  137. onPress={() =>
  138. this.props.navigation.navigate('forget_password')
  139. }
  140. style={{color: '#B6B8C0'}}>
  141. 忘记密码?
  142. </Text>
  143. </View>
  144. </WingBlank>
  145. <TouchableOpacity
  146. style={login_css.button}
  147. onPress={() => this.login()}>
  148. <View style={login_css.buttonView}>
  149. <Text style={login_css.buttonText}>登 录</Text>
  150. </View>
  151. </TouchableOpacity>
  152. <View style={login_css.login_text_view}>
  153. {this.state.user_status === 1 ? (
  154. <Text
  155. onPress={() => this.changeState(2)}
  156. style={{color: '#1263EA'}}>
  157. 短信验证码登录
  158. </Text>
  159. ) : (
  160. <Text
  161. onPress={() => this.changeState(1)}
  162. style={{color: '#1263EA'}}>
  163. 账号登录
  164. </Text>
  165. )}
  166. </View>
  167. <WhiteSpace />
  168. <WhiteSpace />
  169. </ScrollView>
  170. </View>
  171. </View>
  172. {/*<View style={{alignItems: 'center', justifyContent: 'center', height: 50}}>*/}
  173. {/* <TouchableOpacity*/}
  174. {/* onPress={() => {*/}
  175. {/* this.loginWeChat();*/}
  176. {/* }}>*/}
  177. {/* <Image source={require('../../source/img/wechat.png')} />*/}
  178. {/* </TouchableOpacity>*/}
  179. {/*</View>*/}
  180. </View>
  181. </SafeAreaView>
  182. );
  183. }
  184. //是否显示Loading层
  185. setLoadingStatus(isLoading) {
  186. this.setState({
  187. spinner: isLoading,
  188. });
  189. }
  190. //更新验证码状态
  191. updateVerificationCodeStatus(timeLeft, callback, begin) {
  192. if (timeLeft > 0) {
  193. this.state.begin = 1;
  194. let that = this;
  195. let interval = setInterval(function () {
  196. if (that.state.timeLeft < 1) {
  197. clearInterval(interval);
  198. callback(that);
  199. that.setState({
  200. isDisable: false,
  201. });
  202. } else {
  203. let totalTime = that.state.timeLeft;
  204. that.setState({
  205. timeLeft: totalTime - 1,
  206. isDisable: true,
  207. });
  208. }
  209. }, 1000);
  210. }
  211. }
  212. //开始验证码计时
  213. beginCountDown = async () => {
  214. let {status, msg} = CheckPhoneNumber(`${this.state.user_name}`);
  215. if (!status) {
  216. ToastShow(1, msg);
  217. return;
  218. }
  219. if (this.state.begin === 1) {
  220. return;
  221. }
  222. let time = this.state.timeLeft;
  223. let afterEnd = this.afterEnd;
  224. let begin = this.state.begin;
  225. this.updateVerificationCodeStatus(time, afterEnd, begin);
  226. await this.sendVerificationCode();
  227. };
  228. //验证码时间
  229. afterEnd(that) {
  230. that.setState({
  231. begin: 0,
  232. timeLeft: 60,
  233. });
  234. }
  235. //修改登录方式状态
  236. changeState = async (status) => {
  237. this.setState({
  238. user_status: status,
  239. user_password: '',
  240. user_verification: '',
  241. });
  242. };
  243. //登录
  244. login = async () => {
  245. if (this.state.user_status === 2) {
  246. if (this.state.user_verification === '') {
  247. ToastShow(1, '验证码不能为空!');
  248. return;
  249. }
  250. let {status, msg} = CheckPhoneNumber(`${this.state.user_name}`);
  251. if (status) {
  252. await this.signIn();
  253. } else {
  254. ToastShow(1, msg);
  255. }
  256. } else {
  257. let {status, msg} = CheckPhoneNumber(`${this.state.user_name}`);
  258. if (status) {
  259. let {status, msg} = CheckPassword(`${this.state.user_password}`);
  260. if (status) {
  261. await this.signIn();
  262. } else {
  263. ToastShow(1, msg);
  264. }
  265. } else {
  266. ToastShow(1, msg);
  267. }
  268. }
  269. };
  270. //提交登录信息
  271. signIn = async () => {
  272. this.setLoadingStatus(true);
  273. const url = '/login';
  274. let token = null;
  275. let response = await RequestNetwork(
  276. url,
  277. token,
  278. {
  279. account: this.state.user_name,
  280. password: this.state.user_password,
  281. smsCode: this.state.user_verification,
  282. reqChannel: 3,
  283. loginType: this.state.user_status,
  284. },
  285. false,
  286. 1,
  287. );
  288. if (response) {
  289. if (response.code === 0) {
  290. this.setLoadingStatus(false);
  291. await StorageData(response);
  292. await this.getCompanyInfo(response.data.account, response.data.token);
  293. await this.getUserInformation();
  294. DeviceEventEmitter.emit('updateCompany', null);
  295. this.props.route.params.refresh(true);
  296. this.props.navigation.goBack();
  297. } else {
  298. this.setLoadingStatus(false);
  299. ToastShow(1, response.msg);
  300. }
  301. } else {
  302. this.setLoadingStatus(false);
  303. ToastShow(1, response.msg);
  304. }
  305. };
  306. // 获取默认企业信息
  307. getCompanyInfo = async (account, token) => {
  308. const url = '/auth/comm/user/findDefaultChoose';
  309. let response = await RequestNetwork(
  310. url,
  311. token,
  312. {
  313. mobile: account,
  314. },
  315. false,
  316. 2,
  317. );
  318. if (response) {
  319. if (response.code === 0) {
  320. await IndividualStorageData('company', JSON.stringify(response.data));
  321. }
  322. }
  323. };
  324. //获取短信验证码
  325. sendVerificationCode = async () => {
  326. let {status, msg} = CheckPhoneNumber(`${this.state.user_name}`);
  327. if (status) {
  328. const url = '/sms/getSmscode';
  329. let token = null;
  330. let response = await RequestNetwork(
  331. url,
  332. token,
  333. {
  334. phoneNo: this.state.user_name,
  335. },
  336. false,
  337. 1,
  338. );
  339. if (response) {
  340. ToastShow(1, '验证码发送成功!');
  341. } else {
  342. ToastShow(1, '验证码发送失败!');
  343. }
  344. } else {
  345. ToastShow(1, msg);
  346. }
  347. };
  348. //获取用户信息
  349. getUserInformation = async () => {
  350. let account = await RetrieveData('account');
  351. let token = await RetrieveData('token');
  352. if (token && account) {
  353. const url = '/auth/comm/user/personalInfo/find';
  354. let response = await RequestNetwork(
  355. url,
  356. token,
  357. {
  358. mobile: account,
  359. },
  360. false,
  361. 2,
  362. );
  363. if (response) {
  364. if (response.code === 0) {
  365. let userHeadImg = {
  366. headImage: '',
  367. sex: 'M',
  368. };
  369. if (response.data !== null) {
  370. userHeadImg.headImage = response.data.avatar;
  371. userHeadImg.sex = response.data.sex;
  372. }
  373. await IndividualStorageData(
  374. 'userHeadImg',
  375. JSON.stringify(userHeadImg),
  376. );
  377. }
  378. }
  379. }
  380. };
  381. }