2025-03-12 10:47:34 +08:00

86 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
// +----------------------------------------------------------------------
// | CRMEB [ CRMEB赋能开发者助力企业发展 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed CRMEB并不是自由软件未经许可不能去掉CRMEB相关版权
// +----------------------------------------------------------------------
// | Author: CRMEB Team <admin@crmeb.com>
// +----------------------------------------------------------------------
namespace crmeb\services\oauth\storage;
use crmeb\basic\BaseStorage;
use crmeb\services\app\MiniProgramService;
use crmeb\services\oauth\OAuthException;
use crmeb\services\oauth\OAuthInterface;
/**
* 小程序登录
* Class MiniProgram
* @package crmeb\services\oauth\storage
*/
class MiniProgram extends BaseStorage implements OAuthInterface
{
protected function initialize(array $config)
{
// TODO: Implement initialize() method.
}
/**
* @param string $openid
* @return array|mixed
*/
public function getUserInfo(string $openid)
{
return [];
}
/**
* 授权登录
* @param string|null $code
* @param array $options
* @return mixed
*/
public function oauth(string $code = null, array $options = [])
{
if (!$code) {
throw new OAuthException(100104);
}
try {
$userInfoCong = MiniProgramService::getUserInfo($code);
$session_key = $userInfoCong['session_key'];
} catch (\Exception $e) {
throw new OAuthException($e->getMessage());
}
if (!isset($userInfoCong['openid'])) {
throw new OAuthException(410075);
}
//是否静默授权
if (isset($options['silence']) && $options['silence'] === true) {
return $userInfoCong;
}
if (empty($options['iv']) || empty($options['encryptedData'])) {
throw new OAuthException(100100);
}
try {
//解密获取用户信息
$userInfo = MiniProgramService::encryptor($session_key, $options['iv'], $options['encryptedData']);
} catch (\Exception $e) {
if ($e->getCode() == '-41003') {
throw new OAuthException(410077);
}
}
return [$userInfoCong, $userInfo];
}
}