added 完成字段上传的全部功能

This commit is contained in:
zhaoxiang 2018-02-22 22:05:23 +08:00
parent 10e36c63e1
commit b1aeed9bdc

View File

@ -9,6 +9,7 @@ namespace app\admin\controller;
use app\model\ApiFields; use app\model\ApiFields;
use app\model\ApiList;
use app\util\DataType; use app\util\DataType;
use app\util\ReturnCode; use app\util\ReturnCode;
@ -135,45 +136,45 @@ class Fields extends Base {
return $this->buildSuccess([]); return $this->buildSuccess([]);
} }
/**
* 批量上传返回字段
* @author zhaoxiang <zhaoxiang051405@gmail.com>
* @return array
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\ModelNotFoundException
* @throws \think\exception\DbException
*/
public function upload() { public function upload() {
if (IS_POST) { $hash = $this->request->post('hash');
$hash = I('post.hash'); $type = $this->request->post('type');
$type = I('post.type'); $jsonStr = $this->request->post('jsonStr');
$jsonStr = I('post.jsonStr');
$jsonStr = html_entity_decode($jsonStr); $jsonStr = html_entity_decode($jsonStr);
$data = json_decode($jsonStr, true); $data = json_decode($jsonStr, true);
D('ApiList')->where(array('hash' => $hash))->save(array('returnStr' => json_encode($data))); ApiList::update(['returnStr' => json_encode($data)], ['hash' => $hash]);
$this->handle($data['data'], $dataArr); $this->handle($data['data'], $dataArr);
$old = D('ApiFields')->where(array( $old = (new ApiFields())->where([
'hash' => $hash, 'hash' => $hash,
'type' => $type 'type' => $type
))->select(); ])->select();
$old = $this->buildArrFromObj($old);
$oldArr = array_column($old, 'showName'); $oldArr = array_column($old, 'showName');
$newArr = array_column($dataArr, 'showName'); $newArr = array_column($dataArr, 'showName');
$addArr = array_diff($newArr, $oldArr); $addArr = array_diff($newArr, $oldArr);
$delArr = array_diff($oldArr, $newArr); $delArr = array_diff($oldArr, $newArr);
if ($delArr) { if ($delArr) {
D('ApiFields')->where(array('showName' => array('in', $delArr)))->delete(); ApiFields::destroy(['showName' => ['in', $delArr]]);
} }
if ($addArr) { if ($addArr) {
$addData = array(); $addData = [];
foreach ($dataArr as $item) { foreach ($dataArr as $item) {
if (in_array($item['showName'], $addArr)) { if (in_array($item['showName'], $addArr)) {
$addData[] = $item; $addData[] = $item;
} }
} }
D('ApiFields')->addAll($addData); (new ApiFields())->insertAll($addData);
}
if ($type == 0) {
S('ApiRequest_' . $hash, null);
} else {
S('ApiResponse_' . $hash, null);
}
S('ApiReturnType_' . $hash, null);
$this->ajaxSuccess('操作成功');
} else {
$this->display();
} }
return $this->buildSuccess([]);
} }
private function handle($data, &$dataArr, $prefix = 'data', $index = 'data') { private function handle($data, &$dataArr, $prefix = 'data', $index = 'data') {