更换model目录

This commit is contained in:
cano
2024-03-04 04:28:48 +08:00
parent f4f61a5f4c
commit bebbee4184
29 changed files with 90 additions and 146 deletions

View File

@ -0,0 +1,63 @@
<?php
namespace App\Models\Api\Customer;
use App\Models\Api\Base\ApiBaseModel;
use Illuminate\Support\Carbon;
class CustomerChangeInfoLogModel extends ApiBaseModel
{
protected $table = 'customer_change_info_log';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'type',
'uid',
'column',
'value',
'before_value',
'after_value',
'pid',
'remark_key',
'remark_desc',
'created_at',
];
const PID_SYSTEM = 0; //系统默认pid
const TYPE_CHANG_USER_ACTIVE_STATUS = 1;
const TYPE = [
self::TYPE_CHANG_USER_ACTIVE_STATUS => '修改用户活跃状态',
];
const REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES = 'dailyCheckUserActiveStatusYes';
const REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO = 'dailyCheckUserActiveStatusNo';
const REMARK = [
self::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES => '每日检查用户活跃状态-活跃',
self::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO => '每日检查用户活跃状态-不活跃',
];
function addLog($aItem): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
$sDateTime = Carbon::now()->toDateTimeString();
$aItem['created_at'] = $sDateTime;
return $this->addItem($aItem);
}
function addUserActiveStatusLog($uid,$beforeValue,$value,$remark_key): \Illuminate\Database\Eloquent\Model|\Illuminate\Database\Eloquent\Builder|array|null
{
$aLogInsert = [
'type' => CustomerChangeInfoLogModel::TYPE_CHANG_USER_ACTIVE_STATUS,
'uid' => $uid,
'column' => CustomerUserExtendModel::COL_IS_ACTIVE,
'before_value' => $beforeValue,
'pid' => CustomerChangeInfoLogModel::PID_SYSTEM,
'value' => $value,
'after_value' => $value,
'remark_key' => $remark_key,
'remark_desc' => CustomerChangeInfoLogModel::REMARK[$remark_key],
];
return $this->addLog($aLogInsert);
}
}

View File

@ -0,0 +1,19 @@
<?php
namespace App\Models\Api\Customer;
use App\Models\Api\Base\ApiBaseModel;
class CustomerLoginHistoryModel extends ApiBaseModel
{
protected $table = 'customer_login_history';
protected $primaryKey = 'id';
protected $fillable = [
'id',
'status',
'uid',
'device',
'created_at',
];
}

View File

