The public account is used to receive membership cards.
1, membership card to receive also need js-sdk interface (can refer to get the public account to obtain the user's geographic location information) (Reference URL: http://gaoboy.com/article/25.html)
2. A signature package needs to be obtained separately, which is different from the method used to obtain the geographical location of a user (here we will talk about how to obtain the signature package)
Obtain the js-sdk signature package:
1. combine the current url, timestamp, random string, and jsapiticket
// Call the js-sdk signature package public function getSignPackage () {$ jsapiTicket = $ this-> getJsApiTicket (); // note that the URL must be dynamically obtained and cannot be hardcode. (get the url of the current webpage) $ protocol = (! Empty ($ _ SERVER ['https']) & $ _ SERVER ['https']! = 'Off' | $ _ SERVER ['server _ port'] = 443 )? "Https: //": "http: //"; $ url = "$ protocol $ _ SERVER [HTTP_HOST] $ _ SERVER [REQUEST_URI]"; // timestamp $ timestamp = time (); // obtain a random string $ nonceStr = $ this-> createNonceStr (); // The Order of parameters here should be sorted by the key value ASCII code in ascending order $ string = "jsapi_ticket = $ jsapiTicket & noncestr = $ nonceStr & timestamp = $ timestamp & url = $ url "; // The generated string is $ signature = sha1 ($ string) for signature; $ signPackage = array ("appId" => $ this-> appid, "nonceStr" => $ nonceStr, "timestamp" => $ timestamp, "url" => $ url, "signature" => $ signature, "rawString" => $ string); return $ signPackage ;}
Obtain the membership card signature package:
// Use the signature package public function getHuiYuanSignPackage () {$ apiTicket = $ this-> getApiTicket (); // note that the URL must be dynamically obtained and cannot be hardcode. (get the url of the current webpage) $ protocol = (! Empty ($ _ SERVER ['https']) & $ _ SERVER ['https']! = 'Off' | $ _ SERVER ['server _ port'] = 443 )? "Https: //": "http: //"; $ url = "$ protocol $ _ SERVER [HTTP_HOST] $ _ SERVER [REQUEST_URI]"; // timestamp $ timestamp = time (); // obtain a random string // $ nonceStr = $ this-> createNonceStr (); // here, the parameter order should be sorted by the key value ASCII code in ascending order $ string = $ timestamp. $ apiTicket. "pVYA_t3RCVF_yhNcO6QCeAmb-1UI"; // The generated string is $ signature = sha1 ($ string) for signature; $ signPackage = array ("timestamp" => $ timestamp, "signature" => $ signature,); return $ signPackage ;}
Code Description:
HTML page:
// Introduce the js file <script src = "http://res.wx.qq.com/open/js/jweixin-1.0.0.js"> </script> <script type = "text/JavaScript"> // configure the information verification interface (enter the js-sdk to obtain signature package parameters) 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: [// All APIs to be called must be added to this list 'addcard']}); wx. ready (function () {// Add coupon document. querySelector ('# addCard '). onclick = function () {wx. addCard ({cardList: [{cardId: "", // id cardExt: '{"timestamp": "<? Php echo $ huiyuanPackage ['timestamp']?> "," Signature ":" <? Php echo $ huiyuanPackage ['signature']?> "} '// Signature package of the membership card}], // callback function after successful (whether the table database receives the membership card status through the callback function) success: function (res) {$. ajax ({url: '_ CONTROLLER _/editHuiYuan', type: 'post', ype: 'json', data: {is_LingQu: '1', user_id: "<? Php echo $ user ['user _ id']?> "}, Success: function () {$ (" # addCard ").html (" my membership card ") ;}}}) ;}); </script>
Code in the controller:
Class Library: http://www.bkjia.com/article/115732.htm
Public function index () {$ user_id = session ('user _ id'); if ($ user_id) {$ jssdk = new \ Home \ Model \ WechatModel (); $ signPackage = $ jssdk-> GetSignPackage (); // get the js-sdk signature package $ huiyuanPackage = $ jssdk-> getHuiYuanSignPackage (); get membership card signature package // get user information $ user = M ('user')-> where (array ('user _ id' => $ user_id )) -> find (); // product favorites quantity statistics $ goods_count = M ('goods _ shoucang ')-> where (array ('user _ id' => $ user_id )) -> count (); // store favorites quantity statistics $ shop_count = M ('shop _ shoucang ')-> where (array ('user _ id' => $ user_id )) -> count ();} else {// determines whether the user exists $ 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_accesen en ['openid']); session ('user _ id', $ rst ['user _ id']); $ jssdk = new \ Home \ Model \ WechatModel (); $ signPackage = $ jssdk-> GetSignPackage (); $ huiyuanPackage = $ jssdk-> getHuiYuanSignPackage (); // get user information $ user = M ('user')-> where (array ('user _ id' => $ rst ['user _ id']) -> find (); // product favorites quantity statistics $ goods_count = M ('goods _ shoucang ') -> where (array ('user _ id' => $ rst ['user _ id'])-> count (); // store favorites quantity statistics $ 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 ['vince ']. '-'. $ 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 (); // get user information $ user = M ('user ') -> where (array ('user _ id' => $ id)-> find (); // product favorites quantity statistics $ goods_count = M ('goods _ shoucang ') -> where (array ('user _ id' => $ id)-> count (); // store favorites count $ shop_count = M ('shop _ shoucang ') -> where (array ('user _ id' => $ id)-> count () ;}$ this-> assign ('signpackage', $ signPackage ); $ this-> assign ('huiyunpackage', $ huiyuanPackage); $ this-> assign ('user', $ user); $ this-> assign ('shop _ count ', $ shop_count); $ this-> assign ('goods _ count', $ goods_count); $ this-> display ();}
The above is a small part of the public number introduced to you to achieve membership card collection function, I hope to help you, if you have any questions, please leave a message, the small part will reply to you in a timely manner. Thank you very much for your support for the help House website!