StoreService.php 7.6 KB

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