diff --git a/crmeb/crmeb/utils/ProgramProvider.php b/crmeb/crmeb/utils/ProgramProvider.php new file mode 100644 index 00000000..743c98e6 --- /dev/null +++ b/crmeb/crmeb/utils/ProgramProvider.php @@ -0,0 +1,30 @@ + '', + 'template_id' => '', + 'page' => '', + 'data' => [], + ]; + + /** + * Message backup. + * + * @var array + */ + protected $messageBackup; + + protected $required = ['template_id', 'touser']; + + /** + * ProgramSubscribeService constructor. + * @param AccessToken $accessToken + */ + public function __construct(AccessToken $accessToken) + { + parent::__construct($accessToken); + + $this->messageBackup = $this->message; + + } + + /** + * 获取当前拥有的模板列表 + * @return \EasyWeChat\Support\Collection|null + * @throws \EasyWeChat\Core\Exceptions\HttpException + */ + public function getTemplateList() + { + return $this->parseJSON('get', [self::API_GET_TEMPLATE_LIST]); + } + + /** + * 获取公众模板列表 + * @param string $ids + * @param int $start + * @param int $limit + * @return \EasyWeChat\Support\Collection|null + * @throws \EasyWeChat\Core\Exceptions\HttpException + */ + public function getPublicTemplateList(string $ids, int $start = 0, int $limit = 10) + { + $params = [ + 'ids' => $ids, + 'start' => $start, + 'limit' => $limit + ]; + return $this->parseJSON('get', [self::API_GET_PUBLIC_TEMPLATE, $params]); + } + + /** + * 获取模板分类 + * @return \EasyWeChat\Support\Collection|null + * @throws \EasyWeChat\Core\Exceptions\HttpException + */ + public function getTemplateCate() + { + return $this->parseJSON('get', [self::API_GET_TEMPLATE_CATE]); + } + + /** + * 获取模板标题下的关键词列表 + * @param string $tid 模板标题 id,可通过接口获取 + * @return \EasyWeChat\Support\Collection|null + * @throws \EasyWeChat\Core\Exceptions\HttpException + */ + public function getPublicTemplateKeywords(string $tid) + { + $params = [ + 'tid' => $tid + ]; + return $this->parseJSON('get', [self::API_GET_TEMPLATE_KEYWORKS, $params]); + } + + /** + * 添加订阅模板消息 + * @param string $tid 模板标题 id,可通过接口获取,也可登录小程序后台查看获取 + * @param array $kidList 模板序列号 关键词顺序可以自由搭配(例如 [3,5,4] 或 [4,5,3]),最多支持5个,最少2个关键词组合 + * @param string $sceneDesc 服务场景描述,15个字以内 + * @return \EasyWeChat\Support\Collection|null + * @throws \EasyWeChat\Core\Exceptions\HttpException + */ + public function addTemplate(string $tid, array $kidList, string $sceneDesc = '') + { + $params = [ + 'tid' => $tid, + 'kidList' => $kidList, + 'sceneDesc' => $sceneDesc, + ]; + return $this->parseJSON('json', [self::API_SET_TEMPLATE_ADD, $params]); + } + + /** + * 删除模板消息 + * @param string $priTmplId + * @return \EasyWeChat\Support\Collection|null + * @throws \EasyWeChat\Core\Exceptions\HttpException + */ + public function delTemplate(string $priTmplId) + { + $params = [ + 'priTmplId' => $priTmplId + ]; + return $this->parseJSON('json', [self::API_SET_TEMPLATE_DEL, $params]); + } + + /** + * 发送订阅消息 + * @param array $data + * @return \EasyWeChat\Support\Collection|null + * @throws InvalidArgumentException + * @throws \EasyWeChat\Core\Exceptions\HttpException + */ + public function send(array $data = []) + { + $params = array_merge($this->message, $data); + + foreach ($params as $key => $value) { + if (in_array($key, $this->required, true) && empty($value) && empty($this->message[$key])) { + throw new InvalidArgumentException("Attribute '$key' can not be empty!"); + } + + $params[$key] = empty($value) ? $this->message[$key] : $value; + } + + $params['data'] = $this->formatData($params['data']); + + $this->message = $this->messageBackup; + + return $this->parseJSON('json', [self::API_SUBSCRIBE_SEND, $params]); + } + + /** + * TODO: 未实现 + * @param $data + * @return mixed + */ + protected function formatData($data) + { + return $data; + } + + + /** + * Magic access.. + * + * @param $method + * @param $args + * @return $this + */ + public function __call($method, $args) + { + $map = [ + 'template' => 'template_id', + 'templateId' => 'template_id', + 'uses' => 'template_id', + 'to' => 'touser', + 'receiver' => 'touser', + 'url' => 'url', + 'link' => 'url', + 'data' => 'data', + 'with' => 'data', + ]; + + if (0 === stripos($method, 'with') && strlen($method) > 4) { + $method = lcfirst(substr($method, 4)); + } + + if (0 === stripos($method, 'and')) { + $method = lcfirst(substr($method, 3)); + } + + if (isset($map[$method])) { + $this->message[$map[$method]] = array_shift($args); + } + + return $this; + } + +} \ No newline at end of file diff --git a/crmeb/vendor/overtrue/wechat/src/Foundation/Application.php b/crmeb/vendor/overtrue/wechat/src/Foundation/Application.php index 6244f9fc..bf383877 100644 --- a/crmeb/vendor/overtrue/wechat/src/Foundation/Application.php +++ b/crmeb/vendor/overtrue/wechat/src/Foundation/Application.php @@ -106,6 +106,7 @@ class Application extends Container ServiceProviders\OpenPlatformServiceProvider::class, ServiceProviders\MiniProgramServiceProvider::class, ServiceProviders\CommentServiceProvider::class, + \crmeb\utils\ProgramProvider::class, ]; /**