App.jsx 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695
  1. import React, {Component} from 'react';
  2. import {Layout, Menu, Button, Spin, Icon, LocaleProvider, Dropdown, Avatar, Badge} from 'antd';
  3. import {BrowserRouter as Router, Route, Link, Switch, Redirect} from "react-router-dom";
  4. import {Query} from "react-apollo";
  5. import gql from "graphql-tag";
  6. import moment from 'moment';
  7. import 'moment/locale/zh-cn';
  8. import {FormattedMessage} from 'react-intl';
  9. import zhCN from 'antd/lib/locale-provider/zh_CN';
  10. import QuantService from "./quantService/QuantService";
  11. import TrialCase from "./graphqlService/TrialCase";
  12. import UserCreate from "./graphqlService/UserCreate";
  13. import WxTrialCase from "./wechatService/WxTrialCase";
  14. import WxUserCreate from "./wechatService/WxUserCreate";
  15. import Login from "../login/Login";
  16. import Ticket from '../ticket/Ticket';
  17. import './graphqlService/component/graphql/index.css';
  18. import './index.css'
  19. import Create from "./graphqlService/component/schema/Create";
  20. import WxCreate from "./wechatService/wxCreate/WxCreate";
  21. import {CASE_AND_PROJECT, GET_USER} from "../gql";
  22. import axios from 'axios';
  23. import {getCookie, setCookie} from "../cookie";
  24. import {logoutUrl} from "../config";
  25. import Graphiql from "./common/Graphiql";
  26. axios.defaults.withCredentials = true;
  27. const {SubMenu} = Menu;
  28. const {Header, Sider} = Layout;
  29. moment.locale('en');
  30. class App extends Component {
  31. constructor(props) {
  32. super(props);
  33. this.state = {
  34. menuLevel1: "graphql-service",
  35. sideBar: "ecommerce",
  36. collapsed: false,
  37. inlineCollapsed: false,
  38. language: props.language,
  39. locale: props.language === "中文" ? zhCN : undefined,
  40. languageButton: props.language === "中文" ? "English" : "中文",
  41. visible: false,
  42. wxVisible: false,
  43. userID: '',
  44. avatar: ''
  45. };
  46. }
  47. componentWillMount() {
  48. let urlList = window.location.pathname.split("/");
  49. let urlListLength = urlList.length;
  50. if (urlListLength > 2) {
  51. this.setState({menuLevel1: urlList[1]});
  52. if (urlList[3] !== 'index') {
  53. this.setState({sideBar: urlList[3]});
  54. }
  55. }
  56. let userID = getCookie('user_id');
  57. if (userID === undefined || '') {
  58. axios.get(this.state.getIdUrl)
  59. .then((res) => {
  60. if (res.data !== '') {
  61. setCookie("user_id", res.data);
  62. this.setState({
  63. userID: res.data
  64. })
  65. }
  66. })
  67. .catch(function (err) {
  68. console.log(err);
  69. });
  70. } else {
  71. this.setState({
  72. userID
  73. })
  74. }
  75. }
  76. onCollapse = (collapsed) => {
  77. this.setState({collapsed});
  78. };
  79. switchMenu = (menuName, e) => {
  80. this.setState({
  81. [menuName]: e.key,
  82. });
  83. };
  84. switchMenuTab = (menuName, e) => {
  85. this.setState({
  86. [menuName]: e.key,
  87. });
  88. if (e.key === "graphql-service") {
  89. this.setState({sideBar: "ecommerce"});
  90. }
  91. };
  92. switchSidebar = (value) => {
  93. this.setState({
  94. sideBar: value,
  95. });
  96. };
  97. switchMenuLevel = (menuName, value) => {
  98. this.setState({
  99. [menuName]: value,
  100. });
  101. };
  102. showModal = () => {
  103. this.setState({
  104. visible: true,
  105. });
  106. };
  107. wxShowModal = () => {
  108. this.setState({
  109. wxVisible: true,
  110. });
  111. };
  112. hideModal = () => {
  113. this.setState({
  114. visible: false,
  115. });
  116. };
  117. wxHideModal = () => {
  118. this.setState({
  119. wxVisible: false,
  120. });
  121. };
  122. changeLocale = (e) => {
  123. e.stopPropagation();
  124. let {language} = this.state;
  125. // console.log('app language',language);
  126. let changeLanguage = language === "中文" ? "English" : "中文";
  127. let local = language === "中文" ? undefined : zhCN;
  128. let languageButton = language === "中文" ? "中文" : "English";
  129. sessionStorage.setItem("language", changeLanguage);
  130. this.props.changeLanguage(changeLanguage);
  131. this.setState({
  132. language: changeLanguage,
  133. locale: local,
  134. languageButton: languageButton
  135. });
  136. if (!local) {
  137. moment.locale('en');
  138. } else {
  139. moment.locale('zh-cn');
  140. }
  141. };
  142. render() {
  143. const {locale, languageButton, visible, wxVisible} = this.state;
  144. return (
  145. <Router>
  146. <Layout style={{minHeight: '100vh'}}>
  147. <Header className="header" style={{position: 'fixed', zIndex: 1, width: '100%'}}>
  148. <Link to="/"
  149. onClick={() => this.setState({menuLevel1: "graphql-service", sideBar: "ecommerce"})}>
  150. <div className="logo-wrapper">
  151. <div className='logo'/>
  152. </div>
  153. </Link>
  154. <Menu
  155. theme="dark"
  156. mode="horizontal"
  157. selectedKeys={[this.state.menuLevel1]}
  158. style={{lineHeight: '64px'}}
  159. onClick={(e) => this.switchMenuTab('menuLevel1', e)}
  160. >
  161. <Menu.Item key="graphql-service">
  162. <Link to="/graphql-service/trial-case/index"><FormattedMessage
  163. id="Graphql Service"/></Link>
  164. </Menu.Item>
  165. <Menu.Item key="wechat-service">
  166. <Link to="/wechat-service/trial-case/index"><FormattedMessage
  167. id="Wechat Service"/></Link>
  168. </Menu.Item>
  169. <Menu.Item key="quant-service">
  170. <Link to="/quant-service/trial-case/index"><FormattedMessage id="Quantization Service"/></Link>
  171. </Menu.Item>
  172. </Menu>
  173. {
  174. this.state.userID === '' ?
  175. <Link to="/login">
  176. <Button className='login-button' type='primary'
  177. onClick={() => this.switchMenuLevel('menuLevel1', 'user')}>
  178. <FormattedMessage id="Login"/></Button>
  179. </Link>
  180. :
  181. <User
  182. userID={this.state.userID}
  183. languageButton={this.state.languageButton}
  184. changeLocale={this.changeLocale}
  185. switchMenuLevel={this.switchMenuLevel}
  186. />
  187. }
  188. </Header>
  189. {(() => {
  190. switch (this.state.menuLevel1) {
  191. case 'graphql-service':
  192. return (
  193. <Sider
  194. width={200}
  195. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  196. collapsible
  197. collapsed={this.state.collapsed}
  198. onCollapse={this.onCollapse}
  199. >
  200. <GraphqlSidebar inlineCollapsed={this.state.inlineCollapsed}
  201. sideBar={this.state.sideBar} switchMenu={this.switchMenu}
  202. showModal={this.showModal}/>
  203. </Sider>
  204. );
  205. case 'wechat-service':
  206. return (
  207. <Sider
  208. width={200}
  209. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  210. collapsible
  211. collapsed={this.state.collapsed}
  212. onCollapse={this.onCollapse}
  213. >
  214. <WxConfigSiderbar inlineCollapsed={this.state.inlineCollapsed}
  215. sideBar={this.state.sideBar} switchMenu={this.switchMenu}
  216. wxShowModal={this.wxShowModal}/>
  217. </Sider>
  218. );
  219. case 'quant-service':
  220. return (
  221. <Sider
  222. width={200}
  223. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  224. collapsible
  225. collapsed={this.state.collapsed}
  226. onCollapse={this.onCollapse}
  227. >
  228. <Menu
  229. theme="dark"
  230. mode="inline"
  231. inlineCollapsed={this.state.inlineCollapsed}
  232. defaultSelectedKeys={['quant-service']}
  233. defaultOpenKeys={['trial-case']}
  234. // openKeys={['cloud-function']}
  235. onClick={(e) => this.switchMenu('sideBar', e)}
  236. selectedKeys={['quant-service']}
  237. style={{
  238. borderRight: 0,
  239. overflow: 'auto',
  240. height: '100vh',
  241. left: '0',
  242. width: '200px',
  243. position: 'fixed'
  244. }}
  245. >
  246. <SubMenu key="trial-case"
  247. title={<span><Icon type="appstore" theme="twoTone"/>
  248. <span><FormattedMessage id="Case Show"/></span></span>}>
  249. <Menu.Item key="quant-service">
  250. <Link to="/quant-service/trial-case/quant case">quant case</Link>
  251. </Menu.Item>
  252. </SubMenu>
  253. <Menu.Item key="instructions">
  254. <a href="https://ioobot-document.netlify.com/" title="instructions"
  255. target="instructions">
  256. <Icon type="file-text" theme="twoTone"/>
  257. <span><FormattedMessage id="Instructions"/></span>
  258. </a>
  259. </Menu.Item>
  260. </Menu>
  261. </Sider>
  262. );
  263. case 'user':
  264. return (
  265. <Sider
  266. width={200}
  267. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  268. collapsible
  269. collapsed={this.state.collapsed}
  270. onCollapse={this.onCollapse}
  271. >
  272. <Menu
  273. theme="dark"
  274. defaultSelectedKeys={['cloud-settings']}
  275. onClick={(e) => this.switchMenu('sideBar', e)}
  276. selectedKeys={[this.state.sideBar]}
  277. style={{
  278. borderRight: 0,
  279. overflow: 'auto',
  280. height: '100vh',
  281. left: '0',
  282. width: '200px',
  283. position: 'fixed'
  284. }}
  285. >
  286. <Menu.Item key="account">
  287. <Icon type="setting" theme="twoTone"/>
  288. <span><FormattedMessage id="Account center"/></span>
  289. <Link to="/login/account"/>
  290. </Menu.Item>
  291. <Menu.Item key="cloud-settings">
  292. <Icon type="cloud" theme="twoTone"/>
  293. <span><FormattedMessage id="Cloud settings"/></span>
  294. <Link to="/login/cloud"/>
  295. </Menu.Item>
  296. </Menu>
  297. </Sider>
  298. );
  299. case 'ticket':
  300. return (
  301. ''
  302. );
  303. default:
  304. return (
  305. <Sider
  306. width={200}
  307. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  308. collapsible
  309. collapsed={this.state.collapsed}
  310. onCollapse={this.onCollapse}
  311. >
  312. <GraphqlSidebar inlineCollapsed={this.state.inlineCollapsed}
  313. sideBar={this.state.sideBar} switchMenu={this.switchMenu}
  314. showModal={this.showModal}/>
  315. </Sider>
  316. );
  317. }
  318. })()}
  319. <Create visible={visible} hideModal={this.hideModal} switchSidebar={this.switchSidebar}/>
  320. <WxCreate visible={wxVisible} hideModal={this.wxHideModal} switchSidebar={this.switchSidebar}/>
  321. <LocaleProvider locale={locale}>
  322. <Layout style={{marginTop: '64px', zIndex: '0'}}
  323. key={locale ? locale.locale : 'en'/* Have to refresh for production environment */}>
  324. <Switch>
  325. <Route path="/" exact component={TrialCase}/>
  326. <Route path="/graphql-service/trial-case/:case" component={TrialCase}/>
  327. <Route path="/graphql-service/my-create/:case" component={UserCreate}/>
  328. <Route path="/wechat-service/trial-case/:case" component={WxTrialCase}/>
  329. <Route path="/wechat-service/my-create/:case" component={WxUserCreate}/>
  330. <Route path="/quant-service/:sidebar/:case" component={QuantService}/>
  331. <Route path="/quant-service/:sidebar/:case" component={QuantService}/>
  332. <Route path="/login/:setting" component={Login}/>
  333. <Route path="/login" component={Login}/>
  334. <Route path="/ticket" component={Ticket}/>
  335. <Route path="/graphiql" component={Graphiql}/>
  336. <Redirect path="*" to="/"/>
  337. </Switch>
  338. </Layout>
  339. </LocaleProvider>
  340. </Layout>
  341. </Router>
  342. );
  343. }
  344. }
  345. export default App;
  346. class GraphqlSidebar extends Component {
  347. constructor(props) {
  348. super(props);
  349. this.state = {
  350. userID: getCookie('user_id'),
  351. }
  352. }
  353. render() {
  354. return (
  355. <Query query={gql(CASE_AND_PROJECT)} variables={{projectType: 'graphql', user_id: this.state.userID}}>
  356. {
  357. ({loading, error, data}) => {
  358. if (loading) return <Spin style={{marginLeft: 3}}/>;
  359. if (error) return 'error!';
  360. // console.log('CASE_AND_PROJECT data', data);
  361. data.caseProject.forEach((project) => {
  362. switch (project.schema_id.schemaName) {
  363. case 'ecommerce' :
  364. localStorage.setItem('ecommerce', project.schema_id.schemaData);
  365. break;
  366. case 'order':
  367. localStorage.setItem('order', project.schema_id.schemaData);
  368. break;
  369. case 'bills':
  370. localStorage.setItem('bills', project.schema_id.schemaData);
  371. break;
  372. default:
  373. break;
  374. }
  375. });
  376. return (
  377. <Menu
  378. theme="dark"
  379. mode="inline"
  380. inlineCollapsed={this.props.inlineCollapsed}
  381. defaultSelectedKeys={['ecommerce']}
  382. defaultOpenKeys={['trial-case', 'my-create']}
  383. // openKeys={['trial-case', 'my-create']}
  384. onClick={(e) => this.props.switchMenu('sideBar', e)}
  385. selectedKeys={[this.props.sideBar]}
  386. style={{
  387. borderRight: 0,
  388. overflow: 'auto',
  389. height: '100vh',
  390. left: '0',
  391. width: '200px',
  392. position: 'fixed'
  393. }}
  394. >
  395. <SubMenu
  396. key="trial-case"
  397. title={<span><Icon type="appstore" theme="twoTone"/><span><FormattedMessage
  398. id="Case Show"/></span></span>}>
  399. {
  400. data.caseProject.map((project) =>
  401. <Menu.Item key={project.projectName}>
  402. <Link to={{
  403. pathname: `/graphql-service/trial-case/${project.projectName}`,
  404. state: {
  405. schemaName: project.projectName,
  406. schemaID: project.schema_id.id,
  407. projectID: project.id
  408. }
  409. }}><FormattedMessage id={project.projectName}/></Link>
  410. </Menu.Item>)
  411. }
  412. </SubMenu>
  413. <Menu.Item key="create-graphql" onClick={this.props.showModal}>
  414. <Icon type="edit" theme="twoTone"/>
  415. <span><FormattedMessage id="Create"/></span>
  416. <Icon type="plus" style={{
  417. position: 'absolute',
  418. top: '35%',
  419. right: '6px',
  420. color: 'white'
  421. }}/>
  422. </Menu.Item>
  423. <SubMenu
  424. key="my-create"
  425. title={<span><Icon type="user" theme="outlined"/><span><FormattedMessage
  426. id="My Create"/></span></span>}>
  427. {
  428. data.project.map((project) =>
  429. <Menu.Item key={project.projectName}>
  430. <Link to={{
  431. pathname: `/graphql-service/my-create/${project.projectName}`,
  432. state: {
  433. schemaName: project.projectName,
  434. schemaID: project.schema_id.id,
  435. projectID: project.id
  436. }
  437. }}>{project.projectName}</Link>
  438. </Menu.Item>)
  439. }
  440. </SubMenu>
  441. <Menu.Item key="instructions">
  442. <a href="https://ioobot-document.netlify.com/" title="instructions" target="_blank"
  443. rel="noopener noreferrer">
  444. <Icon type="file-text" theme="twoTone"/>
  445. <span><FormattedMessage id="Instructions"/></span>
  446. </a>
  447. </Menu.Item>
  448. </Menu>
  449. )
  450. }
  451. }
  452. </Query>
  453. )
  454. }
  455. }
  456. class WxConfigSiderbar extends Component {
  457. constructor(props) {
  458. super(props);
  459. this.state = {
  460. userID: getCookie('user_id'),
  461. }
  462. }
  463. render() {
  464. return (
  465. <Query query={gql(CASE_AND_PROJECT)} variables={{projectType: 'wx', user_id: this.state.userID}}>
  466. {
  467. ({loading, error, data}) => {
  468. // console.log('CASE_WXCONFIG_AND_PROJECT data', data);
  469. if (loading) return <Spin style={{marginLeft: 3}}/>;
  470. if (error) return 'error!';
  471. return (
  472. <Menu
  473. theme="dark"
  474. mode="inline"
  475. inlineCollapsed={this.props.inlineCollapsed}
  476. // defaultSelectedKeys={['my-wechat']}
  477. defaultOpenKeys={['trial-case', 'my-create']}
  478. // openKeys={['trial-case', 'my-create']}
  479. onClick={(e) => this.props.switchMenu('sideBar', e)}
  480. selectedKeys={[this.props.sideBar]}
  481. style={{
  482. borderRight: 0,
  483. overflow: 'auto',
  484. height: '100vh',
  485. left: '0',
  486. width: '200px',
  487. position: 'fixed'
  488. }}
  489. >
  490. <SubMenu key="trial-case" title={<span><Icon type="appstore" theme="twoTone"/>
  491. <span><FormattedMessage id="Case Show"/></span>
  492. </span>}>
  493. {
  494. data.caseProject.map((project) => {
  495. if (project) {
  496. let appName = project.projectName;
  497. let configID = project.wxConfig_id.id;
  498. let projectID = project.id;
  499. return (
  500. <Menu.Item key={appName}>
  501. <Link to={{
  502. pathname: `/wechat-service/trial-case/${appName}`,
  503. state: {
  504. appName,
  505. configID,
  506. projectID
  507. }
  508. }}><FormattedMessage id={appName}/></Link>
  509. </Menu.Item>
  510. )
  511. }
  512. return false;
  513. })
  514. }
  515. </SubMenu>
  516. <Menu.Item key="create-config" onClick={this.props.wxShowModal}>
  517. <Icon type="edit" theme="twoTone"/>
  518. <span><FormattedMessage id="Create"/></span>
  519. <Icon type="plus" style={{
  520. position: 'absolute',
  521. top: '35%',
  522. right: '6px',
  523. color: 'white'
  524. }}/>
  525. </Menu.Item>
  526. <SubMenu key="my-create" title={<span><Icon type="user" theme="outlined"/>
  527. <span><FormattedMessage id="My Create"/></span>
  528. </span>}>
  529. {
  530. data.project.map((project) => {
  531. if (project) {
  532. let appName = project.projectName;
  533. let configID = project.wxConfig_id.id;
  534. let projectID = project.id;
  535. return (
  536. <Menu.Item key={appName}>
  537. <Link to={{
  538. pathname: `/wechat-service/my-create/${appName}`,
  539. state: {
  540. appName,
  541. configID,
  542. projectID
  543. }
  544. }}>{appName}</Link>
  545. </Menu.Item>
  546. )
  547. }
  548. return false;
  549. })
  550. }
  551. </SubMenu>
  552. <Menu.Item key="instructions">
  553. <a href="https://ioobot-document.netlify.com/" title="instructions" target="_blank"
  554. rel="noopener noreferrer">
  555. <Icon type="file-text" theme="twoTone"/>
  556. <span><FormattedMessage id="Instructions"/></span>
  557. </a>
  558. </Menu.Item>
  559. </Menu>
  560. )
  561. }
  562. }
  563. </Query>
  564. )
  565. }
  566. }
  567. class User extends Component {
  568. constructor(props) {
  569. super(props);
  570. this.state = {
  571. show: false
  572. }
  573. }
  574. logout = () => {
  575. axios.get(logoutUrl)
  576. .then((res) => {
  577. console.log('logout success',res);
  578. setCookie("user_id", '');
  579. })
  580. .catch((err) => {
  581. });
  582. };
  583. render() {
  584. return (
  585. <Query query={gql(GET_USER)} variables={{id: this.props.userID}}>
  586. {
  587. ({loading, error, data}) => {
  588. if (loading) {
  589. return <Spin style={{marginLeft: 3}}/>
  590. }
  591. if (error) {
  592. return 'error!';
  593. }
  594. let user = data.user_by_id;
  595. if (user !== null) {
  596. const menu = (
  597. <Menu className={'user-detail'}>
  598. <Menu.Item className={'user-info'}>
  599. <p className={'user-info-nickname'}>{user.nickname}</p>
  600. <p className={'user-info-email'}>{user.email}</p>
  601. </Menu.Item>
  602. <Menu.Item>
  603. <a href='https://www.ioobot.com' onClick={(e) => {
  604. e.preventDefault();
  605. this.props.changeLocale(e)
  606. }}>{this.props.languageButton}</a>
  607. </Menu.Item>
  608. <Menu.Item>
  609. <Link to="/login">
  610. <div onClick={() => this.props.switchMenuLevel('menuLevel1', 'user')}>
  611. <FormattedMessage id="Account center"/>
  612. </div>
  613. </Link>
  614. </Menu.Item>
  615. <Menu.Item>
  616. <Link to="/ticket">
  617. <div onClick={() => this.props.switchMenuLevel('menuLevel1', 'ticket')}>
  618. <FormattedMessage id="Submit Support Ticket"/>
  619. </div>
  620. </Link>
  621. </Menu.Item>
  622. <Menu.Item className={'login-out'}>
  623. <a href='https://www.ioobot.com' onClick={(e) => {
  624. e.preventDefault();
  625. this.logout();
  626. }}><FormattedMessage id="exit"/></a>
  627. </Menu.Item>
  628. </Menu>
  629. );
  630. return (
  631. <div className='login-nickname' onClick={()=>{}}>
  632. <Dropdown overlay={menu} placement="bottomRight" trigger={['click']}>
  633. <div>
  634. <span style={{ marginRight: 5 }}>
  635. <Badge dot><Avatar shape="user" icon="user" /></Badge>
  636. </span>
  637. {/*<Icon type="down" />*/}
  638. </div>
  639. </Dropdown>
  640. </div>
  641. )
  642. }
  643. }
  644. }
  645. </Query>
  646. )
  647. }
  648. }