king
2025-05-13 1a176e4bdba485301385caac1a29102e598d25cc
src/views/login/loginform.jsx
@@ -1,20 +1,15 @@
import React, {Component} from 'react'
import PropTypes from 'prop-types'
import { Form, Input, Button, Checkbox, Select, Modal, message, AutoComplete } from 'antd'
import { UserOutlined, LockOutlined, QrcodeOutlined, RedoOutlined, CloseCircleOutlined } from '@ant-design/icons'
import { Form, Input, Button, Checkbox, Modal, message, AutoComplete, Select } from 'antd'
import { UserOutlined, LockOutlined, CloseCircleOutlined } from '@ant-design/icons'
import md5 from 'md5'
import moment from 'moment'
import Api from '@/api'
import Utils from '@/utils/utils.js'
import options from '@/store/options.js'
import asyncLoadComponent from '@/utils/asyncLoadComponent'
import wxicon from '@/assets/img/wx-icon.png'
import './index.scss'
const { warning } = Modal
let LoginVerCodeTimer = null
const QrCode = asyncLoadComponent(() => import('@/components/qrcode'))
class LoginTabForm extends Component {
  static propTpyes = {
@@ -28,35 +23,25 @@
  }
  state = {
    activeKey: 'uname_pwd',
    scanId: '',
    username: '',
    password: '',
    remember: true,
    protocol: null,
    delay: null,
    loginWays: [],
    smsId: '',
    verdisabled: false,
    hasScan: false,
    timeout: false,
    vispwd: window.GLOB.vispwd,
    wayLabels: {},
    dict: window.GLOB.dict,
    users: [],
    lang: sessionStorage.getItem('lang')
  }
  timer = null
  splitTime = 0
  UNSAFE_componentWillMount () {
    const { loginWays } = this.props
    let remember = true
    if (localStorage.getItem(window.location.href.split('#')[0] + 'users')) { // 过渡
      localStorage.setItem(window.GLOB.sysSign + 'users', localStorage.getItem(window.location.href.split('#')[0] + 'users'))
      localStorage.setItem(window.GLOB.sysSign + 'remember', localStorage.getItem(window.location.href.split('#')[0] + 'remember'))
      localStorage.removeItem(window.location.href.split('#')[0] + 'users')
    }
    
    if (localStorage.getItem(window.GLOB.sysSign + 'remember') === 'false') {
      remember = false
@@ -66,29 +51,25 @@
    }
    let smsId = ''
    let hasScan = false
    let _loginWays = []
    let wayLabels = {app_scan: 'APP扫码', weixin_scan: '微信扫码', uname_pwd: '账号登录', sms_vcode: '短信登录'}
    // let wayLabels = {app_scan: 'APP扫码', weixin_scan: '微信扫码', uname_pwd: '账号登录', sms_vcode: '短信登录'}
    loginWays.forEach(item => {
      item.label = window.GLOB.dict[item.type] || wayLabels[item.type]
      wayLabels[item.type] = item.label
      if (item.type === 'sms_vcode') {
        smsId = item.smsId
        _loginWays.push(item)
      } else if (item.type === 'uname_pwd') {
        _loginWays.push(item)
      } else if (item.type === 'app_scan') {
        _loginWays.push(item)
        hasScan = true
      } else if (item.type === 'weixin_scan') {
        _loginWays.push(item)
        hasScan = true
      }
    })
    // loginWays.forEach(item => {
    //   item.label = window.GLOB.dict[item.type] || wayLabels[item.type]
    //   wayLabels[item.type] = item.label
    //   if (item.type === 'sms_vcode') {
    //     smsId = item.smsId
    //     _loginWays.push(item)
    //   } else if (item.type === 'uname_pwd') {
    //     _loginWays.push(item)
    //   } else if (item.type === 'app_scan') {
    //     _loginWays.push(item)
    //     hasScan = true
    //   } else if (item.type === 'weixin_scan') {
    //     _loginWays.push(item)
    //     hasScan = true
    //   }
    // })
    let activeKey = _loginWays[0].type
    let users = localStorage.getItem(window.GLOB.sysSign + 'users')
    let _user = null
    
@@ -102,82 +83,24 @@
      users = []
    }
    if (users[0]) {
      _user = users[0]
    }
    this.setState({
      users: users,
      username: activeKey === 'uname_pwd' && _user ? _user.username : '',
      password: activeKey === 'uname_pwd' && _user ? _user.password : '',
      username: _user ? _user.username : '',
      password: _user ? _user.password : '',
      smsId: smsId,
      loginWays: _loginWays,
      activeKey,
      scanId: activeKey === 'app_scan' || activeKey === 'weixin_scan' ? Utils.getuuid() : '',
      timeout: false,
      remember,
      wayLabels,
      hasScan
    })
    if (activeKey === 'app_scan' || activeKey === 'weixin_scan') {
      this.splitTime = 0
      this.timer = setTimeout(() => {
        this.checkResult()
      }, 10000)
    }
  }
  checkResult = () => {
    const { scanId } = this.state
    this.splitTime += 10000
    let _param = {
      func: 'webapi_get_binding_key',
      scan_type: 'pc',
      id: scanId,
      UserName: ''
    }
    _param.userid = sessionStorage.getItem('visitorUserID')
    _param.LoginUID = sessionStorage.getItem('visitorLoginUID')
    if (this.splitTime >= 180000) {
      this.setState({
        timeout: true
      })
      return
    }
    Api.getSystemConfig(_param).then(res => {
      if (!res.status) {
        message.warning(res.message)
        return
      } else if (res.thd_party_appid && res.thd_party_member_id && res.thd_party_openid) {
        this.props.authLogin(res.thd_party_appid, res.thd_party_openid, res.thd_party_member_id, scanId)
      } else {
        this.timer = setTimeout(() => {
          this.checkResult()
        }, 10000)
      }
      remember
    })
  }
  handleConfirm = () => {
    const { activeKey } = this.state
    // 表单提交时检查输入值是否正确
    return new Promise((resolve, reject) => {
      this.props.form.validateFieldsAndScroll((err, values) => {
        if (!err) {
          if (activeKey === 'uname_pwd') {
            values.username = values.username.replace(/\t+|\v+|\s+/g, '')
            values.password = values.password.replace(/\t+|\v+|\s+/g, '')
          } else if (activeKey === 'sms_vcode') {
            values.phone = values.phone.replace(/\t+|\v+|\s+/g, '')
            values.vercode = values.vercode.replace(/\t+|\v+|\s+/g, '')
          }
          resolve({type: activeKey, ...values})
          values.username = values.username.replace(/\t+|\v+|\s+/g, '')
          values.password = values.password.replace(/\t+|\v+|\s+/g, '')
          resolve({type: 'uname_pwd', ...values})
        } else {
          reject(err)
        }
@@ -193,7 +116,7 @@
  }
  handleSubmit = e => {
    const { activeKey, dict } = this.state
    const { dict } = this.state
    // 登录参数检验
    e && e.preventDefault()
    if (!this.props.auth) {
@@ -206,35 +129,19 @@
      return
    }
    if (activeKey === 'uname_pwd') {
      if (!this.props.form.getFieldValue('username')) {
        const wrap = document.getElementById('username')
        const input = wrap ? wrap.getElementsByTagName('input')[0] : null
        if (input) {
          input.focus()
        }
      } else if (!this.props.form.getFieldValue('password')) {
        const input = document.getElementById('password')
        if (input) {
          input.focus()
        }
      } else {
        this.props.handleSubmit()
    if (!this.props.form.getFieldValue('username')) {
      const wrap = document.getElementById('username')
      const input = wrap ? wrap.getElementsByTagName('input')[0] : null
      if (input) {
        input.focus()
      }
    } else if (activeKey === 'sms_vcode') {
      if (!this.props.form.getFieldValue('phone')) {
        const input = document.getElementById('phone')
        if (input) {
          input.focus()
        }
      } else if (!this.props.form.getFieldValue('vercode')) {
        const input = document.getElementById('vercode')
        if (input) {
          input.focus()
        }
      } else {
        this.props.handleSubmit()
    } else if (!this.props.form.getFieldValue('password')) {
      const input = document.getElementById('password')
      if (input) {
        input.focus()
      }
    } else {
      this.props.handleSubmit()
    }
  }
@@ -244,38 +151,6 @@
    if (input) {
      input.focus()
    }
  }
  onChangeTab = (key) => {
    const { activeKey, loginWays } = this.state
    if (key === 'scan') {
      key = loginWays.filter(item => item.type === 'app_scan' || item.type === 'weixin_scan')[0].type
    }
    this.setState({activeKey: key, scanId: key === 'app_scan' || key === 'weixin_scan' ? Utils.getuuid() : ''})
    if (activeKey === 'app_scan' || activeKey === 'weixin_scan') {
      this.timer && clearTimeout(this.timer)
    }
    if (key === 'app_scan' || key === 'weixin_scan') {
      this.splitTime = 0
      this.setState({timeout: false})
      this.timer = setTimeout(() => {
        this.checkResult()
      }, 10000)
    }
  }
  reCode = () => {
    this.splitTime = 0
    this.setState({timeout: false, scanId: Utils.getuuid()})
    this.timer = setTimeout(() => {
      this.checkResult()
    }, 10000)
  }
  getvercode = () => {
@@ -428,6 +303,14 @@
    localStorage.setItem(window.GLOB.sysSign + 'users', window.btoa(window.encodeURIComponent(JSON.stringify(_users))))
  }
  changeAgree = (val) => {
    this.setState({protocol: val})
    if (this.scanParam && val) {
      this.props.authLogin(this.scanParam.thd_party_appid, this.scanParam.thd_party_openid, this.scanParam.thd_party_member_id, this.scanParam.scanId)
    }
  }
  /**
   * @description 组件销毁,清除state更新
   */
@@ -435,19 +318,17 @@
    this.setState = () => {
      return
    }
    this.timer && clearTimeout(this.timer)
  }
  render() {
    const { langList } = this.props
    const { langList, isDisabled } = this.props
    const { getFieldDecorator } = this.props.form
    const { activeKey, verdisabled, delay, loginWays, remember, scanId, timeout, hasScan, users, wayLabels, dict, lang } = this.state
    const { remember, users, dict, lang, vispwd } = this.state
    return (
      <Form className="login-form" id="login-form" onSubmit={this.handleSubmit}>
        <div className="login-way-title">{wayLabels[activeKey]}</div>
        {hasScan && activeKey !== 'app_scan' && activeKey !== 'weixin_scan' ? <div className="scan-icon" onClick={() => this.onChangeTab('scan')}><QrcodeOutlined /></div> : null}
        {activeKey === 'uname_pwd' ? <div className="form-item-wrap">
      <Form className={`login-form login-form-1`} id="login-form" onSubmit={this.handleSubmit}>
        <p className="title">{this.props.platName}</p>
        <div className="form-item-wrap">
          <Form.Item>
            {getFieldDecorator('username', {
              rules: [{ required: true, message: dict['username_required'] || '请输入用户名' }],
@@ -482,7 +363,7 @@
                  message: dict['password_required'] || '请输入密码',
                }
              ]
            })(<Input.Password placeholder={dict['password'] || '密码'} prefix={<LockOutlined style={{ color: 'rgba(0,0,0,.25)' }} />} />)}
            })(<Input.Password placeholder={dict['password'] || '密码'} visibilityToggle={vispwd} prefix={<LockOutlined style={{ color: 'rgba(0,0,0,.25)' }} />} />)}
          </Form.Item>
          {window.GLOB.keepKey ? <Form.Item className="minline">
            {getFieldDecorator('remember', {
@@ -505,103 +386,10 @@
            )}
          </Form.Item> : null}
          <Form.Item className="btn-login">
            <Button type="primary" htmlType="submit" className="login-form-button" disabled={this.props.isDisabled} loading={this.props.isDisabled}>
              {dict['log_in'] || '登录'}
            <Button type="primary" htmlType="submit" className="login-form-button" disabled={isDisabled} loading={isDisabled}>
            {dict['log_in'] || '登录'}
            </Button>
          </Form.Item>
          {window.GLOB.sysType === 'cloud' && options.cdomain.indexOf('mk9h') > -1 ? <Form.Item className="register-line">
            <a href="http://www.minkesoft.com/signup" target="_blank" rel="noopener noreferrer" className="register">注册</a>
            <a href="http://www.minkesoft.com/forgotPwd" target="_blank" rel="noopener noreferrer" className="forgot">忘记密码?</a>
          </Form.Item> : null}
        </div> : null}
        {activeKey === 'sms_vcode' ? <div className="form-item-wrap">
          <Form.Item>
            {getFieldDecorator('phone', {
              rules: [{ required: true, message: dict['phone_no_required'] || '请输入手机号' }],
              initialValue: '',
            })(
              <Input
                placeholder={dict['phone_no'] || '手机号'}
                autoComplete="off"
              />
            )}
          </Form.Item>
          <Form.Item className="vercode">
            {getFieldDecorator('vercode', {
              initialValue: '',
              rules: [
                {
                  required: true,
                  message: dict['vercode_required'] || '请输入验证码',
                }
              ]
            })(
              <Input
                addonAfter={
                  <Button type="link" className="vercode" size="small" disabled={verdisabled} onClick={this.getvercode}>
                    {delay ? `${delay}s` : dict['query_vercode'] || '获取验证码'}
                  </Button>
                }
                placeholder={dict['vercode'] || '验证码'}
                autoComplete="off"
              />
            )}
          </Form.Item>
          {langList && langList.length > 0 ? <Form.Item className="minline right">
            {getFieldDecorator('lang', {
              initialValue: lang,
            })(
              <Select
                onChange={(value) => {this.changelang(value)}}
                getPopupContainer={() => document.getElementById('login-form')}
              >
                {langList.map((item, index) => {
                  return <Select.Option key={index} value={item.Lang}>{item.LangName}</Select.Option>
                })}
              </Select>
            )}
          </Form.Item> : null}
          <Form.Item className="btn-login">
            <Button type="primary" htmlType="submit" className="login-form-button" disabled={this.props.isDisabled} loading={this.props.isDisabled}>
              {dict['log_in'] || '登录'}
            </Button>
          </Form.Item>
          {window.GLOB.sysType === 'cloud' && options.cdomain.indexOf('mk9h') > -1 ? <Form.Item className="register-line">
            <a href="http://www.minkesoft.com/signup" target="_blank" rel="noopener noreferrer" className="register">注册</a>
            <a href="http://www.minkesoft.com/forgotPwd" target="_blank" rel="noopener noreferrer" className="forgot">忘记密码?</a>
          </Form.Item> : null}
        </div> : null}
        {activeKey === 'weixin_scan' ? <div className="form-item-wrap">
          <div className="form-scan-wrap">
            <div className="qr-wrap">
              {scanId ? <QrCode card={{qrWidth: 500, color: '#000000'}} value={`https://cloud.mk9h.cn/mob/mknotice.html?originurl=${window.btoa(window.GLOB.baseurl + 'mob/index.html#/wx/' + scanId)}`}/> : null}
              {timeout ? <div className="qrcode-out">
                <RedoOutlined onClick={this.reCode} />
                {dict['code_expired'] || '二维码已失效。'}
              </div> : null}
            </div>
            <img src={wxicon} alt=""/>{dict['wechat_scan'] || '请使用微信扫一扫登录'}
          </div>
        </div> : null}
        {activeKey === 'app_scan' ? <div className="form-item-wrap">
          <div className="form-scan-wrap">
            <div className="qr-wrap">
              {scanId ? <QrCode card={{qrWidth: 500, color: '#000000'}} value={`mkpcscan,${window.GLOB.appkey},${scanId}`}/> : null}
              {timeout ? <div className="qrcode-out">
                <RedoOutlined onClick={this.reCode} />
                {dict['code_expired'] || '二维码已失效。'}
              </div> : null}
            </div>
            {dict['client_scan'] || '请使用客户端扫一扫登录'}
          </div>
        </div> : null}
        <div className={'login-ways ' + (activeKey === 'app_scan' || activeKey === 'weixin_scan' ? 'center' : '')}>
          {loginWays.map(item => {
            if (activeKey === item.type) return null
            if (item.type === 'app_scan' && activeKey !== 'weixin_scan') return null
            if (item.type === 'weixin_scan' && activeKey !== 'app_scan') return null
            return (<span key={item.type} onClick={() => this.onChangeTab(item.type)}>{item.label}</span>)
          })}
        </div>
      </Form>
    )