UserHandler.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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\webscoket\handler;
  12. use app\common\repositories\store\service\StoreServiceLogRepository;
  13. use app\common\repositories\store\service\StoreServiceRepository;
  14. use app\common\repositories\system\merchant\MerchantRepository;
  15. use app\common\repositories\user\UserRepository;
  16. use crmeb\services\JwtTokenService;
  17. use crmeb\services\SwooleTaskService;
  18. use Swoole\Server;
  19. use think\db\exception\DataNotFoundException;
  20. use think\db\exception\DbException;
  21. use think\db\exception\ModelNotFoundException;
  22. use think\exception\ValidateException;
  23. use think\facade\Db;
  24. use Throwable;
  25. use Firebase\JWT\ExpiredException;
  26. /**
  27. * Class UserHandler
  28. * @package app\webscoket\handler
  29. * @author xaboy
  30. * @day 2020-04-29
  31. */
  32. class UserHandler
  33. {
  34. /**
  35. * @var Server
  36. */
  37. protected $server;
  38. /**
  39. * UserHandler constructor.
  40. * @param Server $server
  41. */
  42. public function __construct(Server $server)
  43. {
  44. $this->server = $server;
  45. }
  46. /**
  47. * @param array $data
  48. * @return mixed
  49. * @author xaboy
  50. * @day 2020-05-06
  51. */
  52. public function test(array $data)
  53. {
  54. return app('json')->success($data);
  55. }
  56. /**
  57. * @param array $data
  58. * @return mixed
  59. * @throws DataNotFoundException
  60. * @throws DbException
  61. * @throws ModelNotFoundException
  62. * @author xaboy
  63. * @day 2020-05-06
  64. */
  65. public function login(array $data)
  66. {
  67. $token = $data['token'] ?? '';
  68. if (!$token) return app('json')->message('err_tip', '登录过期');
  69. /**
  70. * @var UserRepository $repository
  71. */
  72. $repository = app()->make(UserRepository::class);
  73. $service = new JwtTokenService();
  74. try {
  75. $payload = $service->parseToken($token);
  76. } catch (ExpiredException $e) {
  77. $repository->checkToken($token);
  78. $payload = $service->decode($token);
  79. } catch (Throwable $e) {//Token 过期
  80. return app('json')->message('err_tip', '登录过期');
  81. }
  82. if ('user' != $payload->jti[1])
  83. return app('json')->message('err_tip', '登录过期');
  84. $user = $repository->get($payload->jti[0]);
  85. if (!$user)
  86. return app('json')->message('err_tip', '账号不存在');
  87. if (!$user['status'])
  88. return app('json')->message('err_tip', '账号已被禁用');
  89. return app('json')->success(['uid' => $user->uid, 'data' => $user->toArray()]);
  90. }
  91. /**
  92. * @param array $data
  93. * @return mixed
  94. * @author xaboy
  95. * @day 2020-05-06
  96. */
  97. public function uid(array $data)
  98. {
  99. return app('json')->success(['uid' => $data['frame']->uid]);
  100. }
  101. public function service_chat_end(array $result)
  102. {
  103. app()->make(StoreServiceLogRepository::class)->unChat($result['frame']->uid, true);
  104. }
  105. public function chat_end(array $result)
  106. {
  107. app()->make(StoreServiceLogRepository::class)->unChat($result['frame']->uid);
  108. }
  109. public function service_chat_start(array $result)
  110. {
  111. app()->make(StoreServiceLogRepository::class)->serviceToChat($result['frame']->uid, $result['data']['uid']);
  112. }
  113. public function chat_start(array $result)
  114. {
  115. app()->make(StoreServiceLogRepository::class)->userToChat($result['frame']->uid, $result['data']['mer_id']);
  116. }
  117. public function service_chat(array $result)
  118. {
  119. $data = $result['data'];
  120. $frame = $result['frame'];
  121. if (!isset($data['msn_type']) || !isset($data['msn']) || !isset($data['uid']))
  122. return app('json')->message('err_tip', '数据格式错误');
  123. if (!$data['msn']) return app('json')->message('err_tip', '请输入发送内容');
  124. if (!in_array($data['msn_type'], [1, 2, 3, 4, 5, 6, 7, 8]))
  125. return app('json')->message('err_tip', '消息类型有误');
  126. $service = app()->make(StoreServiceRepository::class)->getService($frame->uid);
  127. if (!$service || !$service['status'])
  128. return app('json')->message('err_tip', '没有权限');
  129. $storeServiceLogRepository = app()->make(StoreServiceLogRepository::class);
  130. if (!$storeServiceLogRepository->issetLog($data['uid'], $service->mer_id))
  131. return app('json')->message('err_tip', '不能主动发送给用户');
  132. $data['msn'] = trim(strip_tags(str_replace(["\n", "\t", "\r", " ", "&nbsp;"], '', htmlspecialchars_decode($data['msn']))));
  133. $data['mer_id'] = $service->mer_id;
  134. $data['service_id'] = $service->service_id;
  135. $data['send_type'] = 1;
  136. try {
  137. $storeServiceLogRepository->checkMsn($service->mer_id, $data['uid'], $data['msn_type'], $data['msn']);
  138. } catch (ValidateException $e) {
  139. return app('json')->message('err_tip', $e->getMessage());
  140. }
  141. $log = $storeServiceLogRepository->create($data);
  142. $storeServiceLogRepository->getSendData($log);
  143. $log->set('service', $service->visible(['service_id', 'avatar', 'nickname'])->toArray());
  144. $storeServiceLogRepository->serviceToChat($frame->uid, $data['uid']);
  145. $log = $log->toArray();
  146. SwooleTaskService::chatToUser([
  147. 'uid' => $data['uid'],
  148. 'data' => $log,
  149. 'except' => [$frame->fd]
  150. ]);
  151. return app('json')->message('chat', $log);
  152. }
  153. public function send_chat(array $result)
  154. {
  155. $data = $result['data'];
  156. $frame = $result['frame'];
  157. if (!isset($data['msn_type']) || !isset($data['msn']) || !isset($data['mer_id']))
  158. return app('json')->message('err_tip', '数据格式错误');
  159. if (!$data['msn']) return app('json')->message('err_tip', '请输入发送内容');
  160. if (!in_array($data['msn_type'], [1, 2, 3, 4, 5, 6, 7, 8]))
  161. return app('json')->message('err_tip', '消息类型有误');
  162. if (!app()->make(MerchantRepository::class)->exists(intval($data['mer_id'])))
  163. return app('json')->message('err_tip', '商户不存在');
  164. $service = app()->make(StoreServiceRepository::class)->getChatService($data['mer_id'], $frame->uid);
  165. if (!$service)
  166. return app('json')->message('err_tip', '该商户暂无有效客服');
  167. $data['msn'] = filter_emoji(trim(strip_tags(str_replace(["\n", "\t", "\r", " ", "&nbsp;"], '', htmlspecialchars_decode($data['msn'])))));
  168. if (!$data['msn'])
  169. return app('json')->message('err_tip', '内容字符无效');
  170. $data['uid'] = $frame->uid;
  171. $data['service_id'] = $service->service_id;
  172. $data['send_type'] = 0;
  173. $storeServiceLogRepository = app()->make(StoreServiceLogRepository::class);
  174. try {
  175. $storeServiceLogRepository->checkMsn($data['mer_id'], $frame->uid, $data['msn_type'], $data['msn']);
  176. } catch (ValidateException $e) {
  177. return app('json')->message('err_tip', $e->getMessage());
  178. }
  179. $log = $storeServiceLogRepository->create($data);
  180. $storeServiceLogRepository->getSendData($log);
  181. $storeServiceLogRepository->userToChat($data['uid'], $data['mer_id']);
  182. $log->user;
  183. $log = $log->toArray();
  184. //TODO 发送给客服,是否在线,发送提醒
  185. SwooleTaskService::chatToService([
  186. 'uid' => $service->uid,
  187. 'data' => $log,
  188. 'except' => [$frame->fd]
  189. ]);
  190. return app('json')->message('chat', $log);
  191. }
  192. public function close($result)
  193. {
  194. app()->make(StoreServiceLogRepository::class)->unChat($result['uid']);
  195. app()->make(StoreServiceLogRepository::class)->unChat($result['uid'], true);
  196. }
  197. }