LoginInput.js 13 KB

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