SyncMessageJob.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. <?php
  2. namespace app\jobs\notice;
  3. use app\services\message\TemplateMessageServices;
  4. use crmeb\basic\BaseJobs;
  5. use crmeb\exceptions\AdminException;
  6. use crmeb\services\app\MiniProgramService;
  7. use crmeb\services\app\WechatService;
  8. use crmeb\traits\QueueTrait;
  9. use think\facade\Log;
  10. class SyncMessageJob extends BaseJobs
  11. {
  12. use QueueTrait;
  13. /**
  14. * 同步小程序订阅消息
  15. * @param $template
  16. * @return bool
  17. */
  18. public function syncSubscribe($template)
  19. {
  20. $errCode = [-1, 40001, 40002, 40013, 40125, 41002, 41004, 43104, 45009, 200011, 200012, 200014];
  21. /** @var TemplateMessageServices $templateMessageServices */
  22. $templateMessageServices = app()->make(TemplateMessageServices::class);
  23. if ($template['tempkey']) {
  24. if ($template['tempid']) {
  25. try {
  26. MiniProgramService::delSubscribeTemplate($template['tempid']);
  27. } catch (\Throwable $e) {
  28. $wechatErr = $e->getMessage();
  29. if (is_string($wechatErr)) {
  30. Log::error('删除旧订阅消息模版失败:' . $wechatErr);
  31. return true;
  32. }
  33. if (in_array($wechatErr->getCode(), $errCode)) {
  34. Log::error('删除旧订阅消息模版失败:' . $wechatErr->getCode());
  35. return true;
  36. }
  37. Log::error('删除旧订阅消息模版失败:' . $wechatErr->getMessage());
  38. return true;
  39. }
  40. }
  41. try {
  42. $works = MiniProgramService::getSubscribeTemplateKeyWords($template['tempkey']);
  43. } catch (\Throwable $e) {
  44. $wechatErr = $e->getMessage();
  45. if (is_string($wechatErr)) {
  46. Log::error('获取关键词列表失败:' . $wechatErr);
  47. return true;
  48. }
  49. if (in_array($wechatErr->getCode(), $errCode)) {
  50. Log::error('获取关键词列表失败:' . $wechatErr->getCode());
  51. return true;
  52. }
  53. Log::error('获取关键词列表失败:' . $wechatErr->getMessage());
  54. return true;
  55. }
  56. $kid = [];
  57. if ($works) {
  58. $works = array_combine(array_column($works, 'name'), $works);
  59. $content = is_array($template['content']) ? $template['content'] : explode("\n", $template['content']);
  60. foreach ($content as $c) {
  61. $name = explode('{{', $c)[0] ?? '';
  62. if ($name && isset($works[$name])) {
  63. $kid[] = $works[$name]['kid'];
  64. }
  65. }
  66. }
  67. if ($kid) {
  68. try {
  69. $tempid = MiniProgramService::addSubscribeTemplate($template['tempkey'], $kid, $template['name']);
  70. } catch (\Throwable $e) {
  71. $wechatErr = $e->getMessage();
  72. if (is_string($wechatErr)) {
  73. Log::error('添加订阅消息模版失败:' . $wechatErr);
  74. return true;
  75. }
  76. if (in_array($wechatErr->getCode(), $errCode)) {
  77. Log::error('添加订阅消息模版失败:' . $wechatErr->getCode());
  78. return true;
  79. }
  80. Log::error('添加订阅消息模版失败:' . $wechatErr->getMessage());
  81. return true;
  82. }
  83. $templateMessageServices->update($template['id'], ['tempid' => $tempid, 'kid' => json_encode($kid), 'add_time' => time()], 'id');
  84. return true;
  85. }
  86. }
  87. return true;
  88. }
  89. /**
  90. * 同步公众号模版消息
  91. * @param $template
  92. */
  93. public function syncWechat($template)
  94. {
  95. /** @var TemplateMessageServices $templateMessageServices */
  96. $templateMessageServices = app()->make(TemplateMessageServices::class);
  97. try {
  98. $res = WechatService::addTemplateId($template['tempkey']);
  99. } catch (\Throwable $e) {
  100. Log::error('同步模版消息失败:' . $e->getMessage());
  101. return true;
  102. }
  103. if(!$res->errcode && $res->template_id){
  104. $templateMessageServices->update($template['id'],['tempid'=>$res->template_id]);
  105. }
  106. }
  107. }