64 lines
1.4 KiB
PHP
64 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace support\auth;
|
|
|
|
use EasyWeChat\MiniApp\Application;
|
|
use support\exception\BusinessException;
|
|
|
|
class Weixin
|
|
{
|
|
|
|
public $app;
|
|
public $config;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->config = [
|
|
'app_id' => '',
|
|
'secret' => '',
|
|
'response_type' => 'array',
|
|
'log' => [
|
|
'level' => 'debug',
|
|
'file' => __DIR__ . '/wechat.log',
|
|
],
|
|
];
|
|
$this->app = new Application($this->config);
|
|
}
|
|
|
|
public function appId()
|
|
{
|
|
return $this->config['app_id'];
|
|
}
|
|
|
|
public function nickname()
|
|
{
|
|
return '微信用户' . time() . rand(1, 100);
|
|
}
|
|
|
|
public function avatar()
|
|
{
|
|
return '/image/avatar.jpg';
|
|
}
|
|
|
|
public function login($code)
|
|
{
|
|
$result = $this->app->getUtils()->codeToSession($code);
|
|
if (isset($result['openid'])) {
|
|
return $result;
|
|
}
|
|
throw new BusinessException('登录失败', 500);
|
|
}
|
|
|
|
public function getPhoneNumber($code, $iv, $encryptedData)
|
|
{
|
|
$utils = $this->app->getUtils();
|
|
$response = $utils->codeToSession($code);
|
|
$session = $utils->decryptSession($response['session_key'], $iv, $encryptedData);
|
|
if (empty($session['phoneNumber'])) {
|
|
throw new BusinessException('获取手机号失败', 500);
|
|
}
|
|
return $session['phoneNumber'];
|
|
}
|
|
|
|
}
|