发布推送

This commit is contained in:
ROmani
2024-02-26 00:41:25 +08:00
parent 70304f730b
commit 1a9008b318
33 changed files with 1515 additions and 216 deletions

View File

@ -1,105 +0,0 @@
<?php
namespace App\Models\Customer;
use App\Const\RedisConst;
use App\Models\Base\CustomerBaseModel;
use App\Service\AuthService;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Redis;
class CustomerUser extends CustomerBaseModel
{
protected $table = 'customer_users';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'status',
'im_user_id',
'country_name',
'username',
'password',
'nickname',
'email',
'phone_area',
'phone',
'is_google_auth',
'created_at',
'updated_at',
];
protected function password(): Attribute
{
return Attribute::make(
set: fn (string $value) => Hash::make($value),
);
}
function checkPasswd($iUid,$sPasswd):bool
{
$oUser = $this->where('id',$iUid)->first();
if(empty($oUser)) return false;
return Hash::check($sPasswd,$oUser->password);
}
function addUser($aItem): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
$sDateTime = Carbon::now()->toDateTimeString();
$aItem['created_at'] = $sDateTime;
$aItem['updated_at'] = $sDateTime;
return $this->addItem($aItem);
}
function findItemByAccount($aData,$col=['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
if(!empty($aData['username'])){
return $this->findItemByUsername($aData['username'],$col);
}
if(!empty($aData['phone_area']) && !empty($aData['phone'])){
return $this->findItemByPhone($aData['phone_area'],$aData['phone'],$col);
}
if(!empty($aData['email'])){
return $this->findItemByEmail($aData['email'],$col);
}
return null;
}
function findItemByUsername($sUsername,$col=['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('username',$sUsername)->first($col);
}
function findItemByPhone($sPhoneArea,$sPhone,$col=['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('phone_area',$sPhoneArea)->where('phone',$sPhone)->first($col);
}
function findItemByEmail($sEmail,$col=['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('email',$sEmail)->first($col);
}
function findUserByUidWithCache($iUid): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Builder|array|null
{
return Cache::remember(RedisConst::ORM_CACHE_USER.$iUid,RedisConst::ORM_FIND_CACHE_SECOND,function ()use ($iUid){
return $this->findItem($iUid);
});
}
// function setUserInfo($iUid,$sNickname): bool|int
// {
// return $this->updateItem([
// 'id' => $iUid,
// 'nickname' => $sNickname,
// 'email' => $sNickname,
// 'phone_area' => $sNickname,
// ]);
// }
}

View File

@ -0,0 +1,72 @@
<?php
namespace App\Models\Customer;
use App\Exceptions\ModelException;
use App\Models\Base\CustomerBaseModel;
class CustomerUserExtendModel extends CustomerBaseModel
{
protected $table = 'customer_user_extend';
protected $primaryKey = 'uid';
protected $fillable = [
'uid',
'is_active',
'fans_num',
'follow_num',
'updated_at',
];
//是否活跃用户
const IS_ACTIVE_YES = 1;
const IS_ACTIVE_NO = 2;
const IS_ACTIVE = [
self::IS_ACTIVE_YES => '活跃',
self::IS_ACTIVE_NO => '不活跃',
];
//增加用户扩展信息
function addExtend($aItem): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
if(empty($aItem['uid'])) throw new ModelException('uid error');
if($this->findItem($aItem['uid'],['uid'])) return null; //已存在
$sDateTime = date('Y-m-d H:i:s');
$aItem['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');
return $this->newQuery()->where('uid',$uid)->increment('fans_num');
}
//减去当前追随者总数
function decrFansNum($uid): int
{
$oExtend = $this->findItem($uid,'uid');
if(!$oExtend) throw new ModelException('user extend not found');
return $this->newQuery()->where('uid',$uid)->decrement('fans_num');
}
//增加当前订阅总数
function incrFollowNum($uid): int
{
$oExtend = $this->findItem($uid,'uid');
if(!$oExtend) throw new ModelException('user extend not found');
return $this->newQuery()->where('uid',$uid)->increment('follow_num');
}
//减去当前订阅总数
function decrFollowNum($uid): int
{
$oExtend = $this->findItem($uid,'uid');
if(!$oExtend) throw new ModelException('user extend not found');
return $this->newQuery()->where('uid',$uid)->decrement('follow_num');
}
}

View File

@ -0,0 +1,141 @@
<?php
namespace App\Models\Customer;
use App\Cache\Table\TableCustomerUserCache;
use App\Models\Base\CustomerBaseModel;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
class CustomerUserModel extends CustomerBaseModel
{
protected $table = 'customer_users';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'status',
'im_user_id',
'country_name',
'username',
'password',
'nickname',
'email',
'phone_area',
'phone',
'is_google_auth',
'google_auth_secret',
'created_at',
'updated_at',
];
protected $hidden = [
'password',
'google_auth_secret',
];
//插入密码hash加密
protected function password(): Attribute
{
return Attribute::make(
set: fn(string $value) => Hash::make($value),
);
}
//对比密码是否正确
function checkPasswd($iUid, $sPasswd): bool
{
$oUser = $this->where('id', $iUid)->first();
if (empty($oUser)) return false;
return Hash::check($sPasswd, $oUser->password);
}
//添加用户
function addUser($aItem): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
$sDateTime = Carbon::now()->toDateTimeString();
$aItem['created_at'] = $sDateTime;
$aItem['updated_at'] = $sDateTime;
return $this->addItem($aItem);
}
//查找账户-所有方式
function findItemByAccount($aData, $col = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
$oQuery = $this->newQuery();
if (!empty($aData['username'])) {
$oQuery->orWhere('username', $aData['username']);
} elseif (!empty($aData['email'])) {
$oQuery->orWhere('email', $aData['email']);
} elseif (!empty($aData['phone']) && !empty($aData['phone_area'])) {
$oQuery->orWhere([
'phone' => $aData['phone'],
'phone_area' => $aData['phone_area'],
]);
} else {
throw new \Exception('findItemByAccount params error');
}
return $oQuery->first($col);
}
//查找账户-用户名
function findItemByUsername($sUsername, $col = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('username', $sUsername)->first($col);
}
//查找账户-手机
function findItemByPhone($sPhoneArea, $sPhone, $col = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('phone_area', $sPhoneArea)->where('phone', $sPhone)->first($col);
}
//查找账户-邮箱
function findItemByEmail($sEmail, $col = ['*']): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
return $this->newQuery()->where('email', $sEmail)->first($col);
}
//根据uid从缓存中查询用户信息不存在则从数据库中查询
/**
* @throws \Exception
*/
function findUserByUidWithCache($iUid): array|null
{
$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));
// }
// //生成user缓存key
// function getCacheKey($iUid): string
// {
// if(empty($iUid)) throw new \Exception('getCacheKey params error');
// return RedisConst::ORM_CACHE_USER . $iUid;
// }
// function setUserInfo($iUid,$sNickname): bool|int
// {
// return $this->updateItem([
// 'id' => $iUid,
// 'nickname' => $sNickname,
// 'email' => $sNickname,
// 'phone_area' => $sNickname,
// ]);
// }
}