WechatTemplateService.php 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Niucloud-admin 企业快速开发的saas管理平台
  4. // +----------------------------------------------------------------------
  5. // | 官方网址:https://www.niucloud.com
  6. // +----------------------------------------------------------------------
  7. // | niucloud团队 版权所有 开源版本可自由商用
  8. // +----------------------------------------------------------------------
  9. // | Author: Niucloud Team
  10. // +----------------------------------------------------------------------
  11. namespace app\service\admin\wechat;
  12. use app\dict\notice\NoticeTypeDict;
  13. use app\service\admin\notice\NoticeService;
  14. use app\service\core\notice\CoreNoticeService;
  15. use core\base\BaseAdminService;
  16. use core\exception\NoticeException;
  17. use core\template\TemplateLoader;
  18. use think\db\exception\DataNotFoundException;
  19. use think\db\exception\DbException;
  20. use think\db\exception\ModelNotFoundException;
  21. /**
  22. * easywechat主体提供
  23. * Class WechatConfigService
  24. * @package app\service\core\wechat
  25. */
  26. class WechatTemplateService extends BaseAdminService
  27. {
  28. /**
  29. * 同步微信公众号消息模板
  30. * @param array $keys
  31. * @return true
  32. * @throws DataNotFoundException
  33. * @throws DbException
  34. * @throws ModelNotFoundException
  35. */
  36. public function syncAll(array $keys = [])
  37. {
  38. $core_notice_service = new CoreNoticeService();
  39. $list = $core_notice_service->getList($keys);
  40. if (empty($list)) throw new NoticeException('NOTICE_TEMPLATE_NOT_EXIST');
  41. foreach ($list as $v) {
  42. $this->syncItem($v);
  43. }
  44. return true;
  45. }
  46. /**
  47. * @param $item
  48. * @return true
  49. */
  50. public function syncItem($item)
  51. {
  52. $key = $item[ 'key' ] ?? '';
  53. $wechat = $item[ 'wechat' ] ?? '';
  54. $temp_key = $wechat[ 'temp_key' ] ?? '';
  55. $keyword_name_list = $wechat[ 'keyword_name_list' ] ?? '';
  56. if (empty($temp_key)) $error = 'WECHAT_TEMPLATE_NEED_NO';
  57. $wechat_template_id = $item[ 'wechat_template_id' ];
  58. //删除原来的消息模板
  59. // (new CoreWechatTemplateService())->deletePrivateTemplate($this->site_id, $wechat_template_id);
  60. $template_loader = new TemplateLoader('wechat', [ 'site_id' => $this->site_id ]);
  61. $template_loader->delete([ 'templateId' => $wechat_template_id ]);
  62. //新的消息模板
  63. // $res = (new CoreWechatTemplateService())->addTemplate($this->site_id, $temp_key, $keyword_name_list);
  64. $res = $template_loader->addTemplate([ 'shortId' => $temp_key, 'keyword_name_list' => $keyword_name_list ]);
  65. $notice_service = new NoticeService();
  66. if (isset($res[ 'errcode' ]) && $res[ 'errcode' ] == 0) {
  67. //修改
  68. $notice_service->modify($key, 'wechat_template_id', $res[ 'template_id' ]);
  69. } else {
  70. throw new NoticeException($res[ 'errmsg' ]);
  71. }
  72. return true;
  73. }
  74. /**
  75. * 获取模板消息
  76. * @return array
  77. */
  78. public function getList()
  79. {
  80. $core_notice_service = new CoreNoticeService();
  81. $list = $core_notice_service->getList();
  82. $template = [];
  83. foreach ($list as $k => $v) {
  84. if (in_array(NoticeTypeDict::WECHAT, $v[ 'support_type' ])) $template[] = $v;
  85. }
  86. return $template;
  87. }
  88. }