<?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. Python运维开发基础02-语法基础【转】

    上节作业回顾(讲解+温习60分钟) #!/bin/bash #user login User="yunjisuan" Passwd="666666" User2 ...

  2. vc++基础班[24]---系统各种路径信息的获取

    vc++基础班[24]---系统各种路径信息的获取 ------------------------------------------ Begin ------------------------- ...

  3. 激活函数Sigmoid、Tanh、ReLu、softplus、softmax

    原文地址:https://www.cnblogs.com/nxf-rabbit75/p/9276412.html 激活函数: 就是在神经网络的神经元上运行的函数,负责将神经元的输入映射到输出端. 常见 ...

  4. UVALive 8513 lovers 2017 西安区域赛 B 贪心+multiset

    UVALive 8513 有2种人,每个人有自己的权值$A_i$ $B_i$ 当$A_i + B_i >=K$时 两个人可以配对 问最多多少人可以配对 解法 : 把$/{ A_i /}$ 排序 ...

  5. 016_把普通用户免秘钥加入root用户的几种方式

    一.第一种方式. (1) [root@infra-jyallkv-tikv-pps-7 ~]# tail /etc/sudoers## Allows members of the users grou ...

  6. socket 通信 error:88

    调用函数(setsockopt)来设置 recv buffer 和send  buffer时报错: setsockopt error: Socket operation on non-socket(e ...

  7. kafka manager安装配置和使用

    kafka manager安装配置和使用 .安装yum源 curl https://bintray.com/sbt/rpm/rpm | sudo tee /etc/yum.repos.d/bintra ...

  8. Ex 2_5 求解递推式..._第三次作业

  9. appium+java(二)appium初始化参数部分详解

    Capabilities介绍 实际上它的全称是:Desired capabilities Desired capability是一个JSON对象,包含一组key和value值.它由客户端发送给服务端, ...

  10. [JavaScript]使用ArrayBuffer和Blob编辑二进制流

    Blob()构造方法返回一个新的Blob对象. 内容是包含参数array的二进制字节流. 语法 var aBlob = new Blob( array, options ); 参数 array is ...