2022-08-19 19:48:37 +08:00
|
|
|
<?php
|
|
|
|
|
declare (strict_types = 1);
|
|
|
|
|
|
|
|
|
|
namespace app\common\model\system;
|
2022-11-28 19:11:12 +08:00
|
|
|
use Psr\SimpleCache\InvalidArgumentException;
|
2022-08-19 19:48:37 +08:00
|
|
|
use think\Model;
|
|
|
|
|
use app\common\library\ParseData;
|
|
|
|
|
use think\model\concern\SoftDelete;
|
2022-11-28 19:11:12 +08:00
|
|
|
use think\model\relation\HasMany;
|
|
|
|
|
use think\model\relation\HasOne;
|
2022-08-19 19:48:37 +08:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @mixin \think\Model
|
|
|
|
|
*/
|
|
|
|
|
class User extends Model
|
|
|
|
|
{
|
|
|
|
|
use SoftDelete;
|
|
|
|
|
|
|
|
|
|
// 定义时间戳字段名
|
|
|
|
|
protected $createTime = 'create_time';
|
|
|
|
|
protected $updateTime = 'update_time';
|
|
|
|
|
|
2022-11-28 19:11:12 +08:00
|
|
|
/**
|
|
|
|
|
* 定义第三方登录
|
|
|
|
|
* @return HasMany
|
|
|
|
|
*/
|
|
|
|
|
public function third(): HasMany
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
return $this->hasMany(UserThird::class,'user_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 关联用户组
|
|
|
|
|
*
|
2022-11-28 19:11:12 +08:00
|
|
|
* @return HasOne
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
2022-11-28 19:11:12 +08:00
|
|
|
public function group(): HasOne
|
2022-08-19 19:48:37 +08:00
|
|
|
{
|
|
|
|
|
return $this->hasOne(UserGroup::class,'id','group_id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注册会员前
|
|
|
|
|
* @param object $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function onBeforeInsert(object $model)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新会员前
|
|
|
|
|
* @param object $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function onBeforeUpdate(object $model)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 注册会员后
|
|
|
|
|
* @param object $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function onAfterInsert(object $model)
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 更新会员数据
|
|
|
|
|
* @param object $model
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function onAfterUpdate(object $model)
|
|
|
|
|
{}
|
2022-11-28 19:11:12 +08:00
|
|
|
|
2022-08-19 19:48:37 +08:00
|
|
|
/**
|
|
|
|
|
* 获取头像
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string
|
2022-11-28 19:11:12 +08:00
|
|
|
* @throws InvalidArgumentException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
|
|
|
|
public function getAvatarAttr(string $value, array $data): string
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
if ($value && strpos($value,'://')) {
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (empty($value)) {
|
|
|
|
|
$value = '/static/images/user_default.jpg';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$prefix = cdn_Prefix();
|
|
|
|
|
if (!empty($prefix) && $value) {
|
|
|
|
|
if (!str_contains($value,'data:image')) {
|
|
|
|
|
return $prefix.$value;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 设置头像
|
|
|
|
|
* @param string $value
|
|
|
|
|
* @param array $data
|
|
|
|
|
* @return string
|
2022-11-28 19:11:12 +08:00
|
|
|
* @throws InvalidArgumentException
|
2022-08-19 19:48:37 +08:00
|
|
|
*/
|
|
|
|
|
public function setAvatarAttr(string $value, array $data): string
|
|
|
|
|
{
|
|
|
|
|
return ParseData::setImageAttr($value, $data);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 登录时间
|
|
|
|
|
*/
|
|
|
|
|
public function getLoginTimeAttr($value)
|
|
|
|
|
{
|
|
|
|
|
if (!empty($value)) {
|
|
|
|
|
$value = date('Y-m-d H:i:s',$value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 减少会员积分
|
|
|
|
|
*
|
|
|
|
|
* @param integer $id
|
|
|
|
|
* @param integer $score
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function reduceScore(int $id = 0, int $score = 0)
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
if ($score) {
|
|
|
|
|
self::where('id', $id)->dec('score', $score)->update();
|
|
|
|
|
}
|
|
|
|
|
} catch (\Throwable $th) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|