您的位置:首页 > 移动开发 > 微信开发

小程序发送模板消息

2019-04-07 22:47 155 查看
<?php
namespace api\home\controller;

use think\Controller;

class SendxcxmsgController extends Controller
{
/* 发送模板信息接口地址 */
private $apiUrl;
private $orderData;
private $userInfo;
private $page;
protected $logpath = "./logs/wxasdk_log";

public function __construct()
{
// 获取 小程序 appid等配置
$wxappSettings = cmf_get_option('wxapp_settings')['default'];
if (!$wxappSettings){
return ['code' => 0, 'msg' => '无法获取小程序配置'];
}

$accessToken = \think\Cache::get('wxa_access_token');

if(!$accessToken){
// 微信接口 获取access_token
$getAccessTokenUrl = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$wxappSettings['app_id']}&secret={$wxappSettings['app_secret']}";
$accessTokenData = file_get_contents($getAccessTokenUrl);
$accessTokenData = json_decode($accessTokenData,true);

if (isset($res['errcode']) && $accessTokenData['errcode'] != 0) {
$content = '';
switch (!$accessTokenData || $accessTokenData['errcode']) {
case 40001:
$content = "AppSecret错误或者AppSecret不属于这个小程序,请开发者确认AppSecret的正确性";
break;
case 40164:
$content = "调用接口的IP地址不在白名单中,请在接口IP白名单中进行设置";
break;
}
$this->log($content);
}
$accessToken = $accessTokenData['access_token'];
$expire = $accessTokenData['expires_in'] ? intval($accessTokenData['expires_in']) - 100 : 7200;
\think\Cache::set('wxa_access_token', $accessToken, $expire);
}

// 微信发送小程序模板信息接口地址
$this->apiUrl = "https://api.weixin.qq.com/cgi-bin/message/wxopen/template/send?access_token={$accessToken}";
}

// 提现成功
public function txSuccess($openid,$formid,$log,$page=null)
{
$template = $this->getTemplate('8');
$page = empty($page) ? '/pages/index/index' : $page;
if($log['paytype']==1){
$target = '微信零钱';
} else if($log['paytype']==3){
$target = '银行卡';
}
$value = array(
"keyword1" => array(
"value" => $log['money'],
),
"keyword2" => array(
"value" => date('Y-m-d H:i:s'),
),
"keyword3" => array(
"value" => $target,
),
"keyword4" => array(
"value" => '提现' . $log['money'] .',手续费'. $log['procefees'] . ',实际到账' . $log['gotmoney'],
),
);

$dd = array();

$dd['touser'] = $openid;
$dd['template_id'] = $template['template_id'];
$dd['page'] = $page;  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
$dd['form_id'] = $formid;
$dd['data'] = $value;                        //模板内容,不填则下发空模板
$dd['emphasis_keyword'] = 'keyword1.DATA';

$this->https_curl_json($this->apiUrl, $dd, 'json');
}

// 转账成功
public function transferSuccess($openid,$formid,$money,$account)
{
$template = $this->getTemplate('1');

$value = array(
"keyword1" => array(
"value" => $money.'元',
),
"keyword2" => array(
"value" => date('Y-m-d H:i:s'),
),
"keyword3" => array(
"value" => $account,
),
);

$dd = array();

$dd['touser'] = $openid;
$dd['template_id'] = $template['template_id'];
$dd['page'] = '';  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
$dd['form_id'] = $formid;
$dd['data'] = $value;                        //模板内容,不填则下发空模板
$dd['emphasis_keyword'] = 'keyword1.DATA';

$this->https_curl_json($this->apiUrl, $dd, 'json');
}

// 订单支付成功
public function paySuccess($openid,$order_sn,$page=null)
{
$template = $this->getTemplate('6');

$value = array(
"keyword1" => array(
"value" => $this->orderData['money'].'元',
),
"keyword2" => array(
"value" => date('Y-m-d H:i:s',$this->orderData['create_time']),
),
"keyword3" => array(
"value" => '支付成功',
),
"keyword4" => array(
"value" => $order_sn,
),
);

$dd = array();

$dd['touser'] = $openid;
$dd['template_id'] = $template['template_id'];
$dd['page'] = $page;  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
$dd['form_id'] = $this->orderData['prepay_id'];
$dd['data'] = $value;                        //模板内容,不填则下发空模板
$dd['emphasis_keyword'] = 'keyword1.DATA';

$result = $this->https_curl_json($this->apiUrl, $dd, 'json');
}

// 订单支付错误
public function payError($openid,$order_sn,$page=null)
{
$template = $this->getTemplate('7');

$value = array(
"keyword1" => array(
"value" => $order_sn,
),
"keyword2" => array(
"value" => date('Y-m-d H:i:s',$this->orderData['create_time']),
),
);

$dd = array();

$dd['touser'] = $openid;
$dd['template_id'] = $template['template_id'];
$dd['page'] = $page;  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
$dd['form_id'] = $this->orderData['prepay_id'];
$dd['data'] = $value;                        //模板内容,不填则下发空模板
$dd['emphasis_keyword'] = 'keyword1.DATA';

$result = $this->https_curl_json($this->apiUrl, $dd, 'json');
}

