Skip to content

Commit

Permalink
The mock function supports custom suffix path / mock功能支持自定义后缀路径
Browse files Browse the repository at this point in the history
  • Loading branch information
star7th committed Apr 11, 2021
1 parent 53700cd commit 4f20398
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 30 deletions.
41 changes: 38 additions & 3 deletions server/Application/Api/Controller/MockController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,18 @@ class MockController extends BaseController {
public function add(){
$page_id = I("page_id/d");
$template = I("template");
$path = I("path") ? I("path") : '/';
$login_user = $this->checkLogin();
$uid = $login_user['uid'] ;
$page = M("Page")->where(" page_id = '$page_id' ")->find();
if(!$this->checkItemCreator($uid , $page['item_id'])){
$this->sendError(10303);
return ;
}

if(substr($path, 0, 1) !== '/'){
$path = '/' .$path ;
}
$item_id = $page['item_id'] ;
$json = json_decode(htmlspecialchars_decode($template)) ;
if(!$json){
$this->sendError(10101,'为了服务器安全,只允许写符合json语法的字符串');
Expand All @@ -28,15 +32,17 @@ public function add(){
D("Mock")->where("page_id = '$page_id' ")->save(array(
"uid"=>$uid ,
"template"=>$template ,
"path"=>$path ,
"last_update_time" => date("Y-m-d H:i:s"),
));
}else{
$id = D("Mock")->add(array(
"unique_key"=>$unique_key ,
"uid"=>$uid ,
"page_id"=>$page_id ,
"item_id"=> $page['item_id'] ,
"item_id"=> $item_id ,
"template"=>$template ,
"path"=>$path ,
"addtime" => date("Y-m-d H:i:s"),
"last_update_time" => date("Y-m-d H:i:s"),
"view_times"=>0
Expand All @@ -45,6 +51,7 @@ public function add(){

$this->sendResult(array(
"page_id"=>$page_id ,
"path"=>$path ,
"unique_key"=>$unique_key
));
}
Expand All @@ -64,7 +71,7 @@ public function infoByPageId(){

// 根据唯一key获取mock的响应数据
public function infoByKey(){
$unique_key = $_REQUEST["unique_key"];
$unique_key = I("unique_key") ;
$page = D("Mock")->where(" unique_key = '%s' ",array($unique_key))->find();
$template = $page['template'] ;
$res = http_post("http://127.0.0.1:7123/mock",array(
Expand All @@ -82,5 +89,33 @@ public function infoByKey(){
}

}
// 根据item_id和path获取
public function infoByPath(){
$item_id = I("item_id") ;
$path = I("path") ? I("path"): '/' ;
$page = D("Mock")->where(" item_id = '%s' and path = '%s' ",array($item_id,$path) )->find();
if(!$page){
echo 'no such path';
return;
}
$template = $page['template'] ;
$mock_host = env("MOCK_HOST",'127.0.0.1');
$mock_port = env('MOCK_PORT','7123');
$res = http_post("http://{$mock_host}:{$mock_port}/mock",array(
"template"=> htmlspecialchars_decode($page['template'])
));
if($res){
$sql = " update mock set view_times = view_times + 1 where id = {$page['id']} ";
D("Mock")->execute($sql);
$json = json_decode($res) ;
if(!$json){
$this->sendError(10101,'为了服务器安全,只允许写符合json语法的字符串');
return ;
}
echo $res ;
}else{
echo "mock服务暂时不可用。网站管理员安装完showdoc后需要另行安装mock服务,详情请打开https://www.showdoc.com.cn/help";
}
}

}
8 changes: 7 additions & 1 deletion server/Application/Api/Controller/UpdateController.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UpdateController extends BaseController {

//检测数据库并更新
public function checkDb($showBack = true){
$version_num = 5 ;
$version_num = 6 ;
$db_version_num = D("Options")->get("db_version_num");
if(!$db_version_num || $db_version_num < $version_num ){
$r = $this->updateSqlite();
Expand Down Expand Up @@ -357,6 +357,12 @@ public function updateSqlite(){
}
}

//给mock表增加path字段
if (!$this->_is_column_exist("mock","path")) {
$sql = "ALTER TABLE ".C('DB_PREFIX')."mock ADD path text NOT NULL DEFAULT '';";
D("mock")->execute($sql);
}


//留个注释提醒自己,如果更新数据库结构,务必更改上面的$version_num
//留个注释提醒自己,如果更新数据库结构,务必更改上面的$version_num
Expand Down
5 changes: 5 additions & 0 deletions server/Application/Common/Common/function.php
Original file line number Diff line number Diff line change
Expand Up @@ -265,4 +265,9 @@ function upload_oss($uploadFile){

return false ;

}
//获取环境变量。如果环境变量不存在,将返回第一个参数
function env($name , $default_value = false){
return getenv($name) ? getenv($name) : $default_value ;

}
6 changes: 4 additions & 2 deletions server/Application/Common/Conf/config.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@
'URL_MODEL' => 3 ,//URL兼容模式
'URL_ROUTER_ON' => true,
'URL_ROUTE_RULES'=>array(
':id\d' => 'Home/Item/show?item_id=:1',
':domain\s$' => 'Home/Item/show?item_domain=:1',//item的个性域名
'uid/:id\d' => 'Home/Item/showByUid?uid=:1',
'page/:id\d' => 'Home/Page/single?page_id=:1',
'mock-data/:unique_key\s' => 'Api/Mock/infoByKey?unique_key=:1',
'mock-path/:id\s' => 'Api/Mock/infoByPath?item_id=:1',
':id\d' => 'Home/Item/show?item_id=:1',
':domain\s/:id\d' => 'Home/Page/single?item_domain=:1&page_id=:2',
':domain\s$' => 'Home/Item/show?item_domain=:1',//item的个性域名
),
'URL_CASE_INSENSITIVE'=>true,
'SHOW_ERROR_MSG' => true, // 显示错误信息,这样在部署模式下也能显示错误
Expand Down
58 changes: 34 additions & 24 deletions web_src/src/components/common/Mock.vue
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
<template>
<div>
<el-dialog title="Mock" :visible="true" :close-on-click-modal="false" @close="callback()">
<el-dialog
width="1000px"
title="Mock"
:visible="true"
:close-on-click-modal="false"
@close="callback()"
>
<el-form>
<p v-if="mock_url" style=" margin-bottom:20px;font-size: 16px">
Mock地址 :
<code>{{mock_url}}</code>
<i class="el-icon-document-copy" v-clipboard:copy="mock_url" v-clipboard:success="onCopy"></i>
&nbsp;
<el-button @click="callback(mock_url)" type="text">把地址插入文档中</el-button>
</p>
<el-input
type="textarea"
class="dialoContent"
placeholder="这里填写的是Mock接口的返回结果。你可以直接编辑/粘贴一段json字符串,支持使用MockJs语法(关于MockJs语法,可以查看下方的帮助说明按钮)。输入完毕后,点击保存,就会自动生成Mock地址"
:rows="20"
v-model="content"
></el-input>
<p>
<el-row>
<span>Mock Url和路径 &nbsp;:&nbsp;&nbsp; {{mockUrlPre}}</span>
<el-input class="path-input" v-model="path"></el-input>
<i
class="el-icon-document-copy"
v-clipboard:copy="mock_url"
v-clipboard:success="onCopy"
></i>
</el-row>
</p>
<p>
<el-button type="primary" @click="handleClick">{{$t('save')}}</el-button>&nbsp;
<el-tooltip
Expand Down Expand Up @@ -43,31 +53,34 @@
<script>
import { unescapeHTML } from '@/models/page'
export default {
name: 'JsonBeautify',
name: 'Mock',
props: {
formLabelWidth: '120px',
callback: '',
page_id: ''
page_id: '',
item_id: ''
},
data() {
return {
content: '',
json_table_data: '',
mock_url: ''
mock_url: '',
mockUrlPre: '',
path: '/'
}
},
methods: {
add() {
this.request('/api/mock/add', {
'page_id': this.page_id,
'template': this.content
'template': this.content,
'path': this.path
}).then((data) => {
this.$message({
showClose: true,
message: '保存成功',
type: 'success'
})
this.mock_url = this.getUrl(data.data.unique_key)
this.infoByPageId()
})
},
infoByPageId() {
Expand All @@ -80,20 +93,13 @@ export default {
'page_id': this.page_id
}).then((data) => {
if (data.data && data.data.unique_key && data.data.template) {
this.mock_url = this.getUrl(data.data.unique_key)
// this.mock_url = this.getRootPath() + '/server/mock-data/' + data.data.unique_key
this.mock_url = this.mockUrlPre + data.data.path
this.content = unescapeHTML(data.data.template)
this.path = data.data.path
}
})
},
getUrl(unique_key) {
if (DocConfig.server.indexOf('web') > -1) {
let server = window.location.protocol + '//' + window.location.host + window.location.pathname + 'index.php?s='
server = server.replace(/\/web/g, '/server')
return server + '/mock-data/' + unique_key
} else {
return window.location.protocol + '//' + window.location.host + '/server/index.php?s=' + '/mock-data/' + unique_key
}
},
handleClick() {
this.add()
},
Expand All @@ -106,6 +112,7 @@ export default {
},
mounted() {
this.infoByPageId()
this.mockUrlPre = DocConfig.server + '/mock-path/' + this.item_id + '&path='
}
}
</script>
Expand All @@ -115,4 +122,7 @@ export default {
.el-icon-document-copy {
cursor: pointer;
}
.path-input{
width: 200px;
}
</style>
1 change: 1 addition & 0 deletions web_src/src/components/page/edit/Index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
<!-- mock -->
<Mock
:page_id="page_id"
:item_id="item_id"
v-if="showMockDialog"
:callback="(data)=>{
if(data){
Expand Down

0 comments on commit 4f20398

Please sign in to comment.