123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165 |
- namespace app\api\controller;
- use app\api\controller\base\Base;
- class {$humpName} extends Base
- {
- private function getModel()
- {
- return new \app\common\model\{$humpName}();
- }
- {if condition="array_key_exists('select',$crud)"}
- public function index()
- {
- $post = $this->request->param();
- $validate = new \think\Validate([
- ['id', 'number'],
- ['page', 'number'],
- ['pagenum', 'number|<=:1000']
- ]);
- if (!$validate->check($post)) {
- $this->json_error('提交失败:' . $validate->getError());
- }
- $where = [];
- if (isset($post['id'])) {
- $where['id'] = $post['id'];
- }
- if (isset($post['id'])) {
- $datalist = ($this->getModel())->where($where)->find();
- } else {
- $pagenum = $this->request->param('pagenum', 20, 'intval');
- $datalist = ($this->getModel())->where($where)->paginate($pagenum, true);
- }
- if (empty($datalist)) {
- $this->json_error("没有数据");
- }
- $this->json_success("查询成功", $datalist);
- }
- {/if}
- {if condition="array_key_exists('create',$crud)"}
- public function create()
- {
- $post = $this->request->param();
- //验证
- $validate = new \think\Validate([
- {volist name="$fieldsInfo" id="vo"}
- {php}
- $field_key = $vo['Field'];
- if(in_array($field_key,['id','create_time','update_time'])){
- continue;
- }
- if(!empty($vo['Comment'])){
- $comment = $vo['Comment'];
- $field_key.= "|" . $comment;
- }
- $field_val = "";
- if($vo['Default'] === null){
- $field_val.= "require";
- }
- if(startWith($vo["Type"],'int') || startWith($vo["Type"],'tinyint')){
- $field_val .= empty($field_val) ? "number" : "|number";
- }
- if(startWith($vo["Type"],'varchar')){
- $maxLen = str_replace(['varchar(',')'],['',''],$vo["Type"]);
- $field_val .= empty($field_val) ? "max:".$maxLen : "|max:".$maxLen;
- }
- {/php}
- {notempty name="$field_val"}
- ['{$field_key}', '{$field_val}'],
- {/notempty}
- {/volist}
- ]);
- if (!$validate->check($post)) {
- $this->error('提交失败:' . $validate->getError());
- }
- $model = $this->getModel();
- $res = $model->allowField(true)->save($post);
- if ($res) {
- $this->json_success("创建成功", $model);
- } else {
- $this->json_error("创建失败");
- }
- }
- {/if}
- {if condition="array_key_exists('delete',$crud)"}
- public function delete()
- {
- $post = $this->request->param();
- $validate = new \think\Validate([
- ['id', 'require|number'],
- ]);
- if (!$validate->check($post)) {
- $this->json_error('提交失败:' . $validate->getError());
- }
- $item = ($this->getModel())->where(['id' => $post['id']])->find();
- if (!$item) {
- $this->json_error('找不到该id');
- }
- if (!$item->delete()) {
- $this->json_error('删除失败');
- }
- $this->json_success('删除成功');
- }
- {/if}
- {if condition="array_key_exists('update',$crud)"}
- public function update()
- {
- $post = $this->request->param();
- //验证
- $validate = new \think\Validate([
- ['id', 'require|number'],
- {volist name="$fieldsInfo" id="vo"}
- {php}
- $field_key = $vo['Field'];
- if(in_array($field_key,['id','create_time','update_time'])){
- continue;
- }
- if($vo['Default'] === null){
- continue;
- }
- if(!empty($vo['Comment'])){
- $comment = $vo['Comment'];
- $field_key.= "|" . $comment;
- }
- $field_val = "";
- if(startWith($vo["Type"],'int') || startWith($vo["Type"],'tinyint')){
- $field_val .= empty($field_val) ? "number" : "|number";
- }
- if(startWith($vo["Type"],'varchar')){
- $maxLen = str_replace(['varchar(',')'],['',''],$vo["Type"]);
- $field_val .= empty($field_val) ? "max:".$maxLen : "|max:".$maxLen;
- }
- {/php}
- {notempty name="$field_val"}
- ['{$field_key}', '{$field_val}'],
- {/notempty}
- {/volist}
- ]);
- if (!$validate->check($post)) {
- $this->json_error('提交失败:' . $validate->getError());
- }
- $item = ($this->getModel())->where(['id' => $post['id']])->find();
- if (!$item) {
- $this->json_error('找不到该id');
- }
- if ($item->allowField(true)->save($post)) {
- $this->json_success("修改成功");
- } else {
- $this->json_error("修改失败");
- }
- }
- {/if}
- }
|