From d0e47ea7320b2565ed16662c5516bb181149e383 Mon Sep 17 00:00:00 2001 From: youda Date: Wed, 9 Jul 2025 12:56:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=BE=AE=E4=BF=A1=E7=99=BB?= =?UTF-8?q?=E5=BD=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .env | 6 + app/api/controller/Login.php | 15 +++ app/api/route/app.php | 2 + app/common/server/AppWechat.php | 95 ++++++++++++++++ app/common/server/ConfigRedisHelper.php | 124 +++++++++++++++++++++ app/common/server/platform/AppPlatform.php | 6 +- 6 files changed, 245 insertions(+), 3 deletions(-) create mode 100644 app/common/server/AppWechat.php create mode 100755 app/common/server/ConfigRedisHelper.php diff --git a/.env b/.env index 78e2336..604cae9 100644 --- a/.env +++ b/.env @@ -22,5 +22,11 @@ PORT = 6379 PASSWORD = DB = 3 +[CONFIGREDIS] +HOST = 127.0.0.1 +PORT = 6379 +PASSWORD = +DB = 5 + [YOUDA] YOUDA_ENV=test \ No newline at end of file diff --git a/app/api/controller/Login.php b/app/api/controller/Login.php index 9cad3a8..0525a87 100755 --- a/app/api/controller/Login.php +++ b/app/api/controller/Login.php @@ -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("获取微信用户数据失败"); + } } \ No newline at end of file diff --git a/app/api/route/app.php b/app/api/route/app.php index 0d8a4e5..a0045c7 100755 --- a/app/api/route/app.php +++ b/app/api/route/app.php @@ -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'); diff --git a/app/common/server/AppWechat.php b/app/common/server/AppWechat.php new file mode 100644 index 0000000..7c61116 --- /dev/null +++ b/app/common/server/AppWechat.php @@ -0,0 +1,95 @@ +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]; + } + } + + + + +} diff --git a/app/common/server/ConfigRedisHelper.php b/app/common/server/ConfigRedisHelper.php new file mode 100755 index 0000000..b3b1285 --- /dev/null +++ b/app/common/server/ConfigRedisHelper.php @@ -0,0 +1,124 @@ +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); + } +} \ No newline at end of file diff --git a/app/common/server/platform/AppPlatform.php b/app/common/server/platform/AppPlatform.php index 96d9fa6..c8c059f 100644 --- a/app/common/server/platform/AppPlatform.php +++ b/app/common/server/platform/AppPlatform.php @@ -36,20 +36,20 @@ 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'; // $config['isCheck'] = true; $config['userAgreement'] = "https://zfunbox.cn/pages/guize/guize?type=4"; - + } return $config; }