ApiController.php.tp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. namespace app\api\controller;
  2. use app\api\controller\base\Base;
  3. class {$humpName} extends Base
  4. {
  5. private function getModel()
  6. {
  7. return new \app\common\model\{$humpName}();
  8. }
  9. {if condition="array_key_exists('select',$crud)"}
  10. public function index()
  11. {
  12. $post = $this->request->param();
  13. $validate = new \think\Validate([
  14. ['id', 'number'],
  15. ['page', 'number'],
  16. ['pagenum', 'number|<=:1000']
  17. ]);
  18. if (!$validate->check($post)) {
  19. $this->json_error('提交失败:' . $validate->getError());
  20. }
  21. $where = [];
  22. if (isset($post['id'])) {
  23. $where['id'] = $post['id'];
  24. }
  25. if (isset($post['id'])) {
  26. $datalist = ($this->getModel())->where($where)->find();
  27. } else {
  28. $pagenum = $this->request->param('pagenum', 20, 'intval');
  29. $datalist = ($this->getModel())->where($where)->paginate($pagenum, true);
  30. }
  31. if (empty($datalist)) {
  32. $this->json_error("没有数据");
  33. }
  34. $this->json_success("查询成功", $datalist);
  35. }
  36. {/if}
  37. {if condition="array_key_exists('create',$crud)"}
  38. public function create()
  39. {
  40. $post = $this->request->param();
  41. //验证
  42. $validate = new \think\Validate([
  43. {volist name="$fieldsInfo" id="vo"}
  44. {php}
  45. $field_key = $vo['Field'];
  46. if(in_array($field_key,['id','create_time','update_time'])){
  47. continue;
  48. }
  49. if(!empty($vo['Comment'])){
  50. $comment = $vo['Comment'];
  51. $field_key.= "|" . $comment;
  52. }
  53. $field_val = "";
  54. if($vo['Default'] === null){
  55. $field_val.= "require";
  56. }
  57. if(startWith($vo["Type"],'int') || startWith($vo["Type"],'tinyint')){
  58. $field_val .= empty($field_val) ? "number" : "|number";
  59. }
  60. if(startWith($vo["Type"],'varchar')){
  61. $maxLen = str_replace(['varchar(',')'],['',''],$vo["Type"]);
  62. $field_val .= empty($field_val) ? "max:".$maxLen : "|max:".$maxLen;
  63. }
  64. {/php}
  65. {notempty name="$field_val"}
  66. ['{$field_key}', '{$field_val}'],
  67. {/notempty}
  68. {/volist}
  69. ]);
  70. if (!$validate->check($post)) {
  71. $this->error('提交失败:' . $validate->getError());
  72. }
  73. $model = $this->getModel();
  74. $res = $model->allowField(true)->save($post);
  75. if ($res) {
  76. $this->json_success("创建成功", $model);
  77. } else {
  78. $this->json_error("创建失败");
  79. }
  80. }
  81. {/if}
  82. {if condition="array_key_exists('delete',$crud)"}
  83. public function delete()
  84. {
  85. $post = $this->request->param();
  86. $validate = new \think\Validate([
  87. ['id', 'require|number'],
  88. ]);
  89. if (!$validate->check($post)) {
  90. $this->json_error('提交失败:' . $validate->getError());
  91. }
  92. $item = ($this->getModel())->where(['id' => $post['id']])->find();
  93. if (!$item) {
  94. $this->json_error('找不到该id');
  95. }
  96. if (!$item->delete()) {
  97. $this->json_error('删除失败');
  98. }
  99. $this->json_success('删除成功');
  100. }
  101. {/if}
  102. {if condition="array_key_exists('update',$crud)"}
  103. public function update()
  104. {
  105. $post = $this->request->param();
  106. //验证
  107. $validate = new \think\Validate([
  108. ['id', 'require|number'],
  109. {volist name="$fieldsInfo" id="vo"}
  110. {php}
  111. $field_key = $vo['Field'];
  112. if(in_array($field_key,['id','create_time','update_time'])){
  113. continue;
  114. }
  115. if($vo['Default'] === null){
  116. continue;
  117. }
  118. if(!empty($vo['Comment'])){
  119. $comment = $vo['Comment'];
  120. $field_key.= "|" . $comment;
  121. }
  122. $field_val = "";
  123. if(startWith($vo["Type"],'int') || startWith($vo["Type"],'tinyint')){
  124. $field_val .= empty($field_val) ? "number" : "|number";
  125. }
  126. if(startWith($vo["Type"],'varchar')){
  127. $maxLen = str_replace(['varchar(',')'],['',''],$vo["Type"]);
  128. $field_val .= empty($field_val) ? "max:".$maxLen : "|max:".$maxLen;
  129. }
  130. {/php}
  131. {notempty name="$field_val"}
  132. ['{$field_key}', '{$field_val}'],
  133. {/notempty}
  134. {/volist}
  135. ]);
  136. if (!$validate->check($post)) {
  137. $this->json_error('提交失败:' . $validate->getError());
  138. }
  139. $item = ($this->getModel())->where(['id' => $post['id']])->find();
  140. if (!$item) {
  141. $this->json_error('找不到该id');
  142. }
  143. if ($item->allowField(true)->save($post)) {
  144. $this->json_success("修改成功");
  145. } else {
  146. $this->json_error("修改失败");
  147. }
  148. }
  149. {/if}
  150. }