Wechat.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\services\oauth\storage;
  12. use crmeb\basic\BaseStorage;
  13. use crmeb\services\app\WechatOpenService;
  14. use crmeb\services\app\WechatService;
  15. use crmeb\services\oauth\OAuthException;
  16. use crmeb\services\oauth\OAuthInterface;
  17. /**
  18. * 微信公众号登录
  19. * Class Wechat
  20. * @package crmeb\services\oauth\storage
  21. */
  22. class Wechat extends BaseStorage implements OAuthInterface
  23. {
  24. protected function initialize(array $config)
  25. {
  26. // TODO: Implement initialize() method.
  27. }
  28. /**
  29. * 获取用户信息
  30. * @param string $openid
  31. * @return mixed
  32. */
  33. public function getUserInfo(string $openid)
  34. {
  35. return WechatService::oauth2Service()->getUserInfo($openid)->toArray();
  36. }
  37. /**
  38. * 授权
  39. * @param string|null $code
  40. * @param array $options
  41. * @return \EasyWeChat\Support\Collection|mixed
  42. */
  43. public function oauth(string $code = null, array $options = [])
  44. {
  45. $open = false;
  46. if (!empty($options['open'])) {
  47. $open = true;
  48. }
  49. if (!$open) {
  50. try {
  51. $wechatInfo = WechatService::oauth2Service()->oauth();
  52. } catch (\Throwable $e) {
  53. throw new OAuthException($e->getMessage());
  54. }
  55. } else {
  56. /** @var WechatOpenService $service */
  57. $service = app()->make(WechatOpenService::class);
  58. $wechatInfo = $service->getAuthorizationInfo();
  59. if (!$wechatInfo) {
  60. throw new OAuthException(410131);
  61. }
  62. }
  63. return $wechatInfo;
  64. }
  65. }