[Applet] micro-channel small landing procedures and obtain user information

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

App = const getApp () 

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

index.wxml

< Button Open-type = "getUserInfo" bindgetuserinfo = "the getPerson" > obtain user information </ Button >
        $appid = ''; // 小程序APPID
        $secret = ''; // 小程序secret
        $code=$_GET['code'];
        $url = 'https://api.weixin.qq.com/sns/jscode2session?appid=' . $appid . '&secret='.$secret.'&js_code='.$code.'&grant_type=authorization_code';    
            
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 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 ; // this is to get information

 

Guess you like

Origin www.cnblogs.com/taoshihan/p/11408746.html