StoreProductReply.php 4.9 KB

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