开发项目时自己封装的类,涉及到微信模板消息发送、免单逻辑计算、用户分佣计算等
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, 0); //设置header
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_POST, 1); //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, 1);
$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' => 0,
'pay_status' => 1,
'status'=>array('gt', 0),
'free_amount' => array('gt', 0)
);
//查询还未免单的订单
$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' => 1,
'use_status' => 0,
'status'=>array('gt', 0),
'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']==3){
$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 > 0 ? $free_amount : 0;
$free_data['free_status'] = 0;//默认免单状态为0,表示未免单
//免单进度
$rate = ($free_data['charge'] * 6 - $free_data3) / $free_data['charge'] /6;
//默认免单时间为空
$free_time = null;
if ($free_data3 == 0) {
$free_data['free_status'] = 1;//将免单状态改为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 = "恭喜您,获得好运平台返现奖励!";
$keyword1 = "好运来了";
$keyword2 = $free_data['charge'];
$keyword3 = "好运余额";
$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' => 1]);
}
return;
}
public function senddistributr($money,$id){
//发送合作者返现成功模板消息
$openid = db('user')->where(['id' =>$id])->find();
$touser =$openid['openid'];
$first = "恭喜您,获得好运平台返现奖励!";
$keyword1 = "好运合作者返现";
$keyword2 = $money.'元';
$keyword3 = "好运余额";
$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']/100,2);
$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'] !== 0){
$distribute1 = db('distribute')->where(['id'=>$distribute['pid']])->find();
$data1 = number_format($charge*($distribute1['grade']-$distribute['grade'])/100,2);
$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'] !== 0){
$distribute2 = db('distribute')->where(['id'=>$distribute1['pid']])->find();
$data2 = number_format($charge*($distribute2['grade']-$distribute1['grade'])/100,2);
$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'] !== 0){
$distribute3 = db('distribute')->where(['id'=>$distribute2['pid']])->find();
$data3 = number_format($charge*($distribute3['grade']-$distribute2['grade'])/100,2);
$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;
}
}