Html5 + jquery get WeChat openid

Source: Internet
Author: User
Tags openid

Html5 + jquery get openid

Recently, I was modifying a mobile mall. Because the wap end is HTML5 and the art template technology is widely used, the modification was not suitable at the beginning, and the implementation of HTML5 using openid has never been very good, after understanding and learning the acquisition principle, we finally implemented HTML to get openid, which was slightly changed. Let's talk about it in a few minutes and do it directly:

1. Write a js file for obtaining the accesscode and directly reading the registration information in the User table through openid.

 

$ (Function () {var wxopenid = getcookie ('wxopenid'); var key = getcookie ('key'); if (key = '') {var access_code = GetQueryString ('code'); if (wxopenid = ") {if (access_code = null) {var fromurl = location. href; var url = 'https: // open.weixin.qq.com/connect/oauth2/authorize? Appid = fill in your own appid yo & redirect_uri = '+ encodeURIComponent (fromurl) + '& response_type = code & scope = snsapi_base & state = STATE % 23wechat_redirect & connect_redirect = 1 # wechat_redirect'; location. href = url;} else {$. ajax ({type: 'get', url: ApiUrl + '/index. php? Act = payment & op = getopenid ', async: false, cache: false, data: {code: access_code}, dataType: 'json', success: function (result) {if (result! = Null & result. hasOwnProperty ('openid') & result. openid! = "") {Addcookie ('wxopenid', result. openid, 360000); getlogininfo (result. openid);} else {alert ('identification failed \ n' + result); location. href = fromurl ;}}) ;}} else {if (key = ''& wxopenid! = '') Getlogininfo (wxopenid);} function getlogininfo (wxopenid) {$. ajax ({type: 'get', url: ApiUrl + '/index. php? Act = login & op = autologininfo ', data: {wxopenid: wxopenid}, dataType: 'json', async: false, cache: false, success: function (result) {if (result. return_code = 'OK') {addcookie ('key', result. memberinfo. key); addcookie ('username', result. memberinfo. username);} else {alert (result. return_msg); location. href = WapSiteUrl + '/tmpl/member/login.html ';}}});}}});
Step 2: load the js file in the head of the html page

 

 

          <Script type = "text/javascript" src = "js/config. js "> </script> <script type =" text/javascript "src =" js/zepto. min. js "> </script> <script type =" text/javascript "src =" js/template. js "> </script> <script type =" text/javascript "src =" js/common. js "> </script> <script type =" text/javascript "src =" js/getwxopenid_index.js "> </script> Step 3: implement the background method of openid, this method is not detailed. An official example is provided. 

 

 

/*** GET openid * @ return type */public function getopenidOp () {$ jsApi = new JsApi_pub (); $ code = $ _ GET ['code']; $ jsApi-> setCode ($ code); echo $ jsApi-> getOpenId ();}
function getOpenid(){            $url = $this->createOauthUrlForOpenid();            return $this->httpGet($url);}
/*** Function: generate a url that can obtain the openid */function createOauthUrlForOpenid () {$ urlObj ["appid"] = WxPayConf_pub: APPID; $ urlObj ["secret"] = WxPayConf_pub: APPSECRET; $ urlObj ["code"] = $ this-> code; $ urlObj ["grant_type"] = "authorization_code "; $ bizString = $ this-> formatBizQueryParaMap ($ urlObj, false); // api.weixin.qq.com = 101.226.90.58return "https://api.weixin.qq.com/sns/oauth2/access_token? ". $ BizString ;}
In addition, do not use global variables in js files. When multiple js files are loaded, variables may conflict, resulting in some non-precondition problems;
In this way, the accesscode is directly obtained on the html5 page, and then the openid is obtained in the background using the ajax method;

 

If you want to see the effect, you can scan the QR code to enter the mall (originally, you just wanted to put a QR code, and someone was afraid of what the download link was)



 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.