forked from kuafuRace/phprap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFieldController.php
More file actions
113 lines (87 loc) · 3.22 KB
/
Copy pathFieldController.php
File metadata and controls
113 lines (87 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
namespace app\controllers\home;
use Yii;
use yii\helpers\Html;
use yii\web\Response;
use app\models\Api;
use app\models\field\CreateField;
use app\models\field\UpdateField;
class FieldController extends PublicController
{
/**
* 添加字段
* @param $id
* @param string $method
* @return array|string
*/
public function actionCreate($api_id)
{
$request = Yii::$app->request;
$params = Yii::$app->request->queryParams;
$api = Api::findModel(['encode_id' => $api_id]);
$model = CreateField::findModel();
$assign['project'] = $api->project;
$assign['api'] = $api;
$assign['field'] = $model;
if($params['from'] == 'template'){
$assign['template'] = $api->project->template;
}
if($request->isPost){
Yii::$app->response->format = Response::FORMAT_JSON;
$model->api_id = $api->id;
$model->header_fields = $this->form2json($request->post('header'));
$model->request_fields = $this->form2json($request->post('request'));
$model->response_fields = $this->form2json($request->post('response'));
if($model->store()) {
$callback = url('home/api/show', ['id' => $api->encode_id, 'tab' => 'field']);
return ['status' => 'success', 'message' => '添加成功', 'callback' => $callback];
}
return ['status' => 'error', 'message' => $model->getErrorMessage(), 'label' => $model->getErrorLabel()];
}
return $this->display('/home/field/create', $assign);
}
/**
* 更新字段
* @param $id
* @param string $method
* @return array|string
*/
public function actionUpdate($id)
{
$request = Yii::$app->request;
$model = UpdateField::findModel(['encode_id' => $id]);
$assign['project'] = $model->api->project;
$assign['api'] = $model->api;
$assign['field'] = $model;
if($request->isPost){
Yii::$app->response->format = Response::FORMAT_JSON;
$model->header_fields = $this->form2json($request->post('header'));
$model->request_fields = $this->form2json($request->post('request'));
$model->response_fields = $this->form2json($request->post('response'));
if($model->store()) {
$callback = url('home/api/show', ['id' => $model->api->encode_id, 'tab' => 'field']);
return ['status' => 'success', 'message' => '编辑成功', 'callback' => $callback];
}
return ['status' => 'error', 'message' => $model->getErrorMessage(), 'label' => $model->getErrorLabel()];
}
return $this->display('/home/field/update', $assign);
}
/**
* 表单过滤后转json
* @param $table
* @return false|string
*/
private function form2json($table)
{
if(!is_array($table) || !$table){
return;
}
$array = [];
foreach ($table as $k => $v) {
foreach ($v as $k1 => $v1) {
$array[$k1][$k] = trim(Html::encode($v1));
}
}
return json_encode($array, JSON_UNESCAPED_UNICODE);
}
}