@ -0,0 +1,180 @@
<?php
namespace App\Models\Api\Customer;
use App\Exceptions\ModelException;
use App\Jobs\UserActiveStatusQueue;
use App\Models\Api\Post\PostPushBoxModel;
use App\Models\Api\WebSocket\ApiWsHistoryModel;
use App\Models\Api\Base\ApiBaseModel;
use App\Structs\QueueUserActiveStatusStruct;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
class CustomerUserExtendModel extends ApiBaseModel
{
protected $table = 'customer_user_extend';
protected $primaryKey = 'uid';
protected $fillable = [
'uid',
'is_active',
'fans_num',
'follow_num',
'updated_at',
];
const COL_IS_ACTIVE = 'is_active';
//是否活跃用户
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');
}
//获取用户活跃信息
function getUserActiveListLimit($nowUid, $limit = 500): \Illuminate\Database\Eloquent\Collection|array
{
return $this->newQuery()
->where('id', '>=', $nowUid)
->limit($limit)
->orderBy('id')
->get(['uid', 'is_active']);
}
//检测所有用户活跃状态
function updateAllUserActiveStatus($date = null): void
{
if (empty($date)) $date = Carbon::yesterday()->toDateString();
$oCustomerWsHistoryModel = new ApiWsHistoryModel();
$aActiveUserIdList = $oCustomerWsHistoryModel->getActiveUserIdList($date); //三日内活跃用户
if (empty($aActiveUserIdList)) return;
$oCustomerChangeInfoLogModel = new CustomerChangeInfoLogModel();
$nowUid = 0;
while (true) {
try {
Db::beginTransaction();
$aUserExtendList = $this->getUserActiveListLimit($nowUid, 500);
if (empty($aUserExtendList)) break;
$nowUid = max($aUserExtendList->pluck('uid')->toArray());
foreach ($aUserExtendList as $oUserExtend) {
if (in_array($oUserExtend->uid, $aActiveUserIdList)) { //在活跃列表中
if ($oUserExtend->is_active == self::IS_ACTIVE_YES) continue; //已经是活跃用户
//变更用户状态
$res = $this->newQuery()->where('uid', $oUserExtend->uid)->update(['is_active' => self::IS_ACTIVE_YES]);
if ($res) {
//记录日志
$oCustomerChangeInfoLogModel->addUserActiveStatusLog($oUserExtend->uid, $oUserExtend->is_active, self::IS_ACTIVE_YES, CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES);
$this->activeUserStatusToQueueProducer($oUserExtend->uid); //投递到消息队列
}
} else { //三日内不活跃
if ($oUserExtend->is_active == self::IS_ACTIVE_NO) continue; //已经是不活跃用户
//变更用户状态
$res = $this->newQuery()->where('uid', $oUserExtend->uid)->update(['is_active' => self::IS_ACTIVE_NO]);
if ($res) {
//记录日志
$oCustomerChangeInfoLogModel->addUserActiveStatusLog($oUserExtend->uid, $oUserExtend->is_active, self::IS_ACTIVE_YES, CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_NO);
}
}
}
Db::commit();
} catch (\Exception $e) {
Log::error('updateUserActiveStatus error:' . $e->getMessage());
Db::rollBack();
}
}
}
//登录检测用户活跃状态
function updateUserActiveStatus($uid): void
{
try{
Db::beginTransaction();
$oCustomerWsHistoryModel = new ApiWsHistoryModel();
$aActiveUserId = $oCustomerWsHistoryModel->findActiveUserId($uid, Carbon::yesterday()->toDateString()); //三日内活跃用户
if(empty($aActiveUserId)) return;
$oCustomerUserExtendModel = $this->newQuery()->where('uid', $uid)->first(['uid','is_active']);
if(!$oCustomerUserExtendModel) return;
$oCustomerChangeInfoLogModel = new CustomerChangeInfoLogModel();
$res = $this->newQuery()->where('uid', $uid)->update(['is_active' => self::IS_ACTIVE_YES]);
if ($res) {
//记录日志
$oCustomerChangeInfoLogModel->addUserActiveStatusLog($uid, $oCustomerUserExtendModel->is_active, self::IS_ACTIVE_YES, CustomerChangeInfoLogModel::REMARK_DAILY_CHECK_USER_ACTIVE_STATUS_YES);
$this->activeUserStatusToQueueProducer($uid); //投递到消息队列
}
Db::commit();
}catch (\Exception $e){
Log::error('updateUserActiveStatus error:' . $e->getMessage());
Db::rollBack();
}
}
//投递到消息队列,处理活跃推送信箱更新问题
function activeUserStatusToQueueProducer($uid): void
{
$params = QueueUserActiveStatusStruct::PARAMS;
$params['uid'] = $uid;
$params['queueCreatedAt'] = date('Y-m-d H:i:s');
UserActiveStatusQueue::dispatch($params)->onQueue(QueueUserActiveStatusStruct::QUEUE_NAME);
}
function activeUserStatusQueueConsumer($params): void
{
$uid = $params['uid'];
$oPostPushBoxModel = new PostPushBoxModel();
$oPostPushBoxModel->pullBigFanMasterPostConsumer($uid);
}
}

View File

@ -0,0 +1,143 @@
<?php
namespace App\Models\Api\Customer;
use App\Cache\Table\TableCustomerUserCache;
use App\Models\Api\Base\ApiBaseModel;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Hash;
class CustomerUserModel extends ApiBaseModel
{
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,
// ]);
// }
}