BusinessMessageNoticeBase.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * Name: 芸众商城系统
  5. * Author: 广州市芸众信息科技有限公司
  6. * Profile: 广州市芸众信息科技有限公司位于国际商贸中心的广州,专注于移动电子商务生态系统打造,拥有芸众社交电商系统、区块链数字资产管理系统、供应链管理系统、电子合同等产品/服务。官网 :www.yunzmall.com www.yunzshop.com
  7. * Date: 2022/8/3
  8. * Time: 16:40
  9. */
  10. namespace business\common\notice;
  11. use business\common\models\MessageNotice;
  12. abstract class BusinessMessageNoticeBase
  13. {
  14. /**
  15. * @var MessageNotice
  16. */
  17. protected $messageNotice;
  18. protected $noticeIds;
  19. /**
  20. * @return mixed
  21. */
  22. final public function getNoticeIds()
  23. {
  24. return $this->noticeIds;
  25. }
  26. /**
  27. * @param mixed $noticeIds
  28. */
  29. final public function setNoticeIds($noticeIds): void
  30. {
  31. $this->noticeIds = $noticeIds;
  32. }
  33. final public function getMessageNotice()
  34. {
  35. return $this->messageNotice;
  36. }
  37. final public function setMessageNotice(MessageNotice $messageNotice)
  38. {
  39. $this->messageNotice = $messageNotice;
  40. }
  41. /**
  42. * @param array $userIdArray
  43. * @param array $param
  44. * @param string|mixed $html
  45. */
  46. protected function saveNotice(array $userIdArray, array $param, $html = '')
  47. {
  48. $createData = [
  49. 'param' => $param,
  50. 'html' => $html ?: '',
  51. ];
  52. $createData = array_merge($createData,$this->getMessageNotice()->getAttributes());
  53. $noticeIds = [];
  54. foreach ($userIdArray as $user_id) {
  55. $createData['recipient_id'] = $user_id;
  56. $notice = MessageNotice::create($createData);
  57. if ($notice) {
  58. $noticeIds[] = $notice->id;
  59. }
  60. }
  61. $this->setNoticeIds($noticeIds);
  62. //这里可以设置最后一个保存的消息记录
  63. // if($notice){
  64. // $this->setMessageNotice($notice);
  65. // }
  66. $this->sendSocket($noticeIds);
  67. //return $noticeIds;
  68. }
  69. final protected function sendSocket($noticeIds)
  70. {
  71. //这里可以直接让方法返回,
  72. $notices = MessageNotice::whereIn('id', $noticeIds)->get();
  73. // \Log::debug('-----webSocketNotice-----',$notices->pluck('id')->all());
  74. $signUserIds = $notices->map(function (MessageNotice $notice) {
  75. return $notice->hasOneRecipient->uid ?: null;
  76. })->filter()->unique()->values()->toArray();
  77. if (empty($signUserIds)) {
  78. return;
  79. }
  80. $firstNotice = $notices->first();
  81. $msg = $this->webSocketMessage($firstNotice);
  82. if (empty($msg)) {
  83. return;
  84. }
  85. if (!isset($msg['logo'])) {
  86. $shopSet = \Setting::get('shop.shop');
  87. $msg['logo'] = yz_tomedia($shopSet['logo']);
  88. }
  89. if (!isset($msg['notice_time'])) {
  90. $msg['notice_time'] = date('Y-m-d H:i:s', time());
  91. }
  92. // \Log::debug('-----webSocketNotice-----',$signUserIds);
  93. // \Log::debug('-----webSocketNotice-----',$msg);
  94. $this->webSocketNotice($signUserIds, $msg);
  95. }
  96. /**
  97. * 发送webSocket消息通知
  98. * @param $user_ids array 会员ID数组
  99. * @param $content mixed 文本内容
  100. * @param string $type
  101. */
  102. final protected function webSocketNotice($user_ids, $content, $type = 'businessMessageNotice')
  103. {
  104. \app\process\InnerSocket::send($user_ids, $content, $type);
  105. }
  106. public function sendStaff()
  107. {
  108. return $this->messageNotice->hasOneCreator;
  109. }
  110. //考虑到需要删除
  111. public function delete(){}
  112. /**
  113. * @param MessageNotice $notice
  114. * @return array [code,logo,jump_url,creator_name,notice_time,content,type]
  115. */
  116. abstract public function webSocketMessage(MessageNotice $notice);
  117. /**
  118. * @return mixed
  119. */
  120. abstract public function showBody();
  121. /**
  122. * @param array $data
  123. */
  124. abstract public function create($data);
  125. /**
  126. * 保持与服务容器绑定的标识一致
  127. * @return string
  128. */
  129. abstract public function getPlugin();
  130. /**
  131. * @return string
  132. */
  133. abstract public function getPluginName();
  134. /**
  135. * 消息类型集合
  136. * @return array
  137. */
  138. abstract public function getAllType();
  139. }