StoreProductReply.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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\admin\store;
  12. use crmeb\basic\BaseController;
  13. use app\common\repositories\store\product\ProductReplyRepository;
  14. use app\common\repositories\store\product\ProductRepository;
  15. use app\validate\admin\StoreProductReplyValidate;
  16. use FormBuilder\Exception\FormBuilderException;
  17. use think\App;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * 商品评论
  23. */
  24. class StoreProductReply extends BaseController
  25. {
  26. /**
  27. * @var ProductReplyRepository
  28. */
  29. protected $repository;
  30. /**
  31. * StoreProductReply constructor.
  32. * @param App $app
  33. * @param ProductReplyRepository $repository
  34. */
  35. public function __construct(App $app, ProductReplyRepository $repository)
  36. {
  37. parent::__construct($app);
  38. $this->repository = $repository;
  39. }
  40. /**
  41. * 列表
  42. * @return mixed
  43. * @throws DataNotFoundException
  44. * @throws DbException
  45. * @throws ModelNotFoundException
  46. * @author xaboy
  47. * @day 2020/6/1
  48. */
  49. public function lst()
  50. {
  51. [$page, $limit] = $this->getPage();
  52. $where = $this->request->params(['keyword', 'nickname', 'is_reply', 'date', 'product_id']);
  53. $where['mer_id'] = $this->request->merId() ?: '';
  54. return \app('json')->success($this->repository->getList($where, $page, $limit));
  55. }
  56. /**
  57. * 添加虚拟评论表单
  58. * @param $productId
  59. * @return \think\response\Json
  60. * @author Qinii
  61. */
  62. public function virtualForm($productId = null)
  63. {
  64. if ($productId && !app()->make(ProductRepository::class)->exists($productId)) {
  65. app('json')->fail('商品不存在');
  66. }
  67. return app('json')->success(formToData($this->repository->form($productId)));
  68. }
  69. /**
  70. * 添加虚拟评论
  71. * @param StoreProductReplyValidate $validate
  72. * @return mixed
  73. * @author xaboy
  74. * @day 2020/6/1
  75. */
  76. public function virtualReply(StoreProductReplyValidate $validate)
  77. {
  78. $data = $this->checkParams($validate);
  79. $_name = mb_substr($data['nickname'], 0, 1) . '***';
  80. $name = (strLen($data['nickname']) > 6) ? $_name . mb_substr($data['nickname'], -1, 1) : $_name;
  81. $data['nickname'] = $name;
  82. $productId = $data['product_id'];
  83. unset($data['product_id']);
  84. $this->repository->createVirtual([$productId], $data);
  85. return app('json')->success('添加成功');
  86. }
  87. /**
  88. * 回复评论表单
  89. * @param $id
  90. * @return \think\response\Json
  91. * @author Qinii
  92. */
  93. public function replyForm($id)
  94. {
  95. $merId = $this->request->merId();
  96. if ($merId)
  97. if (!$this->repository->merExists($merId, $id))
  98. return app('json')->fail('数据不存在');;
  99. return app('json')->success(formToData($this->repository->replyForm($id, $merId)));
  100. }
  101. /**
  102. * 回复评论
  103. * @param $id
  104. * @return \think\response\Json
  105. * @author Qinii
  106. */
  107. public function reply($id)
  108. {
  109. $merId = $this->request->merId();
  110. if ($merId)
  111. if (!$this->repository->merExists($merId, $id))
  112. return app('json')->fail('数据不存在');
  113. $merchant_reply_content = $this->request->param('content');
  114. if (!$merchant_reply_content)
  115. return app('json')->fail('请输入回复的内容');
  116. $merchant_reply_time = date('Y-m-d H:i:s');
  117. $is_reply = 1;
  118. $this->repository->update($id, compact('is_reply', 'merchant_reply_content', 'merchant_reply_time'));
  119. return app('json')->success('回复成功');
  120. }
  121. /**
  122. * 删除评论
  123. * @param $id
  124. * @return int
  125. * @throws DbException
  126. * @author xaboy
  127. * @day 2020/6/1
  128. */
  129. public function delete($id)
  130. {
  131. if (!$this->repository->exists($id))
  132. return app('json')->fail('数据不存在');
  133. $this->repository->delete($id);
  134. return app('json')->success('删除成功');
  135. }
  136. /**
  137. * 验证参数
  138. * @param StoreProductReplyValidate $validate
  139. * @return array
  140. * @author xaboy
  141. * @day 2020/6/1
  142. */
  143. public function checkParams(StoreProductReplyValidate $validate)
  144. {
  145. $data = $this->request->params([['product_id', []], 'nickname', 'comment', 'product_score', 'service_score', 'postage_score', 'avatar', ['pics', ''],'create_time']);
  146. $validate->check($data);
  147. $data['product_id'] = $data['product_id']['id'] ?? 0;
  148. return $data;
  149. }
  150. /**
  151. * 排序
  152. * @param $id
  153. * @return \think\response\Json
  154. * @author Qinii
  155. */
  156. public function sort($id)
  157. {
  158. $data = $this->request->params(['sort']);
  159. if (!$this->repository->exists($id))
  160. return app('json')->fail('数据不存在');
  161. $this->repository->update($id, $data);
  162. return app('json')->success('修改成功');
  163. }
  164. }