


Share the WeChat public account to realize the function of receiving membership cards
Jun 16, 2017 am 10:00 AMThis 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
#//調(diào)用js-sdk的簽名包 public function getSignPackage() { $jsapiTicket = $this->getJsApiTicket(); // 注意 URL 一定要動態(tài)獲取,不能 hardcode.(獲取當(dāng)前網(wǎng)頁的url) $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //時間戳 $timestamp = time(); //隨機(jī)字符串獲取 $nonceStr = $this->createNonceStr(); // 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序 $string = "jsapi_ticket=$jsapiTicket&noncestr=$nonceStr×tamp=$timestamp&url=$url"; //生成字符串是用來簽名用的 $signature = sha1($string); $signPackage = array( "appId" => $this->appid, "nonceStr" => $nonceStr, "timestamp" => $timestamp, "url" => $url, "signature" => $signature, "rawString" => $string ); return $signPackage; }## 獲##
//使用會員卡領(lǐng)取的簽名包 public function getHuiYuanSignPackage() { $apiTicket = $this->getApiTicket(); // 注意 URL 一定要動態(tài)獲取,不能 hardcode.(獲取當(dāng)前網(wǎng)頁的url) $protocol = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off' || $_SERVER['SERVER_PORT'] == 443) ? "https://" : "http://"; $url = "$protocol$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; //時間戳 $timestamp = time(); //隨機(jī)字符串獲取 // $nonceStr = $this->createNonceStr(); // 這里參數(shù)的順序要按照 key 值 ASCII 碼升序排序 $string = $timestamp.$apiTicket."pVYA_t3RCVF_yhNcO6QCeAmb-1UI"; //生成字符串是用來簽名用的 $signature = sha1($string); $signPackage = array( "timestamp" => $timestamp, "signature" => $signature, ); return $signPackage; }
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"> //配置信息驗(yàn)證接口(填寫的js-sdk獲取的簽名包的參數(shù)) 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: [ // 所有要調(diào)用的 API 都要加到這個列表中 'addCard' ] }); wx.ready(function(){ //添加卡券 document.querySelector('#addCard').onclick = function () { wx.addCard({ cardList: [ { cardId: "",//微信公眾號內(nèi)創(chuàng)建的會員卡的id cardExt: '{"timestamp":"<?php echo $huiyuanPackage['timestamp'] ?>","signature":"<?php echo $huiyuanPackage['signature'] ?>"}'//會員卡的簽名包 } ], //成功之后的回調(diào)的函數(shù)(通過回調(diào)函數(shù)該表數(shù)據(jù)庫是否領(lǐng)取會員卡的狀態(tài)) 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>
Code in the controller:
Class library: http://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(); //產(chǎn)品收藏數(shù)量統(tǒng)計 $goods_count = M('goods_shoucang')->where(array('user_id' => $user_id))->count(); //門店收藏數(shù)量統(tǒng)計 $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(); //產(chǎn)品收藏數(shù)量統(tǒng)計 $goods_count = M('goods_shoucang')->where(array('user_id' => $rst['user_id']))->count(); //門店收藏數(shù)量統(tǒng)計 $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(); //產(chǎn)品收藏數(shù)量統(tǒng)計 $goods_count = M('goods_shoucang')->where(array('user_id' => $id))->count(); //門店收藏數(shù)量統(tǒng)計 $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(); }
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!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Troubleshooting and solutions to the company's security software that causes some applications to not function properly. Many companies will deploy security software in order to ensure internal network security. ...

H5. The main difference between mini programs and APP is: technical architecture: H5 is based on web technology, and mini programs and APP are independent applications. Experience and functions: H5 is light and easy to use, with limited functions; mini programs are lightweight and have good interactiveness; APPs are powerful and have smooth experience. Compatibility: H5 is cross-platform compatible, applets and APPs are restricted by the platform. Development cost: H5 has low development cost, medium mini programs, and highest APP. Applicable scenarios: H5 is suitable for information display, applets are suitable for lightweight applications, and APPs are suitable for complex functions.

H5 development tools recommendations: VSCode, WebStorm, Atom, Brackets, Sublime Text; Mini Program Development Tools: WeChat Developer Tools, Alipay Mini Program Developer Tools, Baidu Smart Mini Program IDE, Toutiao Mini Program Developer Tools, Taro.

The choice of H5 and applet depends on the requirements. For applications with cross-platform, rapid development and high scalability, choose H5; for applications with native experience, rich functions and platform dependencies, choose applets.

There are differences in the promotion methods of H5 and mini programs: platform dependence: H5 depends on the browser, and mini programs rely on specific platforms (such as WeChat). User experience: The H5 experience is poor, and the mini program provides a smooth experience similar to native applications. Communication method: H5 is spread through links, and mini programs are shared or searched through the platform. H5 promotion methods: social sharing, email marketing, QR code, SEO, paid advertising. Mini program promotion methods: platform promotion, social sharing, offline promotion, ASO, cooperation with other platforms.

The best cryptocurrency trading and analysis platforms include: 1. OKX: the world's number one in trading volume, supports multiple transactions, provides AI market analysis and on-chain data monitoring. 2. Binance: The world's largest exchange, providing in-depth market conditions and new currency first-time offerings. 3. Sesame Open Door: Known for spot trading and OTC channels, it provides automated trading strategies. 4. CoinMarketCap: an authoritative market data platform, covering 20,000 currencies. 5. CoinGecko: Known for community sentiment analysis, it provides DeFi and NFT trend monitoring. 6. Non-small account: a domestic market platform, providing analysis of linkage between A-shares and currency markets. 7. On-chain Finance: Focus on blockchain news and update in-depth reports every day. 8. Golden Finance: 24 small

The login portal for the Douyin web version is https://www.douyin.com/. The login steps include: 1. Open the browser; 2. Enter the URL https://www.douyin.com/; 3. Click the "Login" button and select the login method; 4. Enter the account password; 5. Complete login. The web version provides functions such as browsing, searching, interaction, uploading videos and personal homepage management, and has advantages such as large-screen experience, multi-tasking, convenient account management and data statistics.

10 top scams on cryptocurrency exchanges Common scams: fake exchanges, Ponzi capital trading, contract manipulation, fake coin phishing, customer service fraud, etc. Identification points: Check regulatory licenses, check contract addresses, and be wary of high-yield commitments Must be protected: Use only mainstream exchanges (Binance/Coinbase) Enable hardware wallet Reject share private key/verification code Deal with fraud: take screenshots immediately, freeze assets, report on the platform, and report to the police Core principle: Any request for password/transfer is a fraud!
