WeappTemplateService.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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\weapp;
  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 WeappTemplateService
  24. * @package app\service\core\weapp
  25. */
  26. class WeappTemplateService extends BaseAdminService
  27. {
  28. /**
  29. * 获取订阅消息
  30. * @return array
  31. */
  32. public function getList()
  33. {
  34. $core_notice_service = new CoreNoticeService();
  35. $addon_list = $core_notice_service->getAddonList();
  36. foreach ($addon_list as &$addon) {
  37. foreach ($addon['notice'] as $k => $v) {
  38. if (!in_array(NoticeTypeDict::WEAPP, $v[ 'support_type' ])) {
  39. unset($addon['notice'][$k]);
  40. }
  41. }
  42. $addon['notice'] = array_values($addon['notice']);
  43. }
  44. return $addon_list;
  45. }
  46. /**
  47. * 同步微信公众号消息模板
  48. * @param array $keys
  49. * @return true
  50. * @throws DataNotFoundException
  51. * @throws DbException
  52. * @throws ModelNotFoundException
  53. */
  54. public function syncAll(array $keys = []){
  55. $core_notice_service = new CoreNoticeService();
  56. $list = $core_notice_service->getList($keys);
  57. if(empty($list)) throw new NoticeException('NOTICE_TEMPLATE_NOT_EXIST');
  58. foreach($list as $v){
  59. $this->syncItem($v);
  60. }
  61. return true;
  62. }
  63. /**
  64. * @param $item
  65. * @return true
  66. */
  67. public function syncItem($item){
  68. $key = $item['key'] ?? '';
  69. $weapp = $item['weapp'] ?? [];
  70. $tid = $weapp['tid'] ?? '';
  71. if(empty($tid)) $error = 'WECHAT_TEMPLATE_NEED_NO';
  72. $weapp_template_id = $item['weapp_template_id'];
  73. //删除原来的消息模板
  74. $template_loader = (new TemplateLoader(NoticeTypeDict::WEAPP, ['site_id' => $this->site_id]));
  75. $template_loader->delete(['template_id' => $weapp_template_id ]);
  76. // (new CoreWeappTemplateService())->deleteTemplate($this->site_id, $weapp_template_id);
  77. //新的消息模板
  78. $kid_list = $weapp['kid_list'] ?? [];
  79. $scene_desc = $weapp['scene_desc'] ?? '';
  80. // $res = (new CoreWeappTemplateService())->addTemplate($this->site_id, $tid, $kid_list, $scene_desc);
  81. $res = $template_loader->addTemplate(['tid' => $tid, 'kid_list' => $kid_list, 'scene_desc' => $scene_desc ]);
  82. $notice_service = new NoticeService();
  83. if (isset($res[ 'errcode' ]) && in_array($res[ 'errcode' ], [0, 200022])) {
  84. //修改
  85. $notice_service->modify($key, 'weapp_template_id', $res[ 'priTmplId' ]);
  86. } else {
  87. throw new NoticeException($res[ 'errmsg' ]);
  88. }
  89. return true;
  90. }
  91. }