🎉 增加生成前端代码功能.

This commit is contained in:
smallchill 2019-01-24 18:07:18 +08:00
parent bbf3fcd495
commit 26c6edf27f
7 changed files with 370 additions and 15 deletions

View File

@ -205,12 +205,15 @@ public class BladeGenerator {
pc.setXml("mapper");
mpg.setPackageInfo(pc);
String servicePackage = serviceName.split("-").length > 1 ? serviceName.split("-")[1] : serviceName;
// 自定义配置
InjectionConfig cfg = new InjectionConfig() {
@Override
public void initMap() {
Map<String, Object> map = new HashMap<>(16);
map.put("serviceName", serviceName);
map.put("servicePackage", servicePackage);
this.setMap(map);
}
};
@ -251,6 +254,30 @@ public class BladeGenerator {
return getOutputWebDir() + "/services" + "/" + tableInfo.getEntityName().toLowerCase() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/list.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.upperFirst(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + ".js";
}
});
focList.add(new FileOutConfig("/templates/sword/add.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.upperFirst(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "Add.js";
}
});
focList.add(new FileOutConfig("/templates/sword/edit.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.upperFirst(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "Edit.js";
}
});
focList.add(new FileOutConfig("/templates/sword/view.js.vm") {
@Override
public String outputFile(TableInfo tableInfo) {
return getOutputWebDir() + "/pages" + "/" + StringUtil.upperFirst(servicePackage) + "/" + tableInfo.getEntityName() + "/" + tableInfo.getEntityName() + "View.js";
}
});
cfg.setFileOutConfigList(focList);
mpg.setCfg(cfg);

View File

