Login.php 4.0 KB

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