StoreProductAttrTemplate.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2024 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\controller\api\server;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\store\service\StoreServiceRepository;
  14. use app\validate\merchant\StoreAttrTemplateValidate;
  15. use think\App;
  16. use app\common\repositories\store\StoreAttrTemplateRepository;
  17. use think\exception\HttpResponseException;
  18. /**
  19. * Class StoreProductAttrTemplate
  20. * app\controller\api\server
  21. * 移动客服 - 商品属性模板
  22. */
  23. class StoreProductAttrTemplate extends BaseController
  24. {
  25. protected $merId;
  26. protected $repository;
  27. public function __construct(App $app, StoreAttrTemplateRepository $repository)
  28. {
  29. parent::__construct($app);
  30. $this->repository = $repository;
  31. $this->merId = $this->request->route('merId');
  32. }
  33. /**
  34. * 列表
  35. * @return \think\response\Json
  36. * @author Qinii
  37. * @day 8/24/21
  38. */
  39. public function lst()
  40. {
  41. [$page, $limit] = $this->getPage();
  42. $where = $this->request->params(['keyword']);
  43. $data = $this->repository->getList($this->merId, $where, $page, $limit);
  44. return app('json')->success($data);
  45. }
  46. /**
  47. * 获取所有属性模板
  48. * @return \think\response\Json
  49. * @author Qinii
  50. * @day 8/24/21
  51. */
  52. public function getlist()
  53. {
  54. return app('json')->success($this->repository->list($this->merId));
  55. }
  56. /**
  57. * 创建
  58. * @param StoreAttrTemplateValidate $validate
  59. * @return \think\response\Json
  60. * @author Qinii
  61. */
  62. public function create(StoreAttrTemplateValidate $validate)
  63. {
  64. $data = $this->checkParams($validate);
  65. $data['mer_id'] = $this->merId;
  66. $this->repository->create($data);
  67. return app('json')->success('添加成功');
  68. }
  69. /**
  70. * 编辑
  71. * @param $id
  72. * @param StoreAttrTemplateValidate $validate
  73. * @return \think\response\Json
  74. * @author Qinii
  75. */
  76. public function update($id, StoreAttrTemplateValidate $validate)
  77. {
  78. $merId = $this->merId;
  79. if (!$this->repository->merExists($merId, $id))
  80. return app('json')->fail('数据不存在');
  81. $data = $this->checkParams($validate);
  82. $data['mer_id'] = $merId;
  83. $this->repository->update($id, $data);
  84. return app('json')->success('编辑成功');
  85. }
  86. /**
  87. * 详情
  88. * @param $id
  89. * @return \think\response\Json
  90. * @author Qinii
  91. */
  92. public function detail($id)
  93. {
  94. if (!$this->repository->merExists($this->merId, $id))
  95. return app('json')->fail('数据不存在');
  96. return app('json')->success($this->repository->get($id,$this->merId));
  97. }
  98. /**
  99. * 批量删除
  100. * @param $id
  101. * @return \think\response\Json
  102. * @author Qinii
  103. */
  104. public function batchDelete()
  105. {
  106. $ids = $this->request->param('ids');
  107. $merId = $this->merId;
  108. foreach ($ids as $id){
  109. if (!$this->repository->merExists($merId, $id))
  110. return app('json')->fail('ID:'.$id.' 不存在');
  111. }
  112. $this->repository->delete($ids, $merId);
  113. return app('json')->success('删除成功');
  114. }
  115. /**
  116. * 参数验证
  117. * @param StoreAttrTemplateValidate $validate
  118. * @return array|mixed|string|string[]
  119. * @author Qinii
  120. */
  121. public function checkParams(StoreAttrTemplateValidate $validate)
  122. {
  123. $data = $this->request->params(['template_name', ['template_value', []]]);
  124. $validate->check($data);
  125. return $data;
  126. }
  127. }