login.js 13 KB

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