微信卡包系列-添加微信卡券优惠券

微信卡包系列-添加微信卡券优惠券


本文章主要介绍了,怎么实现把优惠券添加到微信卡包

前端部分

这一部分没有什么好说的

<!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;
     }

到目前为止,已经陈成功添加上了微信卡券了,并生成了二维码
在这里插入图片描述在这里插入图片描述

请继续关注,微信小程序卡券系列之-卡券展示与核销

  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

tiger-doo

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值