add service dir

This commit is contained in:
cano
2024-03-26 20:07:19 +08:00
parent 6ce37d789f
commit 01e93acdd8
20 changed files with 24 additions and 30 deletions

View File

@ -0,0 +1,158 @@
<?php
namespace App\Service\Api;
use App\Const\RedisConst;
use App\Tools\Tools;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Redis;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\NotFoundExceptionInterface;
class AuthService
{
const tokenUidInfo = [
'uid' => '',
'device' => '',
];
const uidTokenList = [
'token_1' => [
'device' => '',
'created_time' => '',
'exp_time' => '',
],
];
function checkTokenLogin($sToken): bool
{
return $this->getUserInfoByToken($sToken) != null;
}
function getUserInfoByToken($sToken)
{
if (empty($sToken)) return null;
$sUidInfo = Redis::get(RedisConst::TOKEN_UID . $sToken);
if (empty($sUidInfo)) return null;
return unserialize($sUidInfo);
}
function setUserInfoToToken($sToken, $iUid, $sDevice)
{
$sUidInfo = serialize([
'uid' => $iUid,
'device' => $sDevice,
]);
return Redis::set(RedisConst::TOKEN_UID . $sToken, $sUidInfo, RedisConst::COMMON_EXP_TIME);
}
function delUserInfoToToken($sToken)
{
return Redis::del(RedisConst::TOKEN_UID . $sToken);
}
function getAllTokenInfoByUid($iUid)
{
$sTokenList = Redis::get(RedisConst::UID_TOKENS . $iUid);
if (empty($sTokenList)) return null;
return unserialize($sTokenList);
}
function checkTokenInUid($iUid, $sToken): bool
{
$aTokenInfoList = $this->getAllTokenInfoByUid($iUid);
if (empty($aTokenInfoList)) return false;
$aTokenList = array_keys($aTokenInfoList);
return in_array($sToken, $aTokenList);
}
function addTokenToUidInfo($iUid, $sToken, $sDevice)
{
$aTokenInfoList = $this->getAllTokenInfoByUid($iUid);
if (empty($aTokenInfoList)) {
$aTokenInfoList = [];
}
$aTokenInfoList[$sToken] = [
'device' => $sDevice,
'created_time' => Carbon::now()->toDateTimeString(),
'exp_time' => Carbon::parse(time() + RedisConst::COMMON_EXP_TIME)->toDateTimeString(),
];
$sTokenList = serialize($aTokenInfoList);
return Redis::set(RedisConst::UID_TOKENS . $iUid, $sTokenList);
}
function delTokenByUidInfo($iUid, $sToken)
{
$aTokenInfoList = $this->getAllTokenInfoByUid($iUid);
if (empty($aTokenInfoList)) return false;
if (!isset($aTokenInfoList[$sToken])) return false;
unset($aTokenInfoList[$sToken]);
$sTokenList = serialize($aTokenInfoList);
return Redis::set(RedisConst::UID_TOKENS . $iUid, $sTokenList);
}
function getTokenFromReq(\Illuminate\Http\Request $request = null)
{
if ($request == null) $request = request();
$sToken = $request->header('_token');
if (!empty($sToken)) return $sToken;
$sToken = $request->input('_token');
if (!empty($sToken)) return $sToken;
return null;
}
function generateTokenStr(): string
{
return time() . Tools::generateRandStr(24);
}
//登入使用
function createTokenToUser($iUid, $sDevice): string
{
do {
$sToken = $this->generateTokenStr();
if (!$this->checkTokenLogin($sToken)) break;
} while (1);
$this->setUserInfoToToken($sToken, $iUid, $sDevice);
$this->addTokenToUidInfo($iUid, $sToken, $sDevice);
return $sToken;
}
//登出使用
function delTokenToUser($iUid, $sToken): void
{
$this->delUserInfoToToken($sToken);
$this->delTokenByUidInfo($iUid, $sToken);
}
function getTokenInfo()
{
$sToken = $this->getTokenFromReq();
if (empty($sToken)) return null;
$aUserInfo = $this->getUserInfoByToken($sToken);
if (empty($aUserInfo)) return null;
return $aUserInfo;
}
function setCurrentUser(array $aUser): void
{
app()->singleton('customerUser',function () use ($aUser){
return $aUser;
});
}
/**
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
*/
function getCurrentUser()
{
if(app()->has('customerUser')){
return app()->get('customerUser');
}
return null;
}
}

View File

