StoreService.php 7.9 KB

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