xy 7 anos atrás
pai
commit
bd9bae1bff

+ 0 - 4
README.md

@@ -1,6 +1,2 @@
 # online
 
-
-## 注意
-1. index.js 里面的 localhost 端口号记得修改
-2. 需要重新 npm install / yarn

+ 2 - 0
package.json

@@ -7,6 +7,7 @@
     "@svgr/webpack": "2.4.1",
     "antd": "^3.10.3",
     "apollo-boost": "^0.1.19",
+    "axios": "^0.18.0",
     "babel-core": "7.0.0-bridge.0",
     "babel-eslint": "9.0.0",
     "babel-jest": "23.6.0",
@@ -32,6 +33,7 @@
     "fs-extra": "7.0.0",
     "graphiql": "^0.12.0",
     "graphql": "^14.0.2",
+    "graphql-request": "^1.8.2",
     "graphql-tag": "^2.10.0",
     "html-webpack-plugin": "4.0.0-alpha.2",
     "identity-obj-proxy": "3.0.0",

+ 4 - 1
src/components/app/App.jsx

@@ -12,6 +12,7 @@ import MonitorNotify from '../monitorNotify/MonitorNotify';
 import DataAnalysis from '../dataAnalysis/DataAnalysis';
 import DataStorage from '../dataStorage/DataStorage';
 import TrialCase from "../trialCase/TrialCase";
+import Login from "../login/login";
 
 import zhCN from 'antd/lib/locale-provider/zh_CN';
 import '../common/graphql/index.css';
