MsgService.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. <?php
  2. declare(strict_types=1);
  3. namespace app\common\service;
  4. use app\common\model\Msg;
  5. /**
  6. * 消息服务
  7. */
  8. abstract class MsgService extends BaseService{
  9. protected $msg_type;
  10. protected $content;
  11. protected $send_to;
  12. protected string $error;
  13. //发送消息事件
  14. abstract protected function sendEvent(Msg $msg):bool;
  15. public function setContent(string $content):MsgService
  16. {
  17. $this->content=$content;
  18. return $this;
  19. }
  20. public function sendTo(mixed $send_to):MsgService
  21. {
  22. $this->send_to=$send_to;
  23. return $this;
  24. }
  25. protected function init()
  26. {
  27. if($this->msg_type === null){
  28. throw new \Exception(__('消息类型不能为空'));
  29. }
  30. }
  31. //生成消息
  32. public function create(string $content='',mixed $send_to='')
  33. {
  34. if($content){
  35. $this->content=$content;
  36. }
  37. if($send_to){
  38. $this->send_to=$send_to;
  39. }
  40. if($this->content === null){
  41. throw new \Exception(__('消息内容不能为空'));
  42. }
  43. if($this->send_to === null){
  44. throw new \Exception(__('消息接收者不能为空'));
  45. }
  46. $classname=get_called_class();
  47. $send_to=$this->send_to;
  48. if(!is_array($this->send_to)){
  49. $send_to=[$send_to];
  50. }
  51. $insert=[];
  52. foreach ($send_to as $v){
  53. $insert[]=[
  54. 'msg_type'=>$this->msg_type,
  55. 'handle'=>$classname,
  56. 'send_to'=>$v,
  57. 'content'=>$this->content,
  58. 'status'=>0
  59. ];
  60. }
  61. (new Msg())->saveAll($insert);
  62. }
  63. //发送消息
  64. public function send(Msg $msg)
  65. {
  66. if($this->sendEvent($msg)){
  67. $msg->status=1;
  68. $msg->save();
  69. }else{
  70. $msg->status=-1;
  71. $msg->error=$this->getError();
  72. $msg->save();
  73. }
  74. }
  75. //读取消息,支持数字id或数组id
  76. public function read(mixed $msg_id)
  77. {
  78. if(is_array($msg_id)){
  79. Msg::where('id','in',$msg_id)->update(['status'=>2]);
  80. }else{
  81. Msg::where('id',$msg_id)->update(['status'=>2]);
  82. }
  83. }
  84. //读取全部消息
  85. public function readall(mixed $send_to)
  86. {
  87. Msg::where(['msg_type'=>$this->msg_type,'send_to'=>$send_to])->update(['status'=>2]);
  88. }
  89. /**
  90. * 获取消息列表,isread为null时获取全部消息,isread为true时获取已读消息,为false时获取未读消息
  91. * @param int $page
  92. * @param int $limit
  93. */
  94. public function list(mixed $send_to,int $page=1,int $limit=10,mixed $isread=null):array
  95. {
  96. $where=function ($query) use ($send_to,$isread){
  97. $query->where('msg_type','=',$this->msg_type);
  98. $query->where('send_to','=',$send_to);
  99. if($isread===null){
  100. $query->where('status','in',[1,2]);
  101. }
  102. if($isread===true){
  103. $query->where('status','=',2);
  104. }
  105. if($isread===false){
  106. $query->where('status','=',1);
  107. }
  108. };
  109. $list=Msg::where($where)->order('id desc')->limit(($page-1)*$limit,$limit)->select()->toArray();
  110. $r=[];
  111. foreach ($list as $v){
  112. $obj=[
  113. 'id'=>$v['id'],
  114. 'createtime'=>$v['createtime'],
  115. ];
  116. if(str_starts_with($v['content'],'{') && str_ends_with($v['content'],'}')){
  117. $obj=array_merge($obj,json_decode($v['content'],true));
  118. }else{
  119. $obj['content']=$v['content'];
  120. }
  121. $r[]=$obj;
  122. }
  123. return $r;
  124. }
  125. public function getError()
  126. {
  127. return $this->error;
  128. }
  129. }