diff --git a/wx/PublicTemplate.php b/wx/PublicTemplate.php new file mode 100644 index 0000000..4f985c2 --- /dev/null +++ b/wx/PublicTemplate.php @@ -0,0 +1,148 @@ +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 $name + * @param $context + * @param string $color + */ + public function replaceKeyword($name, $context, $color = '') + { + $this->addKeyword($name, $context, $color); + } + + + /** + * @param $name + * @param $context + * @param null $color + */ + public function addKeyword($name, $context, $color = null) + { + if (empty($color)) { + $color = '#000'; + } + $this->keywords[$name . '.DATA'] = [ + 'value' => $context, + 'color' => $color + ]; + } + + /** + * @param $context + * @param string $color + */ + public function setFirst($context, $color = '#f00') + { + $this->keywords['first.DATA'] = [ + 'value' => $context, + 'color' => $color + ]; + } + + /** + * @param $context + * @param string $color + */ + public function setRemark($context, $color = '#000') + { + $this->keywords['remark.DATA'] = [ + 'value' => $context, + 'color' => $color + ]; + } + + /** + * @param $appid + * @param $pagepath + */ + public function setMiniprogram($appid, $pagepath) + { + $this->miniprogram = [ + 'appid' => $appid, + 'pagepath' => $pagepath + ]; + } + + /** + * @return Result + * @throws \Exception + * + * 奴隶交易通知 + */ + public function sendTemplate() + { + $url = $this->sendUrl . '?access_token=' . $this->getAccessToken(); + + $default = [ + "touser" => $this->openId, + "template_id" => $this->templateId, + "url" => $this->defaultUrl, + "data" => $this->keywords, + ]; + + if (!empty($this->miniprogram)) { + $default['miniprogram'] = $this->miniprogram; + } + + $params = json_encode($default, JSON_UNESCAPED_UNICODE); + + $this->request->setIsSSL(true); + $this->request->addHeader('content-type', 'application/json'); + + $result = $this->request->post($url, $params); + $result->append('postBody', $params); + + return $result; + } +} diff --git a/wx/Wx.php b/wx/Wx.php index a416761..769c5f0 100644 --- a/wx/Wx.php +++ b/wx/Wx.php @@ -51,6 +51,15 @@ class Wx return Template::getInstance($this->config); } + + /** + * @return Template + */ + public function getPublicTemplate() + { + return PublicTemplate::getInstance($this->config); + } + /** * @return Account */