6
0
Fork 0
jiqu-library-server/app/Providers/Socialite/WechatMini.php

77 lines
2.1 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
namespace App\Providers\Socialite;
use App\Exceptions\BizException;
use EasyWeChat\Factory as EasyWeChatFactory;
use Illuminate\Support\Facades\Cache;
use Overtrue\Socialite\Providers\Base;
use Overtrue\Socialite\User;
class WechatMini extends Base
{
/**
* Undocumented function
*
* @param string $code
* @return \Overtrue\Socialite\User
*/
public function userFromCode(string $code): User
{
$app = EasyWeChatFactory::miniProgram([
'app_id' => $this->config->get('client_id'),
'secret' => $this->config->get('client_secret'),
// 下面为可选项
// 指定 API 调用返回结果的类型array(default)/collection/object/raw/自定义类名
'response_type' => 'array',
'log' => [
'level' => 'debug',
'file' => storage_path('logs/wechat-mini.log'),
],
]);
$result = $app->auth->session($code);
if ($result) {
if (data_get($result, 'errcode')) {
throw new BizException(data_get($result, 'errmsg'));
}
//缓存微信小程序会话密钥48小时
Cache::put($result['openid'], $result['session_key'], 48 * 60 * 60);
return $this->mapUserToObject([
'openid'=>$result['openid'],
// 'unionid'=>$result['unionid'],
]);
} else {
throw new BizException('解析失败');
}
}
protected function getAuthUrl(): string
{
return '';
}
protected function getTokenUrl(): string
{
return '';
}
protected function getUserByToken(string $token): array
{
return [];
}
protected function mapUserToObject(array $user): User
{
return new User([
'id' => $user['openid'] ?? null,
'name' => $user['nickname'] ?? null,
'nickname' => $user['nickname'] ?? null,
'avatar' => $user['headimgurl'] ?? null,
'email' => null,
]);
}
}