@@ -102,10 +103,11 @@ class App extends Component {
                             </Menu.Item>
                         </Menu>
 
+                        <Link to="/login"><Button className='login-button' type='primary'>Login</Button></Link>
+
                         <div className="change-locale">
                             <Button size="small" onClick={(e)=>this.changeLocale(e)}>{language}</Button>
                         </div>
-
                     </Header>
 
                     <Sider
@@ -211,6 +213,7 @@ class App extends Component {
                                 <Route path="/data-storage/" component={DataStorage}/>
                                 <Route path="/data-analysis/" component={DataAnalysis}/>
                                 <Route path="/monitor-notification/" component={MonitorNotify}/>
+                                <Route path="/login" component={Login}/>
                                 <Redirect path="*" to="/"/>
                             </Switch>
                         </Layout>

+ 7 - 0
src/components/app/index.css

@@ -16,4 +16,11 @@
     top: 0;
     right: 24px;
     float: right;
+}
+
+.login-button {
+    position: absolute;
+    top: 15px;
+    right: 100px;
+    float: right;
 }

+ 29 - 0
src/components/common/config/Config.js

@@ -0,0 +1,29 @@
+import React, {Component} from 'react';
+import {Input} from 'antd';
+
+
+class Config extends Component {
+    constructor(props) {
+        super(props);
+        this.state = {
+
+        };
+    }
+
+    render() {
+        return (
+            <div>
+                <span className='cloud-name'>Tencent: </span>
+                <div style={{marginBottom: 15}}>
+                    <div>
+                        ID: <Input style={{width: 250}} value={cloud.secretId}/>
+                        Key: <Input style={{width: 250}}
+                                    value={cloud.secretKey}/>
+                    </div>
+                </div>
+            </div>
+        )
+    }
+}
+
+export default Config;

+ 0 - 0
src/components/common/config/index.css


+ 0 - 1
src/components/common/deploy/Deploy.jsx

@@ -50,7 +50,6 @@ class Deploy extends Component {
         let userID = this.props.userID;
         return (
             <div>
-
                 {
                     this.state.show === 'all' ?
                         <Query query={SHOW_CLOUD} variables={{user_id: userID}}>

+ 1 - 1
src/components/gql.js

@@ -3,8 +3,8 @@ import gql from "graphql-tag";
 const GET_USER = gql`
             query USER($id: ID!) {
                 user_by_id(id: $id) {
-                    avatar,
                     nickname
+                    avatar
                 }
             }
         `;

+ 258 - 0
src/components/login/login.js

@@ -0,0 +1,258 @@
+import React, {Component} from 'react';
+
+import {Layout, Input, Button, Spin} from 'antd';
+
+import {ADD_USER, GET_USER} from "../gql";
+import {Mutation} from "react-apollo";
+import axios from 'axios';
+import {request} from 'graphql-request'
+
+axios.defaults.withCredentials = true;
+
+const {Content} = Layout;
+const idGen = (kind) => {
+    return kind + '_' + Date.now() + '_' + Math.random().toString().slice(-8);
+};
+
+class Login extends Component {
+    constructor() {
+        super();
+        this.state = {
+            userID: '',
+            hasLogin: false,
+            hasRegister: false,
+            user: {
+                nickname: 'its a default nickname',
+                avatar: ''
+            },
+            register_username: '',
+            register_password: '',
+            login_username: '',
+            login_password: '',
+            login_url: 'http://localhost:8999/login',
+            register_url: 'http://localhost:8999/resetpassword',
+            getID_url: 'http://localhost:8999/getuserid',
+            loginStatus: '',
+            loginOnce: true
+        }
+    }
+
+
+    login = () => {
+        let _this = this;
+        axios.get(`${this.state.login_url}?user-name=${this.state.login_username}&password=${this.state.login_password}`)
+            .then((res) => {
+                _this.setState({
+                    userID: res.data,
+                    loginStatus: 'logined',
+                    loginOnce: false
+                });
+            })
+            .catch((err) => {
+                _this.setState({
+                    loginStatus: 'failed',
+                    loginOnce: false
+                });
+            });
+    };
+
+    register = () => {
+        return (
+            <Mutation mutation={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>
+                            <div style={{marginTop: 10}}>
+                                <span>username: </span>
+                                <Input
+                                    placeholder=""
+                                    onChange={(e) => {
+                                        // antd 获取不到 target,百度来的下面这句代码
+                                        e.persist();
+                                        this.setState({register_username: e.target.value});
+                                    }}
+                                    style={{width: 200}}
+                                />
+                            </div>
+                            <div style={{marginTop: 10}}>
+                                <span>password: </span>
+                                <Input
+                                    placeholder=""
+                                    onChange={(e) => {
+                                        // antd 获取不到 target,百度来的下面这句代码
+                                        e.persist();
+                                        this.setState({register_password: e.target.value});
+                                    }}
+                                    style={{width: 200}}
+                                />
+                            </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: ''
+                                    }
+                                });
+                                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>
+                    );
+                }}
+            </Mutation>
+
+        )
+    };
+
+    componentWillMount() {
+        let _this = this;
+        axios.get(this.state.getID_url)
+            .then((res) => {
+                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)
+                    //     )
+                    // }
+                    )
+                }
+            })
+            .catch((err) => {
+                console.log(err);
+            });
+    }
+
+    render() {
+        return (
+            <div>
+                <Layout style={{padding: '24px', zIndex: '0'}}>
+                    <Content style={{padding: '24px', minHeight: 280, background: '#fff'}}>
+                        {
+                            <div>
+                                <div className='login'>
+                                    {
+                                        this.state.hasLogin ?
+                                            this.state.loginOnce ?
+                                                this.login()
+                                                :
+                                                ''
+                                            :
+                                            <div>
+                                                <span style={{marginRight: 20}}><b>login:</b></span>
+                                                <div style={{marginTop: 10}}>
+                                                    <span>username: </span>
+                                                    <Input
+                                                        placeholder=""
+                                                        onChange={(e) => {
+                                                            // antd 获取不到 target,百度来的下面这句代码
+                                                            e.persist();
+                                                            this.setState({login_username: e.target.value});
+                                                        }}
+                                                        style={{width: 200}}
+                                                    />
+                                                </div>
+                                                <div style={{marginTop: 10}}>
+                                                    <span>password: </span>
+                                                    <Input
+                                                        placeholder=""
+                                                        onChange={(e) => {
+                                                            // antd 获取不到 target,百度来的下面这句代码
+                                                            e.persist();
+                                                            this.setState({login_password: e.target.value});
+                                                        }}
+                                                        style={{width: 200}}
+                                                    />
+                                                </div>
+                                                <Button type='primary' onClick={() => {
+                                                    this.setState({
+                                                        hasLogin: true
+                                                    })
+                                                }}>ok</Button>
+                                            </div>
+                                    }
+                                    {
+                                        this.state.loginStatus === 'logined' ?
+                                            <div>
+                                                <span style={{marginRight: '10px'}}>welcome, {this.state.user.nickname}</span>
+                                                <Button onClick={() => {
+                                                    this.setState({
+                                                        hasLogin: false,
+                                                        loginStatus: '',
+                                                        loginOnce: true
+                                                    })
+                                                }}>exit</Button>
+                                            </div>
+                                            :
+                                            this.state.loginStatus === 'failed' ?
+                                                <div>
+                                                    <span style={{marginRight: '10px'}}>login failed</span>
+                                                    <Button onClick={() => {
+                                                        this.setState({
+                                                            hasLogin: false,
+                                                            loginStatus: '',
+                                                            loginOnce: true
+                                                        })
+                                                    }}>relogin</Button>
+                                                </div>
+                                                :
+                                                ''
+                                    }
+                                </div>
+                                <div className='register' style={{marginTop: 20}}>
+                                    {
+                                        this.state.hasRegister ?
+                                            <div>ok, login please</div>
+                                            :
+                                            this.register()
+                                    }
+                                </div>
+
+                            </div>
+                        }
+
+                    </Content>
+                </Layout>
+            </div>
+        )
+    }
+}
+
+export default Login;
+

