diff --git a/calc/package-lock.json b/calc/package-lock.json index 539a37a..430f649 100644 --- a/calc/package-lock.json +++ b/calc/package-lock.json @@ -13,6 +13,7 @@ "@testing-library/react": "^13.3.0", "@testing-library/user-event": "^13.5.0", "bootstrap": "^5.2.0", + "jquery": "^3.6.1", "react": "^18.2.0", "react-dom": "^18.2.0", "react-redux": "^8.0.2", @@ -10607,6 +10608,11 @@ "node": ">=10" } }, + "node_modules/jquery": { + "version": "3.6.1", + "resolved": "https://registry.npmmirror.com/jquery/-/jquery-3.6.1.tgz", + "integrity": "sha512-opJeO4nCucVnsjiXOE+/PcCgYw9Gwpvs/a6B1LL/lQhwWwpbVEVYDZ1FokFr8PRc7ghYlrFPuyHuiiDNTQxmcw==" + }, "node_modules/js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmmirror.com/js-tokens/-/js-tokens-4.0.0.tgz", @@ -23947,6 +23953,11 @@ } } }, + "jquery": { + "version": "3.6.1", + "resolved": "https://registry.npmmirror.com/jquery/-/jquery-3.6.1.tgz", + "integrity": "sha512-opJeO4nCucVnsjiXOE+/PcCgYw9Gwpvs/a6B1LL/lQhwWwpbVEVYDZ1FokFr8PRc7ghYlrFPuyHuiiDNTQxmcw==" + }, "js-tokens": { "version": "4.0.0", "resolved": "https://registry.npmmirror.com/js-tokens/-/js-tokens-4.0.0.tgz", diff --git a/calc/package.json b/calc/package.json index 8a16e58..0130abc 100644 --- a/calc/package.json +++ b/calc/package.json @@ -8,6 +8,7 @@ "@testing-library/react": "^13.3.0", "@testing-library/user-event": "^13.5.0", "bootstrap": "^5.2.0", + "jquery": "^3.6.1", "react": "^18.2.0", "react-dom": "^18.2.0", "react-redux": "^8.0.2", diff --git a/calc/src/components/app.jsx b/calc/src/components/app.jsx index 32d5f81..72f3371 100644 --- a/calc/src/components/app.jsx +++ b/calc/src/components/app.jsx @@ -5,18 +5,46 @@ import Calc from './content/calc'; import Login from './content/login'; import Register from './content/register'; import { Routes, Route, Navigate } from 'react-router-dom'; +import $ from 'jquery'; + class App extends Component { - state = { } + state = { + is_login: true, + username: "zfp", + } + + // api会有跨域问题 + componentDidMount = () => { + $.ajax({ + url: "https://app165.acapp.acwing.com.cn/calculator/get_status/", + type: "get", + success: resp => { + console.log(resp); + if (resp.result === "login") { + this.setState({ + is_login: true, + username: resp.username, + }) + }else { + this.setState({ + is_login: false, + }) + } + } + + }) + } + render() { return (
- + }> }> - }> + : }> }> }> } /> diff --git a/calc/src/components/content/login.jsx b/calc/src/components/content/login.jsx index ad580d4..f4657b9 100644 --- a/calc/src/components/content/login.jsx +++ b/calc/src/components/content/login.jsx @@ -1,13 +1,65 @@ import React, { Component } from 'react'; import Base from './base'; - +import $ from 'jquery'; class Login extends Component { - state = { } + state = { + error_msg: "", + username: "", + password: "", + } + + handleClickLogin = e => { + e.preventDefault(); + if (this.state.username === "") { + this.setState({error_msg: "用户名不能为空"}); + }else if (this.state.password === "") { + this.setState({error_msg: "密码不能为空"}); + }else { + $.ajax({ + url: "https://app165.acapp.acwing.com.cn/calculator/login/", + type: "GET", + data: { + username: this.state.username, + password: this.state.password, + }, + dataType: "json", + success: resp => { + if (resp.result === "success") { + window.location.href = "/home"; + }else { + this.setState({error_msg: resp.result}); + } + } + + }); + + } + } render() { return ( -

登录

+
+
+
+
+
+ + {this.setState({username: e.target.value})}} type="email" className="form-control" id="username" aria-describedby="emailHelp"/> +
+
+ + {this.setState({password: e.target.value})}} type="password" className="form-control" id="password"/> +
+ +
+ {this.state.error_msg} +
+ +
+
+
+
); } diff --git a/calc/src/components/content/register.jsx b/calc/src/components/content/register.jsx index 251aa7a..4eebfe4 100644 --- a/calc/src/components/content/register.jsx +++ b/calc/src/components/content/register.jsx @@ -1,12 +1,78 @@ import React, { Component } from 'react'; import Base from './base'; +import $ from 'jquery'; class Register extends Component { - state = { } + state = { + error_msg: "", + username: "", + confirm_password: "", + password: "", + }; + + handleClickRegister = e => { + e.preventDefault(); + if (this.state.username === "") { + this.setState({error_msg: "用户名不能为空"}); + }else if (this.state.password === "") { + this.setState({error_msg: "密码不能为空"}); + }else if (this.state.confirm_password === "") { + this.setState({error_msg: "确认密码不能为空"}); + }else if (this.state.confirm_password !== this.state.password) { + this.setState({error_msg: "两次密码输入不一致"}); + }else { + $.ajax({ + url: "https://app165.acapp.acwing.com.cn/calculator/register/", + type: "GET", + data: { + username: this.state.username, + password: this.state.password, + password_confirm: this.state.confirm_password + }, + dataType: "json", + success: resp => { + if (resp.result === "success") { + window.location.href = "/home"; + }else { + this.setState({error_msg: resp.result}); + } + } + + }); + } + console.log(this.state); + } + + render() { return ( -

注册

+
+
+
+
+
+ + {this.setState({username: e.target.value})}} type="email" className="form-control" id="username" aria-describedby="emailHelp"/> +
+
+ + {this.setState({password: e.target.value})}} type="password" className="form-control" id="password"/> +
+ +
+ + {this.setState({confirm_password: e.target.value})}} type="password" className="form-control" id="confirm_password"/> +
+ +
+ {this.state.error_msg} +
+ +
+
+
+
); } diff --git a/calc/src/components/navbar.jsx b/calc/src/components/navbar.jsx index aa93644..e68a8f4 100644 --- a/calc/src/components/navbar.jsx +++ b/calc/src/components/navbar.jsx @@ -1,7 +1,59 @@ import React, { Component } from 'react'; import { Link } from 'react-router-dom'; +import $ from 'jquery'; + class NavBar extends Component { state = { } + + render_calc = () => { + if (this.props.is_login){ + return ( +
  • + 计算器 +
  • ); + } + else return ""; + } + + handleClickLogout = () => { + $.ajax({ + url: "https://app165.acapp.acwing.com.cn/calculator/logout/", + type: "GET", + success: resp => { + console.log(resp); + if (resp.result === "success") { + window.location.href = "/home"; + + } + } + }); + } + + render_logout = () => { + if (this.props.is_login) { + return ( +
      +
    • +
      {this.props.username}
      +
    • +
    • +
      退出
      +
    • +
    ); + }else { + return ( +
      +
    • + 登录 +
    • +
    • + 注册 +
    • +
    ); + } + } + + render() { return (