[Applet] micro letter how small landing procedures and obtain user information

Figure:

1. applet js-side call framework landed API, to obtain a temporary code, take this code to call your own server end interface

2. In your own server, you can use app_id app_secrect code to get the user's openid, this is the user id and a unique id will not change, you can put the id back to the end applet
applet locally saved end openid

3. Get the user information is simple and does not require its own server processing, requires only a small API program by the client.
Wxml front page using a button, the button has increased by several attributes, the method may call back to the applet js portion, and with the user's personal information, such as a nickname and the like avatar

4. This time you can put the user's openid and nickname, avatar, an interface to call your own server, saved to the database

index.js

Copy the code
App getApp = const () 

Page ({ 
  Data: { 
    OpenID: "" 
  }, 
  // button callback method of 
  the getPerson: function (E) { 
    the console.log (this.data.openid); 
    the console.log (E); 
  } , 
  onLoad: function () { 
    var = the this Self; 
    wx.login ({ 
      Success (RES) { 
        // JS call log in order to obtain the code 
        IF (res.code) { 
          // code to call themselves by the service interface to obtain openid 
          WX .request ({ 
            URL: 'https://api.sopans.com/third/wxOpenId.php', 
            Data: { 
              code: res.code 
            }, 
            Success: function (wxInfo) { 
              the console.log (wxInfo);
              = wxInfo.data.openid self.data.openid 
            } 
          }) 
        } {the else 
          the console.log ( 'Login failed!' + res.errMsg) 
        } 
      } 
    }) 
  }, 
})
Copy the code

index.wxml

<Button open-type = "getUserInfo" bindgetuserinfo = "getPerson"> obtain user information </ button>
Copy the code
        $ appid = ''; // applet the APPID 
        $ Secret = ''; // applet Secret 
        $ code = $ _ the GET [ 'code']; 
        $ URL = 'https://api.weixin.qq.com/sns ? / jscode2session AppID = '.. $ AppID' & Secret = '. $ Secret.' & js_code = & grant_type = authorization_code '' $ code.. ';     
            
        $ curl = curl_init (); 
        curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, to true); 
        curl_setopt ($ curl, CURLOPT_TIMEOUT, 500); 
        curl_setopt ($ curl, CURLOPT_SSL_VERIFYPEER, to false); 
        curl_setopt ($ curl, CURLOPT_SSL_VERIFYHOST, to false); 
        curl_setopt ($ curl, CURLOPT_URL to, $ URL); 
        $ RES = the curl_exec ($ curl); 
        curl_close ($ curl); 
        
        echo $ RES; // here is information acquired
Copy the code

Guess you like

Origin www.cnblogs.com/xxzzxv/p/11548487.html