site_id; $core_notice_service = new CoreNoticeService(); $list = $core_notice_service->getList($site_id); $template = []; foreach ($list as $k => $v){ if(in_array(NoticeTypeDict::WEAPP, $v['support_type'])) $template[] = $v; } return $template; } /** * 同步微信公众号消息模板 * @param array $keys * @return true * @throws DataNotFoundException * @throws DbException * @throws ModelNotFoundException */ public function syncAll(array $keys = []){ $site_id = $this->site_id; $core_notice_service = new CoreNoticeService(); $list = $core_notice_service->getList($site_id, $keys); if(empty($list)) throw new NoticeException('NOTICE_TEMPLATE_NOT_EXIST'); foreach($list as $v){ $this->syncItem($v); } return true; } /** * @param $item * @return true */ public function syncItem($item){ $key = $item['key'] ?? ''; $weapp = $item['weapp'] ?? []; $tid = $weapp['tid'] ?? ''; if(empty($tid)) $error = 'WECHAT_TEMPLATE_NEED_NO'; $weapp_template_id = $item['weapp_template_id']; //删除原来的消息模板 $template_loader = (new TemplateLoader(NoticeTypeDict::WEAPP, ['site_id' => $this->site_id])); $template_loader->delete(['template_id' => $weapp_template_id ]); // (new CoreWeappTemplateService())->deleteTemplate($this->site_id, $weapp_template_id); //新的消息模板 $kid_list = $weapp['kid_list'] ?? []; $scene_desc = $weapp['scene_desc'] ?? ''; // $res = (new CoreWeappTemplateService())->addTemplate($this->site_id, $tid, $kid_list, $scene_desc); $res = $template_loader->addTemplate(['tid' => $tid, 'kid_list' => $kid_list, 'scene_desc' => $scene_desc ]); $notice_service = new NoticeService(); if (isset($res[ 'errcode' ]) && $res[ 'errcode' ] == 0) { //修改 $notice_service->modify($key, 'weapp_template_id', $res[ 'priTmplId' ]); } else { throw new NoticeException($res[ 'errmsg' ]); } return true; } }