From 07df6767b4174c3227e0afed13cf1234d125b097 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 30 Apr 2020 22:25:50 +0800 Subject: [PATCH] add clear --- wchat/base/Subject.php | 143 ++++++++++++++++++++++++++++++++++++ wchat/base/Template.php | 155 ++++++++++++++++++++++++++++++++++++++++ wchat/common/Result.php | 2 +- wchat/qq/Subject.php | 128 +++------------------------------ wchat/qq/Template.php | 139 ++--------------------------------- wchat/wx/Subject.php | 127 ++------------------------------ wchat/wx/Template.php | 142 ++---------------------------------- 7 files changed, 325 insertions(+), 511 deletions(-) create mode 100644 wchat/base/Subject.php create mode 100644 wchat/base/Template.php diff --git a/wchat/base/Subject.php b/wchat/base/Subject.php new file mode 100644 index 0000000..b04d848 --- /dev/null +++ b/wchat/base/Subject.php @@ -0,0 +1,143 @@ +keywords = $keywords; + } + + /** + * @param string $templateId + */ + public function setTemplateId(string $templateId) + { + $this->templateId = $templateId; + } + + /** + * @param string $openId + */ + public function setOpenId(string $openId) + { + $this->openId = $openId; + } + + /** + * @param string $page + */ + public function setPage(string $page) + { + $this->page = $page; + } + + /** + * @param string $emphasis_keyword + */ + public function setEmphasisKeyword(string $emphasis_keyword) + { + $this->emphasis_keyword = $emphasis_keyword; + } + + /** + * @param $index + * @param $context + * @param $color + */ + public function replaceKeyword($index, $context, $color = '') + { + if (empty($color)) { + $color = '#000'; + } + $this->keywords['keyword' . $index] = [ + 'value' => $context, + 'color' => $color + ]; + } + + + /** + * @param $color + * @param $context + */ + public function addKeyword($context, $color = null) + { + if (empty($color)) { + $color = '#000'; + } + $this->keywords['keyword' . (count($this->keywords) + 1)] = [ + 'value' => $context, + 'color' => $color + ]; + } + + abstract public function getUrl(): string; + + /** + * @return Result + * @throws \Exception + */ + public function sendTemplate() + { + $url = $this->getUrl() . '?access_token=' . $this->config->getAccessToken(); + $params = [ + "touser" => $this->openId, + "template_id" => $this->templateId, + "page" => $this->page, + "data" => $this->keywords, + ]; + + if (!empty($this->emphasis_keyword)) { + $params['emphasis_keyword'] = $this->emphasis_keyword; + } + + $params = json_encode($params, JSON_UNESCAPED_UNICODE); + + $this->request->setIsSSL(true); + $this->request->addHeader('content-type', 'application/json'); + + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $result = $this->request->post($url, $params); + $result->append('postBody', $params); + + return $this->reset($result); + } + + /** + * @param $result + * @return mixed + */ + public function reset($result) + { + $this->openId = ''; + $this->keywords = []; + $this->templateId = ''; + $this->page = ''; + + return $result; + } + +} diff --git a/wchat/base/Template.php b/wchat/base/Template.php new file mode 100644 index 0000000..95e29fc --- /dev/null +++ b/wchat/base/Template.php @@ -0,0 +1,155 @@ +keywords = $keywords; + } + + /** + * @param string $templateId + */ + public function setTemplateId(string $templateId) + { + $this->templateId = $templateId; + } + + /** + * @param string $formId + */ + public function setFormId(string $formId) + { + $this->formId = $formId; + } + + /** + * @param string $openId + */ + public function setOpenId(string $openId) + { + $this->openId = $openId; + } + + /** + * @param string $page + */ + public function setPage(string $page) + { + $this->page = $page; + } + + /** + * @param string $emphasis_keyword + */ + public function setEmphasisKeyword(string $emphasis_keyword) + { + $this->emphasis_keyword = $emphasis_keyword; + } + + /** + * @param $index + * @param $context + * @param $color + */ + public function replaceKeyword($index, $context, $color = '') + { + if (empty($color)) { + $color = '#000'; + } + $this->keywords['keyword' . $index] = [ + 'value' => $context, + 'color' => $color + ]; + } + + + /** + * @param $color + * @param $context + */ + public function addKeyword($context, $color = null) + { + if (empty($color)) { + $color = '#000'; + } + $this->keywords['keyword' . (count($this->keywords) + 1)] = [ + 'value' => $context, + 'color' => $color + ]; + } + + abstract public function getUrl(): string; + + /** + * @return Result + * @throws \Exception + * + * 奴隶交易通知 + */ + public function sendTemplate() + { + $access_token = $this->config->getAccessToken(); + if (empty($access_token)) { + throw new \Exception('request access_token con\'t null.'); + } + + $url = $this->getUrl() . '?access_token=' . $access_token; + $params = [ + "touser" => $this->openId, + "template_id" => $this->templateId, + "page" => $this->page, + "form_id" => $this->formId, + "data" => $this->keywords, + ]; + + if (!empty($this->emphasis_keyword)) { + $params['emphasis_keyword'] = $this->emphasis_keyword; + } + $params = json_encode($params, JSON_UNESCAPED_UNICODE); + $this->request->setIsSSL(true); + $this->request->addHeader('content-type', 'application/json'); + $this->request->setErrorField('errcode'); + $this->request->setErrorMsgField('errmsg'); + $result = $this->request->post($url, $params); + $result->append('postBody', $params); + + return $this->reset($result); + } + + /** + * @param $result + * @return mixed + */ + private function reset($result) + { + $this->openId = ''; + $this->keywords = []; + $this->formId = ''; + $this->templateId = ''; + $this->page = ''; + + return $result; + } + +} diff --git a/wchat/common/Result.php b/wchat/common/Result.php index bf25a47..29641e6 100644 --- a/wchat/common/Result.php +++ b/wchat/common/Result.php @@ -152,7 +152,7 @@ class Result public function append($key, $data) { if (!is_array($this->data)) { - $this->data = []; + $this->data = ['origin' => $this->data]; } $this->data[$key] = $data; return $this; diff --git a/wchat/qq/Subject.php b/wchat/qq/Subject.php index ba84fea..210bdcf 100644 --- a/wchat/qq/Subject.php +++ b/wchat/qq/Subject.php @@ -6,133 +6,21 @@ namespace wchat\qq; use wchat\common\Result; -class Subject extends SmallProgram +/** + * Class Subject + * @package wchat\qq + */ +class Subject extends \wchat\base\Subject { - - private $keywords = []; - private $templateId = ''; - private $openId = ''; - private $defaultUrl = ''; - private $page = 'pages/index/index'; - private $emphasis_keyword = ''; - private $sendUrl = 'https://api.q.qq.com/api/json/subscribe/SendSubscriptionMessage'; - /** - * @param array $keywords + * @return string */ - public function setKeywords(array $keywords) + public function getUrl(): string { - $this->keywords = $keywords; + return $this->getUrl(); } - /** - * @param string $templateId - */ - public function setTemplateId(string $templateId) - { - $this->templateId = $templateId; - } - - /** - * @param string $openId - */ - public function setOpenId(string $openId) - { - $this->openId = $openId; - } - - /** - * @param string $defaultUrl - */ - public function setDefaultUrl(string $defaultUrl) - { - $this->defaultUrl = $defaultUrl; - } - - /** - * @param string $page - */ - public function setPage(string $page) - { - $this->page = $page; - } - - /** - * @param string $emphasis_keyword - */ - public function setEmphasisKeyword(string $emphasis_keyword) - { - $this->emphasis_keyword = $emphasis_keyword; - } - - /** - * @param $index - * @param $context - * @param $color - */ - public function replaceKeyword($index, $context, $color = '') - { - if (empty($color)) { - $color = '#000'; - } - $this->keywords['keyword' . $index] = [ - 'value' => $context, - 'color' => $color - ]; - } - - - /** - * @param $color - * @param $context - */ - public function addKeyword($context, $color = null) - { - if (empty($color)) { - $color = '#000'; - } - $this->keywords['keyword' . (count($this->keywords) + 1)] = [ - 'value' => $context, - 'color' => $color - ]; - } - - /** - * @return Result - * @throws \Exception - * - * 奴隶交易通知 - */ - public function sendTemplate() - { - $url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken(); - $params = [ - "touser" => $this->openId, - "template_id" => $this->templateId, - "page" => $this->page, - "data" => $this->keywords, - ]; - - if (!empty($this->emphasis_keyword)) { - $params['emphasis_keyword'] = $this->emphasis_keyword; - } - - $params = json_encode($params, JSON_UNESCAPED_UNICODE); - - $this->request->setIsSSL(true); - $this->request->addHeader('content-type', 'application/json'); - - $result = $this->request->post($url, $params); - $result->append('postBody', $params); - - $this->openId = ''; - $this->keywords = []; - $this->templateId = ''; - $this->page = ''; - - return $result; - } } diff --git a/wchat/qq/Template.php b/wchat/qq/Template.php index 694ce2a..b2f3677 100644 --- a/wchat/qq/Template.php +++ b/wchat/qq/Template.php @@ -11,143 +11,18 @@ namespace wchat\qq; use wchat\common\Result; -class Template extends SmallProgram +/** + * Class Template + * @package wchat\qq + */ +class Template extends \wchat\base\Template { - private $keywords = []; - private $templateId = ''; - private $formId = ''; - private $openId = ''; - private $defaultUrl = ''; - private $page = 'pages/index/index'; - private $emphasis_keyword = ''; - private $sendUrl = 'https://api.q.qq.com/api/json/template/send'; - - /** - * @param array $keywords - */ - public function setKeywords(array $keywords) + public function getUrl(): string { - $this->keywords = $keywords; + return $this->sendUrl; } - /** - * @param string $templateId - */ - public function setTemplateId(string $templateId) - { - $this->templateId = $templateId; - } - - /** - * @param string $formId - */ - public function setFormId(string $formId) - { - $this->formId = $formId; - } - - /** - * @param string $openId - */ - public function setOpenId(string $openId) - { - $this->openId = $openId; - } - - /** - * @param string $defaultUrl - */ - public function setDefaultUrl(string $defaultUrl) - { - $this->defaultUrl = $defaultUrl; - } - - /** - * @param string $page - */ - public function setPage(string $page) - { - $this->page = $page; - } - - /** - * @param string $emphasis_keyword - */ - public function setEmphasisKeyword(string $emphasis_keyword) - { - $this->emphasis_keyword = $emphasis_keyword; - } - - /** - * @param $index - * @param $context - * @param $color - */ - public function replaceKeyword($index, $context, $color = '') - { - if (empty($color)) { - $color = '#000'; - } - $this->keywords['keyword' . $index] = [ - 'value' => $context, - 'color' => $color - ]; - } - - - /** - * @param $color - * @param $context - */ - public function addKeyword($context, $color = null) - { - if (empty($color)) { - $color = '#000'; - } - $this->keywords['keyword' . (count($this->keywords) + 1)] = [ - 'value' => $context, - 'color' => $color - ]; - } - - /** - * @return Result - * @throws \Exception - * - * 奴隶交易通知 - */ - public function sendTemplate() - { - $url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken(); - - $params = [ - "touser" => $this->openId, - "template_id" => $this->templateId, - "page" => $this->page, - "form_id" => $this->formId, - "data" => $this->keywords, - ]; - - if (!empty($this->emphasis_keyword)) { - $params['emphasis_keyword'] = $this->emphasis_keyword; - } - $params = json_encode($params, JSON_UNESCAPED_UNICODE); - $this->request->setIsSSL(true); - $this->request->addHeader('content-type', 'application/json'); - $this->request->setErrorField('errcode'); - $this->request->setErrorMsgField('errmsg'); - $result = $this->request->post($url, $params); - $result->append('postBody', $params); - - $this->openId = ''; - $this->keywords = []; - $this->formId = ''; - $this->templateId = ''; - $this->page = ''; - - return $result; - } } diff --git a/wchat/wx/Subject.php b/wchat/wx/Subject.php index fab5908..17316fb 100644 --- a/wchat/wx/Subject.php +++ b/wchat/wx/Subject.php @@ -6,135 +6,16 @@ namespace wchat\wx; use wchat\common\Result; -class Subject extends SmallProgram +class Subject extends \wchat\base\Subject { - - private $keywords = []; - private $templateId = ''; - private $openId = ''; - private $defaultUrl = ''; - private $page = 'pages/index/index'; - private $emphasis_keyword = ''; - private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/subscribe/send'; - /** - * @param array $keywords + * @return string */ - public function setKeywords(array $keywords) + public function getUrl(): string { - $this->keywords = $keywords; - } - - /** - * @param string $templateId - */ - public function setTemplateId(string $templateId) - { - $this->templateId = $templateId; - } - - /** - * @param string $openId - */ - public function setOpenId(string $openId) - { - $this->openId = $openId; - } - - /** - * @param string $defaultUrl - */ - public function setDefaultUrl(string $defaultUrl) - { - $this->defaultUrl = $defaultUrl; - } - - /** - * @param string $page - */ - public function setPage(string $page) - { - $this->page = $page; - } - - /** - * @param string $emphasis_keyword - */ - public function setEmphasisKeyword(string $emphasis_keyword) - { - $this->emphasis_keyword = $emphasis_keyword; - } - - /** - * @param $index - * @param $context - * @param $color - */ - public function replaceKeyword($index, $context, $color = '') - { - if (empty($color)) { - $color = '#000'; - } - $this->keywords['keyword' . $index] = [ - 'value' => $context, - 'color' => $color - ]; - } - - - /** - * @param $color - * @param $context - */ - public function addKeyword($context, $color = null) - { - if (empty($color)) { - $color = '#000'; - } - $this->keywords['keyword' . (count($this->keywords) + 1)] = [ - 'value' => $context, - 'color' => $color - ]; - } - - /** - * @return Result - * @throws \Exception - * - * 奴隶交易通知 - */ - public function sendTemplate() - { - $url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken(); - $params = [ - "touser" => $this->openId, - "template_id" => $this->templateId, - "page" => $this->page, - "data" => $this->keywords, - ]; - - if (!empty($this->emphasis_keyword)) { - $params['emphasis_keyword'] = $this->emphasis_keyword; - } - - $params = json_encode($params, JSON_UNESCAPED_UNICODE); - - $this->request->setIsSSL(true); - $this->request->addHeader('content-type', 'application/json'); - - $this->request->setErrorField('errcode'); - $this->request->setErrorMsgField('errmsg'); - $result = $this->request->post($url, $params); - $result->append('postBody', $params); - - $this->openId = ''; - $this->keywords = []; - $this->templateId = ''; - $this->page = ''; - - return $result; + return $this->sendUrl; } } diff --git a/wchat/wx/Template.php b/wchat/wx/Template.php index a7ff468..0f0e4f2 100644 --- a/wchat/wx/Template.php +++ b/wchat/wx/Template.php @@ -8,145 +8,17 @@ namespace wchat\wx; -use wchat\common\Result; - -class Template extends SmallProgram +/** + * Class Template + * @package wchat\wx + */ +class Template extends \wchat\base\Template { - - private $keywords = []; - private $templateId = ''; - private $formId = ''; - private $openId = ''; - private $defaultUrl = ''; - private $page = 'pages/index/index'; - private $emphasis_keyword = ''; - private $sendUrl = 'https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send'; - - /** - * @param array $keywords - */ - public function setKeywords(array $keywords) + public function getUrl(): string { - $this->keywords = $keywords; + return $this->sendUrl; } - /** - * @param string $templateId - */ - public function setTemplateId(string $templateId) - { - $this->templateId = $templateId; - } - - /** - * @param string $formId - */ - public function setFormId(string $formId) - { - $this->formId = $formId; - } - - /** - * @param string $openId - */ - public function setOpenId(string $openId) - { - $this->openId = $openId; - } - - /** - * @param string $defaultUrl - */ - public function setDefaultUrl(string $defaultUrl) - { - $this->defaultUrl = $defaultUrl; - } - - /** - * @param string $page - */ - public function setPage(string $page) - { - $this->page = $page; - } - - /** - * @param string $emphasis_keyword - */ - public function setEmphasisKeyword(string $emphasis_keyword) - { - $this->emphasis_keyword = $emphasis_keyword; - } - - /** - * @param $index - * @param $context - * @param $color - */ - public function replaceKeyword($index, $context, $color = '') - { - if (empty($color)) { - $color = '#000'; - } - $this->keywords['keyword' . $index] = [ - 'value' => $context, - 'color' => $color - ]; - } - - - /** - * @param $color - * @param $context - */ - public function addKeyword($context, $color = null) - { - if (empty($color)) { - $color = '#000'; - } - $this->keywords['keyword' . (count($this->keywords) + 1)] = [ - 'value' => $context, - 'color' => $color - ]; - } - - /** - * @return Result - * @throws \Exception - * - * 奴隶交易通知 - */ - public function sendTemplate() - { - $url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken(); - $params = [ - "touser" => $this->openId, - "template_id" => $this->templateId, - "page" => $this->page, - "form_id" => $this->formId, - "data" => $this->keywords, - ]; - - if (!empty($this->emphasis_keyword)) { - $params['emphasis_keyword'] = $this->emphasis_keyword; - } - $params = json_encode($params, JSON_UNESCAPED_UNICODE); - - $this->request->setIsSSL(true); - $this->request->addHeader('content-type', 'application/json'); - $this->request->setErrorField('errcode'); - $this->request->setErrorMsgField('errmsg'); - $result = $this->request->post($url, $params); - $result->append('postBody', $params); - - $this->openId = ''; - $this->keywords = []; - $this->formId = ''; - $this->templateId = ''; - $this->page = ''; - - return $result; - } }