Micro-channel public number of acquisition openId

In the small partners to develop micro-channel public number, or applet in the micro-channel built-in browser to open the project, the first problem encountered is how to obtain openId, Xiao Bian gave you today is how to bring obtain openId.

  First,    we need to get micro letter backstage appid developer, this is the administrator appid taken when setting up micro letter backstage, and the only, but also need to set a callback domain name in the micro letter backstage.

  Secondly, after these are ready, we can use the micro-channel built-in method to get openId:

 

 

     Note: The underlined part is openId domain name and a callback to be acquired, and location.href = url when the page is first rendered automatically get openId, of course, these preparations or

 1 //截取URL字段
 2     GetQueryString: function(name) {
 3       var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
 4       var r = window.location.search.substr(1).match(reg);
 5       if (r != null) {
 6         return unescape(r[2]);
 7       }
 8       return null;
 9     },
10     getToken: function() {
11       //判断是否有openid
12       if (this.$cookieStore.getCookie("openid") == null) {
13         var url =
14           "https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx1234567890&redirect_uri=" +
15           encodeURIComponent(
16             "https://www.baidu.com/"
17           ) +
18           "&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect&connect_redirect=1#wechat_redirect";
19         location.href = url;
20         var code = this.GetQueryString("code");
21         // console.log(code);
22         axios({
23           url: "接口名" + code
24         }).then(res => {
25           // console.log(res);
26           if (res.data.code == 0) {
27             this.$cookieStore.setCookie("openid", res.data.result);
28           }
29         });
30       } else {
31         this.openid = this.$cookieStore.getCookie("openid");
32       }
33     },

 

      We want to use the chart to get the code value, through the interface, to get openId, then there is openId cookie in each call on it.

This is the method to get openId of small to give us, here is the complete code.

Guess you like

Origin www.cnblogs.com/kanglinen/p/11490276.html