123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396 |
- import React, {Component} from 'react';
- import {
- View,
- Text,
- TextInput,
- TouchableOpacity,
- Image,
- SafeAreaView,
- DeviceEventEmitter,
- ScrollView,
- } from 'react-native';
- import {WhiteSpace, WingBlank} from '@ant-design/react-native';
- import {
- IndividualStorageData,
- RetrieveData,
- StorageData,
- } from '../../data/storage';
- import {RequestNetwork} from '../../data/encryption';
- import Spinner from 'react-native-loading-spinner-overlay';
- import public_css from '../../source/css/public_css';
- import loading_css from '../../source/css/loading_css';
- import login_css from './login_css';
- import {CheckPhoneNumber, CheckPassword} from '../../source/inspect/inspect';
- import {ToastShow} from '../../components/toast/toast';
- export default class login extends Component {
- constructor(props) {
- super(props);
- this.props.navigation.dangerouslyGetParent().setOptions({
- tabBarVisible: false,
- });
- this.afterEnd = this.afterEnd;
- this.state = {
- user_name: '',
- user_password: '',
- user_verification: '',
- user_status: 1,
- token: '',
- timeLeft: 60,
- begin: 0,
- isDisable: false,
- spinner: false,
- initRealmMark: false,
- initData: 'loading',
- };
- }
- render() {
- return (
- <SafeAreaView style={public_css.body}>
- <View style={public_css.body}>
- <View style={login_css.bg}>
- <View style={login_css.login_bg_img}>
- <Image
- source={require('../../source/img/login/login_bg.png')}
- style={login_css.bgImg}
- resizeMode="cover"
- />
- </View>
- <Spinner
- visible={this.state.spinner}
- textContent={this.state.initData}
- textStyle={loading_css.spinnerTextStyle}
- />
- <View style={login_css.full_box}>
- <ScrollView style={login_css.inner_con}>
- <WhiteSpace />
- <WingBlank>
- <View style={login_css.input_item}>
- <Text style={login_css.text_label}>手机号:</Text>
- <TextInput
- style={login_css.text_input}
- ref="mobileInput"
- placeholder="请输入您的手机号码"
- clearButtonMode="while-editing"
- secureTextEntry={false}
- keyboardType={'numeric'}
- onChangeText={(text) => {
- this.setState({
- user_name: text,
- });
- }}
- />
- </View>
- </WingBlank>
- <WingBlank>
- {this.state.user_status === 1 ? (
- <View style={login_css.input_item}>
- <Text style={login_css.text_label}>密码:</Text>
- <TextInput
- style={login_css.text_input}
- ref="passwordInput"
- placeholder="请输入您的密码"
- clearButtonMode="while-editing"
- secureTextEntry
- value={this.state.user_password}
- onChangeText={(text) => {
- this.setState({
- user_password: text,
- });
- }}
- />
- </View>
- ) : (
- <View style={login_css.input_item}>
- <Text style={login_css.text_label}>验证码:</Text>
- <TextInput
- style={login_css.text_input}
- placeholder="请输入您的短信验证码"
- clearButtonMode="while-editing"
- value={this.state.user_verification}
- onChangeText={(text) => {
- this.setState({
- user_verification: text,
- });
- }}
- />
- <TouchableOpacity
- style={login_css.vertify}
- disabled={this.state.isDisable}
- onPress={this.beginCountDown.bind(this)}>
- <Text style={login_css.texts}>
- {this.state.begin === 0
- ? '获取验证码'
- : this.state.timeLeft + '秒后重试'}
- </Text>
- </TouchableOpacity>
- </View>
- )}
- </WingBlank>
- <WhiteSpace />
- <WingBlank>
- <View
- style={{
- flexDirection: 'row',
- justifyContent: 'space-between',
- }}>
- <Text
- onPress={() =>
- this.props.navigation.navigate('forget_password')
- }
- style={{color: '#B6B8C0'}}>
- 忘记密码?
- </Text>
- </View>
- </WingBlank>
- <TouchableOpacity
- style={login_css.button}
- onPress={() => this.login()}>
- <View style={login_css.buttonView}>
- <Text style={login_css.buttonText}>登 录</Text>
- </View>
- </TouchableOpacity>
- <View style={login_css.login_text_view}>
- {this.state.user_status === 1 ? (
- <Text
- onPress={() => this.changeState(2)}
- style={{color: '#1263EA'}}>
- 短信验证码登录
- </Text>
- ) : (
- <Text
- onPress={() => this.changeState(1)}
- style={{color: '#1263EA'}}>
- 账号登录
- </Text>
- )}
- </View>
- <WhiteSpace />
- <WhiteSpace />
- </ScrollView>
- </View>
- </View>
- {/*<View style={{alignItems: 'center', justifyContent: 'center', height: 50}}>*/}
- {/* <TouchableOpacity*/}
- {/* onPress={() => {*/}
- {/* this.loginWeChat();*/}
- {/* }}>*/}
- {/* <Image source={require('../../source/img/wechat.png')} />*/}
- {/* </TouchableOpacity>*/}
- {/*</View>*/}
- </View>
- </SafeAreaView>
- );
- }
- //是否显示Loading层
- setLoadingStatus(isLoading) {
- this.setState({
- spinner: isLoading,
- });
- }
- //更新验证码状态
- updateVerificationCodeStatus(timeLeft, callback, begin) {
- if (timeLeft > 0) {
- this.state.begin = 1;
- let that = this;
- let interval = setInterval(function () {
- if (that.state.timeLeft < 1) {
- clearInterval(interval);
- callback(that);
- that.setState({
- isDisable: false,
- });
- } else {
- let totalTime = that.state.timeLeft;
- that.setState({
- timeLeft: totalTime - 1,
- isDisable: true,
- });
- }
- }, 1000);
- }
- }
- //开始验证码计时
- beginCountDown = async () => {
- let {status, msg} = CheckPhoneNumber(`${this.state.user_name}`);
- if (!status) {
- ToastShow(1, msg);
- return;
- }
- if (this.state.begin === 1) {
- return;
- }
- let time = this.state.timeLeft;
- let afterEnd = this.afterEnd;
- let begin = this.state.begin;
- this.updateVerificationCodeStatus(time, afterEnd, begin);
- await this.sendVerificationCode();
- };
- //验证码时间
- afterEnd(that) {
- that.setState({
- begin: 0,
- timeLeft: 60,
- });
- }
- //修改登录方式状态
- changeState = async (status) => {
- this.setState({
- user_status: status,
- user_password: '',
- user_verification: '',
- });
- };
- //登录
- login = async () => {
- if (this.state.user_status === 2) {
- if (this.state.user_verification === '') {
- ToastShow(1, '验证码不能为空!');
- return;
- }
- let {status, msg} = CheckPhoneNumber(`${this.state.user_name}`);
- if (status) {
- await this.signIn();
- } else {
- ToastShow(1, msg);
- }
- } else {
- let {status, msg} = CheckPhoneNumber(`${this.state.user_name}`);
- if (status) {
- let {status, msg} = CheckPassword(`${this.state.user_password}`);
- if (status) {
- await this.signIn();
- } else {
- ToastShow(1, msg);
- }
- } else {
- ToastShow(1, msg);
- }
- }
- };
- //提交登录信息
- signIn = async () => {
- this.setLoadingStatus(true);
- const url = '/login';
- let token = null;
- let response = await RequestNetwork(
- url,
- token,
- {
- account: this.state.user_name,
- password: this.state.user_password,
- smsCode: this.state.user_verification,
- reqChannel: 3,
- loginType: this.state.user_status,
- },
- false,
- 1,
- );
- if (response) {
- if (response.code === 0) {
- this.setLoadingStatus(false);
- await StorageData(response);
- await this.getCompanyInfo(response.data.account, response.data.token);
- await this.getUserInformation();
- DeviceEventEmitter.emit('updateCompany', null);
- this.props.route.params.refresh(true);
- this.props.navigation.goBack();
- } else {
- this.setLoadingStatus(false);
- ToastShow(1, response.msg);
- }
- } else {
- this.setLoadingStatus(false);
- ToastShow(1, response.msg);
- }
- };
- // 获取默认企业信息
- getCompanyInfo = async (account, token) => {
- const url = '/auth/comm/user/findDefaultChoose';
- let response = await RequestNetwork(
- url,
- token,
- {
- mobile: account,
- },
- false,
- 2,
- );
- if (response) {
- if (response.code === 0) {
- await IndividualStorageData('company', JSON.stringify(response.data));
- }
- }
- };
- //获取短信验证码
- sendVerificationCode = async () => {
- let {status, msg} = CheckPhoneNumber(`${this.state.user_name}`);
- if (status) {
- const url = '/sms/getSmscode';
- let token = null;
- let response = await RequestNetwork(
- url,
- token,
- {
- phoneNo: this.state.user_name,
- },
- false,
- 1,
- );
- if (response) {
- ToastShow(1, '验证码发送成功!');
- } else {
- ToastShow(1, '验证码发送失败!');
- }
- } else {
- ToastShow(1, msg);
- }
- };
- //获取用户信息
- getUserInformation = async () => {
- let account = await RetrieveData('account');
- let token = await RetrieveData('token');
- if (token && account) {
- const url = '/auth/comm/user/personalInfo/find';
- let response = await RequestNetwork(
- url,
- token,
- {
- mobile: account,
- },
- false,
- 2,
- );
- if (response) {
- if (response.code === 0) {
- let userHeadImg = {
- headImage: '',
- sex: 'M',
- };
- if (response.data !== null) {
- userHeadImg.headImage = response.data.avatar;
- userHeadImg.sex = response.data.sex;
- }
- await IndividualStorageData(
- 'userHeadImg',
- JSON.stringify(userHeadImg),
- );
- }
- }
- }
- };
- }
|