Share the WeChat public account to realize the function of receiving membership cards

怪我咯
Release: 2017-06-16 10:00:09
Original
8493 people have browsed it

This article mainly introduces the relevant information on the WeChat public account to realize the membership card collection function. Friends who need it can refer to

1. The collection of membership cards also requires the js-sdk interface (you can refer to Obtaining WeChat The public account obtains the user’s geographical location information) (reference URL: http://gaoboy.com/article/25.html)

2. One more thing than obtaining the user’s geographical location information is that you need to obtain the signature package separately. , the signature method is also different from obtaining the user's geographical location (here we will talk about the method of obtaining the signature package)

Obtain the js-sdk signature package:

1. The current URL, timestamp, random string, JSAPITICKET for combination

#
 //调用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; 
 }
Copy after login

## 获##
//使用会员卡领取的签名包
 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; 
 }
Copy after login

Detailed code description:

HTML page:

  //引入微信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: &#39;<?PHP echo $signPackage["appId"];?>&#39;,
 timestamp: &#39;<?php echo $signPackage["timestamp"];?>&#39;,
 nonceStr: &#39;<?php echo $signPackage["nonceStr"];?>&#39;,
 signature: &#39;<?php echo $signPackage["signature"];?>&#39;,
 jsApiList: [
  // 所有要调用的 API 都要加到这个列表中
  &#39;addCard&#39;
  ]
   });
wx.ready(function(){
        //添加卡券
    document.querySelector(&#39;#addCard&#39;).onclick = function () {
     wx.addCard({
      cardList: [
      {
       cardId: "",//微信公众号内创建的会员卡的id
       cardExt: &#39;{"timestamp":"<?php echo $huiyuanPackage[&#39;timestamp&#39;] ?>","signature":"<?php echo $huiyuanPackage[&#39;signature&#39;] ?>"}&#39;//会员卡的签名包
      }
      ],
     //成功之后的回调的函数(通过回调函数该表数据库是否领取会员卡的状态)
      success: function (res) {
      $.ajax({
      url: &#39;__CONTROLLER__/editHuiYuan&#39;,
      type: &#39;post&#39;,
      dataType: &#39;json&#39;,
      data: {is_LingQu: &#39;1&#39;,user_id:"<?php echo $user[&#39;user_id&#39;] ?>"},
      success:function(){
      $("#addCard").html("我的会员卡");
      }
      })
      }
     });
    };
   });
</script>
Copy after login

Code in the controller:

Class library: http://www.jb51.net/article/115732.htm

 public function index(){
 $user_id = session(&#39;user_id&#39;);
  if($user_id){
  $jssdk = new \Home\Model\WechatModel();
  $signPackage = $jssdk->GetSignPackage();//获取js-sdk签名包
  $huiyuanPackage = $jssdk->getHuiYuanSignPackage();获取会员卡签名包
  //获取用户信息 
  $user = M(&#39;user&#39;)->where(array(&#39;user_id&#39; => $user_id))->find();
  //产品收藏数量统计
  $goods_count = M(&#39;goods_shoucang&#39;)->where(array(&#39;user_id&#39; => $user_id))->count();
  //门店收藏数量统计
  $shop_count = M(&#39;shop_shoucang&#39;)->where(array(&#39;user_id&#39; => $user_id))->count();
  }else{
  //判断该用户是否存在
  $model = new \Home\Model\WechatModel();
  $openid_accesstoken = $model->openId();
  $rst = M(&#39;user&#39;)->where(array(&#39;user_openid&#39; => $openid_accesstoken[&#39;openid&#39;]))->find();
  if($rst){
   session(&#39;openid&#39;,$openid_accesstoken[&#39;openid&#39;]);
   session(&#39;user_id&#39;, $rst[&#39;user_id&#39;]);
   $jssdk = new \Home\Model\WechatModel();
   $signPackage = $jssdk->GetSignPackage();
   $huiyuanPackage = $jssdk->getHuiYuanSignPackage();
   //获取用户信息 
   $user = M(&#39;user&#39;)->where(array(&#39;user_id&#39; => $rst[&#39;user_id&#39;]))->find();
   //产品收藏数量统计
   $goods_count = M(&#39;goods_shoucang&#39;)->where(array(&#39;user_id&#39; => $rst[&#39;user_id&#39;]))->count();
   //门店收藏数量统计
   $shop_count = M(&#39;shop_shoucang&#39;)->where(array(&#39;user_id&#39; => $rst[&#39;user_id&#39;]))->count();
  }else{
   $userInfo = $model->getOpenId($openid_accesstoken[&#39;openid&#39;],$openid_accesstoken[&#39;access_token&#39;]);
     $data = array(
      &#39;user_img&#39; => $userInfo[&#39;headimgurl&#39;],
      &#39;user_openid&#39; => $userInfo[&#39;openid&#39;],
      &#39;user_name&#39; => filter($userInfo[&#39;nickname&#39;]),
      &#39;user_register_time&#39; => time(),
      &#39;city&#39; => $userInfo[&#39;province&#39;].&#39;-&#39;.$userInfo[&#39;city&#39;],
     );
   $id = M(&#39;user&#39;)->add($data);
   session(&#39;openid&#39;, $userInfo[&#39;openid&#39;]);
   session(&#39;user_id&#39;,$id);
   $jssdk = new \Home\Model\WechatModel();
   $signPackage = $jssdk->GetSignPackage();
   $huiyuanPackage = $jssdk->getHuiYuanSignPackage();
   //获取用户信息 
   $user = M(&#39;user&#39;)->where(array(&#39;user_id&#39; => $id))->find();
   //产品收藏数量统计
   $goods_count = M(&#39;goods_shoucang&#39;)->where(array(&#39;user_id&#39; => $id))->count();
   //门店收藏数量统计
   $shop_count = M(&#39;shop_shoucang&#39;)->where(array(&#39;user_id&#39; => $id))->count();
   }
  }
  $this->assign(&#39;signPackage&#39;, $signPackage);
  $this->assign(&#39;huiyuanPackage&#39;, $huiyuanPackage);
  $this->assign(&#39;user&#39;, $user);
  $this->assign(&#39;shop_count&#39;, $shop_count);
  $this->assign(&#39;goods_count&#39;, $goods_count);
  $this->display();
 }
Copy after login

The above is the detailed content of Share the WeChat public account to realize the function of receiving membership cards. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!