Skip to content

Commit

Permalink
add mock panel
Browse files Browse the repository at this point in the history
  • Loading branch information
fwon committed Apr 1, 2017
1 parent 51c3469 commit dbab21b
Show file tree
Hide file tree
Showing 8 changed files with 350 additions and 24 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,6 @@ client/node_modules
pack
rule_custom
rules.json
npm-debug.log
mock-project.json
npm-debug.log
yarn.lock
1 change: 1 addition & 0 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"element-ui": "^1.0.0",
"highlight.js": "^9.10.0",
"lodash": "^4.17.4",
"mockjs": "^1.0.1-beta3",
"moment": "^2.17.1",
"vue": "^2.1.6",
"vue-codemirror": "^2.1.8",
Expand Down
13 changes: 11 additions & 2 deletions client/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ export default {
return {
currentTab: 'net'
}
},
created() {
this.currentTab = location.hash.slice(2)
}
}
</script>
Expand All @@ -38,19 +41,24 @@ html, body {
overflow: hidden;
width: 100%;
height: 100%;
padding: 0;
margin: 0;
font-family: sans-serif;
}
body {
font-family: Helvetica, sans-serif;
-webkit-user-select: none;
user-select: none;
padding: 0;
margin: 0;
}
pre {
margin: 0;
padding: 10px;
overflow-x: scroll;
}
h1,h2,h3,h4 {
margin: 0;
padding: 0;
}
#app {
display: -webkit-box;
height:100%;
Expand Down Expand Up @@ -87,6 +95,7 @@ pre {
width: 100%;
height: 100%;
color: #fff;
font-size: 14px;
text-decoration: none;
box-sizing: border-box;
padding-left: 15px;
Expand Down
246 changes: 246 additions & 0 deletions client/src/components/mock.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
<template>
<div id="mock">
<div class="mock-project">
<el-dialog title="添加项目" v-model="addProjectStatus">
<el-input type="input" v-model="newProjectName"></el-input>
<div slot="footer" class="dialog-footer">
<el-button @click="addProjectStatus = false">取 消</el-button>
<el-button type="primary" @click="addProject">确 定</el-button>
</div>
</el-dialog>
<el-button type="primary" :plain="true" icon="plus" @click="addProjectStatus = true">添加项目</el-button>
<ul class="mock-project-list el-menu el-menu-vertical-demo">
<li
class="mock-project-item el-menu-item"
:class="{'is-active': item.id === currentProject.id}"
v-for="item in projects"
@click="switchProject(item)">
{{item.name}}
<i class="el-icon-circle-close" @click="deleteProject(item)"></i>
</li>
</ul>
</div>
<div class="mock-path" v-if="currentProject.id">
<el-dialog size="large" title="路径配置" v-model="addPathStatus">
<div class="mock-path-editor">
<div class="mock-path-editor__left">
<h4>Request Url</h4>
<el-input type="input" v-model="pathRequest.url"></el-input>
<h4>Request Method</h4>
<el-input type="input" v-model="pathRequest.method"></el-input>
</div>
<div class="mock-path-editor__right">
<h4>Status Code</h4>
<el-input type="input" v-model="pathResponse.status"></el-input>
<h4>Response Format</h4>
<el-input type="input" v-model="pathResponse.format"></el-input>
<h4>Response Headers</h4>
<el-input type="input" v-model="pathResponse.headers"></el-input>
<h4>Response Body</h4>
<codemirror v-model="pathResponse.body" :options="editorOption"></codemirror>
</div>
</div>
<div slot="footer" class="dialog-footer">
<el-button @click="addPathStatus = false">取 消</el-button>
<el-button type="primary" @click="savePath">保 存</el-button>
</div>
</el-dialog>
<h2>当前项目:{{currentProject.name}}</h2>
<el-button type="primary" :plain="true" icon="plus" @click="addPathStatus = true">添加接口</el-button>
<el-table
class="mock-path__list"
:data="projectPaths"
border
@selection-change="handlePathChange">
<el-table-column
type="selection"
width="55">
</el-table-column>
<el-table-column
prop="path"
label="路径"
width="400">
</el-table-column>
<el-table-column
prop="method"
label="Method"
width="100">
</el-table-column>
<el-table-column label="操作">
<template scope="scope">
<el-button
size="small"
@click="handleEdit(scope.$index, scope.row)">
编辑</el-button>
<el-button
size="small"
type="danger"
@click="handleDelete(scope.$index, scope.row)">
删除</el-button>
</template>
</el-table-column>
</el-table>
</div>
</div>
</template>
<script>
import {codemirror} from 'vue-codemirror';
import util from '../lib/util';
import _ from 'lodash';
export default {
data() {
return {
addProjectStatus: false,//添加新项目弹窗
currentProject: {}, //当前项目对象
newProjectName: '', //新项目名称
projects: [], //项目列表
projectPaths: [], //某项目下的规则列表
addPathStatus: false, //添加规则弹窗
pathId: null, //正在编辑的路径id
pathRequest: { //正在编辑的请求
url: '/xxx/xxx',
method: 'GET'
},
pathResponse: { //正在编辑的返回数据
status: '200',
format: 'application/json;charset=UTF-8',
headers: 'Access-Control-Allow-Origin:*;Connection:keep-alive;',
body: ''
},
editorOption: { //返回的数据body
mode: 'text/javascript',
lineNumbers: true,
theme: 'monokai',
},
}
},
computed: {
},
components: {
codemirror
},
mounted() {
const projs = this.$remoteApi.getMockProjects();
this.projects = JSON.parse(projs);
},
methods: {
addProject() {
if (!this.newProjectName) {
this.$alert('名称不能为空');
return;
}
const nameIndex = _.findIndex(this.projects, {name: this.newProjectName});
if (nameIndex !== -1) {
this.$alert('该项目已存在!');
} else {
const project = {
id: util.generateUUIDv4(),
name: this.newProjectName
}
this.projects.push(project);
this.$remoteApi.saveMockProject(this.projects);
this.addProjectStatus = false;
this.newProjectName = '';
}
},
deleteProject(item) {
this.$confirm('确定要删除该项目吗?', {
type: 'warning'
}).then(() => {
const idIndex = _.findIndex(this.projects, {id: item.id});
this.projects.splice(idIndex, 1);
this.$remoteApi.saveMockProject(this.projects);
if (this.currentProject.id === item.id) {
this.currentProject = {};
}
});
},
switchProject(item) {
console.log(item);
const self = this;
this.currentProject = item;
this.$remoteApi.getProjectPaths.then((data) => {
self.projectPaths = JSON.parse(data);
});
},
savePath() {
//TODO 保存规则到某个项目,区分新增和修改
if (this.projectPaths) {}
},
handleEdit() {
},
handleDelete() {
},
handlePathChange() {
}
}
}
</script>
<style scope>
#mock {
display: -webkit-box;
-webkit-box-orient: horizontal;
padding: 30px;
}
.mock-project {
width: 200px;
}
.mock-project-list {
margin-top: 20px;
}
.mock-project-item {
position: relative;
text-align: center;
&:hover {
i {
display: inline-block;
}
}
i {
display: none;
position: absolute;
right: 0;
top: 40%;
}
&.is-active {
color: #fff;
background-color: #669999;
}
}
.mock-path {
-webkit-box-flex: 1;
margin-left: 40px;
h2 {
color: #669999;
margin-bottom: 27px;
}
.CodeMirror {
height: 100px;
}
}
.mock-path-editor {
display: -webkit-box;
-webkit-box-orient: horizontal;
h4 {
padding: 10px 0;
}
}
.mock-path-editor__left, .mock-path-editor__right {
width: 50%;
-webkit-box-flex: 1;
padding: 0 20px;
}
.mock-path-editor__left {
border-right: 1px solid #ccc;
}
.mock-path__list {
margin-top: 20px;
}
</style>
29 changes: 12 additions & 17 deletions client/src/components/rule.vue
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<el-table-column
prop="name"
label="规则">
</el-table-column>index
</el-table-column>
<el-table-column label="操作">
<template scope="scope">
<el-button
Expand Down Expand Up @@ -76,24 +76,25 @@
import * as types from '../store/mutation-types';
import {mapState} from 'vuex';
import {codemirror} from 'vue-codemirror';
import util from '../lib/util';
import _ from 'lodash';
export default {
data() {
return {
ruleKey: {//样例的id和name列表
ruleKey: { //样例的id和name列表
id: '',
name: ''
},
ruleName: '',//样例规则的名称
ruleValue: '',//样例的代码字符串
ruleName: '', //样例规则的名称
ruleValue: '', //样例的代码字符串
editorOption: {
mode: 'text/javascript',
lineNumbers: true,
theme: 'monokai',
},
eidtRuleStatus: false,
currentSelectIndex: null,
eidtRuleStatus: false, //编辑规则弹窗
currentSelectIndex: null, //目前选中的规则
ruleOptions: [{
value: 'modify_request_data',
label: '修改请求数据'
Expand Down Expand Up @@ -149,7 +150,7 @@ export default {
console.log(label);
this.$remoteApi.fetchSampleRule(val).then((data) => {
self.ruleKey = {
id: self.generateUUIDv4(),
id: util.generateUUIDv4(),
name: self.ruleOptions[_.findIndex(self.ruleOptions, ['value', val])]['label'],
};
self.ruleValue = data;
Expand All @@ -169,16 +170,16 @@ export default {
saveRule() {
console.log('add')
const self = this;
let rule = {
id: self.ruleKey.id || self.generateUUIDv4(),
const rule = {
id: self.ruleKey.id || util.generateUUIDv4(),
name: self.ruleKey.name,
};
if (!rule.name || !this.ruleValue) {
this.$alert('无效规则,请重新填写');
return;
}
let nameIndex = _.findIndex(this.rulesData, {name: rule.name});
let idIndex = _.findIndex(this.rulesData, {id: rule.id});
const nameIndex = _.findIndex(this.rulesData, {name: rule.name});
const idIndex = _.findIndex(this.rulesData, {id: rule.id});
if (idIndex !== -1) {
console.log('aaaa')
this.$store.commit(types.MODIFY_PROXY_RULE, {
Expand Down Expand Up @@ -226,12 +227,6 @@ export default {
//写入文件
this.$remoteApi.saveRulesIntoFile(this.rulesData);
this.$remoteApi.saveCustomRuleToFile(id, this.ruleValue);
},
generateUUIDv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16|0, v = c == 'x' ? r : (r&0x3|0x8);
return v.toString(16);
});
}
}
}
Expand Down
Loading

0 comments on commit dbab21b

Please sign in to comment.