import React, {Component} from 'react'; import {Layout, Button, notification, Spin} from 'antd'; import {getCookie} from "../../../cookie"; import {graphqlUrl, storeFile} from "../../../config"; import {SHOW_CLOUD} from "../../../gql"; import {request} from 'graphql-request' import axios from 'axios'; import {deployAll} from "../../../config"; import {ADD_APIGWPATH, ADD_APIGROUP, ADD_DEPLOY, ADD_PROJECT} from "../../../gql"; import {idGen} from "../../../func"; const {Content} = Layout; axios.defaults.withCredentials = true; class DeployCloudChoose extends Component { constructor() { super(); this.state = { userID: 'demo', dbKind: 'mongodb', // mongodb fc-db cloudName: 'tencent', cloudID: 'tencent_CloudID', secretID: '', secretKey: '', appId: '', bucketName: '', disabled: false } } componentWillMount() { this._isMounted = true; let userID = getCookie('user_id') || this.state.userID; if (userID !== undefined && userID !== '') { this.setState({ userID }); // 查询是否设置 cloud request(graphqlUrl, SHOW_CLOUD, {user_id: userID}).then(data => { data.cloud_by_props.forEach(cloud => { switch (cloud.cloudName) { case 'tencent': this.setState({ cloudID: cloud.id, secretID: cloud.secretId, secretKey: cloud.secretKey, appId: cloud.appId, cloudName: 'tencent' }); break; case 'aliyun': // 没写 break; case 'amazon': // 没写 break; default: break; } }); } ) } if (this.props.location && this.props.location.state) { switch (this.props.location.state.schemaID) { case 'order_schemaID': this.setState({ bucketName: 'appointment' }); break; case 'ecommerce_schemaID': this.setState({ bucketName: 'e-commerce' }); break; default: break; } } } componentWillUnmount() { this._isMounted = false; } deploy() { let _this = this; let {userID, dbKind, bucketName} = this.state; if (bucketName === '') { console.log('state, 没有传值'); } else { // 开始调用 console.log('开始调用'); this.setState({ disabled: true }); let now = new Date().getTime(), functionName = this.state.userID + '_' + bucketName.substr(0, 2) + '_' + now, serviceName = this.state.userID + '_' + bucketName.substr(0, 2) + '_' + now, resources = [`${bucketName}/schema.edn`, `${bucketName}/resolve-map.edn`, `${bucketName}/${dbKind}/${userID}/deploy-conf.edn`]; if (dbKind === 'mongodb') { resources.push(`${bucketName}/${dbKind}/${userID}/mongo-config.edn`) } console.log('now', now, 'functionName', functionName, 'serviceName', serviceName, 'resources', resources); axios.post(deployAll, { 'fc-name': functionName, 'bucket': dbKind === 'mongodb'? 'native-fc' : 'fcdb-deploy', 'object-file': 'fc-only.zip', 'res-bucket': 'case', 'resources': resources, 'service-name': serviceName, 'path': "/*" }) .then(function (response) { console.log('response', response); if(response['data']['apigw-result'] && response['data']['fc-result']) { // 处理数据 let result = response['data']['apigw-result']; let apiData = result['api-info']; let serviceData = result['service-info']; // 存数据 let pathID = idGen('path'), groupID = idGen('group'), deployID = idGen('deploy'), projectID = idGen('project'); let {apiId, path, method} = apiData; let {serviceName, serviceId, subDomain} = serviceData; let {userID, cloudID} = _this.state; let pathVarObj = { id: idGen('path'), user_id: userID, apiGWGroup_id: pathID, deploy_id: deployID, apiGWName: functionName, requestMethod: method, apiGWPath: path, apiId: apiId, apiGWDesc: '', serviceType: 'SCF', timeout: 300, createdAt: now, updatedAt: '' }; let projectVarObj = { id: projectID, projectType: 'case', cloud_id: cloudID, user_id: userID, projectName: functionName, database_id: '', apiGWGroup_id: '', deploy_id: '', case_id: '', wxConfig_id: '', schema_id: '', createdAt: now, updatedAt: '', projectStatus: 'deployed' }; let groupVarObj = { id: groupID, cloud_id: cloudID, user_id: userID, userStatus: '', userDomain: '', groupName: serviceName, frontType: '', region: '', environmentName: '', defaultDomain: subDomain, status: '', serviceId: serviceId, createdAt: now, updatedAt: '' }; let deployVarObj = { id: deployID, cloud_id: cloudID, functionName, cosObjectName: '', region: '', cosBucketRegion: '', description: '', cosBucketName: '', vpcId: '', subnetId: '', memorySize: 512, timeout: 300, handler: '', serviceName: "", fc_id: '', createdAt: now, updatedAt: '' }; let add_apigwpath = request(graphqlUrl, ADD_APIGWPATH, pathVarObj), add_project = request(graphqlUrl, ADD_PROJECT, projectVarObj), add_apigroup = request(graphqlUrl, ADD_APIGROUP, groupVarObj), add_deploy = request(graphqlUrl, ADD_DEPLOY, deployVarObj); Promise.all([add_apigwpath, add_project, add_apigroup, add_deploy]) .then(value => { console.log(value); // 展示数据 if(this._isMounted) { _this.setState({ disabled: false }); } _this.props.changeTabBar('my-deploy'); _this.props.history.push({ pathname: `/common/deploy`, state: { // 处理传回数据,直接拼接 url: `http://${subDomain}/test/graphql` } }); }) .catch(err => { console.log(err); }); } else { console.log('deployAll 失败'); } }) .catch(function (error) { console.log('axios error', error); }); } } render() { let {userID, disabled, appId, secretID, secretKey, bucketName, dbKind} = this.state; return (