PackSpecs.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare (strict_types = 1);
  3. namespace app\admin\controller\goods;
  4. use app\common\controller\Backend;
  5. use app\admin\traits\Actions;
  6. use think\annotation\route\Group;
  7. use think\annotation\route\Route;
  8. use app\common\model\PackSpecs as PackSpecsModel;
  9. #[Group("goods/pack_specs")]
  10. class PackSpecs extends Backend
  11. {
  12. use Actions;
  13. protected function _initialize()
  14. {
  15. parent::_initialize();
  16. $this->model = new PackSpecsModel();
  17. }
  18. /**
  19. * 查看
  20. */
  21. #[Route('GET,JSON', 'index')]
  22. public function index()
  23. {
  24. if (false === $this->request->isAjax()) {
  25. return $this->fetch();
  26. }
  27. if ($this->request->post('selectpage')) {
  28. return $this->selectpage();
  29. }
  30. [$where, $order, $limit, $with] = $this->buildparams();
  31. $list = $this->model
  32. ->withJoin($with, 'left')
  33. //如果没有使用operate filter过滤的情况下,推荐使用with关联,可以提高查询效率
  34. //->with($with)
  35. ->where($where)
  36. ->order($order)
  37. ->paginate($limit);
  38. $result = ['total' => $list->total(), 'rows' => $list->items()];
  39. return json($result);
  40. }
  41. //添加
  42. #[Route("GET,POST","add")]
  43. public function add()
  44. {
  45. if (false === $this->request->isPost()) {
  46. return $this->fetch();
  47. }
  48. $params = $this->request->post("row/a");
  49. if (empty($params)) {
  50. return $this->jsonError('提交的参数不能为空');
  51. }
  52. $result=$this->model->save($params);
  53. if ($result === false) {
  54. return $this->jsonError('没有新增任何数据');
  55. }
  56. return $this->jsonSuccess();
  57. }
  58. //修改
  59. #[Route("GET,POST","edit")]
  60. public function edit(mixed $row=null)
  61. {
  62. $ids = $this->request->get('ids');
  63. if(!$row || is_array($row)){
  64. $row = $this->model->find($ids);
  65. }
  66. if(count($this->volidateFields)>0){
  67. foreach ($this->volidateFields as $field=>$value){
  68. if($row[$field]!=$value){
  69. return $this->jsonError(__('没有操作权限'));
  70. }
  71. }
  72. }
  73. if (false === $this->request->isPost()) {
  74. $this->assign('row', $row);
  75. return $this->fetch();
  76. }
  77. $params = $this->request->post("row/a");
  78. $params['updatetime']=time();
  79. if (empty($params)) {
  80. return $this->jsonError('提交的参数不能为空');
  81. }
  82. $result=$this->model->saveAll([$params]);
  83. if ($result === false) {
  84. return $this->jsonError('没有修改任何数据');
  85. }
  86. return $this->jsonSuccess();
  87. }
  88. }