keywords = $keywords; } /** * @param $templateId */ public function setTemplateId($templateId) { $this->templateId = $templateId; } /** * @param $openId */ public function setOpenId($openId) { $this->openId = $openId; } /** * @param $page */ public function setPage($page) { $this->page = $page; } /** * @param $emphasis_keyword */ public function setEmphasisKeyword($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(); /** * @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; } }