StoreService.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. <?php
  2. namespace app\controller\merchant\store\service;
  3. use ln\basic\BaseController;
  4. use app\common\repositories\store\service\StoreServiceRepository;
  5. use app\validate\merchant\StoreServiceValidate;
  6. use FormBuilder\Exception\FormBuilderException;
  7. use think\App;
  8. use think\db\exception\DataNotFoundException;
  9. use think\db\exception\DbException;
  10. use think\db\exception\ModelNotFoundException;
  11. use app\common\repositories\store\service\StoreServiceLogRepository;
  12. /**
  13. * Class StoreService
  14. * @package app\controller\merchant\store\service
  15. * @author xaboy
  16. * @day 2020/5/29
  17. */
  18. class StoreService extends BaseController
  19. {
  20. /**
  21. * @var StoreServiceRepository
  22. */
  23. protected $repository;
  24. /**
  25. * @var StoreServiceLogRepository
  26. */
  27. protected $logRepository;
  28. /**
  29. * StoreService constructor.
  30. * @param App $app
  31. * @param StoreServiceRepository $repository
  32. */
  33. public function __construct(App $app, StoreServiceRepository $repository, StoreServiceLogRepository $logRepository)
  34. {
  35. parent::__construct($app);
  36. $this->repository = $repository;
  37. $this->logRepository = $logRepository;
  38. }
  39. /**
  40. * @return mixed
  41. * @throws DataNotFoundException
  42. * @throws DbException
  43. * @throws ModelNotFoundException
  44. * @author xaboy
  45. * @day 2020/5/29
  46. */
  47. public function lst()
  48. {
  49. $where = $this->request->params(['keyword', 'status']);
  50. [$page, $limit] = $this->getPage();
  51. $where['mer_id'] = $this->request->merId();
  52. return app('json')->success($this->repository->getList($where, $page, $limit));
  53. }
  54. /**
  55. * @return mixed
  56. * @throws FormBuilderException
  57. * @author xaboy
  58. * @day 2020/5/29
  59. */
  60. public function createForm()
  61. {
  62. return app('json')->success(formToData($this->repository->form()));
  63. }
  64. /**
  65. * @param StoreServiceValidate $validate
  66. * @return mixed
  67. * @author xaboy
  68. * @day 2020/5/29
  69. */
  70. public function create(StoreServiceValidate $validate)
  71. {
  72. $data = $this->checkParams($validate);
  73. $data['mer_id'] = $this->request->merId();
  74. if ($this->repository->issetService($data['mer_id'], $data['uid']))
  75. return app('json')->fail('该用户已绑定客服');
  76. $this->repository->create($data);
  77. return app('json')->success('添加成功');
  78. }
  79. /**
  80. * @param StoreServiceValidate $validate
  81. * @return array
  82. * @author xaboy
  83. * @day 2020/5/29
  84. */
  85. public function checkParams(StoreServiceValidate $validate)
  86. {
  87. $data = $this->request->params([['uid', []], 'nickname', 'status', 'customer', 'is_verify', 'notify', 'avatar', 'phone', ['sort', 0]]);
  88. $validate->check($data);
  89. if (!$data['avatar']) $data['avatar'] = $data['uid']['src'];
  90. $data['uid'] = $data['uid']['id'];
  91. return $data;
  92. }
  93. /**
  94. * @param $id
  95. * @return mixed
  96. * @throws FormBuilderException
  97. * @throws DataNotFoundException
  98. * @throws DbException
  99. * @throws ModelNotFoundException
  100. * @author xaboy
  101. * @day 2020/5/29
  102. */
  103. public function updateForm($id)
  104. {
  105. if (!$this->repository->merExists($this->request->merId(), $id))
  106. return app('json')->fail('数据不存在');
  107. return app('json')->success(formToData($this->repository->updateForm($id)));
  108. }
  109. /**
  110. * @param $id
  111. * @param StoreServiceValidate $validate
  112. * @return mixed
  113. * @throws DbException
  114. * @author xaboy
  115. * @day 2020/5/29
  116. */
  117. public function update($id, StoreServiceValidate $validate)
  118. {
  119. $data = $this->checkParams($validate);
  120. if (!$this->repository->merExists($merId = $this->request->merId(), $id))
  121. return app('json')->fail('数据不存在');
  122. if ($this->repository->issetService($merId, $data['uid'], $id))
  123. return app('json')->fail('该用户已绑定客服');
  124. $this->repository->update($id, $data);
  125. return app('json')->success('修改成功');
  126. }
  127. /**
  128. * @param int $id
  129. * @return mixed
  130. * @throws DbException
  131. * @author xaboy
  132. * @day 2020/5/29
  133. */
  134. public function changeStatus($id)
  135. {
  136. $status = $this->request->param('status');
  137. if (!$this->repository->merExists($this->request->merId(), $id))
  138. return app('json')->fail('数据不存在');
  139. $this->repository->update($id, ['status' => $status == 1 ? 1 : 0]);
  140. return app('json')->success('修改成功');
  141. }
  142. /**
  143. * @param $id
  144. * @return mixed
  145. * @throws DbException
  146. * @author xaboy
  147. * @day 2020/5/29
  148. */
  149. public function delete($id)
  150. {
  151. if (!$this->repository->merExists($this->request->merId(), $id))
  152. return app('json')->fail('数据不存在');
  153. $this->repository->delete($id);
  154. return app('json')->success('删除成功');
  155. }
  156. /**
  157. * TODO 客服的全部用户
  158. * @param $id
  159. * @return mixed
  160. * @author Qinii
  161. * @day 2020-06-18
  162. */
  163. public function serviceUserList($id)
  164. {
  165. if (!$this->repository->merExists($this->request->merId(), $id))
  166. return app('json')->fail('数据不存在');
  167. [$page, $limit] = $this->getPage();
  168. return app('json')->success($this->logRepository->getServiceUserList($id, $page, $limit));
  169. }
  170. /**
  171. * TODO 商户的全部用户列表
  172. * @return mixed
  173. * @author Qinii
  174. * @day 2020-06-19
  175. */
  176. public function merchantUserList()
  177. {
  178. [$page, $limit] = $this->getPage();
  179. return app('json')->success($this->logRepository->getMerchantUserList($this->request->merId(), $page, $limit));
  180. }
  181. /**
  182. * TODO 用户与客服聊天记录
  183. * @param $id
  184. * @param $uid
  185. * @return mixed
  186. * @author Qinii
  187. * @day 2020-06-19
  188. */
  189. public function getUserMsnByService($id, $uid)
  190. {
  191. [$page, $limit] = $this->getPage();
  192. if (!$this->repository->getWhereCount(['service_id' => $id, 'mer_id' => $this->request->merId()]))
  193. return app('json')->fail('客服不存在');
  194. return app('json')->success($this->logRepository->getUserMsn($uid, $page, $limit, $this->request->merId(), $id));
  195. }
  196. /**
  197. * TODO 用户与商户聊天记录
  198. * @param $id
  199. * @return mixed
  200. * @author Qinii
  201. * @day 2020-06-19
  202. */
  203. public function getUserMsnByMerchant($id)
  204. {
  205. [$page, $limit] = $this->getPage();
  206. return app('json')->success($this->logRepository->getUserMsn($id, $page, $limit, $this->request->merId()));
  207. }
  208. }