Access to WeChat public address openId


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 =;
       if (r != null) {
         return unescape(r[2]);
       return null;
     getToken: function() {
       //Determine whether there is openid
       if (this.$cookieStore.getCookie("openid") == null) {
         var url =
           "" +
           ) +
         location.href = url;
         var code = this.GetQueryString("code");
         // console.log(code);
           URL: "interface name" + code
         }).then(res => {
           // console.log(res);
           if ( == 0) {
       } 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.