Login.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. import React, {Component} from 'react';
  2. import {Layout, Input, Button, Spin} from 'antd';
  3. import Config from './Config'
  4. import {ADD_USER, GET_USER, SEARCH_USER} from "../gql";
  5. import {Mutation} from "react-apollo";
  6. import axios from 'axios';
  7. import {request} from 'graphql-request'
  8. import gql from "graphql-tag";
  9. // todo: _.debounce 的引入
  10. import _ from 'lodash';
  11. axios.defaults.withCredentials = true;
  12. const {Content} = Layout;
  13. const idGen = (kind) => {
  14. return kind + '_' + Date.now() + '_' + Math.random().toString().slice(-8);
  15. };
  16. class Login extends Component {
  17. constructor() {
  18. super();
  19. this.state = {
  20. userID: '',
  21. hasLogin: false,
  22. hasRegister: false,
  23. nickname: 'its a default nickname',
  24. avatar: '',
  25. register_username: '',
  26. register_password: '',
  27. register_nickname: '',
  28. login_username: '',
  29. login_password: '',
  30. login_url: 'http://123.206.193.98:8999/login',
  31. register_url: 'http://123.206.193.98:8999/resetpassword',
  32. getID_url: 'http://123.206.193.98:8999/getuserid',
  33. loginStatus: '',
  34. loginOnce: true,
  35. usernameTip: false
  36. }
  37. }
  38. login = () => {
  39. let _this = this;
  40. // todo: should be post
  41. axios.get(`${this.state.login_url}?user-name=${this.state.login_username}&password=${this.state.login_password}`)
  42. .then((res) => {
  43. _this.setState({
  44. userID: res.data,
  45. loginStatus: 'logined',
  46. loginOnce: false
  47. });
  48. request('http://123.206.193.98:3000/graphql', GET_USER, {id: res.data}).then(data => {
  49. this.setState({
  50. avatar: data.user_by_id.avatar,
  51. nickname: data.user_by_id.nickname
  52. })
  53. }
  54. )
  55. })
  56. .catch((err) => {
  57. _this.setState({
  58. loginStatus: 'failed',
  59. loginOnce: false
  60. });
  61. });
  62. };
  63. register = () => {
  64. return (
  65. <Mutation mutation={gql(ADD_USER)}>
  66. {(create_user, {loading, error}) => {
  67. if (error)
  68. return 'error';
  69. if (loading)
  70. return <Spin style={{marginLeft: 3}}/>;
  71. return (
  72. <div>
  73. <span style={{marginRight: 20}}><b>register:</b></span>
  74. <div style={{marginTop: 10}}>
  75. <span>username: </span>
  76. <Input
  77. placeholder=""
  78. onChange={(e) => {
  79. // antd 获取不到 target,百度来的下面这句代码
  80. e.persist();
  81. this.setState({register_username: e.target.value});
  82. }}
  83. style={{width: 200}}
  84. />
  85. {
  86. this.state.usernameTip ?
  87. <span><b>username has been used!</b></span>
  88. :
  89. ''
  90. }
  91. </div>
  92. <div style={{marginTop: 10}}>
  93. <span>password: </span>
  94. <Input
  95. placeholder=""
  96. onChange={(e) => {
  97. // antd 获取不到 target,百度来的下面这句代码
  98. e.persist();
  99. this.setState({register_password: e.target.value});
  100. }}
  101. style={{width: 200}}
  102. />
  103. </div>
  104. <div style={{marginTop: 10}}>
  105. <span>nickname: </span>
  106. <Input
  107. placeholder=""
  108. onChange={(e) => {
  109. // antd 获取不到 target,百度来的下面这句代码
  110. e.persist();
  111. this.setState({register_nickname: e.target.value});
  112. }}
  113. style={{width: 200}}
  114. />
  115. </div>
  116. <Button type='primary' onClick={() => {
  117. request('http://123.206.193.98:3000/graphql', SEARCH_USER, {username: this.state.register_username}).then(data => {
  118. if (data.user_by_props.length === 0) {
  119. let id = idGen('user');
  120. let _this = this;
  121. create_user({
  122. variables: {
  123. id,
  124. email: '',
  125. updatedAt: '',
  126. password: '',
  127. telephone: '',
  128. nickname: this.state.register_nickname,
  129. username: this.state.register_username,
  130. createdAt: new Date().getTime(),
  131. openid: '',
  132. avatar: ''
  133. }
  134. });
  135. axios.get(`${this.state.register_url}?user-id=${id}&password=${this.state.register_password}&token=WXgraphql4Io`)
  136. .then(function (response) {
  137. if (response.status === 200)
  138. _this.setState({
  139. hasRegister: true
  140. })
  141. })
  142. .catch(function (error) {
  143. console.log(error);
  144. });
  145. } else {
  146. this.setState({
  147. usernameTip: true
  148. });
  149. setTimeout(() => {
  150. this.setState({
  151. usernameTip: false
  152. });
  153. }, 1500)
  154. }
  155. });
  156. }}>ok</Button>
  157. </div>
  158. );
  159. }}
  160. </Mutation>
  161. )
  162. };
  163. componentWillMount() {
  164. let _this = this;
  165. axios.get(this.state.getID_url)
  166. .then((res) => {
  167. if (res.data !== '') {
  168. _this.setState({
  169. userID: res.data,
  170. hasLogin: true,
  171. loginOnce: false,
  172. loginStatus: 'logined'
  173. }, () => {
  174. request('http://123.206.193.98:3000/graphql', GET_USER, {id: res.data}).then(data => {
  175. this.setState({
  176. avatar: data.user_by_id.avatar,
  177. nickname: data.user_by_id.nickname
  178. })
  179. }
  180. )
  181. }
  182. )
  183. }
  184. })
  185. .catch((err) => {
  186. console.log(err);
  187. });
  188. }
  189. render() {
  190. return (
  191. <div>
  192. <Layout style={{padding: '24px', zIndex: '0'}}>
  193. <Content style={{padding: '24px', minHeight: 280, background: '#fff'}}>
  194. {
  195. <div>
  196. <div className='login'>
  197. {
  198. this.state.hasLogin ?
  199. this.state.loginOnce ?
  200. this.login()
  201. :
  202. ''
  203. :
  204. <div>
  205. <span style={{marginRight: 20}}><b>login:</b></span>
  206. <div style={{marginTop: 10}}>
  207. <span>username: </span>
  208. <Input
  209. placeholder=""
  210. onChange={(e) => {
  211. // antd 获取不到 target,百度来的下面这句代码
  212. e.persist();
  213. this.setState({login_username: e.target.value});
  214. }}
  215. style={{width: 200}}
  216. />
  217. </div>
  218. <div style={{marginTop: 10}}>
  219. <span>password: </span>
  220. <Input
  221. placeholder=""
  222. onChange={(e) => {
  223. e.persist();
  224. this.setState({login_password: e.target.value});
  225. }}
  226. style={{width: 200}}
  227. />
  228. </div>
  229. <Button type='primary' onClick={() => {
  230. this.setState({
  231. hasLogin: true
  232. })
  233. }}>ok</Button>
  234. </div>
  235. }
  236. {
  237. this.state.loginStatus === 'logined' ?
  238. <div>
  239. <span
  240. style={{marginRight: '10px'}}>welcome, {this.state.nickname}</span>
  241. <Button onClick={() => {
  242. this.setState({
  243. hasLogin: false,
  244. loginStatus: '',
  245. loginOnce: true
  246. });
  247. // cookie.remove('ring-session')
  248. }}>exit</Button>
  249. <Config userID={this.state.userID}/>
  250. </div>
  251. :
  252. this.state.loginStatus === 'failed' ?
  253. <div>
  254. <span style={{marginRight: '10px'}}>login failed</span>
  255. <Button onClick={() => {
  256. this.setState({
  257. hasLogin: false,
  258. loginStatus: '',
  259. loginOnce: true
  260. })
  261. }}>relogin</Button>
  262. </div>
  263. :
  264. ''
  265. }
  266. </div>
  267. <div className='register' style={{marginTop: 20}}>
  268. {
  269. this.state.hasRegister ?
  270. <div>ok, login please</div>
  271. :
  272. this.state.loginStatus === 'logined' ?
  273. ''
  274. :
  275. this.register()
  276. }
  277. </div>
  278. </div>
  279. }
  280. </Content>
  281. </Layout>
  282. </div>
  283. )
  284. }
  285. }
  286. export default Login;