<?php
namespace kj;
//卡券相关类
class weiXinKaJuan
{
private $appid;
private $appsecret;
public function __construct($appid,$appsecret)
{
$this->appid = $appid;
$this->appsecret = $appsecret;
}
/**
* 核销--线上核销
* @param string $type 创建的卡券类型
* @param string $pic 卡券的logo
* @return array 返回卡卷所需的Json数据
*/
public function codeEncrypt($data)
{
$data = json_decode($data,true);
$data = $data['0'];
$token = $this->getAcctoken();
$url = "https://api.weixin.qq.com/card/code/decrypt?access_token=".$token;
$datas['encrypt_code'] = $data['encrypt_code'];
$data_post = json_encode($datas,JSON_UNESCAPED_UNICODE);
$res = $this->postUrl($url,$data_post);
$arr = json_decode($res,true);
if($arr['errcode']>0){
return $arr;
}else{
return $this->codeUse($arr['code'],$data['card_id']);
}
}
/**
* 核销--核销
* @param string $type 创建的卡券类型
* @param string $pic 卡券的logo
* @return array 返回卡卷所需的Json数据
*/
public function codeUse($code,$card_id)
{
if($this->codeStatus($code,$card_id)){
$token = $this->getAcctoken();
$url = "https://api.weixin.qq.com/card/code/consume?access_token=".$token;
$data = array(
"code"=>$code,
);
$data_post = json_encode($data,JSON_UNESCAPED_UNICODE);
$res = $this->postUrl($url,$data_post);
$arr = json_decode($res,true);
return $arr;
}else{
return '卡券无效';
}
}
/**
* 核销--查询code
* @param string $code code
* @param string $card_id 卡券的card_id
* @return bool true/false
*/
private function codeStatus($code,$card_id)
{
$token = $this->getAcctoken();
$url = "https://api.weixin.qq.com/card/code/get?access_token=".$token;
$data = array(
"card_id"=>$card_id,
"code"=>$code,
"check_consume"=>true
);
$data_post = json_encode($data,JSON_UNESCAPED_UNICODE);
$res = $this->postUrl($url,$data_post);
$arr = json_decode($res,true);
if($arr['errcode'] > 0){
return false;
}else {
return $arr['can_consume'];
}
}
/**
* 投放--群发 根据表情发送图文消息
* @param string $type 创建的卡券类型
* @param string $pic 卡券的logo
* @return array 返回卡卷所需的Json数据
*/
public function sendQunfa($media_id){
$token = $this->getAcctoken();
$url = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=".$token;
$data = array(
"filter"=>array(
"is_to_all"=>true
),
"mpnews"=>array(
"media_id"=>$media_id
),
"msgtype"=>"mpnews",
"send_ignore_reprint"=>0
);
$data_post = json_encode($data,JSON_UNESCAPED_UNICODE);
$res = $this->postUrl($url,$data_post);
$arr = json_decode($res,true);
return $arr;
}
/**
* 投放--群发添加新素材
* @param string $card_id 卡券card_id
* @return array 返回卡卷所需的Json数据
*/
public function sendNewsucai($card_id)
{
$token = $this->getAcctoken();
$arr = $this->sendGroup($card_id);
$content = $arr['content'];
$url = "https://api.weixin.qq.com/cgi-bin/material/add_news?access_token=".$token;
$pic = $this->setScpic('./300300.png');
$data['title'] = '群发卡券';
$data['thumb_media_id'] = $pic;
$data['authod'] = 'test';
$data['content_source_url'] = '11';
$data['content'] = $content;
$data['show_cover_pic'] = 1;
$data['digest'] = '111';
$datas['articles'][] = $data;
$data_post = json_encode($datas,JSON_UNESCAPED_UNICODE);
$res = $this->postUrl($url,$data_post);
$arr = json_decode($res,true);
return $arr['media_id'];
}
/**
* 投放--上传素材图片
* @param string $path 图片本地地址
* @return string 返回图片路径
*/
public function setScpic($path)
{
$scmedia_id = isset($_COOKIE['scmedia_id'])?$_COOKIE['scmedia_id']:'';
if($scmedia_id == '') {
$token = $this->getAcctoken();
$url="https://api.weixin.qq.com/cgi-bin/material/add_material?access_token=".$token;
$filedata = array(
"media" => "@".$path,
"type" => "image",
);
$result = $this->postUrl($url, $filedata);
$arr = json_decode($result, true);
if ($arr['media_id'] != '') {
setcookie("sucaipic", $arr['media_id'], time() + 315360000);
return $arr['media_id'];
} else {
return $arr['errmsg'];
}
}else {
setcookie("scmedia_id", $scmedia_id, time() + 315360000);
return $scmedia_id;
}
}
/**
* 投放--群发获取素材
* @param string $type 创建的卡券类型
* @param string $pic 卡券的logo
* @return array 返回卡卷所需的Json数据
*/
public function sendGroup($card_id)
{
$token = $this->getAcctoken();
$url = "https://api.weixin.qq.com/card/mpnews/gethtml?access_token=".$token;
$data['card_id'] = $card_id;
$data_post = json_encode($data,JSON_UNESCAPED_UNICODE);
$res = $this->postUrl($url,$data_post);
$arr = json_decode($res,true);
return $arr;
}
/**
* 投放--创建卡券领取货架
* @param string $type 创建的卡券类型
* @param string $pic 卡券的logo
* @return array 返回卡卷所需的Json数据
*/
public function setHuojia($card_id_arr)
{
$token = $this->getAcctoken();
$url = "https://api.weixin.qq.com/card/landingpage/create?access_token=".$token;
$data = array(
"banner"=>"http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZJkmG8xXhiaHqkKSVMMWeN3hLut7X7hicFN",
"page_title"=> "优惠大派送",
"can_share"=>true,
"scene"=>"SCENE_NEAR_BY",
"card_list"=>$card_id_arr
);
$data_post = json_encode($data,JSON_UNESCAPED_UNICODE);
$res = $this->postUrl($url,$data_post);
$arr = json_decode($res,true);
return $arr;
}
/**
* 投放--创建二维码
* @param string $card_id 卡券的card_id
* @return html 返回二维码图片
*/
public function setQrcodepic($card_id)
{
$arr = $this->setQrcode($card_id);
if($arr['errcode'] > 0){
return $arr;
}else{
$ticket = $arr['ticket'];
$url ="https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=".$ticket;
return "<img src='".$url."'>";
}
}
/**
* 投放--创建二维码
* @param string $card_id 卡券的card_id
* @return array 返回卡卷所需的Json数据
*/
private function setQrcode($card_id)
{
$token = $this->getAcctoken();
$url = "https://api.weixin.qq.com/card/qrcode/create?access_token=".$token;
$data = array(
"action_name"=>"QR_CARD",
"expire_seconds"=>1800,
"action_info"=>array(
"card"=>array(
"card_id"=>$card_id,
"is_unique_code"=>false ,
"outer_str"=>"12b"
)
)
);
$data_post = json_encode($data,JSON_UNESCAPED_UNICODE);
$res = $this->postUrl($url,$data_post);
$arr = json_decode($res,true);
return $arr;
}
/**
* 创建---卡券
* @param string $type 创建的卡券类型
* @param string $data 卡券的相关信息
* $data = array(
'pic'=>$pic,
'type'=>'GROUPON',
'brand_name'=>'',
'code_type'=>'CODE_TYPE_QRCODE',
'title'=>'团购券',
'color'=>'Color010',
'notice'=>'使用时向服务员出示此券',
'service_phone'=>'服务电话',
'description'=>'不可与其他优惠同享\n如需团购券发票,请在消费时向商户提出\n店内均可使用,仅限堂食',
'date_info'=>array(
'type'=>'DATE_TYPE_FIX_TIME_RANGE',
'begin_timestamp'=>1566382996,
'end_timestamp'=>1566469396
),
'sku'=>array(
'quantity'=>500000
),
'use_limit'=>1000,
'get_limit'=>3,
'use_custom_code'=>false,
'bind_openid'=>false,
'can_share'=>true,
'can_give_friend'=>true,
'use_all_locations'=>true,
'source'=>'大众点评',
'use_condition'=>array(
"accept_category"=>"鞋类",
"reject_category"=>"阿迪达斯",
"can_use_with_other_discount"=>true
),
'abstract'=>array(
"abstract"=>"微信餐厅推出多种新季菜品,期待您的光临",
"icon_url_list"=>array("http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sj
piby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0")
),
'text_image_list'=>array(
array(
"image_url"=> "http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sjpiby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0",
"text"=>"此菜品精选食材,以独特的烹饪方法,最大程度地刺激食 客的味蕾"
),
array(
"image_url"=> "http://mmbiz.qpic.cn/mmbiz/p98FjXy8LacgHxp3sJ3vn97bGLz0ib0Sfz1bjiaoOYA027iasqSG0sjpiby4vce3AtaPu6cIhBHkt6IjlkY9YnDsfw/0",
"text"=>"此菜品精选食材,以独特的烹饪方法,最大程度地刺激食 客的味蕾"
)
),
'time_limit'=>array(
array(
"type"=>"MONDAY",
"begin_hour"=>0,
"end_hour"=>10,
"begin_minute"=>10,
"end_minute"=>59
),
array(
"type"=>"HOLIDAY"
)
),
'business_service'=>array(
"BIZ_SERVICE_FREE_WIFI",
"BIZ_SERVICE_WITH_PET",
"BIZ_SERVICE_FREE_PARK",
"BIZ_SERVICE_DELIVER"
),
);
* @return json 返回信息
*/
public function addCard($data)
{
$token = $this->getAcctoken();
$url = "https://api.weixin.qq.com/card/create?access_token=".$token;
$data_post = $this->cardInfo($data);
if($data_post == ''){
return error;
}
$res = $this->postUrl($url,$data_post);
return $res;
}
/**
* 上传卡券图片
* @param string $path 图片本地地址
* @return string 返回图片路径
*/
public function setPic($path)
{
$pic = isset($_COOKIE['pic'])?$_COOKIE['pic']:'';
if($pic == '') {
$token = $this->getAcctoken();
$url = "https://api.weixin.qq.com/cgi-bin/media/uploadimg?access_token=" . $token;
$data = array('buffer' => '@' . $path);
$res = $this->postUrl($url, $data);
$arr = json_decode($res, true);
if ($arr['url'] != '') {
setcookie("pic", $arr['url'], time() + 315360000);
return $arr['url'];
} else {
return $arr['errmsg'];
}
}else {
setcookie("pic", $pic, time() + 315360000);
return $pic;
}
}
/**
* 获取access_token
* @return string access_token
*/
public function getAcctoken()
{
$token = isset($_COOKIE['access_token'])?$_COOKIE['access_token']:'';
if($token == '' || $this->is_time($token)){
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$json = $this->getUrl($url);
$arr = json_decode($json,true);
setcookie("access_token", $arr['access_token'], time()+$arr['expires_in']);
return $arr['access_token'];
}else{
return $token;
}
}
/**
* 获取access_token
* @param string $token access_token
* @return bool true/false
*/
private function is_time($token)
{
$url = "https://api.weixin.qq.com/cgi-bin/getcallbackip?access_token=".$token;
$json = $this->getUrl($url);
$arr = json_decode($json,true);
if($arr['errcode'] > 0){
return true;
}else{
return false;
}
}
/**
* 创建卡券信息接口
* @param array $data 创建的卡券信息
* @return json 返回卡卷所需的Json数据
*/
private function cardInfo($data)
{
$mr = array('pic','type');
$canshu = array('logo_url', 'brand_name', 'code_type', 'title', 'color','notice','service_phone','description',
'date_info','sku',
'use_limit','get_limit','use_custom_code','bind_openid','can_share','can_give_friend',
'location_id_list',
'center_title','center_sub_title','center_url','custom_url_name','custom_url','custom_url_sub_title','promotion_url_name','promotion_url','source','use_all_locations'
);
$arr = array();
$pic = $this->setPic($data['pic']);
$base_info = array();
$advanced_info = array();
$base_info['logo_url'] = $pic;
foreach ($data as $k=>$v) {
if(in_array($k,$canshu))
{
$base_info[$k] = $v;
}else if(!in_array($k,$mr)){
$advanced_info[$k] = $v;
}
}
switch ($data['type']){
case 'GROUPON';//团购券
$arr['card_type'] = 'GROUPON';
$arr['groupon']['base_info'] = $base_info;
$arr['groupon']['advanced_info'] = $advanced_info;
$arr['groupon']['deal_detail'] = "双人套餐\n -进口红酒一支。\n孜然牛肉一份。";
break;
case 'CASH';//代金券
$arr['card_type'] = 'CASH';
$arr['cash']['base_info'] = $base_info;
$arr['cash']['advanced_info'] = $advanced_info;
$arr['cash']['least_cost'] = 1000;
$arr['cash']['reduce_cost'] = 100;
break;
case 'DISCOUNT';//折扣券
$arr['card_type'] = 'DISCOUNT';
$arr['discount']['base_info'] = $base_info;
$arr['discount']['advanced_info'] = $advanced_info;
$arr['discount']["discount"] = 20;
break;
case 'GIFT';//兑换券
$arr['card_type'] = 'GIFT';
$arr['gif']['base_info'] = $base_info;
$arr['gif']['advanced_info'] = $advanced_info;
$arr['gif']['gift'] = '可兑换音乐木盒一个';
break;
case 'GENERAL_COUPON';//优惠券
$arr['card_type'] = 'GENERAL_COUPON';
$arr['general_coupon']['base_info'] = $base_info;
$arr['general_coupon']['advanced_info'] = $advanced_info;
$arr['general_coupon']['default_detail'] = "优惠券专用,填写优惠详情";
break;
default;
return '';
}
$show['card'] = $arr;
return json_encode($show,JSON_UNESCAPED_UNICODE);
}
/**
* curl-get
* @param string $url 地址
* @return json 返回信息
*/
private function getUrl($url)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true) ;
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true) ;
curl_setopt($curl, CURLOPT_HTTPHEADER, array());
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
/**
* curl-post
* @param string $url 地址
* @param array $post_data 提交数据
* @return json 返回信息
*/
private function postUrl($url,$post_data)
{
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
curl_setopt($curl, CURLOPT_HTTPHEADER, array());
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
$data = curl_exec($curl);
curl_close($curl);
return $data;
}
}
微信卡券自写接口
最新推荐文章于 2021-04-11 04:49:34 发布