ChatMessageService.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | LARAVEL8.0 框架 [ LARAVEL ][ RXThinkCMF ]
  4. // +----------------------------------------------------------------------
  5. // | 版权所有 2017~2021 LARAVEL研发中心
  6. // +----------------------------------------------------------------------
  7. // | 官方网站: http://www.laravel.cn
  8. // +----------------------------------------------------------------------
  9. // | Author: laravel开发员 <laravel.qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace App\Services;
  12. use App\Models\ChatMessageModel;
  13. use App\Models\ConfigModel;
  14. /**
  15. * 聊天-服务类
  16. * Class ChatMessageService
  17. * @package App\Services
  18. */
  19. class ChatMessageService extends BaseService
  20. {
  21. // 静态对象
  22. protected static $instance = null;
  23. /**
  24. * 构造函数
  25. * ChatMessageService constructor.
  26. */
  27. public function __construct()
  28. {
  29. $this->model = new ChatMessageModel();
  30. }
  31. /**
  32. * 静态入口
  33. * @return static|null
  34. */
  35. public static function make(){
  36. if(!self::$instance){
  37. self::$instance = (new static());
  38. }
  39. return self::$instance;
  40. }
  41. /**
  42. * 绑定用户
  43. * @param $fd
  44. * @param $data
  45. * @return bool
  46. */
  47. public function bind($fd, $data)
  48. {
  49. $userId = isset($data['from_uid'])? intval($data['from_uid']) : 0;
  50. if($userId<=0){
  51. $this->error = '1013';
  52. return false;
  53. }
  54. RedisService::set("chats:bind:{$userId}", ['fd'=> $fd, 'user_id'=> $userId], 86400);
  55. return true;
  56. }
  57. /**
  58. * 添加或编辑
  59. * @return array
  60. */
  61. public function saveData($data)
  62. {
  63. $data = [
  64. 'type'=> isset($data['type'])? $data['type'] : 1,
  65. 'message_type'=> isset($data['message_type'])? $data['message_type'] : 1,
  66. 'from_uid'=> isset($data['from_uid'])? $data['from_uid'] : 1,
  67. 'to_uid'=> isset($data['to_uid'])? $data['to_uid'] : 1,
  68. 'order_no'=> isset($data['order_no'])? $data['order_no'] : '',
  69. 'chat_key'=> getChatKey($data['from_uid'],$data['to_uid']),
  70. 'message'=> isset($data['message'])? $data['message'] : '',
  71. 'create_time'=> time(),
  72. 'update_time'=> time(),
  73. 'is_read'=> 2,
  74. 'status'=> 1,
  75. 'mark'=> 1,
  76. ];
  77. return parent::edit($data); // TODO: Change the autogenerated stub
  78. }
  79. /**
  80. * 推送保存消息,如订单消息
  81. * @param $data 消息数据
  82. * @return int|number
  83. */
  84. public function pushMessage($data)
  85. {
  86. return $this->model->edit($data);
  87. }
  88. }