From b172a13ec7bd532d27d061d628bd2f502d598fb8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E6=98=8A=E5=A4=A9?= <442384644@qq.com> Date: Thu, 24 Nov 2022 11:55:09 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=90=E7=A8=8B=E5=BA=8F=E7=9B=AE=E5=BD=95?= =?UTF-8?q?=E3=80=91=E5=90=8C=E6=AD=A5=E5=B0=8F=E7=A8=8B=E5=BA=8F=E8=AE=A2?= =?UTF-8?q?=E9=98=85=E6=B6=88=E6=81=AF=E5=92=8C=E5=85=AC=E4=BC=97=E5=8F=B7?= =?UTF-8?q?=E6=A8=A1=E7=89=88=E6=B6=88=E6=81=AF=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../application/routine/RoutineTemplate.php | 58 ++------- .../v1/application/wechat/WechatTemplate.php | 22 ++-- crmeb/app/jobs/notice/SyncMessageJob.php | 112 ++++++++++++++++++ .../crmeb/services/app/MiniProgramService.php | 42 ++++++- 4 files changed, 166 insertions(+), 68 deletions(-) create mode 100644 crmeb/app/jobs/notice/SyncMessageJob.php diff --git a/crmeb/app/adminapi/controller/v1/application/routine/RoutineTemplate.php b/crmeb/app/adminapi/controller/v1/application/routine/RoutineTemplate.php index 621548ad3..01f635f8b 100644 --- a/crmeb/app/adminapi/controller/v1/application/routine/RoutineTemplate.php +++ b/crmeb/app/adminapi/controller/v1/application/routine/RoutineTemplate.php @@ -10,6 +10,7 @@ // +---------------------------------------------------------------------- namespace app\adminapi\controller\v1\application\routine; +use app\jobs\notice\SyncMessageJob; use app\services\other\QrcodeServices; use app\services\message\TemplateMessageServices; use app\services\system\attachment\SystemAttachmentServices; @@ -54,61 +55,16 @@ class RoutineTemplate extends AuthController throw new AdminException(400236); } $all = $this->services->getTemplateList(['status' => 1, 'type' => 0]); - $errData = []; - $errCode = [-1, 40001, 40002, 40013, 40125, 41002, 41004, 43104, 45009, 200011, 200012, 200014]; + $list = MiniProgramService::getSubscribeTemplateList(); + foreach ($list->data as $v) { + MiniProgramService::delSubscribeTemplate($v['priTmplId']); + } if ($all['list']) { - $time = time(); foreach ($all['list'] as $template) { - if ($template['tempkey']) { - if (!isset($template['kid'])) { - return app('json')->fail('数据库模版表(template_message)缺少字段:kid'); - } - if (isset($template['kid']) && $template['kid']) { - continue; - } - $works = []; - try { - $works = MiniProgramService::getSubscribeTemplateKeyWords($template['tempkey']); - } catch (\Throwable $e) { - $wechatErr = $e->getMessage(); - if (is_string($wechatErr)) throw new AdminException($wechatErr); - if (in_array($wechatErr->getCode(), $errCode)) { - throw new AdminException($wechatErr->getCode()); - } - $errData[1] = '获取关键词列表失败:' . $wechatErr->getMessage(); - } - $kid = []; - if ($works) { - $works = array_combine(array_column($works, 'name'), $works); - $content = is_array($template['content']) ? $template['content'] : explode("\n", $template['content']); - foreach ($content as $c) { - $name = explode('{{', $c)[0] ?? ''; - if ($name && isset($works[$name])) { - $kid[] = $works[$name]['kid']; - } - } - } - if ($kid && isset($template['kid']) && !$template['kid']) { - $tempid = ''; - try { - $tempid = MiniProgramService::addSubscribeTemplate($template['tempkey'], $kid, $template['name']); - } catch (\Throwable $e) { - $wechatErr = $e->getMessage(); - if (is_string($wechatErr)) throw new AdminException($wechatErr); - if (in_array($wechatErr->getCode(), $errCode)) { - throw new AdminException($wechatErr->getCode()); - } - $errData[2] = '添加订阅消息模版失败:' . $wechatErr->getMessage(); - } - if ($tempid != $template['tempid']) { - $this->services->update($template['id'], ['tempid' => $tempid, 'kid' => json_encode($kid), 'add_time' => $time], 'id'); - } - } - } + SyncMessageJob::dispatchDo('SyncSubscribe', [$template]); } } - $msg = $errData ? implode('\n', $errData) : '同步成功'; - return app('json')->success($msg); + return app('json')->success(100038); } /** diff --git a/crmeb/app/adminapi/controller/v1/application/wechat/WechatTemplate.php b/crmeb/app/adminapi/controller/v1/application/wechat/WechatTemplate.php index 271a34cf8..f7247f614 100644 --- a/crmeb/app/adminapi/controller/v1/application/wechat/WechatTemplate.php +++ b/crmeb/app/adminapi/controller/v1/application/wechat/WechatTemplate.php @@ -11,6 +11,7 @@ namespace app\adminapi\controller\v1\application\wechat; use app\adminapi\controller\AuthController; +use app\jobs\notice\SyncMessageJob; use crmeb\exceptions\AdminException; use app\services\message\TemplateMessageServices; use crmeb\services\app\WechatService; @@ -47,23 +48,14 @@ class WechatTemplate extends AuthController if (!sys_config('wechat_appid') || !sys_config('wechat_appsecret')) { throw new AdminException(400248); } - $tempall = $this->services->getTemplateList(['status' => 1, 'type' => 1]); - //获取微信平台已经添加的模版 - $list = WechatService::getPrivateTemplates();//获取所有模版 - foreach ($list->template_list as $v) - { - //删除已有模版 + $all = $this->services->getTemplateList(['status' => 1, 'type' => 1]); + $list = WechatService::getPrivateTemplates(); + foreach ($list->template_list as $v) { WechatService::deleleTemplate($v['template_id']); } - - foreach ($tempall['list'] as $temp) - { - //添加模版消息 - $res = WechatService::addTemplateId($temp['tempkey']); - if(!$res->errcode && $res->template_id){ - $this->services->update($temp['id'],['tempid'=>$res->template_id]); - } + foreach ($all['list'] as $template) { + SyncMessageJob::dispatchDo('SyncWechat', [$template]); } - return app('json')->success(400249); + return app('json')->success(100038); } } diff --git a/crmeb/app/jobs/notice/SyncMessageJob.php b/crmeb/app/jobs/notice/SyncMessageJob.php new file mode 100644 index 000000000..b5ea31407 --- /dev/null +++ b/crmeb/app/jobs/notice/SyncMessageJob.php @@ -0,0 +1,112 @@ +make(TemplateMessageServices::class); + if ($template['tempkey']) { + if ($template['tempid']) { + try { + MiniProgramService::delSubscribeTemplate($template['tempid']); + } catch (\Throwable $e) { + $wechatErr = $e->getMessage(); + if (is_string($wechatErr)) { + Log::error('删除旧订阅消息模版失败:' . $wechatErr); + return true; + } + if (in_array($wechatErr->getCode(), $errCode)) { + Log::error('删除旧订阅消息模版失败:' . $wechatErr->getCode()); + return true; + } + Log::error('删除旧订阅消息模版失败:' . $wechatErr->getMessage()); + return true; + } + } + try { + $works = MiniProgramService::getSubscribeTemplateKeyWords($template['tempkey']); + } catch (\Throwable $e) { + $wechatErr = $e->getMessage(); + if (is_string($wechatErr)) { + Log::error('获取关键词列表失败:' . $wechatErr); + return true; + } + if (in_array($wechatErr->getCode(), $errCode)) { + Log::error('获取关键词列表失败:' . $wechatErr->getCode()); + return true; + } + Log::error('获取关键词列表失败:' . $wechatErr->getMessage()); + return true; + } + $kid = []; + if ($works) { + $works = array_combine(array_column($works, 'name'), $works); + $content = is_array($template['content']) ? $template['content'] : explode("\n", $template['content']); + foreach ($content as $c) { + $name = explode('{{', $c)[0] ?? ''; + if ($name && isset($works[$name])) { + $kid[] = $works[$name]['kid']; + } + } + } + if ($kid) { + try { + $tempid = MiniProgramService::addSubscribeTemplate($template['tempkey'], $kid, $template['name']); + } catch (\Throwable $e) { + $wechatErr = $e->getMessage(); + if (is_string($wechatErr)) { + Log::error('添加订阅消息模版失败:' . $wechatErr); + return true; + } + if (in_array($wechatErr->getCode(), $errCode)) { + Log::error('添加订阅消息模版失败:' . $wechatErr->getCode()); + return true; + } + Log::error('添加订阅消息模版失败:' . $wechatErr->getMessage()); + return true; + } + $templateMessageServices->update($template['id'], ['tempid' => $tempid, 'kid' => json_encode($kid), 'add_time' => time()], 'id'); + return true; + } + } + return true; + } + + /** + * 同步公众号模版消息 + * @param $template + */ + public function syncWechat($template) + { + /** @var TemplateMessageServices $templateMessageServices */ + $templateMessageServices = app()->make(TemplateMessageServices::class); + try { + $res = WechatService::addTemplateId($template['tempkey']); + } catch (\Throwable $e) { + Log::error('同步模版消息失败:' . $e->getMessage()); + return true; + } + if(!$res->errcode && $res->template_id){ + $templateMessageServices->update($template['id'],['tempid'=>$res->template_id]); + } + } +} \ No newline at end of file diff --git a/crmeb/crmeb/services/app/MiniProgramService.php b/crmeb/crmeb/services/app/MiniProgramService.php index 44ab81890..d91855af3 100644 --- a/crmeb/crmeb/services/app/MiniProgramService.php +++ b/crmeb/crmeb/services/app/MiniProgramService.php @@ -16,6 +16,7 @@ use crmeb\services\SystemConfigService; use app\services\pay\PayNotifyServices; use crmeb\services\easywechat\Application; use EasyWeChat\Payment\Order; +use think\facade\Log; use think\facade\Route as Url; use crmeb\utils\Hook; use think\facade\Cache; @@ -252,13 +253,37 @@ class MiniProgramService if (isset($res['errcode']) && $res['errcode'] == 0 && isset($res['priTmplId'])) { return $res['priTmplId']; } else { - throw new AdminException($res['errmsg']); + Log::error('添加订阅消息模版失败:' . $res['errmsg']); } } catch (\Throwable $e) { - throw new AdminException($e); + Log::error('添加订阅消息模版失败:' . $e->getMessage()); } + return true; } + /** + * 删除订阅消息模版 + * @param string $tid + * @param array $kidList + * @param string $sceneDesc + * @return mixed + */ + public static function delSubscribeTemplate(string $priTmplId) + { + try { + $res = self::SubscribenoticeService()->delTemplate($priTmplId); + if (isset($res['errcode']) && $res['errcode'] == 0) { + return true; + } else { + Log::error('删除订阅消息模版失败:' . $res['errmsg']); + } + } catch (\Throwable $e) { + Log::error('删除订阅消息模版失败:' . $e->getMessage()); + } + return true; + } + + /** * 获取模版标题的关键词列表 * @param string $tid @@ -278,6 +303,19 @@ class MiniProgramService } } + /** + * 获取订阅消息列表 + * @return mixed + */ + public static function getSubscribeTemplateList() + { + try { + return self::SubscribenoticeService()->getTemplateList(); + } catch (\Exception $e) { + throw new AdminException($e->getMessage()); + } + } + /** * 支付 * @return \EasyWeChat\Payment\Payment