Html5 + jquery get WeChat openid, html5openid

Source: Internet
Author: User
Tags openid

Html5 + jquery get openid, html5openid

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

<Head> <meta charset = "UTF-8"> <title> Pascal mall </title> <meta name = "viewport" content = "width = device-width, initial-scale = 1.0, maximum-scale = 1.0, user-scalable = 0 "> <meta name =" apple-mobile-web-app-capable "content =" yes "> <meta name =" apple-mobile-web-app -status-bar-style "content =" black "> <meta name =" format-detection "content =" telephone = no "> <link rel =" stylesheet "type =" text/css "href =" css/reset.css "> <link rel =" stylesheet "type =" text/css "href =" css/main.css "> <link rel =" stylesheet "type =" text/css "href =" css/index.css "> <link rel =" stylesheet "type =" text/css "href =" css/member.css "> <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()<span style="white-space:pre"></span>{            $url = $this->createOauthUrlForOpenid();            return $this->httpGet($url);<span style="white-space:pre"></span>}
/** <Span style = "white-space: pre"> </span> * <span style = "white-space: pre"> </span>: generate the url <span style = "white-space: pre"> </span> */<span style = "white-space: pre "> </span> function createOauthUrlForOpenid () <span style =" white-space: pre "> </span> {<span style =" white-space: pre "> </span> $ urlObj [" appid "] = WxPayConf_pub: APPID; <span style =" white-space: pre "> </span> $ urlObj [" secret "] = WxPayConf_pub: APPS ECRET; <span style = "white-space: pre"> </span> $ urlObj ["code"] = $ this-> code; <span style = "white-space: pre"> </span> $ urlObj ["grant_type"] = "authorization_code"; <span style = "white-space: pre "> </span> $ bizString = $ this-> formatBizQueryParaMap ($ urlObj, false); // api.weixin.qq.com = 101.226.90.58 <span style =" white-space: pre "> </span> return" https://api.weixin.qq.com/sns/oauth2/access_token? ". $ BizString; <span style =" white-space: pre "> </span>}
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)





Related Article

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.