PHP微信模板消息发送
<?php
class Wxtemplate extends Base
{
function __construct()
{
$this->appid = config('WXAPP_APPID');
$this->secrect = config('WXAPP_APPSECRET');
$this->accessToken = $this->getToken($this->appid, $this->secrect);
}
/**
* 发送post请求
* @param string $url
* @param string $param
* @return bool|mixed
*/
function request_post($url = '', $param = '')
{
if (empty($url) || empty($param)) {
return false;
}
$postUrl = $url;
$curlPost = $param;
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $postUrl); //抓取指定网页
curl_setopt($ch, CURLOPT_HEADER, ); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, ); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, ); //post提交方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = curl_exec($ch); //运行curl
curl_close($ch);
return $data;
}
/**
* 发送get请求
* @param string $url
* @return bool|mixed
*/
function request_get($url = '')
{
if (empty($url)) {
return false;
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, );
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
/**
* @param $appid
* @param $appsecret
* @return mixed
* 获取token
*/
protected function getToken($appid, $appsecret)
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" . $appid . "&secret=" . $appsecret;
$token = $this->request_get($url);
$token = json_decode(stripslashes($token));
$arr = json_decode(json_encode($token), true);
$access_token = $arr['access_token'];
return $access_token;
}
/**
* 发送自定义的模板消息
* @param $touser
* @param $template_id
* @param $url
* @param $data
* @param string $topcolor
* @return bool
*/
public function doSend($touser, $url,$keyword1,$keyword2,$keyword3,$keyword4, $topcolor = '#7B68EE')
{
$data = array(
"first" => array("value"=>"您好,您的微信支付已成功!", "color"=>"#173177"),
"keyword1"=>array("value"=>$keyword1,"color"=>"#173177"),
"keyword2"=>array("value"=>$keyword2, "color"=>"#173177"),
"keyword3"=> array("value"=>$keyword3, "color"=>"#173177"),
"keyword4"=> array("value"=>$keyword4, "color"=>"#173177"),
"remark"=> array("value"=>"点击查看奖励进度!", "color"=>"#173177"),
);
$template = array(
'touser' => $touser,
'template_id' => config('WX_TEMPLATE'),
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
return $this->request_post($url, urldecode($json_template));
}
/**
* 发送自定义的模板消息TO 商家
* @param $touser
* @param $template_id
* @param $url
* @param $data
* @param string $topcolor
* @return bool
*/
public function doSendToStore($touser, $url,$keyword1,$keyword2,$keyword3,$keyword4,$keyword5, $topcolor = '#7B68EE')
{
$data = array(
"first" => array("value"=>"您有新的订单,请注意查收!", "color"=>"#173177"),
"keyword1"=>array("value"=>$keyword1,"color"=>"#173177"),
"keyword2"=>array("value"=>$keyword2, "color"=>"#173177"),
"keyword3"=> array("value"=>$keyword3, "color"=>"#173177"),
"keyword4"=> array("value"=>$keyword4, "color"=>"#173177"),
"keyword5"=> array("value"=>$keyword5, "color"=>"#173177"),
"remark"=> array("value"=>"", "color"=>"#173177"),
);
$template = array(
'touser' => $touser,
'template_id' => config('WX_TEMPLATE_STORE'),
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
return $this->request_post($url, urldecode($json_template));
}
/**
* 发送自定义的模板消息 (核销发送给会员)
* @param $touser
* @param $template_id
* @param $url
* @param $data
* @param string $topcolor
* @return bool
*/
public function doSendFromCoupon($touser, $url,$keyword1,$keyword2,$keyword3, $topcolor = '#7B68EE')
{
$data = array(
"first" => array("value"=>"您好,您的消费码已经核销成功!", "color"=>"#173177"),
"keyword1"=>array("value"=>$keyword1,"color"=>"#173177"),
"keyword2"=>array("value"=>$keyword2, "color"=>"#173177"),
"keyword3"=> array("value"=>$keyword3, "color"=>"#173177"),
"remark"=> array("value"=>"点击查看消费详情", "color"=>"#173177"),
);
$template = array(
'touser' => $touser,
'template_id' => config('WX_TEMPLATE_COUPON'),
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
return $this->request_post($url, urldecode($json_template));
}
/**
* 发送自定义的模板消息 (用户返现通知)
* @param $touser
* @param $template_id
* @param $url
* @param $data
* @param string $topcolor
* @return bool
*/
public function doSendToUserCashBackInfo($touser,$first, $url,$keyword1,$keyword2,$keyword3,$remark, $topcolor = '#7B68EE')
{
$data = array(
"first" => array("value"=>$first, "color"=>"#173177"),
"keyword1"=>array("value"=>$keyword1,"color"=>"#173177"),
"keyword2"=>array("value"=>$keyword2, "color"=>"#173177"),
"keyword3"=> array("value"=>$keyword3, "color"=>"#173177"),
"remark"=> array("value"=>$remark, "color"=>"#173177"),
);
$template = array(
'touser' => $touser,
'template_id' => config('WX_TEMPLATE_CASH_BACK'),
'url' => $url,
'topcolor' => $topcolor,
'data' => $data
);
$json_template = json_encode($template);
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $this->accessToken;
return $this->request_post($url, urldecode($json_template));
}
//免单逻辑计算
public function free_caculate($store_id)
{
$where = array(
'free_status' => ,
'pay_status' => ,
'status'=>array('gt', ),
'free_amount' => array('gt', )
);
//查询还未免单的订单
$free_data = db('order')->where(['store_id'=>$store_id])->where($where)->order('pay_time asc,order_id asc')->find();
$free = $free_data['free_amount'] ;
$where2 = array(
'pay_status' => ,
'use_status' => ,
'status'=>array('gt', ),
'pay_time' => array('egt', $free_data['pay_time'])
);
//查询为免单订单的后面一条数据
$next_free_data = db('order')->where(['store_id'=>$store_id])->where($where2)->order('pay_time asc,order_id asc')->find();
if($next_free_data['pay_type']==){
$next_free_data = db('order')->where(['store_id'=>$store_id])->where($where2)->order('pay_time asc,order_id asc')->find();
}
//用户每次下单后对数据进行运算
// $free_amount = $free - $next_free_data['pay_able']; //现金支付
$free_amount = $free - $next_free_data['charge']; //全部参与返现
$free_data3 = $free_amount > ? $free_amount : ;
$free_data['free_status'] = ;//默认免单状态为0,表示未免单
//免单进度
$rate = ($free_data['charge'] * - $free_data3) / $free_data['charge'] /;
//默认免单时间为空
$free_time = null;
if ($free_data3 == ) {
$free_data['free_status'] = ;//将免单状态改为1,表示已免单
$re = db('user')->where(['id' => $free_data['uid']])->setInc('balance', $free_data['charge']);
$free_time = time();
if($re){
if($free_data['charge']>=0.01){
//发送用户返现成功模板消息
$res = db('user')->where(['id' => $free_data['uid']])->find();
$touser =$res['openid'];
$first = "恭喜您,获得XX平台返现奖励!";
$keyword1 = "XXXX";
$keyword2 = $free_data['charge'];
$keyword3 = "XX余额";
$remark = "点击查看返现奖励";
$url = 'http://lucky.xdjst.com/Skyfier/Skyfierhtml/personal/personal.html';
$this->doSendToUserCashBackInfo($touser,$first, $url,$keyword1,$keyword2,$keyword3,$remark, $topcolor = '#7B68EE');
}
}
}
$result = db('order')->update(['order_id' => $free_data['order_id'], 'free_amount' => $free_data3, 'free_status' => $free_data['free_status'], 'rate' => $rate, 'free_time' => $free_time]);
if ($result) {
db('order')->update(['order_id' => $next_free_data['order_id'], 'use_status' => ]);
}
return;
}
public function senddistributr($money,$id){
//发送合作者返现成功模板消息
$openid = db('user')->where(['id' =>$id])->find();
$touser =$openid['openid'];
$first = "恭喜您,获得XX平台返现奖励!";
$keyword1 = "XX合作者返现";
$keyword2 = $money.'元';
$keyword3 = "XX余额";
$remark = "点击查看返现奖励";
$url = 'http://lucky.xdjst.com/Skevin/Skevinhtml/cooperation.html';
$this->doSendToUserCashBackInfo($touser,$first, $url,$keyword1,$keyword2,$keyword3,$remark, $topcolor = '#7B68EE');
}
//分配佣金
public function distribute_commission($store_id,$charge){
$store = db('store')->where(['id'=>$store_id])->find();
if($store&&$store['refer_id']){
Db::startTrans(); //启动事务
try {
$distribute = db('distribute')->where(['id'=>$store['refer_id']])->find();
$data = number_format($charge*$distribute['grade']/,);
$income = $distribute['income']+$data;
db('distribute')->where(['id'=>$distribute['id']])->update(['income'=>$income]);
$res = ['uid'=>$store['refer_id'],'store_id'=>$store_id,'income'=>$data,'charge'=>$charge,'create_time'=>time()];
db('rewardinfo')->insert($res);//奖励明细插入数据库
if($data>=0.01){
$this->senddistributr($data,$store['refer_id']);//发送合作者返现成功模板消息
}
if($distribute['pid'] !== ){
$distribute1 = db('distribute')->where(['id'=>$distribute['pid']])->find();
$data1 = number_format($charge*($distribute1['grade']-$distribute['grade'])/,);
$income1 = $distribute1['income']+$data1;
db('distribute')->where(['id'=>$distribute1['id']])->update(['income'=>$income1]);
$res = ['uid'=>$distribute['pid'],'store_id'=>$store_id,'income'=>$data1,'create_time'=>time(),'charge'=>$charge];
db('rewardinfo')->insert($res);
if($data1>=0.01){
$this->senddistributr($data1, $distribute['pid']);//发送合作者返现成功模板消息
}
if($distribute1['pid'] !== ){
$distribute2 = db('distribute')->where(['id'=>$distribute1['pid']])->find();
$data2 = number_format($charge*($distribute2['grade']-$distribute1['grade'])/,);
$income2 = $distribute2['income']+$data2;
db('distribute')->where(['id'=>$distribute2['id']])->update(['income'=>$income2]);
$res = ['uid'=>$distribute1['pid'],'store_id'=>$store_id,'income'=>$data2,'create_time'=>time(),'charge'=>$charge];
db('rewardinfo')->insert($res);
if($data2>=0.01){
$this->senddistributr($data2, $distribute1['pid']);//发送合作者返现成功模板消息
}
if($distribute2['pid'] !== ){
$distribute3 = db('distribute')->where(['id'=>$distribute2['pid']])->find();
$data3 = number_format($charge*($distribute3['grade']-$distribute2['grade'])/,);
$income3 = $distribute3['income']+$data3;
db('distribute')->where(['id'=>$distribute3['id']])->update(['income'=>$income3]);
$res = ['uid'=>$distribute2['pid'],'store_id'=>$store_id,'income'=>$data3,'create_time'=>time(),'charge'=>$charge];
db('rewardinfo')->insert($res);
if($data3>=0.01){
$this->senddistributr($data3, $distribute2['pid']);//发送合作者返现成功模板消息
}
}
}
}
Db::commit(); //提交事务
} catch (\PDOException $e) {
Db::rollback(); //回滚事务
}
}
return;
}
}
PHP微信模板消息发送的更多相关文章
- PHP实现微信模板消息发送给指定用户
使用公众号的模板消息功能,必须是认证过的服务号,需要发送微信号的openid,同一微信号在不同公众号下的openid是不同的,在公众号下是唯一的,获取不到微信号 进入公众平台 功能->模板消息 ...
- php 微信模板消息发送
<?php ini_set ( 'date.timezone', 'Asia/Shanghai' ); define ( 'IN_ASK2', TRUE ); $http_type = ((is ...
- 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送
微信支付之微信模板消息推送 今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...
- C# MVC 微信支付之微信模板消息推送
微信支付之微信模板消息推送 今天我要跟大家分享的是"模板消息"的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信 ...
- 前后端分离djangorestframework—— 接入微信模板消息推送
微信 什么是微信也不多说,跟前面的支付宝一样的 微信支付 微信支付也有个沙箱环境,沙箱环境官方文档 由文档中那句很显眼的话所得,即使是测试环境也需要真实的商户号,所以这个就没法想支付宝那样用沙箱账号来 ...
- java开发微信模板消息推送
发布时间:2018-12-12 技术:springboot+maven 概述 该demo主要涉及微信模板消息推送功能, 详细 代码下载:http://www.demodashi.com/dem ...
- 5分钟连续出现某现象+微信模板消息提醒 PHP
需求场景:用电插座电流连续出现5次电流过高(大于 3A)后停止用电服务,前四次发送电流过高提醒,最后一次发送结束用电服务提醒 思路: Redis key 设为:插座编号+user户编号 value ...
- 应用jfinal发送微信模板消息的一个bug
严格来讲,这不是一个bug,只是我们应用的方式不对.微信发送模板消息的方法是: HttpUtils.post(sendApiUrl + AccessTokenApi.getAccessTokenStr ...
- ThinkPHP3.2.3发送微信模板消息
一.开通模板消息功能 所有服务号都可以在功能->添加功能插件处看到申请模板消息功能的入口,但只有认证后的服务号才可以申请模板消息的使用权限并获得该权限:需要选择公众账号服务所处的2个行业,每月可 ...
随机推荐
- 用vue+element-ui开发后台笔记
1.前端通过 formData: new FormData(), 构造对象传数值给后台! 当传给后台的参数中有图片的时候,需要把需要传输的数据通过构造对象new FormData()的形式存数据,并且 ...
- 新版本微信导致的ios表单bug
解决方法如下: $(document).delegate('input, textarea, select', 'blur', function(){ setTimeout(function(){ $ ...
- Android Day2
紧接前一篇日记: 2.增加文本域 在<LinearLayout>中增加<EditText>元素,无格式设置的时候如下. <EditText android:id=&quo ...
- linux 进程监控软件 supervisor
2017年8月21日 17:51:33 星期一 supervisor python写的, 用来监控进程是否启动, 之前监控进程是否启动, 没有就拉起的shell代码是写在crontab里的, 这个软件 ...
- 我的大学,我的SPR机器人队
时间过的真快,我这个在协会呆了好多年的老油条今年都毕业了,在石油大学大学七年几乎三分之二的时间就是在协会度过的.实话说在北京这是我最亲切的地方,这里有我喜欢的各种设备,有亲爱的老师和一起奋斗的队友,在 ...
- class, extends和super es6语法
摘自https://www.cnblogs.com/queende7/p/8668497.html,谢谢博主的分享!
- [C][代码实例]交换指向常量的二级指针的位置
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <time.h> ...
- Python-多表关联 外键 级联
分表为什么分表 多表关联多表关系 ****** 表之间的关系 为什么要分表 多对一 一个外键 多对多 一个中间表 两个外键 一对一 一个外键加一个唯一约束外键约束 ****** foreign key ...
- liunx contos 7.4 安装redis集群
前前后后安装了几次redis集群,基本上每次安装都会采坑,耗时伤神. 安装redis依赖gcc环境,安装前先检查liunx上面有没有安装GCC 命令:gcc -v 上传redis-4.0.1.tar. ...
- Confluence 6 使用 JMX 界面实时监控
使用 JMX 界面(Java Management Extensions API ),你可以实时的查看你 Confluence 运行实例的状态. JMX 使用的对象被称 MBeans (Managed ...