You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
122 lines
3.7 KiB
122 lines
3.7 KiB
9 months ago
|
<?php
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
|
||
|
// +----------------------------------------------------------------------
|
||
|
// | Author: CRMEB Team <admin@crmeb.com>
|
||
|
// +----------------------------------------------------------------------
|
||
|
|
||
|
|
||
|
namespace crmeb\services\template\storage;
|
||
|
|
||
|
use app\common\repositories\system\notice\SystemNoticeConfigRepository;
|
||
|
use app\common\repositories\wechat\TemplateMessageRepository;
|
||
|
use crmeb\basic\BaseMessage;
|
||
|
use crmeb\services\WechatService;
|
||
|
use think\facade\Log;
|
||
|
|
||
|
class Wechat extends BaseMessage
|
||
|
{
|
||
|
/**
|
||
|
* @var WechatService
|
||
|
*/
|
||
|
protected $service;
|
||
|
protected $miniprogram;
|
||
|
|
||
|
/**
|
||
|
* 初始化
|
||
|
* @param array $config
|
||
|
* @return mixed|void
|
||
|
*/
|
||
|
protected function initialize(array $config)
|
||
|
{
|
||
|
parent::initialize($config); // TODO: Change the autogenerated stub
|
||
|
$this->service = WechatService::create();
|
||
|
$this->miniprogram = [
|
||
|
'appid' => systemConfig('routine_appId'),
|
||
|
];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* @param string $templateId
|
||
|
* @return mixed
|
||
|
*/
|
||
|
public function getTempId(string $templateId)
|
||
|
{
|
||
|
$tempkey = app()->make(SystemNoticeConfigRepository::class)->getSearch(['const_key' => $templateId])->with(['wechatTemplate'])->find();
|
||
|
return $tempkey['wechatTemplate']['tempid'];
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 发送消息
|
||
|
* @param string $templateId
|
||
|
* @param array $data
|
||
|
* @return bool|mixed
|
||
|
*/
|
||
|
public function send(string $templateId, array $data = [])
|
||
|
{
|
||
|
// $templateId = $this->getTemplateCode($templateId);
|
||
|
// if (!$templateId) {
|
||
|
// return ;
|
||
|
// //return $this->setError('Template number does not exist');
|
||
|
// }
|
||
|
$tempid = $this->getTempId($templateId);
|
||
|
if (!$tempid || !$this->openId) {
|
||
|
return ;
|
||
|
//return $this->setError('Template ID does not exist');
|
||
|
}
|
||
|
|
||
|
$miniprogram = [];
|
||
|
if ($this->miniprogram['appid']) $miniprogram = $this->miniprogram;
|
||
|
try {
|
||
|
$res = $this->service->sendTemplate($this->openId, $tempid, $data, $this->toUrl, $this->color,$miniprogram);
|
||
|
$this->clear();
|
||
|
return $res;
|
||
|
} catch (\Exception $e) {
|
||
|
Log::error('发送给openid为:' . $this->openId . '微信模板消息失败,模板id为:' . $tempid . ';错误原因为:' . $e->getMessage());
|
||
|
return $this->setError($e->getMessage());
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 获取所有模板
|
||
|
* @return \EasyWeChat\Support\Collection|mixed
|
||
|
*/
|
||
|
public function list()
|
||
|
{
|
||
|
return $this->service->noticeService()->getPrivateTemplates();
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 添加模板消息
|
||
|
* @param string $shortId
|
||
|
* @return \EasyWeChat\Support\Collection|mixed
|
||
|
*/
|
||
|
public function add(string $shortId)
|
||
|
{
|
||
|
return $this->service->noticeService()->addTemplate($shortId);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 删除模板消息
|
||
|
* @param string $templateId
|
||
|
* @return \EasyWeChat\Support\Collection|mixed
|
||
|
*/
|
||
|
public function delete(string $templateId)
|
||
|
{
|
||
|
return $this->service->noticeService()->deletePrivateTemplate($templateId);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 返回所有支持的行业列表
|
||
|
* @return \EasyWeChat\Support\Collection
|
||
|
*/
|
||
|
public function getIndustry()
|
||
|
{
|
||
|
return $this->service->noticeService()->getIndustry();
|
||
|
}
|
||
|
}
|