ChatHandle.php 4.3 KB

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