欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

微信公众号实现会员卡领取功能

程序员文章站 2024-03-09 16:04:11
1、会员卡的领取也需要js-sdk接口(可以参考获取微信公众号获取用户的地理位置信息)(借鉴网址:) 2、 比获取用户地理位置信息多了一个是需要单独获取签名包,签名方式也...

1、会员卡的领取也需要js-sdk接口(可以参考获取微信公众号获取用户的地理位置信息)(借鉴网址:)

2、 比获取用户地理位置信息多了一个是需要单独获取签名包,签名方式也和获取用户地理位置的不同(这里再说一下获取签名包的方式)

      获取js-sdk签名包:

           1、当前的url、时间戳、随机字符串、jsapiticket进行组合                

 //调用js-sdk的签名包
 public function getsignpackage() {
 $jsapiticket = $this->getjsapiticket();
 // 注意 url 一定要动态获取,不能 hardcode.(获取当前网页的url)
 $protocol = (!empty($_server['https']) && $_server['https'] !== 'off' || $_server['server_port'] == 443) ? "https://" : "http://";
 $url = "$protocol$_server[http_host]$_server[request_uri]";
 //时间戳
 $timestamp = time();
 //随机字符串获取
 $noncestr = $this->createnoncestr();
 // 这里参数的顺序要按照 key 值 ascii 码升序排序
 $string = "jsapi_ticket=$jsapiticket&noncestr=$noncestr&timestamp=$timestamp&url=$url";
 //生成字符串是用来签名用的
 $signature = sha1($string);
 $signpackage = array(
  "appid"  => $this->appid,
  "noncestr" => $noncestr,
  "timestamp" => $timestamp,
  "url"  => $url,
  "signature" => $signature,
  "rawstring" => $string
 );
 return $signpackage; 
 }

     获取会员卡签名包:      

//使用会员卡领取的签名包
 public function gethuiyuansignpackage() {
 $apiticket = $this->getapiticket();
 // 注意 url 一定要动态获取,不能 hardcode.(获取当前网页的url)
 $protocol = (!empty($_server['https']) && $_server['https'] !== 'off' || $_server['server_port'] == 443) ? "https://" : "http://";
 $url = "$protocol$_server[http_host]$_server[request_uri]";
 //时间戳
 $timestamp = time();
 //随机字符串获取
 // $noncestr = $this->createnoncestr();
 // 这里参数的顺序要按照 key 值 ascii 码升序排序
 $string = $timestamp.$apiticket."pvya_t3rcvf_yhnco6qceamb-1ui";
 //生成字符串是用来签名用的
 $signature = sha1($string);
 $signpackage = array(
  "timestamp" => $timestamp,
  "signature" => $signature,
 );
 return $signpackage; 
 }

详细代码说明:

     html页面:         

  //引入微信js文件
   <script src="http://res.wx.qq.com/open/js/jweixin-1.0.0.js"></script>
<script type="text/javascript">
//配置信息验证接口(填写的js-sdk获取的签名包的参数)
wx.config({
 debug: false,
 appid: '<?php echo $signpackage["appid"];?>',
 timestamp: '<?php echo $signpackage["timestamp"];?>',
 noncestr: '<?php echo $signpackage["noncestr"];?>',
 signature: '<?php echo $signpackage["signature"];?>',
 jsapilist: [
  // 所有要调用的 api 都要加到这个列表中
  'addcard'
  ]
   });
wx.ready(function(){
        //添加卡券
    document.queryselector('#addcard').onclick = function () {
     wx.addcard({
      cardlist: [
      {
       cardid: "",//微信公众号内创建的会员卡的id
       cardext: '{"timestamp":"<?php echo $huiyuanpackage['timestamp'] ?>","signature":"<?php echo $huiyuanpackage['signature'] ?>"}'//会员卡的签名包
      }
      ],
     //成功之后的回调的函数(通过回调函数该表数据库是否领取会员卡的状态)
      success: function (res) {
      $.ajax({
      url: '__controller__/edithuiyuan',
      type: 'post',
      datatype: 'json',
      data: {is_lingqu: '1',user_id:"<?php echo $user['user_id'] ?>"},
      success:function(){
      $("#addcard").html("我的会员卡");
      }
      })
      }
     });
    };
   });
</script>

控制器中的代码:

   类库://www.jb51.net/article/115732.htm

 public function index(){
 $user_id = session('user_id');
  if($user_id){
  $jssdk = new \home\model\wechatmodel();
  $signpackage = $jssdk->getsignpackage();//获取js-sdk签名包
  $huiyuanpackage = $jssdk->gethuiyuansignpackage();获取会员卡签名包
  //获取用户信息 
  $user = m('user')->where(array('user_id' => $user_id))->find();
  //产品收藏数量统计
  $goods_count = m('goods_shoucang')->where(array('user_id' => $user_id))->count();
  //门店收藏数量统计
  $shop_count = m('shop_shoucang')->where(array('user_id' => $user_id))->count();
  }else{
  //判断该用户是否存在
  $model = new \home\model\wechatmodel();
  $openid_accesstoken = $model->openid();
  $rst = m('user')->where(array('user_openid' => $openid_accesstoken['openid']))->find();
  if($rst){
   session('openid',$openid_accesstoken['openid']);
   session('user_id', $rst['user_id']);
   $jssdk = new \home\model\wechatmodel();
   $signpackage = $jssdk->getsignpackage();
   $huiyuanpackage = $jssdk->gethuiyuansignpackage();
   //获取用户信息 
   $user = m('user')->where(array('user_id' => $rst['user_id']))->find();
   //产品收藏数量统计
   $goods_count = m('goods_shoucang')->where(array('user_id' => $rst['user_id']))->count();
   //门店收藏数量统计
   $shop_count = m('shop_shoucang')->where(array('user_id' => $rst['user_id']))->count();
  }else{
   $userinfo = $model->getopenid($openid_accesstoken['openid'],$openid_accesstoken['access_token']);
     $data = array(
      'user_img' => $userinfo['headimgurl'],
      'user_openid' => $userinfo['openid'],
      'user_name' => filter($userinfo['nickname']),
      'user_register_time' => time(),
      'city' => $userinfo['province'].'-'.$userinfo['city'],
     );
   $id = m('user')->add($data);
   session('openid', $userinfo['openid']);
   session('user_id',$id);
   $jssdk = new \home\model\wechatmodel();
   $signpackage = $jssdk->getsignpackage();
   $huiyuanpackage = $jssdk->gethuiyuansignpackage();
   //获取用户信息 
   $user = m('user')->where(array('user_id' => $id))->find();
   //产品收藏数量统计
   $goods_count = m('goods_shoucang')->where(array('user_id' => $id))->count();
   //门店收藏数量统计
   $shop_count = m('shop_shoucang')->where(array('user_id' => $id))->count();
   }
  }
  $this->assign('signpackage', $signpackage);
  $this->assign('huiyuanpackage', $huiyuanpackage);
  $this->assign('user', $user);
  $this->assign('shop_count', $shop_count);
  $this->assign('goods_count', $goods_count);
  $this->display();
 }

以上所述是小编给大家介绍的微信公众号实现会员卡领取功能,希望对大家有所帮助