// 充值成功
public function rechargeSuccess($openid,$order_sn,$page=null)
{
$template = $this->getTemplate('4');

$value = array(
"keyword1" => array(
"value" => $this->userInfo['user_nickname'],
),
"keyword2" => array(
"value" => $this->orderData['money'].'元',
),
"keyword3" => array(
"value" => date('Y-m-d H:i:s',$this->orderData['create_time']),
),
);

$dd = array();

$dd['touser'] = $openid;
$dd['template_id'] = $template['template_id'];
$dd['page'] = $page;  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
$dd['form_id'] = $this->orderData['prepay_id'];
$dd['data'] = $value;                        //模板内容,不填则下发空模板
$dd['emphasis_keyword'] = 'keyword1.DATA';

$result = $this->https_curl_json($this->apiUrl, $dd, 'json');
}

// 充值失败
public function rechargeError($openid,$order_sn,$page=null)
{
$template = $this->getTemplate('5');

$value = array(
"keyword1" => array(
"value" => $this->userInfo['user_nickname'],
),
"keyword2" => array(
"value" => date('Y-m-d H:i:s',$this->orderData['create_time']),
)
);

$dd = array();

$dd['touser'] = $openid;
$dd['template_id'] = $template['template_id'];
$dd['page'] = $page;  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
$dd['form_id'] = $this->orderData['prepay_id'];
$dd['data'] = $value;   //模板内容,不填则下发空模板
$dd['emphasis_keyword'] = 'keyword1.DATA';

$result = $this->https_curl_json($this->apiUrl, $dd, 'json');

7ff7
}

//开始充电
public function startCharge($openid,$order_info,$page=null)
{
$template = $this->getTemplate('6');

$value = array(
"keyword1" => array(
"value" => $order_info['order_money'],
),
"keyword2" => array(
"value" => date('Y-m-d H:i:s',$order_info['start_time']),
),
"keyword3" => array(
"value" => "开始充电,预计充电" . $order_info['order_charge_time'] . '分钟',
),
"keyword4" => array(
"value" => $order_info['order_sn'],
),
);

$dd = array();

$dd['touser'] = $openid;
$dd['template_id'] = $template['template_id'];
$dd['page'] = $page;  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
$dd['form_id'] = $order_info['formid'];
$dd['data'] = $value;                        //模板内容,不填则下发空模板
$dd['emphasis_keyword'] = 'keyword1.DATA';

$result = $this->https_curl_json($this->apiUrl, $dd, 'json');
}

//结束充电
public function endCharge($openid,$order_info,$page=null)
{
$template = $this->getTemplate('6');

$value = array(
"keyword1" => array(
"value" => $order_info['order_money'],
),
"keyword2" => array(
"value" => date('Y-m-d H:i:s',$order_info['start_time']),
),
"keyword3" => array(
"value" => "结束充电,充电时长" . $order_info['charge_time'] . '分钟',
),
"keyword4" => array(
"value" => $order_info['order_sn'],
),
);

$dd = array();

$dd['touser'] = $openid;
$dd['template_id'] = $template['template_id'];
$dd['page'] = $page;  //点击模板卡片后的跳转页面,仅限本小程序内的页面。支持带参数,该字段不填则模板无跳转。
$dd['form_id'] = $order_info['formid'];
$dd['data'] = $value;                        //模板内容,不填则下发空模板
$dd['emphasis_keyword'] = 'keyword1.DATA';

$result = $this->https_curl_json($this->apiUrl, $dd, 'json');
}

private function getTemplate($id)
{
if (empty($id)){
return false;
}
$template = db('template_info') ->where('id',$id) ->find();
if (!$template){
return false;
}
return $template;
}

/* 发送json格式的数据,到api接口 -xzz0704  */
private function https_curl_json($url, $data, $type)
{
if ($type == 'json') {
$headers = array("Content-type: application/json;charset=UTF-8", "Accept: application/json", "Cache-Control: no-cache", "Pragma: no-cache");
$data = json_encode($data);
}
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)) {
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
$output = curl_exec($curl);
if (curl_errno($curl)) {
file_put_contents('wxsms_err_log.txt','Errno' . curl_error($curl).PHP_EOL,FILE_APPEND) ;//捕抓异常
}
curl_close($curl);
return $output;
}

protected function log($content, $title = '', $path = '')
{
if (empty($path)) {
$path = $this->logpath;
}
if (!file_exists($path)) {
mkdirs($path);
}
$filepath = $path . '/' . date('Y-m-d', time()) . '.txt';
$str = '';
if (!empty($title)) {
$str .= $title . PHP_EOL;
}
$str .= '收到请求:' . date('Y-m-d H:i:s') . PHP_EOL . json_encode($this->request->param(), JSON_UNESCAPED_UNICODE) . PHP_EOL;
$str .= '日志内容:' . $content . PHP_EOL;
file_put_contents($filepath, $str, FILE_APPEND);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: