Skip to content

Commit

Permalink
Merge pull request #213 from IamChuancey/master
Browse files Browse the repository at this point in the history
  • Loading branch information
IamChuancey authored Apr 2, 2019
2 parents a129e11 + 6714ef6 commit 0a708bd
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/common/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
class Common {

}
Common.USER_REGISTER='Enroll';
Common.FABRIC_CA_SERVER='fabric-ca-server-url';
Common.REGISTER_USER_NAME='user name';
Common.REGISTER_PASSWORD='password';
Common.REGISTER_CERTIFICATE='certificate directory';
Common.CURRENT_BLOCKS = 'Current Blocks';
Common.BLOCK_HASH = 'Block Hash';
Common.TRANSACTION_NUM = 'Transaction Num';
Expand Down
5 changes: 5 additions & 0 deletions src/common/common_cn.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
class Common {

}
Common.USER_REGISTER='注册';
Common.FABRIC_CA_SERVER='fabric-ca-server-url';
Common.REGISTER_USER_NAME='用户名';
Common.REGISTER_PASSWORD='密码';
Common.REGISTER_CERTIFICATE='证书存放目录';
Common.CURRENT_BLOCKS = '最近区块';
Common.BLOCK_HASH = '哈希值';
Common.TRANSACTION_NUM = '交易数量';
Expand Down
136 changes: 134 additions & 2 deletions src/components/UserLayout.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@

// TODO: 登录页(秘钥导入页面)
import React from 'react';
import { Button, Input, Layout, Icon, message } from 'antd';
import {Button, Input, Layout, Icon, message, Form, Modal} from 'antd';
import { getFabricClientSingleton } from '../util/fabric';
import { getConfigDBSingleton } from '../util/createDB';


const FormItem = Form.Item;
const ButtonGroup = Button.Group;
const path = require('path');
const yaml = require('js-yaml');
Expand All @@ -19,6 +19,64 @@ const { Content } = Layout;

const bcgd = path.join(__dirname, '../../resources/styles/image/blc.jpg');

const RegisterForm = Form.create()(
class extends React.Component {
constructor(props) {
super(props);
this.state = {
Common: localStorage.getItem('language') === 'cn' ? require('../common/common_cn') : require('../common/common'),
};
}
render() {
const { visible, onCancel, onCreate, form } = this.props;
const { getFieldDecorator } = form;
return (
<Modal
visible={visible}
title={this.state.Common.USER_REGISTER}
okText={this.state.Common.CREATE}
cancelText={this.state.Common.CANCEL}
onCancel={onCancel}
onOk={onCreate}
centered
width="480px"
>
<Form layout="vertical">
<FormItem label={this.state.Common.FABRIC_CA_SERVER}>
{getFieldDecorator('server', {
rules: [{}],
})(
<Input placeholder={this.state.Common.FABRIC_CA_SERVER} />,
)}
</FormItem>
<FormItem label={this.state.Common.REGISTER_USER_NAME} >
{getFieldDecorator('username', {
rules: [{}],
})(
<Input placeholder={this.state.Common.REGISTER_USER_NAME} />,
)}
</FormItem>
<FormItem label={this.state.Common.REGISTER_PASSWORD} >
{getFieldDecorator('password', {
rules: [{}],
})(
<Input placeholder={this.state.Common.REGISTER_PASSWORD} />,
)}
</FormItem>
<FormItem label={this.state.Common.REGISTER_CERTIFICATE} >
{getFieldDecorator('directory', {
rules: [{}],
})(
<Input placeholder={this.state.Common.REGISTER_CERTIFICATE} />,
)}
</FormItem>
</Form>
</Modal>
);
}
},
);

function _configImportPathHelper(filePath, parentPath) {
return path.isAbsolute(filePath) ? filePath : path.join(parentPath, filePath);
}
Expand All @@ -28,6 +86,7 @@ export default class UserLayout extends React.Component {
super(props);

this.state = {
visibleRegisterForm:false,
expand: false,
peerGrpcUrl: 'grpcs://localhost:7051',
peerEventUrl: 'grpcs://localhost:7053',
Expand Down Expand Up @@ -67,6 +126,10 @@ export default class UserLayout extends React.Component {
this.changeLangtoCn = this.changeLangtoCn.bind(this);
this.toggle=this.toggle.bind(this);
this.registerUser=this.registerUser.bind(this);
this.saveFormRef=this.saveFormRef.bind(this);
this.handleCancel=this.handleCancel.bind(this);
this.handleCreate=this.handleCreate.bind(this);
this.showRegisterForm=this.showRegisterForm.bind(this);
}

toggle(){
Expand All @@ -76,6 +139,7 @@ export default class UserLayout extends React.Component {

registerUser(){
logger.info("Register new user here!");
this.showRegisterForm();
}

onClick() {
Expand Down Expand Up @@ -210,6 +274,68 @@ export default class UserLayout extends React.Component {
localStorage.setItem('language', 'cn');
this.setState({ Common: require('../common/common_cn') });
}
saveFormRef(formRef) {
this.formRef = formRef;
}
handleCancel() {
this.setState({ visibleRegisterForm: false });
}
handleCreate() {
const form = this.formRef.props.form;
form.validateFields((err, values) => {
if (err) {
return;
}
logger.info('Received values of instanitateform: ', values.server);
//调用接口这块有问题,需处理
db.update({ id: 0 },
{ $set: {
peerGrpcUrl: this.state.peerGrpcUrl,
peerEventUrl: this.state.peerEventUrl,
ordererUrl: this.state.ordererUrl,
mspid: this.state.mspid,
tlsPeerPath: this.state.tlsPeerPath,
tlsOrdererPath: this.state.tlsOrdererPath,
tlsCAServerPath: this.state.tlsCAServerPath,
peerSSLTarget: this.state.peerSSLTarget,
ordererSSLTarget: this.state.ordererSSLTarget,
caServerUrl: values.server,
keyPath: this.state.keyPath,
certPath: this.state.certPath,
path: 'resources/key/users/'
} },
{},
() => {
getFabricClientSingleton().then((fabricClient) => {
const enrReq = {
enrollmentID: values.username,
enrollmentSecret: values.password,
};
return fabricClient.enroll(enrReq);
}).then((enrollment) => {
const certificate=values.directory+"/"+values.username+".pem";
const privatekey=values.directory+"/"+values.username+".pri";
logger.info('cert directory',certificate);
logger.info('pri key directory',privatekey);
fs.writeFile(certificate, enrollment.certificate, (err) => {
if (err) {
throw err;
}
});
fs.writeFile(privatekey, enrollment.key.toBytes(), (err) => {
if (err) {
throw err;
}
});
});
});
form.resetFields();
this.setState({visibleRegisterForm:false})
});
}
showRegisterForm(){
this.setState({visibleRegisterForm:true})
}
render() {
const LayoutStyle = {
maxHeight: '680px',
Expand Down Expand Up @@ -407,6 +533,12 @@ export default class UserLayout extends React.Component {
<Button type="primary" style={buttonStyle} onClick={this.onClick}>{this.state.Common.LOGIN}</Button>
</div>
</Content>
<RegisterForm
wrappedComponentRef={this.saveFormRef}
visible={this.state.visibleRegisterForm}
onCancel={this.handleCancel}
onCreate={this.handleCreate}
/>
</div>
</Layout>

Expand Down

0 comments on commit 0a708bd

Please sign in to comment.