123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- <?php
- namespace ln\services\template\storage;
- use app\common\repositories\wechat\TemplateMessageRepository;
- use ln\basic\BaseMessage;
- use ln\services\WechatService;
- use think\facade\Log;
- class Wechat extends BaseMessage
- {
-
- protected $service;
-
- protected function initialize(array $config)
- {
- parent::initialize($config);
- $this->service = WechatService::create();
- }
-
- public function getTempId(string $templateId)
- {
- return app()->make(TemplateMessageRepository::class)->getTempId($templateId, 1);
- }
-
- public function send(string $templateId, array $data = [])
- {
- $templateId = $this->getTemplateCode($templateId);
- if (!$templateId) {
- return ;
-
- }
- $tempid = $this->getTempId($templateId);
- if (!$tempid) {
- return ;
-
- }
- if (!$this->openId) {
- return ;
-
- }
- try {
- $res = $this->service->sendTemplate($this->openId, $tempid, $data, $this->toUrl, $this->color);
- $this->clear();
- return $res;
- } catch (\Exception $e) {
- $this->isLog() && Log::error('发送给openid为:' . $this->openId . '微信模板消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
- return $this->setError($e->getMessage());
- }
- }
-
- public function list()
- {
- return $this->service->noticeService()->getPrivateTemplates();
- }
-
- public function add(string $shortId)
- {
- return $this->service->noticeService()->addTemplate($shortId);
- }
-
- public function delete(string $templateId)
- {
- return $this->service->noticeService()->deletePrivateTemplate($templateId);
- }
-
- public function getIndustry()
- {
- return $this->service->noticeService()->getIndustry();
- }
- }
|