增加微信登录

This commit is contained in:
youda 2025-07-09 12:56:11 +08:00
parent 910979ca65
commit d0e47ea732
6 changed files with 245 additions and 3 deletions

6
.env
View File

@ -22,5 +22,11 @@ PORT = 6379
PASSWORD =
DB = 3
[CONFIGREDIS]
HOST = 127.0.0.1
PORT = 6379
PASSWORD =
DB = 5
[YOUDA]
YOUDA_ENV=test

View File

@ -16,6 +16,7 @@ use think\facade\Log;
use app\common\server\TencentCosUploader;
use app\common\model\ProfitMoney;
use app\common\helper\EnvHelper;
use app\common\server\AppWechat;
class Login extends Base
{
private $uploader;
@ -1006,4 +1007,18 @@ class Login extends Base
return $this->renderError($loginResult['msg'] ?? "登录失败");
}
}
public function getWxUserInfo()
{
$code = request()->param('code', '');
if ($code == "") {
return $this->renderError("参数错误");
}
$appWechat = new AppWechat();
$userInfo = $appWechat->getAccountUserInfo($code);
if ($userInfo) {
return $this->renderSuccess("", $userInfo);
}
return $this->renderError("获取微信用户数据失败");
}
}

View File

@ -21,6 +21,8 @@ Route::any('login_bind_mobile', 'Login/login_bind_mobile');
Route::any('wx_app_login', 'Login/wxAppLogin');
Route::any('bindMobile', 'Login/bindMobile');
Route::any('get_wx_userinfo', 'Login/getWxUserInfo');
#Index.php首页
#============================
Route::any('index', 'Index/index');

View File

@ -0,0 +1,95 @@
<?php
namespace app\common\server;
class AppWechat
{
private $wx_appid = "wx112eed47787bcc0a";
private $wx_secret = "6fcc2b8fd22595bd88889157ad9cddd9";
/**
* 获取用户基础信息
* @param mixed $code
* @return array|\think\response\Json
*/
public function getAccessToken($code)
{
//connect/oauth2/authorize
//https://open.weixin.qq.com/connect/oauth2/authorize
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" . $this->wx_appid . "&secret=" . $this->wx_secret . "&code=" . $code . "&grant_type=authorization_code";
$resUserInfo = $this->get_curl_data($url);
if (isset($resUserInfo['errcode'])) {
return null;
}
$openid = $resUserInfo['openid'];#openid
$access_token = $resUserInfo['access_token']; # 7200
$unionid = ''; # 7200
if (isset($resUserInfo['unionid'])) {
$unionid = $resUserInfo['unionid'];
}
$data = [
'openid' => $openid,
'unionid' => $unionid,
'access_token_time' => $access_token,
];
return $data;
}
/**
* 拉取用户信息(需scope为 snsapi_userinfo)
* @param mixed $data
* @return mixed
*/
public function getUserInfo($data)
{
$openid = $data['openid'];
$access_token = $data['access_token_time'];
//connect/oauth2/authorize
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=" . $access_token . "&openid=" . $openid . "&lang=zh_CN";
$resUserInfo = $this->get_curl_data($url);
if (isset($resUserInfo['errcode'])) {
return null;
}
$user = $resUserInfo;#openid
return $user;
}
public function getAccountUserInfo($code)
{
$account = $this->getAccessToken($code);
if ($account) {
return $this->getUserInfo($account);
}
return null;
}
/**
* @param $url 请求链接
*/
public function get_curl_data($url)
{
$headerArray = array("Content-type:application/json;", "Accept:application/json");
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headerArray);
$response = curl_exec($ch);
if ($response) {
curl_close($ch);
return json_decode($response, true);
} else {
$error = curl_errno($ch);
curl_close($ch);
return ['errcode' => 1];
}
}
}

View File

@ -0,0 +1,124 @@
<?php
namespace app\common\server;
/**
* Class RedisHelper
*
* 这是一个用于操作 Redis 的帮助类,提供了基本的 Redis 操作方法。
*/
class ConfigRedisHelper
{
/**
* @var \Redis Redis 实例(静态属性)
*/
private static $redis;
/**
* 获取 Redis 实例
*
* @return \Redis
*/
public function getRedis()
{
if (self::$redis === null) {
// 从环境变量中获取 Redis 主机和端口,默认为 127.0.0.1 和 6379
$host = env('configredis.host', '127.0.0.1');
$port = env('configredis.port', '6379');
// 从环境变量中获取 Redis 数据库编号,默认为 0
$db = env('configredis.db', '0');
// 创建 Redis 实例
self::$redis = new \Redis();
// 连接到 Redis 服务器
$this->connect($host, $port);
// 选择数据库
self::$redis->select((int)$db);
}
return self::$redis;
}
/**
* 连接到 Redis 服务器
*
* @param string $host Redis 主机
* @param int $port Redis 端口
* @return bool
*/
private function connect($host, $port)
{
return self::$redis->connect($host, (int)$port);
}
/**
* Redis 中获取指定键的值
*
* @param string $key 键名
* @return mixed 键对应的值
*/
public function get($key)
{
return $this->getRedis()->get($key);
}
/**
* 设置指定键的值,并可选设置过期时间(默认 600 秒)
*
* @param string $key 键名
* @param mixed $value 键值
* @param int $timeout 过期时间(秒)
* @return bool 设置是否成功
*/
public function set($key, $value, $timeout = 600)
{
if ($timeout > 0) {
return $this->getRedis()->setex($key, $timeout, $value);
}
return $this->getRedis()->set($key, $value);
}
/**
* 删除指定键
*
* @param string $key 键名
* @return int 被删除的键的数量
*/
public function delete($key)
{
return $this->getRedis()->del($key);
}
/**
* 检查指定键是否存在
*
* @param string $key 键名
* @return bool 键是否存在
*/
public function exists($key)
{
return $this->getRedis()->exists($key);
}
/**
* 设置指定键的过期时间
*
* @param string $key 键名
* @param int $timeout 过期时间(秒)
* @return bool 设置是否成功
*/
public function expire($key, $timeout)
{
return $this->getRedis()->expire($key, $timeout);
}
/**
* 选择 Redis 数据库
*
* @param int $db 数据库编号
* @return bool 选择是否成功
*/
public function select($db)
{
return $this->getRedis()->select($db);
}
}

View File

@ -36,14 +36,14 @@ class AppPlatform extends BasePlatform
$config = [
'isWebPay' => false,
'buildVersion' => '106',
'version' => '1.0.2',
'version' => '1.0.1',
'userAgreement' => "https://zfunbox.cn?_p=cb20xad0e35094521ae46a1d1fb0ddd1&time=" . time()
];
$is_env_test = EnvHelper::getIsTest();
if ($is_env_test) {
$config['userAgreement'] = "https://testweb.zfunbox.cn?_p=cb20xad0e35094521ae46a1d1fb0ddd1&time=" . time();
}
$config['userAgreement'] = " https://192.168.1.21:3001?_p=cb20xad0e35094521ae46a1d1fb0ddd1&time=" . time();
// $config['userAgreement'] = " https://192.168.1.21:3001?_p=cb20xad0e35094521ae46a1d1fb0ddd1&time=" . time();
$configVersion = (int) preg_replace('/[^0-9]/', '', $config['version']);
if ($versionNumber >= $configVersion) {
$config['buildVersion'] = '105';