StoreService.php 7.2 KB

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