Login.php 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. namespace app\wechat\controller\api;
  3. use app\wechat\service\WechatService;
  4. use think\admin\Controller;
  5. /**
  6. * 微信扫码授权登录
  7. * Class Login
  8. * @package app\wechat\controller\api
  9. */
  10. class Login extends Controller
  11. {
  12. /**
  13. * 数据缓存时间
  14. * @var integer
  15. */
  16. protected $expire = 3600;
  17. /**
  18. * 授权码前缀
  19. * @var string
  20. */
  21. protected $prefix = 'wxlogin';
  22. /**
  23. * 扫描显示二维码
  24. * @throws \Endroid\QrCode\Exceptions\ImageFunctionFailedException
  25. * @throws \Endroid\QrCode\Exceptions\ImageFunctionUnknownException
  26. * @throws \Endroid\QrCode\Exceptions\ImageTypeInvalidException
  27. * @throws \think\exception\HttpResponseException
  28. */
  29. public function qrc()
  30. {
  31. $mode = intval(input('mode', '0'));
  32. $code = $this->prefix . md5(uniqid('t', true) . rand(10000, 99999));
  33. $text = url('wechat/api.login/oauth', [], false, true) . "?code={$code}&mode={$mode}";
  34. // 生成二维码并返回结果
  35. $qrcode = new \Endroid\QrCode\QrCode();
  36. $qrcode->setText($text)->setSize(300)->setPadding(20);
  37. $content = base64_encode($qrcode->setImageType('png')->get());
  38. $this->success('获取二维码成功', ['code' => $code, 'image' => "data:image/png;base64,{$content}"]);
  39. }
  40. /**
  41. * 微信授权结果处理
  42. * @throws \WeChat\Exceptions\InvalidResponseException
  43. * @throws \WeChat\Exceptions\LocalCacheException
  44. * @throws \think\admin\Exception
  45. * @throws \think\db\exception\DataNotFoundException
  46. * @throws \think\db\exception\DbException
  47. * @throws \think\db\exception\ModelNotFoundException
  48. */
  49. public function oauth()
  50. {
  51. $this->code = input('code', '');
  52. $this->mode = input('mode', '0');
  53. if (stripos($this->code, $this->prefix) === 0) {
  54. $this->url = $this->request->url(true);
  55. $this->fans = WechatService::instance()->getWebOauthInfo($this->url, $this->mode);
  56. if (is_array($this->fans) && isset($this->fans['openid'])) {
  57. $this->fans['token'] = md5(uniqid('t', true) . rand(10000, 99999));
  58. $this->app->cache->set("wxlogin{$this->code}", $this->fans, $this->expire);
  59. $this->app->cache->set($this->fans['openid'], $this->fans['token'], $this->expire);
  60. $this->message = '授权成功';
  61. $this->fetch('success');
  62. } else {
  63. $this->message = '授权失败';
  64. $this->fetch('failed');
  65. }
  66. } else {
  67. $this->message = '授权失败';
  68. $this->fetch('failed');
  69. }
  70. }
  71. /**
  72. * 获取授权信息
  73. * 用定时器请求这个接口
  74. * @throws \think\exception\HttpResponseException
  75. */
  76. public function query()
  77. {
  78. $this->code = input('code', '');
  79. if (stripos($this->code, $this->prefix) === 0) {
  80. $this->ckey = "wxlogin{$this->code}";
  81. $this->fans = $this->app->cache->get($this->ckey, new \stdClass());
  82. $this->success('获取授权信息', $this->fans);
  83. } else {
  84. $this->error("授权CODE不能为空!");
  85. }
  86. }
  87. }