StoreService.php 6.7 KB

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