|
|
@@ -2,10 +2,13 @@ import React, {Component} from 'react';
|
|
|
|
|
|
import {Layout, Input, Button, Spin} from 'antd';
|
|
|
|
|
|
-import {ADD_USER, GET_USER} from "../gql";
|
|
|
+import {ADD_USER, GET_USER, SEARCH_USER} from "../gql";
|
|
|
import {Mutation} from "react-apollo";
|
|
|
import axios from 'axios';
|
|
|
import {request} from 'graphql-request'
|
|
|
+import gql from "graphql-tag";
|
|
|
+import cookie from 'react-cookies'
|
|
|
+import _ from 'lodash';
|
|
|
|
|
|
axios.defaults.withCredentials = true;
|
|
|
|
|
|
@@ -33,7 +36,8 @@ class Login extends Component {
|
|
|
register_url: 'http://localhost:8999/resetpassword',
|
|
|
getID_url: 'http://localhost:8999/getuserid',
|
|
|
loginStatus: '',
|
|
|
- loginOnce: true
|
|
|
+ loginOnce: true,
|
|
|
+ usernameTip: false
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -58,12 +62,13 @@ class Login extends Component {
|
|
|
|
|
|
register = () => {
|
|
|
return (
|
|
|
- <Mutation mutation={ADD_USER}>
|
|
|
+ <Mutation mutation={gql(ADD_USER)}>
|
|
|
{(create_user, {loading, error}) => {
|
|
|
if (error)
|
|
|
return 'error';
|
|
|
if (loading)
|
|
|
return <Spin style={{marginLeft: 3}}/>;
|
|
|
+
|
|
|
return (
|
|
|
<div>
|
|
|
<span style={{marginRight: 20}}><b>register:</b></span>
|
|
|
@@ -75,9 +80,16 @@ class Login extends Component {
|
|
|
// antd 获取不到 target,百度来的下面这句代码
|
|
|
e.persist();
|
|
|
this.setState({register_username: e.target.value});
|
|
|
+
|
|
|
}}
|
|
|
style={{width: 200}}
|
|
|
/>
|
|
|
+ {
|
|
|
+ this.state.usernameTip?
|
|
|
+ <span><b>username has been used!</b></span>
|
|
|
+ :
|
|
|
+ ''
|
|
|
+ }
|
|
|
</div>
|
|
|
<div style={{marginTop: 10}}>
|
|
|
<span>password: </span>
|
|
|
@@ -92,36 +104,45 @@ class Login extends Component {
|
|
|
/>
|
|
|
</div>
|
|
|
<Button type='primary' onClick={() => {
|
|
|
- // 没做 username 查询
|
|
|
- // request('http://123.206.193.98:3000/graphql', GET_USER).then(data =>
|
|
|
- // console.log(data)
|
|
|
- // );
|
|
|
- let id = idGen('userID');
|
|
|
- let _this = this;
|
|
|
- create_user({
|
|
|
- variables: {
|
|
|
- id,
|
|
|
- email: '',
|
|
|
- updatedAt: '',
|
|
|
- password: '',
|
|
|
- telephone: '',
|
|
|
- nickname: '',
|
|
|
- username: this.state.register_username,
|
|
|
- createdAt: new Date().getTime(),
|
|
|
- openid: '',
|
|
|
- avatar: ''
|
|
|
+ request('http://123.206.193.98:3000/graphql', SEARCH_USER, {username: this.state.register_username}).then(data => {
|
|
|
+ if (data.user_by_props.length === 0) {
|
|
|
+ let id = idGen('userID');
|
|
|
+ let _this = this;
|
|
|
+ create_user({
|
|
|
+ variables: {
|
|
|
+ id,
|
|
|
+ email: '',
|
|
|
+ updatedAt: '',
|
|
|
+ password: '',
|
|
|
+ telephone: '',
|
|
|
+ nickname: '',
|
|
|
+ username: this.state.register_username,
|
|
|
+ createdAt: new Date().getTime(),
|
|
|
+ openid: '',
|
|
|
+ avatar: ''
|
|
|
+ }
|
|
|
+ });
|
|
|
+ axios.get(`${this.state.register_url}?user-id=${id}&password=${this.state.register_password}&token=WXgraphql4Io`)
|
|
|
+ .then(function (response) {
|
|
|
+ if (response.status === 200)
|
|
|
+ _this.setState({
|
|
|
+ hasRegister: true
|
|
|
+ })
|
|
|
+ })
|
|
|
+ .catch(function (error) {
|
|
|
+ console.log(error);
|
|
|
+ });
|
|
|
+ } else {
|
|
|
+ this.setState({
|
|
|
+ usernameTip: true
|
|
|
+ });
|
|
|
+ setTimeout(()=>{
|
|
|
+ this.setState({
|
|
|
+ usernameTip: false
|
|
|
+ });
|
|
|
+ }, 1500)
|
|
|
}
|
|
|
});
|
|
|
- axios.get(`${this.state.register_url}?user-id=${id}&password=${this.state.register_password}&token=WXgraphql4Io`)
|
|
|
- .then(function (response) {
|
|
|
- if (response.status === 200)
|
|
|
- _this.setState({
|
|
|
- hasRegister: true
|
|
|
- })
|
|
|
- })
|
|
|
- .catch(function (error) {
|
|
|
- console.log(error);
|
|
|
- });
|
|
|
|
|
|
}}>ok</Button>
|
|
|
</div>
|
|
|
@@ -139,18 +160,21 @@ class Login extends Component {
|
|
|
if (res.data !== '') {
|
|
|
console.log(res);
|
|
|
_this.setState({
|
|
|
- userID: res.data,
|
|
|
- hasLogin: true,
|
|
|
- loginOnce: false,
|
|
|
- loginStatus: 'logined'
|
|
|
- },
|
|
|
- // 这里不知道为啥一直报错CORS
|
|
|
- // 存进 state 的 nickename 和 avatar 等
|
|
|
- // () => {
|
|
|
- // request('http://123.206.193.98:3000/graphql', GET_USER, {id: res.data}).then(data =>
|
|
|
- // console.log(data)
|
|
|
- // )
|
|
|
- // }
|
|
|
+ userID: res.data,
|
|
|
+ hasLogin: true,
|
|
|
+ loginOnce: false,
|
|
|
+ loginStatus: 'logined'
|
|
|
+ }, () => {
|
|
|
+ request('http://123.206.193.98:3000/graphql', GET_USER, {id: res.data}).then(data => {
|
|
|
+ this.setState({
|
|
|
+ user: {
|
|
|
+ avatar: data.user_by_id.avatar,
|
|
|
+ nickname: data.user_by_id.nickname
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ )
|
|
|
+ }
|
|
|
)
|
|
|
}
|
|
|
})
|
|
|
@@ -210,13 +234,15 @@ class Login extends Component {
|
|
|
{
|
|
|
this.state.loginStatus === 'logined' ?
|
|
|
<div>
|
|
|
- <span style={{marginRight: '10px'}}>welcome, {this.state.user.nickname}</span>
|
|
|
+ <span
|
|
|
+ style={{marginRight: '10px'}}>welcome, {this.state.user.nickname}</span>
|
|
|
<Button onClick={() => {
|
|
|
this.setState({
|
|
|
hasLogin: false,
|
|
|
loginStatus: '',
|
|
|
loginOnce: true
|
|
|
- })
|
|
|
+ });
|
|
|
+ // cookie.remove('ring-session')
|
|
|
}}>exit</Button>
|
|
|
</div>
|
|
|
:
|