+ 51 - 196
src/components/trialCase/TrialCase.jsx

@@ -1,171 +1,57 @@
 import React, {Component} from 'react';
-import GenerateJs from "../common/generateJs/GenerateJs";
-import {Layout, Menu, Input, Button, Spin} from 'antd';
+import {Layout, Menu} from 'antd';
 
+import GenerateJs from "../common/generateJs/GenerateJs";
 import Deploy from '../common/deploy/Deploy';
+import Config from '../common/config/Config';
 import Schema from '../common/schema/Schema';
 import Graphql from "../common/graphql/Graphql";
 import CaseNotification from "../common/caseNotification/CaseNotification";
 import CaseMetabase from "../common/caseMetabase/CaseMetabase";
-import {ADD_USER, GET_USER} from "../gql";
-import {Query, Mutation} from "react-apollo";
-import fetch from "isomorphic-fetch";
+import {GET_USER} from "../gql";
+import axios from 'axios';
+import {request} from 'graphql-request'
+
+axios.defaults.withCredentials = true;
 
 const {Content} = Layout;
-const idGen = (kind) => {
-    return kind + '_' + Date.now() + '_' + Math.random().toString().slice(-8);
-};
+
 
 class TrialCase extends Component {
     constructor() {
         super();
+
         this.state = {
             menuLevel2: "schema",
-            shouldShow: false,
-            userID: '',
-            hasLogin: false,
-            hasRegister: false,
-            user: {
-                nickname: '',
-                avatar: ''
-            },
-            username: '',
-            password: '',
-            url: 'http://localhost:8999/resetpassword'
+            userID: 'ioobot',
+            getID_url: 'http://localhost:8999/getuserid'
         }
     }
 
+
+    componentWillMount() {
+        let _this = this;
+        axios.get(this.state.getID_url)
+            .then((res) => {
+                if (res.data !== '') {
+                    _this.setState({
+                        userID: res.data
+                    })
+                }
+            })
+            .catch(function (err) {
+                console.log(err);
+            });
+    }
+
     switchMenu = (menuName, e) => {
-        console.log('menuName', menuName, 'e', e);
+        // console.log('menuName', menuName, 'e', e);
         this.setState({
             [menuName]: e.key,
         });
     };
 
 
-    login = () => {
-        let id = this.state.userID;
-        return (
-            <Query query={GET_USER} variables={{id}}>
-                {
-                    ({loading, error, data}) => {
-                        if (loading) {
-                            return <Spin style={{marginLeft: 3}}/>
-                        }
-                        if (error) {
-                            return 'error!';
-                        }
-
-                        if (data.user_by_id === null) {
-
-                            return (
-                                <div>
-                                    <span style={{marginRight: '10px'}}>no such person</span>
-                                    <Button onClick={() => {
-                                        this.setState({hasLogin: false})
-                                    }}>relogin</Button>
-                                </div>
-                            );
-                        }
-
-                        else {
-                            // 不可使用 this.setState, 会报错。
-                            // Warning: Cannot update during an existing state transition (such as within `render`).
-                            // Render methods should be a pure function of props and state.
-                            // this.state.user = {
-                            //     avatar: data.user_by_id.avatar,
-                            //     nickname: data.user_by_id.nickname
-                            // };
-
-                            return (
-                                <div>
-                                    <span style={{marginRight: '10px'}}>welcome, {data.user_by_id.nickname}</span>
-                                    <Button onClick={() => {
-                                        this.setState({hasLogin: false})
-                                    }}>exit</Button>
-                                    <Button onClick={() => {
-                                        this.setState({shouldShow: true})
-                                    }}>go to use</Button>
-                                </div>
-                            );
-                        }
-                    }
-                }
-            </Query>
-        )
-    };
-
-    register = () => {
-        return (
-            <Mutation mutation={ADD_USER}>
-                {(create_user, {loading, error}) => {
-                    if (error)
-                        return 'error';
-                    if (loading)
-                        return <Spin style={{marginLeft: 3}}/>;
-                    return (
-                        <div style={{marginTop: 20}}>
-                            <span style={{marginRight: 20}}><b>register:</b></span>
-                            <div style={{marginTop: 10}}>
-                                <span>username: </span>
-                                <Input
-                                    placeholder=""
-                                    onChange={(e) => {
-                                        // antd 获取不到 target,百度来的下面这句代码
-                                        e.persist();
-                                        this.setState({username: e.target.value});
-                                    }}
-                                    style={{width: 200}}
-                                />
-                            </div>
-                            <div style={{marginTop: 10}}>
-                                <span>password: </span>
-                                <Input
-                                    placeholder=""
-                                    onChange={(e) => {
-                                        // antd 获取不到 target,百度来的下面这句代码
-                                        e.persist();
-                                        this.setState({password: e.target.value});
-                                    }}
-                                    style={{width: 200}}
-                                />
-                            </div>
-                            <Button type='primary' onClick={() => {
-                                let id = idGen('userID');
-                                create_user({
-                                    variables: {
-                                        id,
-                                        email: '',
-                                        updatedAt: '',
-                                        password: '',
-                                        telephone: '',
-                                        nickname: '',
-                                        username: this.state.username,
-                                        createdAt: new Date().getTime(),
-                                        openid: '',
-                                        avatar: ''
-                                    }
-                                });
-                                fetch(this.state.url,{
-                                    method: 'POST',
-                                    mode: 'cors',
-                                    body: {
-                                        'user-id': id,
-                                        'password': this.state.password,
-                                        'token': 'WXgraphql4Io',
-                                    }
-                                }).then(res => res.json())
-                                    .catch(error => console.error('Error:', error))
-                                    .then(response => console.log('Success:', response));
-                            }}>ok</Button>
-                        </div>
-                    );
-                }}
-            </Mutation>
-
-        )
-    };
-
     render() {
         return (
             <div>
@@ -177,6 +63,7 @@ class TrialCase extends Component {
                     selectedKeys={[this.state.menuLevel2]}
                 >
                     <Menu.Item key="schema">schema</Menu.Item>
+                    <Menu.Item key="config">config</Menu.Item>
                     <Menu.Item key="deploy">deploy</Menu.Item>
                     <Menu.Item key="graphql">graphql</Menu.Item>
                     <Menu.Item key="graphiql">graphql IDE</Menu.Item>
@@ -188,59 +75,27 @@ class TrialCase extends Component {
                 <Layout style={{padding: '24px', zIndex: '0'}}>
                     <Content style={{padding: '24px', minHeight: 280, background: '#fff', marginTop: '48px'}}>
                         {
-                            this.state.shouldShow === false ?
-                                <div>
-                                    <div className='login'>
-                                        {
-                                            this.state.hasLogin ?
-                                                this.login()
-                                                :
-                                                <div>
-                                                    <span style={{marginRight: 20}}><b>login:</b></span>
-                                                    <Input
-                                                        placeholder="id, eg: 'xy_1'"
-                                                        onChange={(e) => {
-                                                            // antd 获取不到 target,百度来的下面这句代码
-                                                            e.persist();
-                                                            this.setState({userID: e.target.value});
-                                                        }}
-                                                        onPressEnter={() => {
-                                                            this.setState({hasLogin: true})
-                                                        }}
-                                                        style={{width: 200}}
-                                                    />
-                                                </div>
-                                        }
-                                    </div>
-                                    <div className='register'>
-                                        {
-                                            this.register()
-                                        }
-                                    </div>
-
-                                </div>
-
-                                :
-
-                                (() => {
-                                    switch (this.state.menuLevel2) {
-                                        case 'schema':
-                                            return <Schema userID={this.state.userID}/>;
-                                        case 'deploy':
-                                            return <Deploy userID={this.state.userID}/>;
-                                        case 'graphql':
-                                            return <GenerateJs/>;
-                                        case 'graphiql':
-                                            return <Graphql/>;
-                                        case 'metabase':
-                                            return <CaseMetabase/>;
-                                        case 'notification':
-                                            return <CaseNotification/>;
-                                        default:
-                                            return <Graphql/>
-                                    }
-
-                                })()
+                            (() => {
+                                switch (this.state.menuLevel2) {
+                                    case 'schema':
+                                        return <Schema userID={this.state.userID}/>;
+                                    case 'config':
+                                        return <Config userID={this.state.userID}/>;
+                                    case 'deploy':
+                                        return <Deploy userID={this.state.userID}/>;
+                                    case 'graphql':
+                                        return <GenerateJs/>;
+                                    case 'graphiql':
+                                        return <Graphql/>;
+                                    case 'metabase':
+                                        return <CaseMetabase/>;
+                                    case 'notification':
+                                        return <CaseNotification/>;
+                                    default:
+                                        return <Graphql/>
+                                }
+
+                            })()
                         }
 
                     </Content>