App.jsx 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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 WechatService from "../wechatService/WechatService";
  11. import QuantService from "../quantService/QuantService";
  12. import TrialCase from "../trialCase/TrialCase";
  13. import UserCreate from "../userCreate/UserCreate";
  14. import Login from "../login/Login";
  15. import '../common/graphql/index.css';
  16. import './index.css'
  17. import Create from "../common/schema/Create";
  18. import {SHOW_ALL_SCHEMA} from "../gql";
  19. import axios from 'axios';
  20. import {getCookie} from "../cookie";
  21. //案例入口
  22. import ShopApp from '../../case/ShopApp/src/App'
  23. import OrderApp from '../../case/OrderApp/src/App'
  24. import BillApp from '../../case/BillApp/src/App'
  25. axios.defaults.withCredentials = true;
  26. const {SubMenu} = Menu;
  27. const {Header, Sider} = Layout;
  28. moment.locale('en');
  29. class App extends Component {
  30. constructor(props) {
  31. super(props);
  32. this.state = {
  33. menuLevel1: "graphql-service",
  34. sideBar: "ecommerce",
  35. collapsed: false,
  36. inlineCollapsed: false,
  37. language:props.language,
  38. locale: props.language === "中文" ? zhCN : undefined,
  39. languageButton:props.language === "中文" ? "English" : "中文",
  40. visible: false
  41. };
  42. }
  43. componentWillMount(){
  44. console.log('window.location',window.location);
  45. let urlList = window.location.pathname.split("/");
  46. let urlListLength = urlList.length;
  47. console.log('urlList',urlList,urlListLength);
  48. if (urlListLength > 2) {
  49. this.setState({menuLevel1:urlList[1]});
  50. if(urlList[3] !== 'index'){
  51. this.setState({sideBar:urlList[3]});
  52. }
  53. }
  54. }
  55. onCollapse = (collapsed) => {
  56. // console.log(collapsed);
  57. this.setState({collapsed});
  58. };
  59. switchMenu = (menuName, e) => {
  60. // console.log('menuName', menuName, 'e', e);
  61. this.setState({
  62. [menuName]: e.key,
  63. });
  64. };
  65. switchSidebar = (value) => {
  66. this.setState({
  67. sideBar:value,
  68. });
  69. };
  70. switchMenuLevel = (menuName, value) => {
  71. this.setState({
  72. [menuName]: value,
  73. });
  74. };
  75. showModal = () => {
  76. this.setState({
  77. visible: true,
  78. });
  79. };
  80. hideModal = () => {
  81. this.setState({
  82. visible: false,
  83. });
  84. };
  85. changeLocale = (e) => {
  86. e.stopPropagation();
  87. let {language} = this.state;
  88. // console.log('app language',language);
  89. let changeLanguage = language === "中文" ? "English" : "中文";
  90. let local = language === "中文" ? undefined : zhCN;
  91. let languageButton = language === "中文" ? "中文" : "English";
  92. sessionStorage.setItem("language", changeLanguage);
  93. this.props.changeLanguage(changeLanguage);
  94. this.setState({
  95. language: changeLanguage,
  96. locale: local,
  97. languageButton:languageButton
  98. });
  99. if (!local) {
  100. moment.locale('en');
  101. } else {
  102. moment.locale('zh-cn');
  103. }
  104. };
  105. render() {
  106. const {locale, languageButton, visible} = this.state;
  107. return (
  108. <Router>
  109. <Layout style={{minHeight: '100vh'}}>
  110. <Header className="header" style={{position: 'fixed', zIndex: 1, width: '100%'}}>
  111. <Link to="/" onClick={() => this.switchMenuLevel('menuLevel1', "graphql-service")}>
  112. <div className="logo-wrapper">
  113. <div className='logo'/>
  114. </div>
  115. </Link>
  116. <Menu
  117. theme="dark"
  118. mode="horizontal"
  119. // defaultSelectedKeys={['graphql-service']}
  120. selectedKeys={[this.state.menuLevel1]}
  121. style={{lineHeight: '64px'}}
  122. onClick={(e) => this.switchMenu('menuLevel1', e)}
  123. >
  124. <Menu.Item key="graphql-service">
  125. <Link to="/graphql-service/trial-case/index"><FormattedMessage id="Graphql Service"/></Link>
  126. </Menu.Item>
  127. <Menu.Item key="wechat-service">
  128. <Link to="/wechat-service/trial-case/index"><FormattedMessage
  129. id="Wechat Service"/></Link>
  130. </Menu.Item>
  131. <Menu.Item key="quant-service">
  132. <Link to="/quant-service/trial-case/index"><FormattedMessage id="Quantization Service"/></Link>
  133. </Menu.Item>
  134. </Menu>
  135. <Link to="/login">
  136. <Button className='login-button' type='primary' onClick={()=>this.switchMenuLevel('menuLevel1','user')} >Login</Button>
  137. </Link>
  138. <div className="change-locale">
  139. <Button size="small" onClick={(e) => this.changeLocale(e)}>{languageButton}</Button>
  140. </div>
  141. </Header>
  142. {(() => {
  143. switch (this.state.menuLevel1) {
  144. case 'graphql-service':
  145. return (
  146. <Sider
  147. width={200}
  148. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  149. collapsible
  150. collapsed={this.state.collapsed}
  151. onCollapse={this.onCollapse}
  152. >
  153. <GraphqlSidebar inlineCollapsed={this.state.inlineCollapsed}
  154. sideBar={this.state.sideBar} switchMenu={this.switchMenu}
  155. showModal={this.showModal}/>
  156. </Sider>
  157. );
  158. case 'wechat-service':
  159. return (
  160. <Sider
  161. width={200}
  162. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  163. collapsible
  164. collapsed={this.state.collapsed}
  165. onCollapse={this.onCollapse}
  166. >
  167. <Menu
  168. theme="dark"
  169. mode="inline"
  170. inlineCollapsed={this.state.inlineCollapsed}
  171. defaultSelectedKeys={['my-wechat']}
  172. defaultOpenKeys={['my-create']}
  173. // openKeys={['cloud-function']}
  174. onClick={(e) => this.switchMenu('sideBar', e)}
  175. selectedKeys={[this.state.sideBar]}
  176. style={{
  177. borderRight: 0,
  178. overflow: 'auto',
  179. height: '100vh',
  180. left: '0',
  181. width: '200px',
  182. position: 'fixed'
  183. }}
  184. >
  185. <SubMenu key="my-create" title={<span><Icon type="user" theme="outlined"/><span>My Create</span></span>}>
  186. <Menu.Item key="my-wechat">
  187. <Link to="/wechat-service/my-create/my wechat">my wechat</Link></Menu.Item>
  188. </SubMenu>
  189. <Menu.Item key="instructions" >
  190. <a href="https://ioobot-document.netlify.com/" title="instructions" target="instructions">
  191. <Icon type="file-text" theme="twoTone" />
  192. <span>Instructions</span>
  193. </a>
  194. </Menu.Item>
  195. </Menu>
  196. </Sider>
  197. );
  198. case 'quant-service':
  199. return (
  200. <Sider
  201. width={200}
  202. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  203. collapsible
  204. collapsed={this.state.collapsed}
  205. onCollapse={this.onCollapse}
  206. >
  207. <Menu
  208. theme="dark"
  209. mode="inline"
  210. inlineCollapsed={this.state.inlineCollapsed}
  211. defaultSelectedKeys={['quant-service']}
  212. defaultOpenKeys={['trial-case']}
  213. // openKeys={['cloud-function']}
  214. onClick={(e) => this.switchMenu('sideBar', e)}
  215. selectedKeys={[this.state.sideBar]}
  216. style={{
  217. borderRight: 0,
  218. overflow: 'auto',
  219. height: '100vh',
  220. left: '0',
  221. width: '200px',
  222. position: 'fixed'
  223. }}
  224. >
  225. <SubMenu key="trial-case" title={<span><Icon type="appstore" theme="twoTone"/><span>Case Show</span></span>}>
  226. <Menu.Item key="quant-service">
  227. <Link to="/quant-service/trial-case/quant case">quant case</Link>
  228. </Menu.Item>
  229. </SubMenu>
  230. <Menu.Item key="instructions" >
  231. <a href="https://ioobot-document.netlify.com/" title="instructions" target="instructions">
  232. <Icon type="file-text" theme="twoTone" />
  233. <span>Instructions</span>
  234. </a>
  235. </Menu.Item>
  236. </Menu>
  237. </Sider>
  238. );
  239. case 'user':
  240. return (
  241. <Sider
  242. width={200}
  243. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  244. collapsible
  245. collapsed={this.state.collapsed}
  246. onCollapse={this.onCollapse}
  247. >
  248. <Menu
  249. theme="dark"
  250. defaultSelectedKeys={['cloud-settings']}
  251. onClick={(e) => this.switchMenu('sideBar', e)}
  252. selectedKeys={[this.state.sideBar]}
  253. style={{
  254. borderRight: 0,
  255. overflow: 'auto',
  256. height: '100vh',
  257. left: '0',
  258. width: '200px',
  259. position: 'fixed'
  260. }}
  261. >
  262. <Menu.Item key="account">
  263. <Icon type="setting" theme="twoTone" />
  264. <span>Account center</span>
  265. <Link to="/login/"/>
  266. </Menu.Item>
  267. <Menu.Item key="cloud-settings">
  268. <Icon type="cloud" theme="twoTone" />
  269. <span>Cloud settings</span>
  270. <Link to="/login/"/>
  271. </Menu.Item>
  272. </Menu>
  273. </Sider>
  274. );
  275. default:
  276. return (
  277. <Sider
  278. width={200}
  279. style={{background: '#fff', marginTop: '64px', zIndex: '0'}}
  280. collapsible
  281. collapsed={this.state.collapsed}
  282. onCollapse={this.onCollapse}
  283. >
  284. <GraphqlSidebar inlineCollapsed={this.state.inlineCollapsed}
  285. sideBar={this.state.sideBar} switchMenu={this.switchMenu}
  286. showModal={this.showModal}/>
  287. </Sider>
  288. );
  289. }
  290. })()}
  291. <Create visible={visible} hideModal={this.hideModal} switchSidebar={this.switchSidebar}/>
  292. <LocaleProvider locale={locale}>
  293. <Layout style={{marginTop: '64px', zIndex: '0'}}
  294. key={locale ? locale.locale : 'en'/* Have to refresh for production environment */}>
  295. <Switch>
  296. <Route path="/" exact component={TrialCase}/>
  297. <Route path="/graphql-service/trial-case/:case" component={TrialCase}/>
  298. <Route path="/graphql-service/my-create/:case" component={UserCreate}/>
  299. <Route path="/wechat-service/:sidebar/:case" component={WechatService}/>
  300. <Route path="/quant-service/:sidebar/:case" component={QuantService}/>
  301. <Route path="/login" component={Login}/>
  302. <Redirect path="*" to="/"/>
  303. </Switch>
  304. </Layout>
  305. </LocaleProvider>
  306. </Layout>
  307. </Router>
  308. );
  309. }
  310. }
  311. export default App;
  312. class GraphqlSidebar extends Component {
  313. constructor(props) {
  314. super(props);
  315. this.state = {
  316. userID: getCookie('user_id'),
  317. }
  318. }
  319. render() {
  320. return (
  321. <Query query={gql(SHOW_ALL_SCHEMA)} variables={{user_id: this.state.userID}}>
  322. {
  323. ({loading, error, data}) => {
  324. // console.log('user schema data',data);
  325. if (loading) return <Spin style={{marginLeft: 3}}/>;
  326. if (error) return 'error!';
  327. localStorage.setItem('ecommerce', data.caseSchema.find(obj=>obj.schemaName==='ecommerce').schemaData);
  328. localStorage.setItem('subscribe', data.caseSchema.find(obj=>obj.schemaName==='subscribe').schemaData);
  329. localStorage.setItem('bills', data.caseSchema.find(obj=>obj.schemaName==='bills').schemaData);
  330. return (
  331. <Menu
  332. theme="dark"
  333. mode="inline"
  334. inlineCollapsed={this.props.inlineCollapsed}
  335. defaultSelectedKeys={['ecommerce']}
  336. defaultOpenKeys={['trial-case', 'my-create']}
  337. // openKeys={['trial-case', 'my-create']}
  338. onClick={(e) => this.props.switchMenu('sideBar', e)}
  339. selectedKeys={[this.props.sideBar]}
  340. style={{
  341. borderRight: 0,
  342. overflow: 'auto',
  343. height: '100vh',
  344. left: '0',
  345. width: '200px',
  346. position: 'fixed'
  347. }}
  348. >
  349. <SubMenu key="trial-case" title={<span><Icon type="appstore" theme="twoTone"/><span>Case Show</span></span>}>
  350. {
  351. data.caseSchema.map((schema) =>
  352. <Menu.Item key={schema.schemaName}>
  353. <Link to={{
  354. pathname: `/graphql-service/trial-case/${schema.schemaName}`,
  355. state:{
  356. schemaName:schema.schemaName,
  357. schemaID:schema.id
  358. }
  359. }}>{schema.schemaName}</Link>
  360. </Menu.Item>)
  361. }
  362. </SubMenu>
  363. <Menu.Item key="create-graphql" onClick={this.props.showModal}>
  364. <Icon type="edit" theme="twoTone"/>
  365. <span>Create</span>
  366. <Icon type="plus" style={{
  367. position: 'absolute',
  368. top: '35%',
  369. right: '6px',
  370. color: 'white'
  371. }}/>
  372. </Menu.Item>
  373. <SubMenu key="my-create" title={<span><Icon type="user" theme="outlined"/><span>My Create</span></span>}>
  374. {
  375. data.userSchema.map((schema) =>
  376. <Menu.Item key={schema.schemaName}>
  377. <Link to={{
  378. pathname: `/graphql-service/my-create/${schema.schemaName}`,
  379. state:{
  380. schemaName:schema.schemaName,
  381. schemaID:schema.id
  382. }
  383. }}>{schema.schemaName}</Link>
  384. </Menu.Item>)
  385. }
  386. </SubMenu>
  387. <Menu.Item key="instructions">
  388. <a href="https://ioobot-document.netlify.com/" title="instructions" target="_blank" rel="noopener noreferrer">
  389. <Icon type="file-text" theme="twoTone" />
  390. <span>Instructions</span>
  391. </a>
  392. </Menu.Item>
  393. </Menu>
  394. )
  395. }
  396. }
  397. </Query>
  398. )
  399. }
  400. }