MiniProgram.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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\MiniProgramService;
  14. use crmeb\services\oauth\OAuthException;
  15. use crmeb\services\oauth\OAuthInterface;
  16. /**
  17. * 小程序登录
  18. * Class MiniProgram
  19. * @package crmeb\services\oauth\storage
  20. */
  21. class MiniProgram extends BaseStorage implements OAuthInterface
  22. {
  23. protected function initialize(array $config)
  24. {
  25. // TODO: Implement initialize() method.
  26. }
  27. /**
  28. * @param string $openid
  29. * @return array|mixed
  30. */
  31. public function getUserInfo(string $openid)
  32. {
  33. return [];
  34. }
  35. /**
  36. * 授权登录
  37. * @param string|null $code
  38. * @param array $options
  39. * @return mixed
  40. */
  41. public function oauth(string $code = null, array $options = [])
  42. {
  43. if (!$code) {
  44. throw new OAuthException(100104);
  45. }
  46. try {
  47. $userInfoCong = MiniProgramService::getUserInfo($code);
  48. $session_key = $userInfoCong['session_key'];
  49. } catch (\Exception $e) {
  50. throw new OAuthException($e->getMessage());
  51. }
  52. if (!isset($userInfoCong['openid'])) {
  53. throw new OAuthException(410075);
  54. }
  55. //是否静默授权
  56. if (isset($options['silence']) && $options['silence'] === true) {
  57. return $userInfoCong;
  58. }
  59. if (empty($options['iv']) || empty($options['encryptedData'])) {
  60. throw new OAuthException(100100);
  61. }
  62. try {
  63. //解密获取用户信息
  64. $userInfo = MiniProgramService::encryptor($session_key, $options['iv'], $options['encryptedData']);
  65. } catch (\Exception $e) {
  66. if ($e->getCode() == '-41003') {
  67. throw new OAuthException(410077);
  68. }
  69. }
  70. return [$userInfoCong, $userInfo];
  71. }
  72. }