当前位置:   article > 正文

2021-02-19 laravel + EasyWechat + jwt 小程序 授权登录_easywechat6 小程序登录

easywechat6 小程序登录
  1. 安装必要的第三方包
  1. $ composer require tymon/jwt-auth
  2. $ composer require overtrue/wechat:~5.0 -vvv

注意: 第一次安装jwt包后需要执行

$ php artisan jwt:secret
  1. 在控制器中添加用户验证方法
  1. public function auth(Request $request)
  2. {
  3. $code = $request->get('code');
  4. $rawData = $request->get('rawData');
  5. $config = Configs::first()->toArray();
  6. $config = [
  7. 'app_id' => $config['app_id'] ?? env('WECHAT_APP_ID'), // 这里需要自行修改
  8. 'secret' => $config['secret'] ?? env('WECHAT_APP_SECRET'), // 这里需要自行修改
  9. ];
  10. $app = Factory::miniProgram($config);
  11. $data = $app->auth->session($code);
  12. //判断code是否过期
  13. if (isset($data['errcode'])) {
  14. return ['code' => 404, 'massage' => 'code已过期或不正确'];
  15. }
  16. $weappOpenid = $data['openid'];
  17. $weixinSessionKey = $data['session_key'];
  18. $wechat = json_decode($rawData, true);
  19. $user = Members::UpdateOrCreate(['openid' => $weappOpenid], [
  20. 'openid' => $weappOpenid,
  21. 'nickname' => $wechat['nickName'],
  22. // 'gender' => $wechat['gender'],
  23. // 'city' => $wechat['city'], // 'Wenzhou',
  24. // 'province' => $wechat['province'], // 'Zhejiang',
  25. // 'country' => $wechat['country'], // 'China',
  26. 'avatar' => $wechat['avatarUrl'],
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/131939
推荐阅读
相关标签
  

闽ICP备14008679号