@ -1,37 +1,37 @@
#set($moduleName=$table.entityPath.toUpperCase())
export const $!{moduleName}_NAMESPACE = '$!{table.entityPath}';
#set($upperEntityPath=$table.entityPath.toUpperCase())
export const $!{upperEntityPath}_NAMESPACE = '$!{table.entityPath}';
export function $!{moduleName}_LIST(payload) {
export function $!{upperEntityPath}_LIST(payload) {
return {
type: `${$!{moduleName}_NAMESPACE}/fetchList`,
type: `${$!{upperEntityPath}_NAMESPACE}/fetchList`,
payload,
};
}
export function $!{moduleName}_DETAIL(id) {
export function $!{upperEntityPath}_DETAIL(id) {
return {
type: `${$!{moduleName}_NAMESPACE}/fetchDetail`,
type: `${$!{upperEntityPath}_NAMESPACE}/fetchDetail`,
payload: { id },
};
}
export function $!{moduleName}_CLEAR_DETAIL() {
export function $!{upperEntityPath}_CLEAR_DETAIL() {
return {
type: `${$!{moduleName}_NAMESPACE}/clearDetail`,
type: `${$!{upperEntityPath}_NAMESPACE}/clearDetail`,
payload: {},
};
}
export function $!{moduleName}_SUBMIT(payload) {
export function $!{upperEntityPath}_SUBMIT(payload) {
return {
type: `${$!{moduleName}_NAMESPACE}/submit`,
type: `${$!{upperEntityPath}_NAMESPACE}/submit`,
payload,
};
}
export function $!{moduleName}_REMOVE(payload) {
export function $!{upperEntityPath}_REMOVE(payload) {
return {
type: `${$!{moduleName}_NAMESPACE}/remove`,
type: `${$!{upperEntityPath}_NAMESPACE}/remove`,
payload,
};
}

View File

@ -0,0 +1,73 @@
#set($upperEntityPath=$table.entityPath.toUpperCase())
import React, { PureComponent } from 'react';
import { Form, Input, Card, Button } from 'antd';
import { connect } from 'dva';
import Panel from '../../../components/Panel';
import styles from '../../../layouts/Sword.less';
import { $!{upperEntityPath}_SUBMIT } from '../../../actions/$!{table.entityPath}';
const FormItem = Form.Item;
@connect(({ loading }) => ({
submitting: loading.effects['$!{table.entityPath}/submit'],
}))
@Form.create()
class $!{entity}Add extends PureComponent {
handleSubmit = e => {
e.preventDefault();
const { dispatch, form } = this.props;
form.validateFieldsAndScroll((err, values) => {
if (!err) {
dispatch($!{upperEntityPath}_SUBMIT(values));
}
});
};
render() {
const {
form: { getFieldDecorator },
submitting,
} = this.props;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 7 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 12 },
md: { span: 10 },
},
};
const action = (
<Button type="primary" onClick={this.handleSubmit} loading={submitting}>
提交
</Button>
);
return (
<Panel title="新增" back="/$!{cfg.servicePackage}/$!{table.entityPath}" action={action}>
<Form hideRequiredMark style={{ marginTop: 8 }}>
<Card className={styles.card} bordered={false}>
#foreach($field in $!{table.fields})
<FormItem {...formItemLayout} label="$!{field.comment}">
{getFieldDecorator('$!{field.propertyName}', {
rules: [
{
required: true,
message: '请输入$!{field.comment}',
},
],
})(<Input placeholder="请输入$!{field.comment}" />)}
</FormItem>
#end
</Card>
</Form>
</Panel>
);
}
}
export default $!{entity}Add;

View File

@ -0,0 +1,97 @@
#set($upperEntityPath=$table.entityPath.toUpperCase())
import React, { PureComponent } from 'react';
import { Form, Input, Card, Button } from 'antd';
import { connect } from 'dva';
import Panel from '../../../components/Panel';
import styles from '../../../layouts/Sword.less';
import { $!{upperEntityPath}_DETAIL, $!{upperEntityPath}_SUBMIT } from '../../../actions/$!{table.entityPath}';
const FormItem = Form.Item;
@connect(({ $!{table.entityPath}, loading }) => ({
$!{table.entityPath},
submitting: loading.effects['code/submit'],
}))
@Form.create()
class $!{entity}Edit extends PureComponent {
componentWillMount() {
const {
dispatch,
match: {
params: { id },
},
} = this.props;
dispatch($!{upperEntityPath}_DETAIL(id));
}
handleSubmit = e => {
e.preventDefault();
const {
dispatch,
match: {
params: { id },
},
form,
} = this.props;
form.validateFieldsAndScroll((err, values) => {
if (!err) {
const params = {
id,
...values,
};
console.log(params);
dispatch($!{upperEntityPath}_SUBMIT(params));
}
});
};
render() {
const {
form: { getFieldDecorator },
$!{table.entityPath}: { detail },
submitting,
} = this.props;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 7 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 12 },
md: { span: 10 },
},
};
const action = (
<Button type="primary" onClick={this.handleSubmit} loading={submitting}>
提交
</Button>
);
return (
<Panel title="修改" back="/$!{cfg.servicePackage}/$!{table.entityPath}" action={action}>
<Form hideRequiredMark style={{ marginTop: 8 }}>
<Card className={styles.card} bordered={false}>
#foreach($field in $!{table.fields})
<FormItem {...formItemLayout} label="$!{field.comment}">
{getFieldDecorator('$!{field.propertyName}', {
rules: [
{
required: true,
message: '请输入$!{field.comment}',
},
],
initialValue: detail.$!{field.propertyName},
})(<Input placeholder="请输入$!{field.comment}" />)}
</FormItem>
#end
</Card>
</Form>
</Panel>
);
}
}
export default $!{entity}Edit;

View File

