From 7e3eba92e242f533991210f370a8da3a2250e820 Mon Sep 17 00:00:00 2001 From: pomjay Date: Wed, 25 Jun 2025 09:35:09 +0800 Subject: [PATCH] 11 --- app/Cache/Base/BaseCache.php | 3 + app/Cache/Base/TableBaseCache.php | 3 + .../Table/Api/TableCustomerUserCache.php | 13 ++++ app/Exceptions/Handler.php | 1 + app/Http/Controllers/Base/BaseController.php | 2 +- .../Customer/CustomerUserController.php | 10 +-- app/Http/Controllers/Sms/SmsController.php | 2 +- .../Api/Customer/CustomerUserExtendModel.php | 24 +++++-- app/Models/Api/Customer/CustomerUserModel.php | 3 + app/Models/Api/Follow/FollowHistoryModel.php | 2 +- app/Service/Api/AuthService.php | 23 ++++++- app/Service/Api/VrCodeService.php | 18 +++-- composer.json | 3 +- composer.lock | 69 ++++++++++++++++++- public/.htaccess | 21 ------ public/phpinfo.php | 1 + 16 files changed, 154 insertions(+), 44 deletions(-) create mode 100644 public/phpinfo.php diff --git a/app/Cache/Base/BaseCache.php b/app/Cache/Base/BaseCache.php index 818bb38..968af5f 100644 --- a/app/Cache/Base/BaseCache.php +++ b/app/Cache/Base/BaseCache.php @@ -29,6 +29,7 @@ abstract class BaseCache { $sCacheKey = $this->getPrimaryKey(); $aData = $this->loadData(); + if (empty($aData)) return false; return Cache::put($sCacheKey, $aData, self::CACHE_TTL); } @@ -46,12 +47,14 @@ abstract class BaseCache function getCacheData($primary_key = null): array|string|null { + if ($primary_key === null) $primary_key = $this->primary_key; if (empty($primary_key)) return []; $sCacheKey = $this->getPrimaryKey($primary_key); $sData = Cache::get($sCacheKey); if (empty($sData)) { $this->primary_key = $primary_key; + if ($this->loadToCache()) $sData = Cache::get($sCacheKey); } if (empty($sData)) return null; diff --git a/app/Cache/Base/TableBaseCache.php b/app/Cache/Base/TableBaseCache.php index c638dcd..4d60361 100644 --- a/app/Cache/Base/TableBaseCache.php +++ b/app/Cache/Base/TableBaseCache.php @@ -12,13 +12,16 @@ abstract class TableBaseCache extends BaseCache function loadTable(): array|string|null { + return null; } function loadData(): array|string|null { + //默认获取表数据方式 if (!empty($table_class)) { + $oTable = new $this->table_class(); //排除不需要的字段 if (!empty($this->get_exclude_columns) && $this->get_columns != ['*']) $this->get_columns = array_diff($this->get_columns, $this->get_exclude_columns); diff --git a/app/Cache/Table/Api/TableCustomerUserCache.php b/app/Cache/Table/Api/TableCustomerUserCache.php index 36d5790..8e6b43f 100644 --- a/app/Cache/Table/Api/TableCustomerUserCache.php +++ b/app/Cache/Table/Api/TableCustomerUserCache.php @@ -18,5 +18,18 @@ class TableCustomerUserCache extends TableBaseCache 'google_auth_secret', ]; + function loadData(): array|string|null + { + + $oTable = new $this->table_class(); + $oData = $oTable->findItemByWhere([$this->primary_key_column => $this->primary_key], $this->get_columns); + + if (!$oData) return null; + + $aData = $oData->toArray(); + if (empty($aData)) return null; + + return $aData; + } } diff --git a/app/Exceptions/Handler.php b/app/Exceptions/Handler.php index f53f08b..84d8a8c 100644 --- a/app/Exceptions/Handler.php +++ b/app/Exceptions/Handler.php @@ -27,6 +27,7 @@ class Handler extends ExceptionHandler */ public function register(): void { + $this->renderable(function (NotFoundHttpException $e, Request $request) { return ReplyService::error( 'record not find'); }); diff --git a/app/Http/Controllers/Base/BaseController.php b/app/Http/Controllers/Base/BaseController.php index 602042e..b5a9a85 100644 --- a/app/Http/Controllers/Base/BaseController.php +++ b/app/Http/Controllers/Base/BaseController.php @@ -29,7 +29,7 @@ abstract class BaseController extends Controller function error($msg = 'error', $data = []): \Illuminate\Http\JsonResponse { - ReplyService::error($msg, $data); + return ReplyService::error($msg, $data); } } diff --git a/app/Http/Controllers/Customer/CustomerUserController.php b/app/Http/Controllers/Customer/CustomerUserController.php index 5e8ab54..a1a28c9 100644 --- a/app/Http/Controllers/Customer/CustomerUserController.php +++ b/app/Http/Controllers/Customer/CustomerUserController.php @@ -49,6 +49,7 @@ class CustomerUserController extends CustomerBaseController { $oAuthService = new AuthService(); $token = $oAuthService->getTokenFromReq(); + var_dump($token); $aUser = $oAuthService->getCurrentUser(); $data = [ 'token' => $token, @@ -102,8 +103,8 @@ class CustomerUserController extends CustomerBaseController $oAuthService = new AuthService(); $oImService = new ImService(); $token = $oAuthService->createTokenToUser($oUser->id, $device); - $imToken = $oImService->authUserToken($oUser->id, $device); - + $imToken = "test"; + //$imToken = $oImService->authUserToken($oUser->id, $device); $data = [ 'token' => $token, 'im_token' => $imToken, @@ -157,7 +158,7 @@ class CustomerUserController extends CustomerBaseController $password = $request->input('password'); $device = $request->input('device'); $sVrCode = $request->input('vr_code'); - + var_dump($sVrCode); //check vrcode $oVrCodeService = new VrCodeService(); @@ -186,7 +187,8 @@ class CustomerUserController extends CustomerBaseController //向im注册 $oImService = new ImService(); - if (!$oImService->userUserRegister($oUser->id)) throw new AppException('im register error'); + //暂时不注册 + // if (!$oImService->userUserRegister($oUser->id)) throw new AppException('im register error'); $oAuthService = new AuthService(); $token = $oAuthService->createTokenToUser($oUser->id, $device); diff --git a/app/Http/Controllers/Sms/SmsController.php b/app/Http/Controllers/Sms/SmsController.php index 7f0ec62..c873d10 100644 --- a/app/Http/Controllers/Sms/SmsController.php +++ b/app/Http/Controllers/Sms/SmsController.php @@ -40,7 +40,7 @@ class SmsController extends CustomerBaseController $validator = Validator::make($aReqData, [ 'phone_area' => 'required|alpha_dash:ascii|max:5', - 'phone' => 'required|numeric|max:15', + 'phone' => 'required|digits_between:6,15', ]); if ($validator->fails()) { return $this->error($validator->errors()->first()); diff --git a/app/Models/Api/Customer/CustomerUserExtendModel.php b/app/Models/Api/Customer/CustomerUserExtendModel.php index d9eab0a..2f626d7 100644 --- a/app/Models/Api/Customer/CustomerUserExtendModel.php +++ b/app/Models/Api/Customer/CustomerUserExtendModel.php @@ -46,11 +46,27 @@ class CustomerUserExtendModel extends ApiBaseModel return $this->addItem($aItem); } + function initExtend($uid): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null + { + if (!$uid) throw new ModelException('uid error'); + if ($this->findItem($uid, ['uid'])) return null; //已存在 + + $sDateTime = date('Y-m-d H:i:s'); + $aItem = [ + 'uid' => $uid, + 'is_active' => 1, + 'fans_num' => 0, + 'follow_num' => 0, + 'updated_at' => $sDateTime, + ]; + return $this->addItem($aItem); + } + //增加当前粉丝总数 function incrFansNum($uid): int { $oExtend = $this->findItem($uid, 'uid'); - if (!$oExtend) throw new ModelException('user extend not found'); + if (!$oExtend) $this->initExtend($uid); return $this->newQuery()->where('uid', $uid)->increment('fans_num'); } @@ -58,7 +74,7 @@ class CustomerUserExtendModel extends ApiBaseModel function decrFansNum($uid): int { $oExtend = $this->findItem($uid, 'uid'); - if (!$oExtend) throw new ModelException('user extend not found'); + if (!$oExtend)$this->addExtend(); return $this->newQuery()->where('uid', $uid)->decrement('fans_num'); } @@ -66,7 +82,7 @@ class CustomerUserExtendModel extends ApiBaseModel function incrFollowNum($uid): int { $oExtend = $this->findItem($uid, 'uid'); - if (!$oExtend) throw new ModelException('user extend not found'); + if (!$oExtend) $this->initExtend($uid); return $this->newQuery()->where('uid', $uid)->increment('follow_num'); } @@ -74,7 +90,7 @@ class CustomerUserExtendModel extends ApiBaseModel function decrFollowNum($uid): int { $oExtend = $this->findItem($uid, 'uid'); - if (!$oExtend) throw new ModelException('user extend not found'); + if (!$oExtend) $this->initExtend($uid); return $this->newQuery()->where('uid', $uid)->decrement('follow_num'); } diff --git a/app/Models/Api/Customer/CustomerUserModel.php b/app/Models/Api/Customer/CustomerUserModel.php index e82f4fe..2fdb4d1 100644 --- a/app/Models/Api/Customer/CustomerUserModel.php +++ b/app/Models/Api/Customer/CustomerUserModel.php @@ -109,12 +109,15 @@ class CustomerUserModel extends ApiBaseModel { $oTableCustomerUserCache = new TableCustomerUserCache(); $oTableCustomerUserCache->setPrimaryKey($iUid); + return $oTableCustomerUserCache->getCacheData(); // return Cache::remember($this->getCacheKey($iUid), RedisConst::ORM_FIND_CACHE_SECOND, function () use ($iUid) { // return $this->findItem($iUid); // }); } + + // function delItemFromCache($iUid): bool // { // return Cache::delete($this->getCacheKey($iUid)); diff --git a/app/Models/Api/Follow/FollowHistoryModel.php b/app/Models/Api/Follow/FollowHistoryModel.php index b7e1953..eeaf9b5 100644 --- a/app/Models/Api/Follow/FollowHistoryModel.php +++ b/app/Models/Api/Follow/FollowHistoryModel.php @@ -28,7 +28,7 @@ class FollowHistoryModel extends ApiBaseModel */ function addFollowHistory($method, $uid, $follow_uid): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null { - if(!in_array($method,self::METHOD)) throw new ModelException('method error'); + if(!array_key_exists($method,self::METHOD)) throw new ModelException('method error'); $aItem['method'] = $method; $aItem['uid'] = $uid; $aItem['follow_uid'] = $follow_uid; diff --git a/app/Service/Api/AuthService.php b/app/Service/Api/AuthService.php index e5887bb..3adfdd3 100644 --- a/app/Service/Api/AuthService.php +++ b/app/Service/Api/AuthService.php @@ -31,9 +31,11 @@ class AuthService function getUserInfoByToken($sToken) { + if (empty($sToken)) return null; $sUidInfo = Redis::get(RedisConst::TOKEN_UID . $sToken); if (empty($sUidInfo)) return null; + return unserialize($sUidInfo); } @@ -43,7 +45,14 @@ class AuthService 'uid' => $iUid, 'device' => $sDevice, ]); - return Redis::set(RedisConst::TOKEN_UID . $sToken, $sUidInfo, RedisConst::COMMON_EXP_TIME); + // 临时用predis + return Redis::command('set', [ + RedisConst::TOKEN_UID . $sToken, + $sUidInfo, + 'ex', + RedisConst::COMMON_EXP_TIME, + ]); + //return Redis::set(RedisConst::TOKEN_UID . $sToken, $sUidInfo, RedisConst::COMMON_EXP_TIME); } function delUserInfoToToken($sToken) @@ -78,7 +87,11 @@ class AuthService 'exp_time' => Carbon::parse(time() + RedisConst::COMMON_EXP_TIME)->toDateTimeString(), ]; $sTokenList = serialize($aTokenInfoList); - return Redis::set(RedisConst::UID_TOKENS . $iUid, $sTokenList); + // 临时用predis + return Redis::command('set', [ + RedisConst::UID_TOKENS . $iUid, $sTokenList + ]); + // return Redis::set(RedisConst::UID_TOKENS . $iUid, $sTokenList); } function delTokenByUidInfo($iUid, $sToken) @@ -88,7 +101,11 @@ class AuthService if (!isset($aTokenInfoList[$sToken])) return false; unset($aTokenInfoList[$sToken]); $sTokenList = serialize($aTokenInfoList); - return Redis::set(RedisConst::UID_TOKENS . $iUid, $sTokenList); + // 临时用predis + return Redis::command('set', [ + RedisConst::UID_TOKENS . $iUid, $sTokenList + ]); + //return Redis::set(RedisConst::UID_TOKENS . $iUid, $sTokenList); } function getTokenFromReq(\Illuminate\Http\Request $request = null) diff --git a/app/Service/Api/VrCodeService.php b/app/Service/Api/VrCodeService.php index daefabb..78c06ec 100644 --- a/app/Service/Api/VrCodeService.php +++ b/app/Service/Api/VrCodeService.php @@ -67,7 +67,9 @@ class VrCodeService */ public function setIType($iType): void { - if (!in_array($iType, VrCode::TOPIC)) throw new AppException('invalid sms type'); + if (!in_array($iType, VrCode::TOPIC)) { + throw new AppException('invalid sms type'); + } $this->iType = $iType; } @@ -93,7 +95,7 @@ class VrCodeService { $this->sKey = $this->genKey(); $this->sCode = $this->genCode(); - if (!$this->sendSmsToPhone()) throw new AppException('send sms failed'); + //没有先注释 if (!$this->sendSmsToPhone()) throw new AppException('send sms failed'); $this->setCodeToCache(); } @@ -102,7 +104,8 @@ class VrCodeService */ function genKey(): false|string { - if (!in_array($this->iType, VrCode::TOPIC_PREFIX)) throw new AppException('invalid sms type'); + var_dump($this->iType); + if (!in_array($this->iType, VrCode::TOPIC)) throw new AppException('invalid sms type'); if ($this->iType == VrCode::TOPIC_REGISTER) { return VrCode::TOPIC_PREFIX[VrCode::TOPIC_REGISTER] . md5($this->sPhoneArea . $this->sPhone); } else { @@ -117,7 +120,10 @@ class VrCodeService function setCodeToCache() { - return Redis::set($this->sKey, $this->sCode, VrCode::REDIS_CACHE_EXPIRE); + var_dump($this->sCode); + //临时用predis + return Redis::command('set', [$this->sKey, $this->sCode, 'ex', VrCode::REDIS_CACHE_EXPIRE]); + //return Redis::set($this->sKey, $this->sCode, VrCode::REDIS_CACHE_EXPIRE); } /** @@ -134,7 +140,9 @@ class VrCodeService if ($sReqCode === '000000') return true; } $sCode = $this->getCodeFromCache(); - if (empty($sCode)) return false; + var_dump($sCode); + var_dump($sReqCode); + if (empty($sReqCode)) return false; if ($sCode != $sReqCode) return false; return true; } diff --git a/composer.json b/composer.json index 696eaf1..a937f48 100644 --- a/composer.json +++ b/composer.json @@ -17,7 +17,8 @@ "iexbase/tron-api": "^5.0", "laravel/framework": "^10.10", "laravel/sanctum": "^3.3", - "laravel/tinker": "^2.8" + "laravel/tinker": "^2.8", + "predis/predis": "^3.0" }, "require-dev": { "fakerphp/faker": "^1.9.1", diff --git a/composer.lock b/composer.lock index 7cdb2e2..815ec62 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "eb2ef8d7d9bc89ca893bb6f81e8a32ab", + "content-hash": "25300898d0a947d27d6f30806d0906bc", "packages": [ { "name": "brick/math", @@ -3091,6 +3091,69 @@ ], "time": "2024-02-26T04:55:38+00:00" }, + { + "name": "predis/predis", + "version": "v3.0.1", + "source": { + "type": "git", + "url": "https://github.com/predis/predis.git", + "reference": "34fb0a7da0330df1bab4280fcac4afdeeccc3edf" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/predis/predis/zipball/34fb0a7da0330df1bab4280fcac4afdeeccc3edf", + "reference": "34fb0a7da0330df1bab4280fcac4afdeeccc3edf", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "psr/http-message": "^1.0|^2.0" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^3.3", + "phpstan/phpstan": "^1.9", + "phpunit/phpcov": "^6.0 || ^8.0", + "phpunit/phpunit": "^8.0 || ~9.4.4" + }, + "suggest": { + "ext-relay": "Faster connection with in-memory caching (>=0.6.2)" + }, + "type": "library", + "autoload": { + "psr-4": { + "Predis\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Till Krüss", + "homepage": "https://till.im", + "role": "Maintainer" + } + ], + "description": "A flexible and feature-complete Redis/Valkey client for PHP.", + "homepage": "http://github.com/predis/predis", + "keywords": [ + "nosql", + "predis", + "redis" + ], + "support": { + "issues": "https://github.com/predis/predis/issues", + "source": "https://github.com/predis/predis/tree/v3.0.1" + }, + "funding": [ + { + "url": "https://github.com/sponsors/tillkruss", + "type": "github" + } + ], + "time": "2025-05-16T18:30:32+00:00" + }, { "name": "psr/clock", "version": "1.0.0", @@ -8921,7 +8984,7 @@ ], "aliases": [], "minimum-stability": "stable", - "stability-flags": [], + "stability-flags": {}, "prefer-stable": true, "prefer-lowest": false, "platform": { @@ -8929,6 +8992,6 @@ "ext-bcmath": "*", "ext-gmp": "*" }, - "platform-dev": [], + "platform-dev": {}, "plugin-api-version": "2.6.0" } diff --git a/public/.htaccess b/public/.htaccess index 3aec5e2..e69de29 100644 --- a/public/.htaccess +++ b/public/.htaccess @@ -1,21 +0,0 @@ - - - Options -MultiViews -Indexes - - - RewriteEngine On - - # Handle Authorization Header - RewriteCond %{HTTP:Authorization} . - RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}] - - # Redirect Trailing Slashes If Not A Folder... - RewriteCond %{REQUEST_FILENAME} !-d - RewriteCond %{REQUEST_URI} (.+)/$ - RewriteRule ^ %1 [L,R=301] - - # Send Requests To Front Controller... - RewriteCond %{REQUEST_FILENAME} !-d - RewriteCond %{REQUEST_FILENAME} !-f - RewriteRule ^ index.php [L] - diff --git a/public/phpinfo.php b/public/phpinfo.php new file mode 100644 index 0000000..c4837a3 --- /dev/null +++ b/public/phpinfo.php @@ -0,0 +1 @@ +