@ -0,0 +1,80 @@
<?php
namespace App\Service\Api;
use App\Const\Im;
use App\Exceptions\AppException;
use Illuminate\Support\Env;
use Illuminate\Support\Facades\Http;
class ImService{
const user_register_data = [
'secret' => '',
'users' => [
'userID' => '',
'nickname' => '',
'faceURL' => '',
],
];
/**
* @throws AppException
*/
function userUserRegister($userId, $nickname = '', $faceUrl=''): bool
{
$path = '/user/user_register';
$aData['users'] = [
'userID' => $this->genImUid($userId),
'nickname' => $nickname,
'faceURL' => $faceUrl,
];
$jRes = $this->sendReq($path,$aData);
if(isset($jRes['errCode']) && $jRes['errCode'] === 0) return true;
return false;
}
/**
* @throws AppException
*/
function authUserToken($userId,int $platform): false|array
{
$path = '/auth/user_token';
$aData['users'] = [
'platformID' => $platform,
'userID' => $this->genImUid($userId),
];
$jRes = $this->sendReq($path,$aData);
if(isset($jRes['errCode']) && $jRes['errCode'] === 0){
return [
'token' => $jRes['token'],
'expireTimeSeconds' => $jRes['expireTimeSeconds'],
];
}
return false;
}
function getSecret()
{
return Env::get('IM_SECRET');
}
function getUrl(): string
{
return rtrim(Env::get('IM_URL'),'/');
}
/**
* @throws AppException
*/
function sendReq(string $path,array $aData = [])
{
if (!isset($aData['secret'])) $aData['secret'] = $this->getSecret();
$resp = Http::post($this->getUrl().$path,$aData);
if(!$resp->ok()) throw new AppException('im request error');
return $resp->json();
}
function genImUid($userId): string
{
return Im::USER_PREFIX.$userId;
}
}

View File

@ -0,0 +1,28 @@
<?php
namespace App\Service\Api;
use App\Const\Responses;
class ReplyService
{
static function reply($code, $msg, $data = []): \Illuminate\Http\JsonResponse
{
return response()->json([
'code' => $code,
'msg' => $msg,
'data' => $data,
]);
}
static function success($data = []): \Illuminate\Http\JsonResponse
{
return self::reply(Responses::CODE_SUCCESS, 'success', $data);
}
static function error($msg = 'error', $data = []): \Illuminate\Http\JsonResponse
{
return self::reply(Responses::CODE_ERROR, $msg, $data);
}
}

View File

@ -0,0 +1,21 @@
<?php
namespace App\Service\Api;
use App\Thrid\Sms\Movider\Movider;
use App\Thrid\Sms\SmsInterface;
class SmsService
{
function sendSmsCode($phone, $code): bool
{
return $this->getChannel()->sendSmsCode($phone, $code);
}
function getChannel(): SmsInterface
{
return new Movider();
}
}

View File

@ -0,0 +1,143 @@
<?php
namespace App\Service\Api;
use App\Const\VrCode;
use App\Exceptions\AppException;
use App\Tools\Tools;
use Illuminate\Support\Env;
use Illuminate\Support\Facades\Redis;
class VrCodeService
{
public $sKey = null;
public $sCode = null;
public $sPhoneArea = null;
public $sPhone = null;
public $iType = null;
public $iUid = null;
public function getSKey()
{
return $this->sKey;
}
public function setSKey($sKey): void
{
$this->sKey = $sKey;
}
public function getsCode()
{
return $this->sCode;
}
public function setsCode($sCode): void
{
$this->sCode = $sCode;
}
public function getSPhoneArea()
{
return $this->sPhoneArea;
}
public function setSPhoneArea($sPhoneArea): void
{
$this->sPhoneArea = $sPhoneArea;
}
public function getSPhone()
{
return $this->sPhone;
}
public function setSPhone($sPhone): void
{
$this->sPhone = $sPhone;
}
public function getIType()
{
return $this->iType;
}
/**
* @throws AppException
*/
public function setIType($iType): void
{
if (!in_array($iType, VrCode::TOPIC)) throw new AppException('invalid sms type');
$this->iType = $iType;
}
public function getIUid()
{
return $this->iUid;
}
public function setIUid($iUid): void
{
$this->iUid = $iUid;
}
function sendSmsToPhone(): bool
{
return (new SmsService())->sendSmsCode($this->sPhoneArea.$this->sPhone, $this->sCode);
}
/**
* @throws AppException
*/
function sendCode(): void
{
$this->sKey = $this->genKey();
$this->sCode = $this->genCode();
if (!$this->sendSmsToPhone()) throw new AppException('send sms failed');
$this->setCodeToCache();
}
/**
* @throws AppException
*/
function genKey(): false|string
{
if (!in_array($this->iType, VrCode::TOPIC_PREFIX)) 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 {
return VrCode::TOPIC_PREFIX[VrCode::TOPIC_REGISTER] . $this->iUid;
}
}
function genCode(): string
{
return (string)Tools::getRandNum();
}
function setCodeToCache()
{
return Redis::set($this->sKey, $this->sCode, VrCode::REDIS_CACHE_EXPIRE);
}
/**
* @throws AppException
*/
function getCodeFromCache()
{
return Redis::get($this->genKey());
}
function checkCode($sReqCode): bool
{
if(Env::get('APP_DEBUG') == true){
if ($sReqCode === '000000') return true;
}
$sCode = $this->getCodeFromCache();
if (empty($sCode)) return false;
if ($sCode != $sReqCode) return false;
return true;
}
}