SyncMessageJob.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. namespace app\jobs\notice;
  3. use app\services\message\SystemNotificationServices;
  4. use crmeb\basic\BaseJobs;
  5. use crmeb\services\app\MiniProgramService;
  6. use crmeb\services\app\WechatService;
  7. use crmeb\traits\QueueTrait;
  8. use think\facade\Log;
  9. class SyncMessageJob extends BaseJobs
  10. {
  11. use QueueTrait;
  12. /**
  13. * 同步小程序订阅消息
  14. * @param $template
  15. * @return bool
  16. */
  17. public function syncSubscribe($key, $data)
  18. {
  19. $works = MiniProgramService::getSubscribeTemplateKeyWords($key);
  20. $kid = [];
  21. if ($works) {
  22. $works = array_combine(array_column($works, 'name'), $works);
  23. $content = is_array($data['routine_content']) ? $data['routine_content'] : explode("\n", $data['routine_content']);
  24. foreach ($content as $c) {
  25. $name = explode('{{', $c)[0] ?? '';
  26. if ($name && isset($works[$name])) {
  27. $kid[] = $works[$name]['kid'];
  28. }
  29. }
  30. }
  31. if ($kid) {
  32. try {
  33. $tempid = MiniProgramService::addSubscribeTemplate($key, $kid, $data['name']);
  34. } catch (\Throwable $e) {
  35. Log::error('同步订阅消息失败:' . $e->getMessage());
  36. return true;
  37. }
  38. app()->make(SystemNotificationServices::class)->update(['routine_tempkey' => $key], ['routine_tempid' => $tempid, 'routine_kid' => json_encode($kid)]);
  39. return true;
  40. }
  41. return true;
  42. }
  43. /**
  44. * 同步公众号模版消息
  45. * @param $key
  46. * @param $content
  47. * @return bool
  48. * @author: 吴汐
  49. * @email: 442384644@qq.com
  50. * @date: 2023/8/16
  51. */
  52. public function syncWechat($key, $content)
  53. {
  54. $content = is_array($content) ? $content : explode("\n", $content);
  55. $name = [];
  56. foreach ($content as $c) {
  57. $name[] = explode('{{', $c)[0] ?? '';
  58. }
  59. try {
  60. $res = WechatService::addTemplateId($key, $name);
  61. } catch (\Throwable $e) {
  62. Log::error('同步模版消息失败:' . $e->getMessage());
  63. return true;
  64. }
  65. if (!$res->errcode && $res->template_id) {
  66. app()->make(SystemNotificationServices::class)->update(['wechat_tempkey' => $key], ['wechat_tempid' => $res->template_id]);
  67. }
  68. return true;
  69. }
  70. }