CustomNoticeListener.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. namespace app\listener\notice;
  3. use app\jobs\TemplateJob;
  4. use app\services\message\MessageSystemServices;
  5. use app\services\message\SystemNotificationServices;
  6. use app\services\serve\ServeServices;
  7. use app\services\user\UserServices;
  8. use app\services\wechat\WechatUserServices;
  9. use crmeb\interfaces\ListenerInterface;
  10. use crmeb\services\HttpService;
  11. use think\facade\Log;
  12. class CustomNoticeListener implements ListenerInterface
  13. {
  14. public function handle($event): void
  15. {
  16. [$uid, $infoData, $customTrigger] = $event;
  17. $notificationServices = app()->make(SystemNotificationServices::class);
  18. $noticeList = $notificationServices->getNotList(['custom_trigger' => $customTrigger]);
  19. foreach ($noticeList as $item) {
  20. if ($item['is_system'] == 1) $this->sendSystem($uid, $item, $infoData);
  21. if ($item['is_sms'] == 1) $this->sendSms($uid, $item, $infoData);
  22. if ($item['is_wechat'] == 1) $this->sendWechat($uid, $item, $infoData);
  23. if ($item['is_routine'] == 1) $this->sendRoutine($uid, $item, $infoData);
  24. if ($item['is_ent_wechat'] == 1) $this->sendEntWechat($uid, $item, $infoData);
  25. }
  26. }
  27. public function sendSystem($uid, $noticeData, $infoData)
  28. {
  29. try {
  30. $str = $noticeData['system_text'];
  31. preg_match_all('/\{(\w+)\}/', $str, $matches);
  32. $sendData = $matches[1];
  33. foreach ($sendData as $sendItem) {
  34. $str = str_replace("{" . $sendItem . "}", $infoData[$sendItem], $str);
  35. }
  36. $data = [];
  37. $data['mark'] = 'custom_notice';
  38. $data['uid'] = $uid;
  39. $data['content'] = $str;
  40. $data['title'] = $noticeData['system_title'];
  41. $data['type'] = 1;
  42. $data['add_time'] = time();
  43. $data['data'] = json_encode($sendData);
  44. /** @var MessageSystemServices $MessageSystemServices */
  45. $MessageSystemServices = app()->make(MessageSystemServices::class);
  46. $MessageSystemServices->save($data);
  47. } catch (\Exception $e) {
  48. Log::error('发送站内信失败,失败原因:' . $e->getMessage());
  49. return true;
  50. }
  51. }
  52. public function sendSms($uid, $noticeData, $infoData)
  53. {
  54. try {
  55. $smsType = ['yihaotong', 'aliyun', 'tencent'];
  56. $type = $smsType[sys_config('sms_type', 0)];
  57. $str = $noticeData['sms_text'];
  58. preg_match_all('/\{(\w+)\}/', $str, $matches);
  59. $smsData = $matches[1];
  60. $sendData = [];
  61. foreach ($smsData as $smsItem) {
  62. $sendData[$smsItem] = $infoData[$smsItem];
  63. }
  64. if ($type == 'tencent') {
  65. $sendData = array_values($sendData);
  66. }
  67. app()->make(ServeServices::class)->sms($type)->send($infoData['phone'], $noticeData['sms_id'], $sendData);
  68. return true;
  69. } catch (\Exception $e) {
  70. Log::error('发送短信失败,失败原因:' . $e->getMessage());
  71. return true;
  72. }
  73. }
  74. public function sendWechat($uid, $noticeData, $infoData)
  75. {
  76. try {
  77. $openid = '';
  78. $isDel = app()->make(UserServices::class)->value(['uid' => $uid], 'is_del');
  79. if (!$isDel) $openid = app()->make(WechatUserServices::class)->uidToOpenid($uid, 'wechat');
  80. if ($openid) {
  81. $wechatData = json_decode($noticeData['wechat_data'], true);
  82. $sendData = [];
  83. foreach ($wechatData as $wechatItem) {
  84. $sendData[$wechatItem['key']] = $infoData[$wechatItem['value']];
  85. }
  86. $link = $noticeData['wechat_link'];
  87. preg_match_all('/\{(\w+)\}/', $link, $matches);
  88. $linkData = $matches[1];
  89. foreach ($linkData as $linkItem) {
  90. $link = str_replace("{" . $linkItem . "}", $infoData[$linkItem], $link);
  91. }
  92. TemplateJob::dispatch('doJob', ['wechat', $openid, $noticeData['wechat_tempid'], $sendData, $link, null, $noticeData['wechat_to_routine']]);
  93. }
  94. return true;
  95. } catch (\Exception $e) {
  96. Log::error('发送微信模版消息失败,失败原因:' . $e->getMessage());
  97. return true;
  98. }
  99. }
  100. public function sendRoutine($uid, $noticeData, $infoData)
  101. {
  102. try {
  103. $openid = '';
  104. $isDel = app()->make(UserServices::class)->value(['uid' => $uid], 'is_del');
  105. if (!$isDel) $openid = app()->make(WechatUserServices::class)->uidToOpenid($uid, 'routine');
  106. if ($openid) {
  107. $routineData = json_decode($noticeData['routine_data'], true);
  108. $sendData = [];
  109. foreach ($routineData as $routineItem) {
  110. $sendData[$routineItem['key']] = $infoData[$routineItem['value']];
  111. }
  112. $link = $noticeData['routine_link'];
  113. preg_match_all('/\{(\w+)\}/', $link, $matches);
  114. $linkData = $matches[1];
  115. foreach ($linkData as $linkItem) {
  116. $link = str_replace("{" . $linkItem . "}", $infoData[$linkItem], $link);
  117. }
  118. TemplateJob::dispatch('doJob', ['subscribe', $openid, $noticeData['routine_tempid'], $sendData, $link, null]);
  119. }
  120. return true;
  121. } catch (\Exception $e) {
  122. Log::error('发送小程序订阅消息失败,失败原因:' . $e->getMessage());
  123. return true;
  124. }
  125. }
  126. public function sendEntWechat($uid, $noticeData, $infoData)
  127. {
  128. try {
  129. $str = $noticeData['ent_wechat_text'];
  130. preg_match_all('/\{(\w+)\}/', $str, $matches);
  131. $sendData = $matches[1];
  132. foreach ($sendData as $sendItem) {
  133. $str = str_replace("{" . $sendItem . "}", $infoData[$sendItem], $str);
  134. }
  135. $s = explode('\n', $str);
  136. $d = '';
  137. foreach ($s as $item) {
  138. $d .= $item . "\n>";
  139. }
  140. $d = substr($d, 0, strlen($d) - 2);
  141. HttpService::postRequest($noticeData['url'], json_encode([
  142. 'msgtype' => 'markdown',
  143. 'markdown' => ['content' => $d]
  144. ]));
  145. } catch (\Throwable $e) {
  146. Log::error('发送企业群消息失败,失败原因:' . $e->getMessage());
  147. }
  148. }
  149. }