登录接口示例.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace app\index\controller;
  3. use EasyWeChat\Factory;
  4. use think\facade\Session;
  5. use think\facade\Log;
  6. class Login extends Base
  7. {
  8. private app_id;
  9. private secret;
  10. public function __construct()
  11. {
  12. parent::__construct();
  13. $this->app_id = Config::get('wechat.request.app_id'); //微信appid,推荐写在配置文件或环境变量
  14. $this->secret = Config::get('wechat.request.secret'); //微信appid对应secret,推荐写在配置文件或环境变量
  15. }
  16. //微信登录
  17. public function wechatLogin(){
  18. //回调地址
  19. $callback = urldecode(input("callback", ''));
  20. if ($callback == '') {
  21. //跳去欢迎页
  22. $callback=(String)url("/index",[],"",true);
  23. }
  24. Session::set('target_url', $callback);
  25. $config = [
  26. // 'http' => $this->proxy,
  27. 'app_id' => $this->app_id,
  28. 'secret' => $this->secret,
  29. 'oauth' => [
  30. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  31. 'response_type' => 'array',
  32. 'scopes' => ['snsapi_userinfo'],
  33. 'callback' => (String)url("Login/wechatGetInfo",['callback'=>$callback],"",true),
  34. ],
  35. ];
  36. $app = Factory::officialAccount($config);
  37. $oauth = $app->oauth;
  38. return $oauth->redirect();
  39. }
  40. //获取微信授权信息
  41. public function wechatGetInfo(){
  42. $param_callback = urldecode(input("callback", ''));
  43. //开始授权
  44. $user = Session::get('wx_user', '');
  45. if($user != ''){
  46. $target_url = Session::get('target_url', '');
  47. if($target_url == ''){
  48. $target_url = $param_callback;
  49. }
  50. $callback = $this->merge_link($target_url, $user);
  51. //尝试跳走
  52. header('location:' . $callback); // 跳转到 user/profile
  53. return;
  54. }
  55. $callback = (String)url("Login/wechatGetInfo",['callback'=>$param_callback],"",true);
  56. $config = [
  57. // 'http' => $this->proxy,
  58. 'app_id' => $this->app_id,
  59. 'secret' => $this->secret,
  60. 'oauth' => [
  61. // 指定 API 调用返回结果的类型:array(default)/collection/object/raw/自定义类名
  62. 'response_type' => 'array',
  63. 'scopes' => ['snsapi_userinfo'],
  64. // 'callback' => ,
  65. 'callback' => $callback,
  66. ],
  67. ];
  68. $app = Factory::officialAccount($config);
  69. $oauth = $app->oauth;
  70. // 获取 OAuth 授权结果用户信息
  71. $userObj = $oauth->user()->toArray();
  72. $user = [
  73. "openid" => $userObj["id"],
  74. "nickname" => $userObj["nickname"],
  75. "avatar" => $userObj["avatar"],
  76. ];
  77. Log::info(' 授权信息 user:'.json_encode($user));
  78. Session::set('wx_user', $user);
  79. //授权后更新微信用户信息到数据库
  80. $wx_userinfo = [];
  81. if($user['nickname'] != ''){
  82. $wx_userinfo['nickname'] = $user['nickname'];
  83. }
  84. if($user['avatar'] != ''){
  85. $wx_userinfo['avatar'] = $user['avatar'];
  86. }
  87. if($user['openid'] != '' && count($wx_userinfo) != 0){
  88. /*
  89. * 登录后业务处理
  90. */
  91. }
  92. $targetUrl = Session::get('target_url', '') == '' ? '/' : Session::get('target_url');
  93. $targetUrl = $this->merge_link($targetUrl, $user);
  94. Log::info('授权信息 targetUrl:'.json_encode($targetUrl));
  95. header('location:' . $targetUrl); // 跳转到 user/profile
  96. }
  97. //整合登录链接
  98. private function merge_link($targetUrl, $user=[]){
  99. if(strpos($targetUrl, '#') === false){
  100. $targetUrl .= '/#';
  101. }
  102. if(strpos($targetUrl, '?') === false){
  103. $targetUrl .= '?';
  104. }
  105. else{
  106. $targetUrl .= '&';
  107. }
  108. foreach($user as $k => $v){
  109. $targetUrl .= $k.'='.$v.'&';
  110. }
  111. return $targetUrl;
  112. }
  113. }