<?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. SQL NOLOCK大杂烩

    今天碰到NOLOCK 的问题,就查阅了一些资料,做了相关了解:总结了比较经典,朴实的两篇在此. 电梯直达: SQL Server 中WITH (NOLOCK)浅析 文章本想大篇幅摘抄,因为担心链接失效 ...

  2. 题解-AtCoder Code-Festival2017qualA-E Modern Painting

    Problem CODE-FESTIVAL 2017 qual A 洛谷账户的提交通道 题意:有一个\(n\)行\(m\)列的方格,在边界外有可能有机器人(坐标为\((0,x),(n+1,x),(x, ...

  3. 【转】thread.sleep(0)与thread.sleep(1)的区别

    Thread.Sleep(0) Sleep的意思是告诉操作系统自己要休息n毫秒,这段时间就让给一个就绪的线程吧.当n=0时,意思是要放弃自己剩下的时间片,但是仍然是就绪状态.Sleep(0)只允许那些 ...

  4. iOS post提交数据有嵌套数组的处理方法

    2017年11月21日17:11:43 解决办法, 修改iOS框架里的代码: http://www.jianshu.com/p/130daa0c2fe7 确实有效, 要不然,  内层的每一个键值对都会 ...

  5. django-form介绍

    Django form表单   目录 普通方式手写注册功能 views.py login.html 使用form组件实现注册功能 views.py login2.html 常用字段与插件 initia ...

  6. 前端----css的继承性和层叠性

    css有两大特性; 继承性和层叠性 继承性 继承:给父级设置一些属性,子级继承了父级的该属性, 这就是我们的css中的继承, 需要注意的是 有一些属性是可以继承下来的: color   ,  font ...

  7. f11 全屏

    function fullScreen() { var el = document.documentElement; var rfs = el.requestFullScreen || el.webk ...

  8. C# 中使用 Excel

    using System;using System.Collections.Generic;using System.Text;using System.Reflection;using System ...

  9. Go 开源博客平台 Pipe 1.0.0 发布!

    这是 Pipe 博客平台的第一个正式版,欢迎大家使用和反馈建议! 简介 Pipe 是一款小而美的开源博客平台,通过黑客派账号登录即可使用. 动机 产品层面: 市面上缺乏支持多独立博客的平台级系统 实现 ...

  10. Confluence 6 查看内容索引概要

    内容索引,通常也被称为查找索引,这个索引被用来在 Confluence 中支持查找.这个索引同时也被其他的一些功能使用,例如在归档邮件中构建邮件主题,View Space Activity 的特性和将 ...