App.jsx 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. import React, {Component} from 'react';
  2. import {Layout, Menu, Button, Spin, Icon, LocaleProvider} 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 './graphqlService/component/graphql/index.css';
  17. import './index.css'
  18. import Create from "./graphqlService/component/schema/Create";
  19. import WxCreate from "./wechatService/wxCreate/WxCreate";
  20. import {SHOW_ALL_SCHEMA, SHOW_CASE_SCHEMA,SHOW_PROJECT,CASE_SCHEMA_AND_PROJECT,CASE_WXCONFIG_AND_PROJECT} from "../gql";
  21. import axios from 'axios';
  22. import {getCookie, setCookie} from "../cookie";
  23. axios.defaults.withCredentials = true;
  24. const {SubMenu} = Menu;
  25. const {Header, Sider} = Layout;
  26. moment.locale('en');
  27. class App extends Component {
  28. constructor(props) {
  29. super(props);
  30. this.state = {
  31. menuLevel1: "graphql-service",
  32. sideBar: "ecommerce",
  33. collapsed: false,
  34. inlineCollapsed: false,
  35. language: props.language,
  36. locale: props.language === "中文" ? zhCN : undefined,
  37. languageButton: props.language === "中文" ? "English" : "中文",
  38. visible: false,
  39. wxVisible: false
  40. };
  41. }
  42. componentWillMount() {
  43. let urlList = window.location.pathname.split("/");
  44. let urlListLength = urlList.length;
  45. if (urlListLength > 2) {
  46. this.setState({menuLevel1: urlList[1]});
  47. if (urlList[3] !== 'index') {
  48. this.setState({sideBar: urlList[3]});
  49. }
  50. }
  51. let userID = getCookie('user_id');
  52. if (userID === undefined || '') {
  53. axios.get(this.state.getIdUrl)
  54. .then((res) => {
  55. if (res.data !== '') {
  56. setCookie("user_id", res.data);
  57. }
  58. })
  59. .catch(function (err) {
  60. console.log(err);
  61. });
  62. }
  63. }
  64. onCollapse = (collapsed) => {
  65. this.setState({collapsed});
  66. };
  67. switchMenu = (menuName, e) => {
  68. this.setState({
  69. [menuName]: e.key,
  70. });
  71. };
  72. switchMenuTab = (menuName, e) => {
  73. this.setState({
  74. [menuName]: e.key,
  75. });
  76. if (e.key === "graphql-service") {
  77. this.setState({sideBar: "ecommerce"});
  78. }
  79. };
  80. switchSidebar = (value) => {
  81. this.setState({
  82. sideBar: value,
  83. });
  84. };
  85. switchMenuLevel = (menuName, value) => {
  86. this.setState({
  87. [menuName]: value,
  88. });
  89. };
  90. showModal = () => {
  91. this.setState({
  92. visible: true,
  93. });
  94. };
  95. wxShowModal = () => {
  96. this.setState({
  97. wxVisible: true,
  98. });
  99. };
  100. hideModal = () => {
  101. this.setState({
  102. visible: false,
  103. });
  104. };
  105. wxHideModal = () => {
  106. this.setState({
  107. wxVisible: false,
  108. });
  109. };
  110. changeLocale = (e) => {
  111. e.stopPropagation();
  112. let {language} = this.state;
  113. // console.log('app language',language);
  114. let changeLanguage = language === "中文" ? "English" : "中文";
  115. let local = language === "中文" ? undefined : zhCN;
  116. let languageButton = language === "中文" ? "中文" : "English";
  117. sessionStorage.setItem("language", changeLanguage);
  118. this.props.changeLanguage(changeLanguage);
  119. this.setState({
  120. language: changeLanguage,
  121. locale: local,
  122. languageButton: languageButton
  123. });
  124. if (!local) {
  125. moment.locale('en');
  126. } else {
  127. moment.locale('zh-cn');
  128. }
  129. };
  130. render() {
  131. const {locale, languageButton, visible, wxVisible} = this.state;
  132. return (
  133. <Router>
  134. <Layout style={{minHeight: '100vh'}}>
  135. <Header className="header" style={{position: 'fixed', zIndex: 1, width: '100%'}}>
  136. <Link to="/"
  137. onClick={() => this.setState({menuLevel1: "graphql-service", sideBar: "ecommerce"})}>
  138. <div className="logo-wrapper">
  139. <div className='logo'/>
  140. </div>
  141. </Link>
  142. <Menu
  143. theme="dark"
  144. mode="horizontal"
  145. selectedKeys={[this.state.menuLevel1]}
  146. style={{lineHeight: '64px'}}
  147. onClick={(e) => this.switchMenuTab('menuLevel1', e)}
  148. >
  149. <Menu.Item key="graphql-service">
  150. <Link to="/graphql-service/trial-case/index"><FormattedMessage
  151. id="Graphql Service"/></Link>
  152. </Menu.Item>
  153. <Menu.Item key="wechat-service">
  154. <Link to="/wechat-service/trial-case/index"><FormattedMessage
  155. id="Wechat Service"/></Link>
  156. </Menu.Item>
  157. {/*<Menu.Item key="quant-service">*/}
  158. {/*<Link to="/quant-service/trial-case/index"><FormattedMessage id="Quantization Service"/></Link>*/}
  159. {/*</Menu.Item>*/}
  160. </Menu>
  161. <Link to="/login">
  162. <Button className='login-button' type='primary'
  163. onClick={() => this.switchMenuLevel('menuLevel1', 'user')}>
  164. <FormattedMessage id="Login"/></Button>
  165. </Link>
  166. <div className="change-locale">
  167. <Button size="small" onClick={(e) => this.changeLocale(e)}>{languageButton}</Button>
  168. </div>
  169. </Header>
  170. {(() => {
  171. switch (this.state.menuLevel1) {
  172. case 'graphql-service':
  173. return (
  174. <Sider
  175. width={200}
  176. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  177. collapsible
  178. collapsed={this.state.collapsed}
  179. onCollapse={this.onCollapse}
  180. >
  181. <GraphqlSidebar inlineCollapsed={this.state.inlineCollapsed}
  182. sideBar={this.state.sideBar} switchMenu={this.switchMenu}
  183. showModal={this.showModal}/>
  184. </Sider>
  185. );
  186. case 'wechat-service':
  187. return (
  188. <Sider
  189. width={200}
  190. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  191. collapsible
  192. collapsed={this.state.collapsed}
  193. onCollapse={this.onCollapse}
  194. >
  195. <WxConfigSiderbar inlineCollapsed={this.state.inlineCollapsed}
  196. sideBar={this.state.sideBar} switchMenu={this.switchMenu}
  197. wxShowModal={this.wxShowModal}/>
  198. </Sider>
  199. );
  200. case 'quant-service':
  201. return (
  202. <Sider
  203. width={200}
  204. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  205. collapsible
  206. collapsed={this.state.collapsed}
  207. onCollapse={this.onCollapse}
  208. >
  209. <Menu
  210. theme="dark"
  211. mode="inline"
  212. inlineCollapsed={this.state.inlineCollapsed}
  213. defaultSelectedKeys={['quant-service']}
  214. defaultOpenKeys={['trial-case']}
  215. // openKeys={['cloud-function']}
  216. onClick={(e) => this.switchMenu('sideBar', e)}
  217. selectedKeys={[this.state.sideBar]}
  218. style={{
  219. borderRight: 0,
  220. overflow: 'auto',
  221. height: '100vh',
  222. left: '0',
  223. width: '200px',
  224. position: 'fixed'
  225. }}
  226. >
  227. <SubMenu key="trial-case" title={<span><Icon type="appstore"
  228. theme="twoTone"/><span>Case Show</span></span>}>
  229. <Menu.Item key="quant-service">
  230. <Link to="/quant-service/trial-case/quant case">quant case</Link>
  231. </Menu.Item>
  232. </SubMenu>
  233. <Menu.Item key="instructions">
  234. <a href="https://ioobot-document.netlify.com/" title="instructions"
  235. target="instructions">
  236. <Icon type="file-text" theme="twoTone"/>
  237. <span><FormattedMessage id="Instructions"/></span>
  238. </a>
  239. </Menu.Item>
  240. </Menu>
  241. </Sider>
  242. );
  243. case 'user':
  244. return (
  245. <Sider
  246. width={200}
  247. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  248. collapsible
  249. collapsed={this.state.collapsed}
  250. onCollapse={this.onCollapse}
  251. >
  252. <Menu
  253. theme="dark"
  254. defaultSelectedKeys={['cloud-settings']}
  255. onClick={(e) => this.switchMenu('sideBar', e)}
  256. selectedKeys={[this.state.sideBar]}
  257. style={{
  258. borderRight: 0,
  259. overflow: 'auto',
  260. height: '100vh',
  261. left: '0',
  262. width: '200px',
  263. position: 'fixed'
  264. }}
  265. >
  266. <Menu.Item key="account">
  267. <Icon type="setting" theme="twoTone"/>
  268. <span><FormattedMessage id="Account center"/></span>
  269. <Link to="/login/account"/>
  270. </Menu.Item>
  271. <Menu.Item key="cloud-settings">
  272. <Icon type="cloud" theme="twoTone"/>
  273. <span><FormattedMessage id="Cloud settings"/></span>
  274. <Link to="/login/cloud"/>
  275. </Menu.Item>
  276. </Menu>
  277. </Sider>
  278. );
  279. default:
  280. return (
  281. <Sider
  282. width={200}
  283. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  284. collapsible
  285. collapsed={this.state.collapsed}
  286. onCollapse={this.onCollapse}
  287. >
  288. <GraphqlSidebar inlineCollapsed={this.state.inlineCollapsed}
  289. sideBar={this.state.sideBar} switchMenu={this.switchMenu}
  290. showModal={this.showModal}/>
  291. </Sider>
  292. );
  293. }
  294. })()}
  295. <Create visible={visible} hideModal={this.hideModal} switchSidebar={this.switchSidebar}/>
  296. <WxCreate visible={wxVisible} hideModal={this.wxHideModal} switchSidebar={this.switchSidebar}/>
  297. <LocaleProvider locale={locale}>
  298. <Layout style={{marginTop: '64px', zIndex: '0'}}
  299. key={locale ? locale.locale : 'en'/* Have to refresh for production environment */}>
  300. <Switch>
  301. <Route path="/" exact component={TrialCase}/>
  302. <Route path="/graphql-service/trial-case/:case" component={TrialCase}/>
  303. <Route path="/graphql-service/my-create/:case" component={UserCreate}/>
  304. <Route path="/wechat-service/trial-case/:case" component={WxTrialCase}/>
  305. <Route path="/wechat-service/my-create/:case" component={WxUserCreate}/>
  306. <Route path="/quant-service/:sidebar/:case" component={QuantService}/>
  307. <Route path="/login/:setting" component={Login}/>
  308. <Route path="/login" component={Login}/>
  309. <Redirect path="*" to="/"/>
  310. </Switch>
  311. </Layout>
  312. </LocaleProvider>
  313. </Layout>
  314. </Router>
  315. );
  316. }
  317. }
  318. export default App;
  319. class GraphqlSidebar extends Component {
  320. constructor(props) {
  321. super(props);
  322. this.state = {
  323. userID: getCookie('user_id'),
  324. }
  325. }
  326. render() {
  327. return (
  328. <Query query={gql(CASE_SCHEMA_AND_PROJECT)} variables={{projectType:'graphql',user_id: this.state.userID}}>
  329. {
  330. ({loading, error, data}) => {
  331. // console.log('CASE_SCHEMA_AND_PROJECT data', data);
  332. if (loading) return <Spin style={{marginLeft: 3}}/>;
  333. if (error) return 'error!';
  334. localStorage.setItem('ecommerce', data.caseSchema.find(obj => obj.schemaName === 'ecommerce').schemaData);
  335. localStorage.setItem('subscribe', data.caseSchema.find(obj => obj.schemaName === 'subscribe').schemaData);
  336. localStorage.setItem('bills', data.caseSchema.find(obj => obj.schemaName === 'bills').schemaData);
  337. return (
  338. <Menu
  339. theme="dark"
  340. mode="inline"
  341. inlineCollapsed={this.props.inlineCollapsed}
  342. defaultSelectedKeys={['ecommerce']}
  343. defaultOpenKeys={['trial-case', 'my-create']}
  344. // openKeys={['trial-case', 'my-create']}
  345. onClick={(e) => this.props.switchMenu('sideBar', e)}
  346. selectedKeys={[this.props.sideBar]}
  347. style={{
  348. borderRight: 0,
  349. overflow: 'auto',
  350. height: '100vh',
  351. left: '0',
  352. width: '200px',
  353. position: 'fixed'
  354. }}
  355. >
  356. <SubMenu
  357. key="trial-case"
  358. title={<span><Icon type="appstore" theme="twoTone"/><span><FormattedMessage id="Case Show"/></span></span>}>
  359. {
  360. data.caseSchema.map((schema) =>
  361. <Menu.Item key={schema.schemaName}>
  362. <Link to={{
  363. pathname: `/graphql-service/trial-case/${schema.schemaName}`,
  364. state: {
  365. schemaName: schema.schemaName,
  366. schemaID: schema.id
  367. }
  368. }}>{schema.schemaName}</Link>
  369. </Menu.Item>)
  370. }
  371. </SubMenu>
  372. <Menu.Item key="create-graphql" onClick={this.props.showModal}>
  373. <Icon type="edit" theme="twoTone"/>
  374. <span><FormattedMessage id="Create"/></span>
  375. <Icon type="plus" style={{
  376. position: 'absolute',
  377. top: '35%',
  378. right: '6px',
  379. color: 'white'
  380. }}/>
  381. </Menu.Item>
  382. <SubMenu
  383. key="my-create"
  384. title={<span><Icon type="user" theme="outlined"/><span><FormattedMessage id="My Create"/></span></span>}>
  385. {
  386. data.project.map((project) =>
  387. <Menu.Item key={project.projectName}>
  388. <Link to={{
  389. pathname: `/graphql-service/my-create/${project.projectName}`,
  390. state: {
  391. schemaName: project.projectName,
  392. schemaID: project.schema_id.id,
  393. projectId: project.id
  394. }
  395. }}>{project.projectName}</Link>
  396. </Menu.Item>)
  397. }
  398. </SubMenu>
  399. <Menu.Item key="instructions">
  400. <a href="https://ioobot-document.netlify.com/" title="instructions" target="_blank"
  401. rel="noopener noreferrer">
  402. <Icon type="file-text" theme="twoTone"/>
  403. <span><FormattedMessage id="Instructions"/></span>
  404. </a>
  405. </Menu.Item>
  406. </Menu>
  407. )
  408. }
  409. }
  410. </Query>
  411. )
  412. }
  413. }
  414. class WxConfigSiderbar extends Component {
  415. constructor(props) {
  416. super(props);
  417. this.state = {
  418. userID: getCookie('user_id'),
  419. }
  420. }
  421. render() {
  422. return (
  423. <Query query={gql(CASE_WXCONFIG_AND_PROJECT)} variables={{projectType:'wx',user_id: this.state.userID}}>
  424. {
  425. ({loading, error, data}) => {
  426. if (loading) return <Spin style={{marginLeft: 3}}/>;
  427. if (error) return 'error!';
  428. return (
  429. <Menu
  430. theme="dark"
  431. mode="inline"
  432. inlineCollapsed={this.props.inlineCollapsed}
  433. defaultSelectedKeys={['my-wechat']}
  434. defaultOpenKeys={['trial-case', 'my-create']}
  435. // openKeys={['trial-case', 'my-create']}
  436. onClick={(e) => this.props.switchMenu('sideBar', e)}
  437. selectedKeys={[this.props.sideBar]}
  438. style={{
  439. borderRight: 0,
  440. overflow: 'auto',
  441. height: '100vh',
  442. left: '0',
  443. width: '200px',
  444. position: 'fixed'
  445. }}
  446. >
  447. <SubMenu key="trial-case" title={<span><Icon type="appstore" theme="twoTone"/>
  448. <span><FormattedMessage id="Case Show"/></span>
  449. </span>}>
  450. {
  451. data.caseWxConfig.map((config) =>
  452. <Menu.Item key={config.appName}>
  453. <Link to={{
  454. pathname: `/wechat-service/trial-case/${config.appName}`,
  455. state: {
  456. appName: config.appName,
  457. configID: config.id
  458. }
  459. }}>{config.appName}</Link>
  460. </Menu.Item>)
  461. }
  462. </SubMenu>
  463. <Menu.Item key="create-config" onClick={this.props.wxShowModal}>
  464. <Icon type="edit" theme="twoTone"/>
  465. <span><FormattedMessage id="Create"/></span>
  466. <Icon type="plus" style={{
  467. position: 'absolute',
  468. top: '35%',
  469. right: '6px',
  470. color: 'white'
  471. }}/>
  472. </Menu.Item>
  473. <SubMenu key="my-create" title={<span><Icon type="user" theme="outlined"/>
  474. <span><FormattedMessage id="My Create"/></span>
  475. </span>}>
  476. {
  477. data.project.map((project) =>{
  478. let appName = project.wxConfig_id.appName;
  479. let configID = project.wxConfig_id.id;
  480. return (
  481. <Menu.Item key={appName}>
  482. <Link to={{
  483. pathname: `/wechat-service/my-create/${appName}`,
  484. state: {
  485. appName,
  486. configID
  487. }
  488. }}>{appName}</Link>
  489. </Menu.Item>
  490. )
  491. })
  492. }
  493. </SubMenu>
  494. <Menu.Item key="instructions">
  495. <a href="https://ioobot-document.netlify.com/" title="instructions" target="_blank"
  496. rel="noopener noreferrer">
  497. <Icon type="file-text" theme="twoTone"/>
  498. <span><FormattedMessage id="Instructions"/></span>
  499. </a>
  500. </Menu.Item>
  501. </Menu>
  502. )
  503. }
  504. }
  505. </Query>
  506. )
  507. }
  508. }