LoginInput.js 13 KB

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