Access to WeChat public address openId

Time:2019-11-14

When WeChat partners are developing WeChat public numbers, small programs or WeChat’s built-in browser, the first problem they will encounter is how to get openId. Today, Xiaobian brings us how to get openId.

  First We need to get the appid from the wechat developer background. The appid is the only one obtained by the administrator when setting the wechat background. In addition, we need to set the callback domain name in the wechat background.

  SecondlyWhen these are ready, we can use wechat’s own method to obtain openid:

 

 

Note: the dashed part is the openid and callback domain name to be obtained, and ‚ location.href = ‚ URL is the openid to be obtained automatically when the page is rendered for the first time. Of course, these are preparations

//Intercept URL field
     GetQueryString: function(name) {
       var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
       var r = window.location.search.substr(1).match(reg);
       if (r != null) {
         return unescape(r[2]);
       }
       return null;
     },
     getToken: function() {
       //Determine whether there is openid
       if (this.$cookieStore.getCookie("openid") == null) {
         var url =
           "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1234567890&redirect_uri=" +
           encodeURIComponent(
             "https://www.baidu.com/"
           ) +
           "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect&connect_redirect=1#wechat_redirect";
         location.href = url;
         var code = this.GetQueryString("code");
         // console.log(code);
         axios({
           URL: "interface name" + code
         }).then(res => {
           // console.log(res);
           if (res.data.code == 0) {
             this.$cookieStore.setCookie("openid", res.data.result);
           }
         });
       } else {
         this.openid = this.$cookieStore.getCookie("openid");
       }
     },

 

We need to use the above method to get the code value, through the interface to get the openid, and then store the openid in the cookie every time we call it.

This is the method of getting openid brought by Xiaobian. Here is the complete code.

Recommended Today

How to search files or directories under CentOS

Search for files or directories: The search for files is great! Because we often need to know where that file is, let’s talk about how to search it! There are also quite excellent ones under Linux Search system! Usually find is not very common! Because the speed is slow, but also very hard disk! Usually […]