当前位置: 移动技术网 > IT编程>开发语言>JavaScript > Thinkphp5微信小程序获取用户信息接口的实例详解

Thinkphp5微信小程序获取用户信息接口的实例详解

2017年12月12日  | 移动技术网IT编程  | 我要评论

thinkphp5微信小程序获取用户信息接口的实例详解

首先在官网下载示例代码, 选php的,

这里有个坑

官方的php文件,编码是utf-8+的, 所以要把文件改为utf-8

然后在thinkphp5 extend文件夹下建立wxxcx命名空间,把官方的几个类文件放进去(这里要注意文件夹名, 命名空间名, 类名的, 大小写,一定要一样,官方的文件名和类名大小写不一样)

然后是自己的thinkphp接口代码:

<?php 
/** 
 * created by phpstorm. 
 * user: leeoo 
 * date: 2017/9/14 0014 
 * time: 10:43 
 */ 
 
namespace app\api\controller\v1; 
 
 
use think\loader; 
use think\request; 
use workerman\protocols\http; 
use wxxcx\wxbizdatacrypt; 
use first\second\foo; 
 
class index 
{ 
  public function index($id) 
  { 
 
    return json(['msg' => $id]); 
  } 
 
  public function dologin() 
  { 
    $code = request::instance()->param('code'); 
    $encrypteddata = request::instance()->param('encrypteddata'); 
    $iv = request::instance()->param('iv'); 
 
    $appid = "你的小程序appid"; 
    $secret = "你的小程序secret"; 
    //appid={$appid}&secret={$secret}&js_code={$code}&grant_type=authorization_code 
    $param = array( 
      'appid' => $appid, 
      'secret' => $secret, 
      'js_code' => $code, 
      'grant_type' => 'authorization_code' 
    ); 
  //http函数为封装的请求函数 
    $res = http("https://api.weixin.qq.com/sns/jscode2session", $param, 'post'); 
 
    $arr = json_decode($res, true); 
 
    $result = $this->wxdecode($encrypteddata, $iv, $arr['session_key'], $appid); 
 
    //return json($result); 
    if ($result) { 
      return json(['code' => 1]); 
    } else { 
      return json(['code' => -1]); 
    } 
 
  } 
 
  public function wxdecode($encrypteddata, $iv, $sessionkey, $appid) 
  { 
    //loader::import('wxxcx\wxbizdatacrypt', extend_path); 
    $pc = new wxbizdatacrypt($appid, $sessionkey); 
    $data = null; 
    $errcode = $pc->decryptdata($encrypteddata, $iv, $data); 
    //echo $data; 
    //return json(['data'=>$data]); 
    $data = json_decode($data); 
 
    if ($errcode == 0) { 
      //print($data . "\n"); 
      //dump($data); 
      return $data; 
    } else { 
      //print($errcode . "\n"); 
      //dump($errcode); 
      return $errcode; 
    } 
 
  } 
} 

http封装函数:

/** 
 * 发送http请求方法 
 * @param string $url  请求url 
 * @param array $params 请求参数 
 * @param string $method 请求方法get/post 
 * @return array $data  响应数据 
 */ 
function http($url, $params, $method = 'get', $header = array(), $multi = false){ 
  $opts = array( 
    curlopt_timeout    => 30, 
    curlopt_returntransfer => 1, 
    curlopt_ssl_verifypeer => false, 
    curlopt_ssl_verifyhost => false, 
    curlopt_httpheader   => $header 
  ); 
  /* 根据请求类型设置特定参数 */ 
  switch(strtoupper($method)){ 
    case 'get': 
      $opts[curlopt_url] = $url . '?' . http_build_query($params); 
      break; 
    case 'post': 
      //判断是否传输文件 
      $params = $multi ? $params : http_build_query($params); 
      $opts[curlopt_url] = $url; 
      $opts[curlopt_post] = 1; 
      $opts[curlopt_postfields] = $params; 
      break; 
    default: 
      throw new exception('不支持的请求方式!'); 
  } 
  /* 初始化并执行curl请求 */ 
  $ch = curl_init(); 
  curl_setopt_array($ch, $opts); 
  $data = curl_exec($ch); 
  $error = curl_error($ch); 
  curl_close($ch); 
  if($error) throw new exception('请求发生错误:' . $error); 
  return $data; 
} 

然后是小程序的代码:

// 获取用户信息 
  wx.getsetting({ 
   success: res => { 
    if (res.authsetting['scope.userinfo']) { 
     // 已经授权,可以直接调用 getuserinfo 获取头像昵称,不会弹框 
     wx.getuserinfo({ 
      success: res => { 
       console.log(res); 
       var encrypteddata = res.encrypteddata 
       var iv = res.iv 
       wx.request({ 
        url: "https://你的服务器地址/dologin",//dologin是访问后端的方法 
        method: "post", 
        data: { 
         code: code, 
         encrypteddata: encrypteddata, 
         iv: iv 
        }, 
        success: function (ret) { 
         console.log(ret); 
        } 
       }) 
        
        
       // 可以将 res 发送给后台解码出 unionid 
       this.globaldata.userinfo = res.userinfo 
 
       // 由于 getuserinfo 是网络请求,可能会在 page.onload 之后才返回 
       // 所以此处加入 callback 以防止这种情况 
       if (this.userinforeadycallback) { 
        this.userinforeadycallback(res) 
       } 
      } 
     }) 
    } 
   } 
  }) 
 }, 

如果有报错, 自己调试一下, 看看哪里的变量有问题  查找原因.

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网