ChatHandle.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. <?php
  2. namespace crmeb\services\workerman\chat;
  3. use app\models\store\StoreOrder;
  4. use app\models\store\StoreProduct;
  5. use app\models\store\StoreServiceLog;
  6. use app\models\user\User;
  7. use app\models\user\WechatUser;
  8. use crmeb\exceptions\AuthException;
  9. use crmeb\repositories\UserRepository;
  10. use crmeb\services\SystemConfigService;
  11. use crmeb\services\WechatService;
  12. use crmeb\services\workerman\ChannelService;
  13. use crmeb\services\workerman\Response;
  14. use think\facade\Log;
  15. use think\facade\Route as Url;
  16. use think\facade\Session;
  17. use Workerman\Connection\TcpConnection;
  18. class ChatHandle
  19. {
  20. protected $service;
  21. public function __construct(ChatService &$service)
  22. {
  23. $this->service = &$service;
  24. }
  25. public function login(TcpConnection &$connection, array $res, Response $response)
  26. {
  27. if (!isset($res['data']) || !$token = $res['data']) {
  28. return $response->close([
  29. 'msg' => '授权失败!'
  30. ]);
  31. }
  32. try {
  33. $authInfo = UserRepository::parseToken($token);
  34. } catch (AuthException $e) {
  35. return $response->close([
  36. 'msg' => $e->getMessage()
  37. ]);
  38. }
  39. $connection->user = $authInfo['user'];
  40. $connection->tokenData = $authInfo['tokenData'];
  41. $this->service->setUser($connection);
  42. return $response->success();
  43. }
  44. public function to_chat(TcpConnection &$connection, array $res)
  45. {
  46. $connection->chatToUid = $res['data']['id'] ?? 0;
  47. }
  48. public function chat(TcpConnection &$connection, array $res, Response $response)
  49. {
  50. $to_uid = $res['data']['to_uid'] ?? 0;
  51. $msn_type = $res['data']['type'] ?? 0;
  52. $msn = $res['data']['msn'] ?? '';
  53. $uid = $connection->user->uid;
  54. if (!$to_uid) return $response->send('err_tip', ['msg' => '用户不存在']);
  55. if ($to_uid == $uid) return $response->send('err_tip', ['msg' => '不能和自己聊天']);
  56. if (!in_array($msn_type, StoreServiceLog::MSN_TYPE)) return $response->send('err_tip', ['msg' => '格式错误']);
  57. $msn = trim(strip_tags(str_replace(["\n", "\t", "\r", " ", "&nbsp;"], '', htmlspecialchars_decode($msn))));
  58. $data = compact('to_uid', 'msn_type', 'msn', 'uid');
  59. $data['add_time'] = time();
  60. $connections = $this->service->user();
  61. $online = isset($connections[$to_uid]) && isset($connections[$to_uid]->chatToUid) && $connections[$to_uid]->chatToUid == $uid;
  62. $data['type'] = $online ? 1 : 0;
  63. StoreServiceLog::query('set wait_timeout=24*3600');
  64. StoreServiceLog::create($data);
  65. $_userInfo = User::getUserInfo($data['uid'], 'nickname,avatar');
  66. $data['nickname'] = $_userInfo['nickname'];
  67. $data['avatar'] = $_userInfo['avatar'];
  68. $data['productInfo'] = [];
  69. if ($msn_type == StoreServiceLog::MSN_TYPE_GOODS && $msn) {
  70. $productInfo = StoreProduct::validWhere()->where('id', $msn)->find();
  71. $data['productInfo'] = $productInfo ? $productInfo->toArray() : [];
  72. }
  73. $data['orderInfo'] = [];
  74. if ($msn_type == StoreServiceLog::MSN_TYPE_ORDER && $msn) {
  75. $order = StoreOrder::getUserOrderDetail($uid, $msn);
  76. if ($order) {
  77. $order = StoreOrder::tidyOrder($order->toArray(), true, true);
  78. $order['add_time_y'] = date('Y-m-d', $order['add_time']);
  79. $order['add_time_h'] = date('H:i:s', $order['add_time']);
  80. $data['orderInfo'] = $order;
  81. }
  82. }
  83. $response->send('chat', $data);
  84. if ($online) {
  85. $response->connection($this->service->user()[$to_uid])->send('reply', $data);
  86. } else {
  87. $userInfo = WechatUser::where('uid', $to_uid)->field('nickname,subscribe,openid,headimgurl')->find();
  88. if ($userInfo && $userInfo['subscribe'] && $userInfo['openid']) {
  89. $head = '客服提醒';
  90. $description = '您有新的消息,请注意查收!';
  91. $url = sys_config('site_url') . '/customer/chat/' . $uid;
  92. $message = WechatService::newsMessage($head, $description, $url, $_userInfo['avatar']);
  93. $userInfo = $userInfo->toArray();
  94. try {
  95. WechatService::staffService()->message($message)->to($userInfo['openid'])->send();
  96. } catch (\Exception $e) {
  97. Log::error($userInfo['nickname'] . '发送失败' . $e->getMessage());
  98. }
  99. }
  100. }
  101. }
  102. }