keywords = $keywords; } /** * @param $templateId */ public function setTemplateId($templateId) { $this->templateId = $templateId; } /** * @param $openId */ public function setOpenId($openId) { $this->openId = $openId; } /** * @param $defaultUrl */ public function setDefaultUrl($defaultUrl) { $this->defaultUrl = $defaultUrl; } /** * @param $name * @param $context * @param string $color */ public function replaceKeyword($name, $context, string $color = '') { $this->keywords[$name] = ['value' => $context, 'color' => $color]; } /** * @param $name * @param $context * @param null $color */ public function addKeyword($name, $context, $color = null) { if (empty($color)) { $color = '#000'; } $this->keywords[$name] = [ 'value' => $context, 'color' => $color ]; } /** * @param $context * @param string $color * @return void */ public function setFirst($context, string $color = '#f00'): void { $this->first = [ 'value' => $context, 'color' => $color ]; } /** * @param $context * @param string $color * @return void */ public function setRemark($context, string $color = '#000'): void { $this->remark = [ 'value' => $context, 'color' => $color ]; } /** * @param $appid * @param string $pagepath * @return void */ public function setMiniprogram($appid, string $pagepath): void { $this->miniprogram = [ 'appid' => $appid, 'pagepath' => $pagepath ]; } /** * @return Result * @throws \Exception * * 奴隶交易通知 */ public function sendTemplate(): Result { $url = $this->sendUrl . '?access_token=' . $this->config->getAccessToken(); $keywords = $this->keywords; $keywords['first'] = $this->first; $keywords['remark'] = $this->remark; $default = [ "touser" => $this->openId, "template_id" => $this->templateId, "url" => $this->defaultUrl, "data" => $keywords, ]; if (!empty($this->miniprogram)) { $default['miniprogram'] = $this->miniprogram; } $client = new Client('api.weixin.qq.com', 443, true); $client->withHeader(['Content-Type' => 'application/json']); $proxyHost = $this->getConfig()->getProxyHost(); $proxyPort = $this->getConfig()->getProxyPort(); if (!empty($proxyHost) && $proxyPort > 0) { $client->withProxyHost($proxyHost)->withProxyPort($proxyPort); } $client->post($url, $default); $client->close(); if (!in_array($client->getStatusCode(), [101, 200, 201])) { return new Result(code: 505, message: $client->getBody()); } $body = json_decode($client->getBody(), true); if (isset($body['errcode']) && $body['errcode'] != 0) { return new Result(code: $body['errcode'], message: $body['errmsg']); } else { return new Result(code: 0, data: $body); } } }