@ -0,0 +1,82 @@
#set($upperEntityPath=$table.entityPath.toUpperCase())
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Button, Col, Form, Input, Row } from 'antd';
import Panel from '../../../components/Panel';
import { $!{upperEntityPath}_LIST } from '../../../actions/$!{table.entityPath}';
import Grid from '../../../components/Sword/Grid';
const FormItem = Form.Item;
@connect(({ $!{table.entityPath}, loading }) => ({
$!{table.entityPath},
loading: loading.models.param,
}))
@Form.create()
class $!{entity} extends PureComponent {
// ============ 查询 ===============
handleSearch = params => {
const { dispatch } = this.props;
dispatch($!{upperEntityPath}_LIST(params));
};
// ============ 查询表单 ===============
renderSearchForm = onReset => {
const { form } = this.props;
const { getFieldDecorator } = form;
return (
<Row gutter={{ md: 8, lg: 24, xl: 48 }}>
<Col md={6} sm={24}>
<FormItem label="查询名称">
{getFieldDecorator('name')(<Input placeholder="查询名称" />)}
</FormItem>
</Col>
<Col>
<div style={{ float: 'right' }}>
<Button type="primary" htmlType="submit">
查询
</Button>
<Button style={{ marginLeft: 8 }} onClick={onReset}>
重置
</Button>
</div>
</Col>
</Row>
);
};
render() {
const code = '$!{table.entityPath}';
const {
form,
loading,
param: { data },
} = this.props;
const columns = [
#foreach($field in $!{table.fields})
{
title: '$!{field.comment}',
dataIndex: '$!{field.propertyName}',
},
#end
];
return (
<Panel>
<Grid
code={code}
form={form}
onSearch={this.handleSearch}
renderSearchForm={this.renderSearchForm}
loading={loading}
data={data}
columns={columns}
/>
</Panel>
);
}
}
export default $!{entity};

View File

@ -1,10 +1,11 @@
#set($moduleName=$table.entityPath.toUpperCase())
#set($upperEntityPath=$table.entityPath.toUpperCase())
import { message } from 'antd';
import { $!{moduleName}_NAMESPACE } from '../actions/$!{table.entityPath}';
import router from 'umi/router';
import { $!{upperEntityPath}_NAMESPACE } from '../actions/$!{table.entityPath}';
import { list, submit, detail, remove } from '../services/$!{table.entityPath}';
export default {
namespace: $!{moduleName}_NAMESPACE,
namespace: $!{upperEntityPath}_NAMESPACE,
state: {
data: {
list: [],
@ -46,6 +47,7 @@ export default {
const response = yield call(submit, payload);
if (response.success) {
message.success('提交成功');
router.push('/$!{cfg.servicePackage}/$!{table.entityPath}');
}
},
*remove({ payload }, { call }) {

View File

@ -0,0 +1,74 @@
#set($upperEntityPath=$table.entityPath.toUpperCase())
import React, { PureComponent } from 'react';
import router from 'umi/router';
import { Form, Card, Button } from 'antd';
import { connect } from 'dva';
import Panel from '../../../components/Panel';
import styles from '../../../layouts/Sword.less';
import { $!{upperEntityPath}_DETAIL } from '../../../actions/$!{table.entityPath}';
const FormItem = Form.Item;
@connect(({ code }) => ({
code,
}))
@Form.create()
class $!{entity}View extends PureComponent {
componentWillMount() {
const {
dispatch,
match: {
params: { id },
},
} = this.props;
dispatch($!{upperEntityPath}_DETAIL(id));
}
handleEdit = () => {
const {
match: {
params: { id },
},
} = this.props;
router.push(`/$!{cfg.servicePackage}/$!{table.entityPath}/edit/${id}`);
};
render() {
const {
$!{table.entityPath}: { detail },
} = this.props;
const formItemLayout = {
labelCol: {
xs: { span: 24 },
sm: { span: 7 },
},
wrapperCol: {
xs: { span: 24 },
sm: { span: 12 },
md: { span: 10 },
},
};
const action = (
<Button type="primary" onClick={this.handleEdit}>
修改
</Button>
);
return (
<Panel title="查看" back="/$!{cfg.servicePackage}/$!{table.entityPath}" action={action}>
<Form hideRequiredMark style={{ marginTop: 8 }}>
<Card className={styles.card} bordered={false}>
#foreach($field in $!{table.fields})
<FormItem {...formItemLayout} label="$!{field.comment}">
<span>{detail.$!{field.propertyName}}</span>
</FormItem>
#end
</Card>
</Form>
</Panel>
);
}
}
export default $!{entity}View;