LoginInput.js 15 KB

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