Service.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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\api\store\service;
  12. use app\common\repositories\store\service\StoreServiceLogRepository;
  13. use app\common\repositories\store\service\StoreServiceRepository;
  14. use app\common\repositories\store\service\StoreServiceUserRepository;
  15. use app\common\repositories\system\ExtendRepository;
  16. use app\common\repositories\system\merchant\MerchantRepository;
  17. use crmeb\basic\BaseController;
  18. use think\App;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\facade\Cache;
  23. /**
  24. * Class Service
  25. * @package app\controller\api\store\service
  26. * @author xaboy
  27. * @day 2020/5/29
  28. */
  29. class Service extends BaseController
  30. {
  31. /**
  32. * @var StoreServiceRepository
  33. */
  34. protected $repository;
  35. /**
  36. * Service constructor.
  37. * @param App $app
  38. * @param StoreServiceRepository $repository
  39. */
  40. public function __construct(App $app, StoreServiceRepository $repository)
  41. {
  42. parent::__construct($app);
  43. $this->repository = $repository;
  44. }
  45. /**
  46. * 聊天历史记录
  47. * @param $id
  48. * @param StoreServiceLogRepository $repository
  49. * @return mixed
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. * @author xaboy
  54. * @day 2020/6/15
  55. */
  56. public function chatHistory($id, StoreServiceLogRepository $repository)
  57. {
  58. [$page, $limit] = $this->getPage();
  59. return app('json')->success($repository->userList($id, $this->request->uid(), $page, $limit));
  60. }
  61. /**
  62. * 用户获取已经聊天的客服列表
  63. * @param StoreServiceLogRepository $repository
  64. * @return mixed
  65. * @throws DataNotFoundException
  66. * @throws DbException
  67. * @throws ModelNotFoundException
  68. * @author xaboy
  69. * @day 2020/6/16
  70. */
  71. public function getList(StoreServiceUserRepository $repository)
  72. {
  73. [$page, $limit] = $this->getPage();
  74. return app('json')->success($repository->userMerchantList($this->request->uid(), $page, $limit));
  75. }
  76. /**
  77. * 客服获取已经聊天的用户列表
  78. * @param StoreServiceLogRepository $repository
  79. * @return mixed
  80. * @throws DataNotFoundException
  81. * @throws DbException
  82. * @throws ModelNotFoundException
  83. * @author xaboy
  84. * @day 2020/6/16
  85. */
  86. public function serviceUserList($merId, StoreServiceUserRepository $repository)
  87. {
  88. [$page, $limit] = $this->getPage();
  89. return app('json')->success($repository->merUserList($merId, $this->request->uid(), $page, $limit));
  90. }
  91. /**
  92. * @param $merId
  93. * @param $id
  94. * @param StoreServiceLogRepository $repository
  95. * @return mixed
  96. * @throws DataNotFoundException
  97. * @throws DbException
  98. * @throws ModelNotFoundException
  99. * @author xaboy
  100. * @day 2020/6/15
  101. */
  102. public function serviceHistory($merId, $id, StoreServiceLogRepository $repository)
  103. {
  104. [$page, $limit] = $this->getPage();
  105. return app('json')->success($repository->merList($merId, $id, $this->request->uid(), $page, $limit));
  106. }
  107. /**
  108. * 客服详情
  109. * @param $merId
  110. * @param $uid
  111. * @param StoreServiceUserRepository $serviceUserRepository
  112. * @return \think\response\Json
  113. * @throws DataNotFoundException
  114. * @throws DbException
  115. * @throws ModelNotFoundException
  116. * @author wuhaotian
  117. * @email 442384644@qq.com
  118. * @date 2024/7/10
  119. */
  120. public function user($merId, $uid, StoreServiceUserRepository $serviceUserRepository)
  121. {
  122. if (!$service = $this->repository->search([
  123. 'uid' => $this->request->uid(),
  124. 'mer_id' => (int)$merId,
  125. 'status' => 1
  126. ])->find()) {
  127. return app('json')->fail('没有权限');
  128. }
  129. $user = $serviceUserRepository->search(['uid' => $uid, 'service_user_id' => $service->service_user_id])->with(['user' => function ($query) use ($merId) {
  130. $query->field('uid,avatar,nickname,is_promoter' . (!$merId ? ',phone' : ''));
  131. }, 'mark' => function ($query) use ($merId) {
  132. $query->where('mer_id', $merId)->bind(['mark' => 'extend_value']);
  133. }])->find();
  134. if (!$user) {
  135. return app('json')->fail('用户不存在');
  136. }
  137. return app('json')->success($user->toArray());
  138. }
  139. /**
  140. * 用户备注
  141. * @param $merId
  142. * @param $uid
  143. * @param StoreServiceUserRepository $serviceUserRepository
  144. * @param ExtendRepository $extendRepository
  145. * @return \think\response\Json
  146. * @author wuhaotian
  147. * @email 442384644@qq.com
  148. * @date 2024/7/10
  149. */
  150. public function mark($merId, $uid, StoreServiceUserRepository $serviceUserRepository, ExtendRepository $extendRepository)
  151. {
  152. $data = $this->request->params(['mark']);
  153. if (!$service = $this->repository->search([
  154. 'uid' => $this->request->uid(),
  155. 'mer_id' => (int)$merId,
  156. 'status' => 1
  157. ])->find()) {
  158. return app('json')->fail('没有权限');
  159. }
  160. if ($service->mer_id && !$serviceUserRepository->existsWhere(['uid' => (int)$uid, 'mer_id' => $service->mer_id])) {
  161. return app('json')->fail('用户不存在');
  162. }
  163. $extendRepository->updateInfo(ExtendRepository::TYPE_SERVICE_USER_MARK, (int)$uid, $service->mer_id, (string)$data['mark']);
  164. return app('json')->success('备注成功');
  165. }
  166. /**
  167. * 商户信息
  168. * @param $id
  169. * @return \think\response\Json
  170. * @author wuhaotian
  171. * @email 442384644@qq.com
  172. * @date 2024/7/10
  173. */
  174. public function merchantInfo($id)
  175. {
  176. if ($id) {
  177. $merchant = app()->make(MerchantRepository::class)->get((int)$id);
  178. if (!$merchant)
  179. return app('json')->fail('商户不存在');
  180. $data = [
  181. 'mer_id' => $merchant['mer_id'],
  182. 'avatar' => $merchant['mer_avatar'],
  183. 'name' => $merchant['mer_name'],
  184. ];
  185. } else {
  186. $config = systemConfig(['site_logo', 'site_name']);
  187. $data = [
  188. 'mer_id' => 0,
  189. 'avatar' => $config['site_logo'],
  190. 'name' => $config['site_name'],
  191. ];
  192. }
  193. return app('json')->success($data);
  194. }
  195. /**
  196. * 是否有客服在线
  197. * @param $id
  198. * @return \think\response\Json
  199. * @throws DataNotFoundException
  200. * @throws DbException
  201. * @throws ModelNotFoundException
  202. * @author wuhaotian
  203. * @email 442384644@qq.com
  204. * @date 2024/7/10
  205. */
  206. public function hasService($id){
  207. $uid = 0;
  208. if ($this->request->isLogin()) {
  209. $uid = $this->request->uid();
  210. }
  211. $data = $this->repository->getChatService($id, $uid);
  212. if (!$data) {
  213. return app('json')->fail('暂无可用客服');
  214. }
  215. return app('json')->success(200);
  216. }
  217. /**
  218. * 扫码登录
  219. * @param $key
  220. * @return \think\response\Json
  221. * @author wuhaotian
  222. * @email 442384644@qq.com
  223. * @date 2024/7/10
  224. */
  225. public function scanLogin($key)
  226. {
  227. $serviceId = (int)$this->request->param('service_id');
  228. if (!$serviceId || !$service = $this->repository->search([
  229. 'uid' => $this->request->uid(),
  230. 'service_id' => $serviceId,
  231. ])->find()) {
  232. return app('json')->fail('用户不存在');
  233. }
  234. if (!$service['is_open'] || !$service['status']) {
  235. return app('json')->fail('账号已被关闭');
  236. }
  237. if (Cache::has('_scan_ser_login' . $key))
  238. Cache::set('_scan_ser_login' . $key, $serviceId);
  239. else
  240. return app('json')->fail('操作超时');
  241. return app('json')->success('登录成功');
  242. }
  243. }