UserCustom.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. import React, {Component} from 'react';
  2. import {Link} from 'react-router-dom';
  3. import {deployAll, graphqlUrl, storeFile, copyCos} from "../../../config";
  4. import {ADD_APIGROUP, ADD_APIGWPATH, ADD_DEPLOY, ADD_PROJECT, SHOW_CLOUD} from "../../../gql";
  5. import {CloudConfig} from "../../../login/CloudConfig";
  6. import {Button, message, Modal, Icon, Steps, Row, Col} from 'antd';
  7. import {FormattedMessage} from 'react-intl';
  8. import {request} from 'graphql-request'
  9. import {idGen} from "../../../func";
  10. import axios from 'axios';
  11. import './index.css';
  12. const Step = Steps.Step;
  13. axios.defaults.withCredentials = true;
  14. class UserCustom extends Component {
  15. constructor(props) {
  16. super(props);
  17. this.state = {
  18. userID: props.userID,
  19. bucketName: props.bucketName,
  20. customName: '',
  21. cloudName: 'tencent',
  22. dbKind: 'fc-db', // mongodb fc-db
  23. disableDeployButton: false,
  24. deployFailed: false
  25. };
  26. this.cloudState = {
  27. cloudID: '',
  28. secretID: '',
  29. secretKey: '',
  30. appId: ''
  31. }
  32. }
  33. getCloudDetail = (cloudID, secretID, secretKey, appId) => {
  34. this.cloudState = {
  35. cloudID,
  36. secretID,
  37. secretKey,
  38. appId
  39. }
  40. };
  41. deploy = () => {
  42. this._isMounted = true;
  43. let _this = this;
  44. let {userID, dbKind, bucketName, customName} = this.state;
  45. let {cloudID} = this.cloudState;
  46. if (bucketName === '') {
  47. console.log('state, 没有传值');
  48. } else {
  49. console.log('开始调用');
  50. this.setState({
  51. disableDeployButton: true
  52. });
  53. let now = new Date().getTime(),
  54. functionName = userID + '_' + customName,
  55. serviceName = userID + '_' + customName,
  56. resources = [`${bucketName}/schema.edn`, `${bucketName}/resolve-map.edn`, `${bucketName}/${dbKind}/${userID}/deploy-conf.edn`, `${bucketName}/html/index.html`, `${bucketName}/wx-config.edn`];
  57. if (dbKind === 'mongodb') {
  58. resources.push(`${bucketName}/${dbKind}/${userID}/mongo-config.edn`)
  59. }
  60. console.log('now', now, 'functionName', functionName, 'serviceName', serviceName, 'resources', resources);
  61. axios.post(deployAll,
  62. {
  63. 'fc-name': functionName,
  64. 'bucket': dbKind === 'mongodb' ? 'native-fc' : 'fcdb-deploy',
  65. 'object-file': 'fc-only.zip',
  66. 'res-bucket': 'case',
  67. 'resources': resources,
  68. 'service-name': serviceName,
  69. 'path': "/*"
  70. })
  71. .then(function (response) {
  72. console.log('response', response);
  73. if (response['data']['apigw-result'] && response['data']['fc-result']) {
  74. // 处理数据
  75. let result = response['data']['apigw-result'];
  76. let apiData = result['api-info'];
  77. let serviceData = result['service-info'];
  78. // 存数据
  79. let pathID = idGen('path'),
  80. groupID = idGen('group'),
  81. deployID = idGen('deploy'),
  82. projectID = idGen('project');
  83. let {apiId, path, method} = apiData;
  84. let {serviceName, serviceId, subDomain} = serviceData;
  85. let pathVarObj = {
  86. id: idGen('path'),
  87. user_id: userID,
  88. apiGWGroup_id: pathID,
  89. deploy_id: deployID,
  90. apiGWName: functionName,
  91. requestMethod: method,
  92. apiGWPath: path,
  93. apiId: apiId,
  94. apiGWDesc: '',
  95. serviceType: 'SCF',
  96. timeout: 300,
  97. createdAt: now,
  98. updatedAt: ''
  99. };
  100. let projectVarObj = {
  101. id: projectID,
  102. projectType: 'case',
  103. cloud_id: cloudID,
  104. user_id: userID,
  105. projectName: functionName,
  106. database_id: '',
  107. apiGWGroup_id: '',
  108. deploy_id: '',
  109. case_id: '',
  110. wxConfig_id: '',
  111. schema_id: '',
  112. createdAt: now,
  113. updatedAt: '',
  114. projectStatus: 'deployed'
  115. };
  116. let groupVarObj = {
  117. id: groupID,
  118. cloud_id: cloudID,
  119. user_id: userID,
  120. userStatus: '',
  121. userDomain: '',
  122. groupName: serviceName,
  123. frontType: '',
  124. region: '',
  125. environmentName: '',
  126. defaultDomain: subDomain,
  127. status: '',
  128. serviceId: serviceId,
  129. createdAt: now,
  130. updatedAt: ''
  131. };
  132. let deployVarObj = {
  133. id: deployID,
  134. cloud_id: cloudID,
  135. functionName,
  136. cosObjectName: '',
  137. region: '',
  138. cosBucketRegion: '',
  139. description: '',
  140. cosBucketName: '',
  141. vpcId: '',
  142. subnetId: '',
  143. memorySize: 512,
  144. timeout: 300,
  145. handler: '',
  146. serviceName: "",
  147. fc_id: '',
  148. createdAt: now,
  149. updatedAt: ''
  150. };
  151. let add_apigwpath = request(graphqlUrl, ADD_APIGWPATH, pathVarObj),
  152. add_project = request(graphqlUrl, ADD_PROJECT, projectVarObj),
  153. add_apigroup = request(graphqlUrl, ADD_APIGROUP, groupVarObj),
  154. add_deploy = request(graphqlUrl, ADD_DEPLOY, deployVarObj);
  155. Promise.all([add_apigwpath, add_project, add_apigroup, add_deploy])
  156. .then(value => {
  157. console.log(value);
  158. // 展示数据
  159. if (_this._isMounted) {
  160. _this.setState({
  161. disableDeployButton: false
  162. });
  163. }
  164. _this.props.history.push({
  165. pathname: `/common/deploy`,
  166. state: {
  167. // 处理传回数据,直接拼接
  168. url: `http://${subDomain}/test/`
  169. }
  170. });
  171. })
  172. .catch(err => {
  173. console.log(err);
  174. });
  175. } else {
  176. console.log('deployAll 失败');
  177. _this.setState({
  178. deployFailed: true,
  179. disableDeployButton: false
  180. })
  181. }
  182. })
  183. .catch(function (error) {
  184. console.log('axios error', error);
  185. });
  186. }
  187. };
  188. componentWillMount() {
  189. this._isMounted = true;
  190. }
  191. componentWillUnmount() {
  192. this._isMounted = false;
  193. }
  194. render() {
  195. let {cloudName, customName, disableDeployButton, userID, check, dbKind, bucketName, deployFailed} = this.state;
  196. let {appId, secretID, secretKey} = this.cloudState;
  197. return (
  198. <div>
  199. <div className="column-menu" onClick={() => {
  200. this.props.backToMe()
  201. }}>
  202. <Icon type="left" style={{color: '#3187FA'}}/>
  203. <FormattedMessage id="back to case show"/>
  204. </div>
  205. <Row>
  206. <Col span={9} offset={2}>
  207. <div className='step-kind'>
  208. 发布体验
  209. </div>
  210. <Steps direction="vertical" current={2}>
  211. <Step title=<Step00/> />
  212. <Step title=<Step01/> />
  213. </Steps>
  214. </Col>
  215. <Col span={9} offset={2}>
  216. <div className='step-kind'>
  217. 发布上线
  218. </div>
  219. <Steps direction="vertical" current={5}>
  220. <Step title=<Step10/> />
  221. <Step title=<Step11 getCloudDetail={this.getCloudDetail} userID={userID}/> />
  222. <Step title=<Step12/> />
  223. <Step title=<Step13/> />
  224. <Step title=<Step14/> />
  225. </Steps>
  226. </Col>
  227. </Row>
  228. </div>
  229. )
  230. }
  231. }
  232. export default UserCustom;
  233. class CloudQueryAndConfig extends Component {
  234. constructor(props) {
  235. super(props);
  236. this.state = {
  237. cloudName: props.cloudName,
  238. userID: props.userID,
  239. cloudID: '',
  240. secretID: '',
  241. secretKey: '',
  242. appId: '',
  243. visible: false,
  244. confirmLoading: false,
  245. }
  246. }
  247. searchCloud = () => {
  248. this._isMounted = true;
  249. let {userID, cloudName} = this.state;
  250. // 如果登录,查询该用户是否设置 cloud
  251. request(graphqlUrl, SHOW_CLOUD, {user_id: userID}).then(data => {
  252. let clouds = data.cloud_by_props.filter(cloud => cloud.cloudName === cloudName);
  253. // 如果限制一个云服务商一个 cloud,那么就是clouds[0]
  254. if (clouds.length === 1) {
  255. let cloud = clouds[0];
  256. let {id, secretId, secretKey, appId} = cloud;
  257. if (this._isMounted) {
  258. this.setState({
  259. cloudID: id,
  260. secretID: secretId,
  261. secretKey,
  262. appId,
  263. });
  264. }
  265. this.props.getCloudDetail(id, secretId, secretKey, appId);
  266. } else if (clouds.length > 1) {
  267. console.log('数据库有多个同一云服务商的 key');
  268. } else {
  269. if (this._isMounted) {
  270. console.log('数据库没有云服务商的 key');
  271. }
  272. }
  273. }
  274. )
  275. };
  276. showModal = () => {
  277. if(this.state.userID) {
  278. this.setState({
  279. visible: true,
  280. });
  281. } else {
  282. message.warning('请先登录');
  283. }
  284. };
  285. handleCancel = () => {
  286. this.setState({
  287. visible: false,
  288. });
  289. this.props.reCheck();
  290. };
  291. componentWillMount() {
  292. this.searchCloud();
  293. this._isMounted = true;
  294. }
  295. componentWillUnmount() {
  296. this._isMounted = false;
  297. }
  298. componentWillReceiveProps(next) {
  299. this.setState({
  300. cloudName: next.cloudName,
  301. userID: next.userID,
  302. }, this.searchCloud);
  303. }
  304. render() {
  305. let {visible, confirmLoading, cloudName} = this.state;
  306. return (
  307. <div>
  308. <Button type='primary' onClick={this.showModal}>点击填写</Button>
  309. <Modal
  310. title="云服务商秘钥设置"
  311. visible={visible}
  312. confirmLoading={confirmLoading}
  313. footer={null}
  314. onCancel={this.handleCancel}
  315. >
  316. <CloudConfig cloudName={cloudName}/>
  317. </Modal>
  318. </div>
  319. )
  320. }
  321. }
  322. const Step00 = (props) => (
  323. <div className='step-block'>
  324. <Button type="primary" size='large' style={{borderRadius: 10}}>一键部署</Button>
  325. <div style={{fontSize: 16}}>
  326. 默认部署至腾讯云. 亚马逊,阿里云等请联系我们
  327. </div>
  328. </div>
  329. );
  330. const Step01 = (props) => (
  331. <div className='step-block'>
  332. 扫码查看结果
  333. </div>
  334. );
  335. const Step10 = (props) => (
  336. <div className='step-block'>
  337. 第一步:注册腾讯云账户
  338. <Button style={{marginLeft: 20}}>使用帮助</Button>
  339. </div>
  340. );
  341. class Step11 extends Component {
  342. constructor(props) {
  343. super(props);
  344. this.state = {
  345. check: 0
  346. }
  347. }
  348. render() {
  349. let {userID, getCloudDetail} = this.props;
  350. let {check} = this.state;
  351. return (
  352. <div className='step-block'>
  353. 第二步:填写腾讯云秘钥,一键部署
  354. <Button style={{marginLeft: 20}}>使用帮助</Button>
  355. <div>
  356. <CloudQueryAndConfig
  357. userID={userID}
  358. getCloudDetail={getCloudDetail}
  359. cloudName='tencent'
  360. check={check}
  361. reCheck={() => {
  362. this.setState({check: check + 1})
  363. }}
  364. />
  365. </div>
  366. </div>
  367. )
  368. }
  369. }
  370. const Step12 = (props) => (
  371. <div className='step-block'>
  372. 第三步:注册微信公众号/小程序
  373. <Button style={{marginLeft: 20}}>使用帮助</Button>
  374. </div>
  375. );
  376. const Step13 = (props) => (
  377. <div className='step-block'>
  378. 第四步:微信公众号/小程序 后台填写配置
  379. <Button style={{marginLeft: 20}}>使用帮助</Button>
  380. </div>
  381. );
  382. const Step14 = (props) => (
  383. <div className='step-block'>
  384. 开始使用
  385. </div>
  386. );