This commit is contained in:
pomjay
2025-06-25 09:35:09 +08:00
parent 81a95f47a0
commit 7e3eba92e2
16 changed files with 154 additions and 44 deletions

View File

@ -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;

View File

@ -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);

View File

@ -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;
}
}

View File

@ -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');
});

View File

@ -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);
}
}

View File

@ -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);

View File

@ -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());

View File

@ -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');
}

View File

@ -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));

View File

@ -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;

View File

@ -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)

View File

@ -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;
}

View File

@ -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",

69
composer.lock generated
View File

@ -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"
}

View File

@ -1,21 +0,0 @@
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
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]
</IfModule>

1
public/phpinfo.php Normal file
View File

@ -0,0 +1 @@
<?php phpinfo();