登录api 获取openid 及 获取用户信息等

前台:

//index.js


Page({
  getUser:function(){ //我是用的bindtap事件 也可以放到  生命周期函数--监听页面加载
    // 登录
    wx.login({
      success: function (res) {
        var code = res.code; //获取code
        wx.getUserInfo({ //得到rawData, signatrue, encryptData
          success: function (data) {
            var rawData = data.rawData;
            var signature = data.signature;
            var encryptedData = data.encryptedData;
            var iv = data.iv;
            wx.request({
              url: 'https://small.gongshangbiaoju.com/api/Index/index',
              data: {
                "code": code,
                "rawData": rawData,
                "signature": signature,
                'iv': iv,
                'encryptedData': encryptedData
              },
              method: 'GET',
              success: function (info) {
                console.log(info)
              },
              fail:function(info){
                console.log(info)
              }
            });
          }
        });
      },
      fail: function (res) {
        console.log(res)
      }
    });
  }
  // login:function(){
  //   wx.login({
  //     success:function(res){
  //       if(res.code){
  //         //发起网络请求
  //         wx.request({
  //           url: 'https://small.gongshangbiaoju.com/api/Index/login',
  //           data: {
  //             code: res.code
  //           },
  //           method:'get',
  //           success:function(e){
  //             console.log(e.data)
  //           }
            
  //         })
  //       }
  //     }
  //   });
  
  // } 
})
\\index.wxml



后台:

\\index.php
checkLogin($code, $rawData, $signature, $encryptedData, $iv);
        file_put_contents ( './data.json', json_encode ( $data ) );
        $map = [
                'openid'=>$data['openId'],
                'nickname'=>json_encode($data['nickName']),
                'gender'=>$data['gender'],
                'language'=>$data['language'],
                'city'=>$data['city'],
                'province'=>$data['province'],
                'country'=>$data['country'],
                'avatarUrl'=>$data['avatarUrl'],
                'session3rd'=>$data['session3rd']
        ];
        $uid = Db::name('user')->insertGetId($map);
        return json($uid);
    }
    /**
     * wx.login
     * 使用 code 换取 openid 和 session_key 
     */
    public function login()
    {
        $code = request()->get('code');//获取微信小程序的code
        $url = config('Wx.url');
        $data = [
            'appid'=>config('Wx.appid'),
            'secret'=>config('Wx.secret'),
            'js_code'=>$code,
            'grant_type'=>config('Wx.grant_type')   
        ];
        $result = http_request($url,$data);
        echo $result;die;   
    }
}
/**
 * 微信接口请求函数http_request
 * @param  $url 访问的url
 * @param  $data附带数据
 */
function http_request($url, $data = null)
{
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, $url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
    if (!empty($data)){
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
    }
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $output = curl_exec($curl);
    curl_close($curl);
    return $output;//返回json数据
}

\wlt\wxmini\WXLoginHelper;参考安装 地址:https://github.com/wulongtao/think-wxminihelper

你可能感兴趣的:(登录api 获取openid 及 获取用户信息等)