added 完成字段编辑删除和修改

This commit is contained in:
zhaoxiang 2018-02-22 21:17:11 +08:00
parent 3f7e4a6138
commit 10e36c63e1
2 changed files with 48 additions and 57 deletions

View File

@ -84,66 +84,55 @@ class Fields extends Base {
} }
} }
/**
* 新增字段
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array
*/
public function add() { public function add() {
if (IS_POST) { $postData = $this->request->post();
$data = I('post.'); $postData['showName'] = $postData['fieldName'];
$data['fieldName'] = $data['showName']; $postData['default'] = $postData['defaults'];
$res = D('ApiFields')->add($data); unset($postData['defaults']);
if ($res === false) { $res = ApiFields::create($postData);
$this->ajaxError('操作失败'); if ($res === false) {
} else { return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
S('ApiRequest_' . $data['hash'], null);
S('ApiResponse_' . $data['hash'], null);
$this->ajaxSuccess('添加成功');
}
} else { } else {
$this->assign('dataType', $this->dataType); return $this->buildSuccess('操作成功');
$this->display();
} }
} }
/**
* 字段编辑
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array
*/
public function edit() { public function edit() {
if (IS_POST) { $postData = $this->request->post();
$data = I('post.'); $postData['showName'] = $postData['fieldName'];
$data['fieldName'] = $data['showName']; $postData['default'] = $postData['defaults'];
$res = D('ApiFields')->where(array('id' => $data['id']))->save($data); unset($postData['defaults']);
if ($res === false) { $res = ApiFields::update($postData);
$this->ajaxError('操作失败'); if ($res === false) {
} else { return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
if ($data['type'] == 0) {
S('ApiRequest_' . $data['hash'], null);
} else {
S('ApiResponse_' . $data['hash'], null);
}
$this->ajaxSuccess('添加成功');
}
} else { } else {
$id = I('get.id'); return $this->buildSuccess([]);
if ($id) {
$detail = D('ApiFields')->where(array('id' => $id))->find();
$this->assign('detail', $detail);
$this->assign('dataType', $this->dataType);
$this->display('add');
}
} }
} }
/**
* 字段删除
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array
*/
public function del() { public function del() {
if (IS_POST) { $id = $this->request->get('id');
$id = I('post.id'); if (!$id) {
if ($id) { return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
$detail = D('ApiFields')->where(array('id' => $id))->find();
if ($detail['type'] == 0) {
S('ApiRequest_' . $detail['hash'], null);
} else {
S('ApiResponse_' . $detail['hash'], null);
}
D('ApiFields')->where(array('id' => $id))->delete();
$this->ajaxSuccess('操作成功');
} else {
$this->ajaxError('缺少参数');
}
} }
ApiFields::destroy($id);
return $this->buildSuccess([]);
} }
public function upload() { public function upload() {
@ -192,10 +181,10 @@ class Fields extends Base {
$addArr = array( $addArr = array(
'fieldName' => $index, 'fieldName' => $index,
'showName' => $prefix, 'showName' => $prefix,
'hash' => I('post.hash'), 'hash' => $this->request->post('hash'),
'isMust' => 1, 'isMust' => 1,
'dataType' => DataType::TYPE_ARRAY, 'dataType' => DataType::TYPE_ARRAY,
'type' => I('post.type') 'type' => $this->request->post('type')
); );
$dataArr[] = $addArr; $dataArr[] = $addArr;
$prefix .= '[]'; $prefix .= '[]';
@ -206,10 +195,10 @@ class Fields extends Base {
$addArr = array( $addArr = array(
'fieldName' => $index, 'fieldName' => $index,
'showName' => $prefix, 'showName' => $prefix,
'hash' => I('post.hash'), 'hash' => $this->request->post('hash'),
'isMust' => 1, 'isMust' => 1,
'dataType' => DataType::TYPE_OBJECT, 'dataType' => DataType::TYPE_OBJECT,
'type' => I('post.type') 'type' => $this->request->post('type')
); );
$dataArr[] = $addArr; $dataArr[] = $addArr;
$prefix .= '{}'; $prefix .= '{}';
@ -218,10 +207,10 @@ class Fields extends Base {
$addArr = array( $addArr = array(
'fieldName' => $index, 'fieldName' => $index,
'showName' => $myPre, 'showName' => $myPre,
'hash' => I('post.hash'), 'hash' => $this->request->post('hash'),
'isMust' => 1, 'isMust' => 1,
'dataType' => DataType::TYPE_STRING, 'dataType' => DataType::TYPE_STRING,
'type' => I('post.type') 'type' => $this->request->post('type')
); );
if (is_numeric($datum)) { if (is_numeric($datum)) {
if (preg_match('/^\d*$/', $datum)) { if (preg_match('/^\d*$/', $datum)) {

View File

@ -8,6 +8,7 @@
namespace app\admin\controller; namespace app\admin\controller;
use app\model\ApiFields;
use app\model\ApiList; use app\model\ApiList;
use app\util\ReturnCode; use app\util\ReturnCode;
@ -123,11 +124,12 @@ class InterfaceList extends Base {
* @author zhaoxiang <zhaoxiang051405@gmail.com> * @author zhaoxiang <zhaoxiang051405@gmail.com>
*/ */
public function del() { public function del() {
$id = $this->request->get('id'); $hash = $this->request->get('hash');
if (!$id) { if (!$hash) {
return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数'); return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
} }
ApiList::destroy($id); ApiList::destroy(['hash' => $hash]);
ApiFields::destroy(['hash' => $hash]);
return $this->buildSuccess([]); return $this->buildSuccess([]);
} }