<?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微信模板消息发送的更多相关文章

  1. PHP实现微信模板消息发送给指定用户

    使用公众号的模板消息功能,必须是认证过的服务号,需要发送微信号的openid,同一微信号在不同公众号下的openid是不同的,在公众号下是唯一的,获取不到微信号 进入公众平台  功能->模板消息 ...

  2. php 微信模板消息发送

    <?php ini_set ( 'date.timezone', 'Asia/Shanghai' ); define ( 'IN_ASK2', TRUE ); $http_type = ((is ...

  3. 【原创分享·微信支付】C# MVC 微信支付之微信模板消息推送

    微信支付之微信模板消息推送                    今天我要跟大家分享的是“模板消息”的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信生存的呀,所以他能不 ...

  4. C# MVC 微信支付之微信模板消息推送

    微信支付之微信模板消息推送                    今天我要跟大家分享的是"模板消息"的推送,这玩意呢,你说用途嘛,那还是真真的牛逼呐.原因在哪?就是因为它是依赖微信 ...

  5. 前后端分离djangorestframework—— 接入微信模板消息推送

    微信 什么是微信也不多说,跟前面的支付宝一样的 微信支付 微信支付也有个沙箱环境,沙箱环境官方文档 由文档中那句很显眼的话所得,即使是测试环境也需要真实的商户号,所以这个就没法想支付宝那样用沙箱账号来 ...

  6. java开发微信模板消息推送

    发布时间:2018-12-12   技术:springboot+maven   概述 该demo主要涉及微信模板消息推送功能, 详细 代码下载:http://www.demodashi.com/dem ...

  7. 5分钟连续出现某现象+微信模板消息提醒 PHP

    需求场景:用电插座电流连续出现5次电流过高(大于 3A)后停止用电服务,前四次发送电流过高提醒,最后一次发送结束用电服务提醒 思路: Redis  key 设为:插座编号+user户编号  value ...

  8. 应用jfinal发送微信模板消息的一个bug

    严格来讲,这不是一个bug,只是我们应用的方式不对.微信发送模板消息的方法是: HttpUtils.post(sendApiUrl + AccessTokenApi.getAccessTokenStr ...

  9. ThinkPHP3.2.3发送微信模板消息

    一.开通模板消息功能 所有服务号都可以在功能->添加功能插件处看到申请模板消息功能的入口,但只有认证后的服务号才可以申请模板消息的使用权限并获得该权限:需要选择公众账号服务所处的2个行业,每月可 ...

随机推荐

  1. Nginx range filter模块数字错误漏洞修复 (Nginx平滑升级) 【转】

    对线上生产环境服务器进行漏洞扫描, 发现有两台前置机器存在Nginx range filter模块数字错误漏洞, 当使用nginx标准模块时,攻击者可以通过发送包含恶意构造range域的header ...

  2. 使用C++编写linux多线程程序

    前言 在这个多核时代,如何充分利用每个 CPU 内核是一个绕不开的话题,从需要为成千上万的用户同时提供服务的服务端应用程序,到需要同时打开十几个页面,每个页面都有几十上百个链接的 web 浏览器应用程 ...

  3. git与eclipse集成之文件回退

    1.1. 文件回退 1.1.1.        添加或修改文件回退,选择要回退的文件,右键Overwrite 1.1.2.        删除文件回退 选择要回退的文件,右键Overwrite 文件变 ...

  4. 统一门户与业务系统的sso整合技术方案(单点登录)

    一.单点登录(SSO,Single Sign On)整合目前计划接入统一门户的所有业务系统均为基于JavaEE技术的B/S架构系统.由于统一门户的单点登录技术选用的是JA-SIG组织开发的Cas Se ...

  5. LOJ 6277-6280 数列分块入门 1-4

    数列分块是莫队分块的前置技能,练习一下 1.loj6277 给出一个长为n的数列,以及n个操作,操作涉及区间加法,单点查值. 直接分块+tag即可 #include <bits/stdc++.h ...

  6. sqlserver 导出数据

    背景 一看到这个标题,还有这个内容,感觉当初记录这个知识点真是记录的太简单了.不过通过这个知识点我还真想起了当初的一些事情.写的题外话可能更有意思,希望每篇文章我都能加个当时的题外记录.当时一直搞or ...

  7. Linux基础知识之bashrc和profile的用途和区别

    使用终端ssh登录Linux操作系统的控制台后,会出现一个提示符号(例如:#或~),在这个提示符号之后可以输入命令,Linux根据输入的命令会做回应,这一连串的动作是由一个所谓的Shell来做处理. ...

  8. centos中释放缓存的方法

    释放缓存区内存的方法 a)清理pagecache(页面缓存) # > /proc/sys/vm/drop_caches 或者 # sysctl - b)清理dentries(目录缓存)和inod ...

  9. fpm...failed: Cannot assign requested address

    2017年6月23日 16:00:28 星期五 启动fpm的时候总是报错, 看了网上说的修改rlimit值但是不管用 后来发现fpm配置文件监听的地址写错了, 127.0.0.1 写成了 172.0. ...

  10. python old six day

    今天主要内容: . is 和== 的区别 . 编程的问题 一.       is和==的区别! is  比较的是内存地址 ==  比较的是值 记住结果就好 ⑴id 通过id() 我们查看到一个变量表示 ...