StoreProductReply.php 4.6 KB

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