本文章主要介绍了,怎么实现把优惠券添加到微信卡包
前端部分
这一部分没有什么好说的
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>添加优惠券</title>
<script src="/public/assets/plugins/jQuery/jquery-2.1.1.min.js"></script>
<style>
.title{
font-size: 20px;
text-align: center;
}
.mainform{
width:400px;
margin: 10px auto;
border: 1px #eee solid;
}
.mainform .list {
height: 50px;
line-height: 50px;
}
.mainform .list span {
display: inline-block;
width:120px;
margin-left:8px;
}
.button {
width:100px;
margin:0 auto;
}
.button button {
border: 1px #eee solid;
background: #4288ce;
padding: 10px;
color: #fff;
border-radius: 6px;
}
</style>
</head>
<body>
<div class="title">添加优惠券</div>
<div class="mainform">
<div class="list">
<span> 优惠券logo</span><input type="text" id="logo" />
</div>
<div class="list">
<span> 品牌名称</span><input type="text" id="brand_name" />
</div>
<div class="list">
<span> 优惠券名称</span><input type="text" id="title" />
</div>
<div class="list">
<span> 优惠券说明</span><input type="text" id="sub_title" />
</div>
<div class="list">
<span> 优惠券数量</span><input type="text" id="quantity" />
</div>
</div>
<div class="button"><button id="btnSave">创建优惠券</button></div>
<script>
$(function(){
$('#btnSave').click(function(){
var logo = $('#logo').val();
var brand_name = $('#brand_name').val();
var title = $('#title').val();
var sub_title = $('#sub_title').val();
var quantity = $('#quantity').val();
var params = {
logo,
brand_name,
title,
sub_title,
quantity
}
$.post('/api/index.php/coupon/card/doadd',params,function(res){
alert("添加成功")
})
})
})
</script>
</body>
</html>
服务器端部分
关于这部分的接口说明,请参考微信官方文档说明: https://developers.weixin.qq.com/doc/offiaccount/Cards_and_Offer/Create_a_Coupon_Voucher_or_Card.html
$logo = input('post.logo');
$brand_name = input('post.brand_name');
$title = input('post.title');
$sub_title = input('post.sub_title');
$quantity = input('post.quantity');
$data=array (
'card' =>
array (
'card_type' => 'GENERAL_COUPON', //卡券类型:优惠券
'general_coupon' =>
array (
'base_info' =>
array (
'logo_url' => $logo,//'http://mmbiz.qpic.cn/mmbiz/iaL1LJM1mF9aRKPZJkmG8xXhiaHqkKSVMMWeN3hLut7X7hicFNjakmxibMLGWpXrEXB33367o7zHN0CwngnQY7zb7g/0',
'brand_name' => $brand_name,
'code_type' => 'CODE_TYPE_QRCODE', //卡券类型
'title' => $title,
'sub_title' => $sub_title,
'color' => 'Color030',//颜色
'notice' => '使用时请出示此券', //展示显示的卡券
'service_phone' => '18022389022',
'date_info' =>
array (
'type' => 'DATE_TYPE_FIX_TERM', //时间类型
'fixed_term' => 90,//有效期限
'fixed_begin_term' => 0,//从什么时候开始
),
'sku' =>
array (
'quantity' => $quantity,//库存
),
'get_limit' => 100,//每个用户最多可以领取数量
'bind_openid' => false,//没有绑定领取的用户
'can_share' => true,//可以分享
'can_give_friend' => false//赠送给朋友
/**
'center_title' => '立即使用1',//中间展示按钮
'center_sub_title' => '点击立即使用',
'center_url' => 'https://www.baidu.com/',//跳转地址
'custom_url_name' => '立即使用2',
'custom_url' => 'https://www.baidu.com/',//底部跳转入口
'custom_url_sub_title' => '点击跳转' **/
),
'default_detail' => '专用优惠券,不可与其他优惠同享'
),
),
);
$wx = new wxhelper(config('wx')['AppID'],config('wx')['AppSecret']);
$res = $wx->cardCreate($data);
- 关于getAccessToken 、 cardCreate 、 cardQrcodeCreate函数
/***
/***
* @return mixed
* 获取access_token
*/
public function getAccessToken() {
// access_token 应该全局存储与更新,以下代码以写入到文件中做示例
$data = json_decode($this->get_php_file(APP_PATH ."access_token.php"));
if ($data->expire_time < time()) {
$url ="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
$res = json_decode($this->httpGet($url));
if(isset($res->errcode))
{
return $res;
}
$access_token = $res->access_token;
if ($access_token) {
$data->expire_time = time() + 7000;
$data->access_token = $access_token;
$this->set_php_file("access_token.php", json_encode($data));
}
} else {
$access_token = $data->access_token;
}
return $access_token;
}
/***
* @param $data
* @return mixed
* 创建卡券
*/
public function cardCreate($data) {
$token= $this->getAccessToken();
$url = 'https://api.weixin.qq.com/card/create?access_token='.$token;
$res=json_decode($this->http_post($url,json_encode($data,JSON_UNESCAPED_UNICODE)));
// return $res;
$card_id = $res->card_id;
return self::cardQrcodeCreate($card_id);
}
function cardQrcodeCreate($card_id) {
$token= $this->getAccessToken();
$url = "https://api.weixin.qq.com/card/qrcode/create?access_token=".$token;
$json='
{
"action_name": "QR_CARD",
"expire_seconds": 1800,
"action_info": {
"card": {
"card_id": "'.$card_id.'",
"is_unique_code": false
}
}
}';
$res=json_decode($this->http_post($url,$json));
return $res;
}
到目前为止,已经陈成功添加上了微信卡券了,并生成了二维码
请继续关注,微信小程序卡券系列之-卡券展示与核销。