multi-store/app/common/service/wechat/WechatTemplate.php

66 lines
2.1 KiB
PHP

<?php
namespace app\common\service\wechat;
use support\exception\BusinessException;
class WechatTemplate
{
protected $token;
protected $Client;
protected $Config;
public function __construct()
{
$this->Client = (new WeChatOaService())->getClient();
$this->Config = (new WeChatOaService())->config;
$this->token = (new WeChatOaService())->app->getAccessToken()->getToken();
}
/**
* @notes 新报价通知
*/
public function NewQuotationNotification($data)
{
$template = [
'touser' => $data['openid'],
'template_id' => 'hfvTch-DcP_UQ83VkD6Z-eMimeRrK8P8zpWC9j2dOKc',
'miniprogram' => 'data',
'appid' => $this->Config['app_id'],
'pagepath' => 'pages/index/index',
'data' => [
'thing2' => ['value' => $data['data1']],
'time6' => ['value' => $data['data2']],
'thing4' => ['value' => $data['data3']],
'phone_number5' => ['value' => $data['data3']],
]
];
return $this->post($template);
}
/**
* @notes 采用通知
*/
public function AdoptingNotifications($data)
{
$template = [
'touser' => $data['openid'],
'template_id' => 'hfvTch-DcP_UQ83VkD6Z-eMimeRrK8P8zpWC9j2dOKc',
'miniprogram' => 'data',
'appid' => $this->Config['app_id'],
'pagepath' => 'pages/index/index',
'data' => [
'thing2' => ['value' => $data['data1']],
'thing3' => ['value' => $data['data2']],
'const4' => ['value' => $data['data3']??'08302669767'],
]
];
return $this->post($template);
}
protected function post($template)
{
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->token;
$res = $this->Client->postJson($url, $template)->toArray();
if ($res['errmsg'] == 'ok' && $res['errcode'] == 0) {
return true;
}
throw new BusinessException($res['errmsg'], 3000);
}
}