mkm efc7286162 feat: 添加或修改API;
fix: 修复错误;
refactor: 重写/重构代码,但未改变API行为;
style: 添加空格、格式化、缺失的分号等;
test: 添加缺失的测试或修正现有的测试;
docs: 更新文档如readme;
build: 更新依赖、项目版本;
ops: 影响操作组件如基础设施、部署、备份、恢复;
chore: 修改.gitignore;

请根据以上信息生成规范的用中文conventional commit message,谨慎选择最能说明更改的Commit type,请控制你的输出在一行内,你的回复中应该仅有一条commit message。
Your reply format:
2024-09-02 15:31:07 +08:00

209 lines
4.9 KiB
PHP

<?php
namespace app\common\model\user;
use app\common\enum\user\UserEnum;
use app\common\model\BaseModel;
use app\common\model\user_label\UserLabel;
use app\common\model\user_ship\UserShip;
use app\common\service\FileService;
use support\Log;
use think\model\concern\SoftDelete;
use Throwable;
/**
* 用户模型
* Class User
* @package app\common\model\user
*/
class User extends BaseModel
{
use SoftDelete;
protected $deleteTime = 'delete_time';
//会员类型
public function userShip()
{
return $this->hasOne(UserShip::class, 'id', 'user_ship')
->bind(['vip_name' => 'title', 'discount', 'limit']);
}
public function userLabel()
{
return $this->hasOne(UserLabel::class, 'label_id', 'label_id')
->bind(['label_name']);
}
/**
* @notes 关联用户授权模型
* @return \think\model\relation\HasOne
* @author 乔峰
* @date 2022/9/22 16:03
*/
public function userAuth()
{
return $this->hasOne(UserAuth::class, 'user_id');
}
/**
* @notes 搜索器-用户信息
* @param $query
* @param $value
* @param $data
* @author 乔峰
* @date 2022/9/22 16:12
*/
public function searchKeywordAttr($query, $value, $data)
{
if ($value) {
$query->where('nickname|mobile', 'like', '%' . $value . '%');
}
}
/**
* @notes 搜索器-注册来源
* @param $query
* @param $value
* @param $data
* @author 乔峰
* @date 2022/9/22 16:13
*/
public function searchChannelAttr($query, $value, $data)
{
if ($value) {
$query->where('channel', '=', $value);
}
}
/**
* @notes 搜索器-注册时间
* @param $query
* @param $value
* @param $data
* @author 乔峰
* @date 2022/9/22 16:13
*/
public function searchCreateTimeStartAttr($query, $value, $data)
{
if ($value) {
$query->where('create_time', '>=', $value);
}
}
/**
* @notes 搜索器-注册时间
* @param $query
* @param $value
* @param $data
* @author 乔峰
* @date 2022/9/22 16:13
*/
public function searchCreateTimeEndAttr($query, $value, $data)
{
if ($value) {
$query->where('create_time', '<=', $value);
}
}
/**
* @notes 头像获取器 - 用于头像地址拼接域名
* @param $value
* @return string
* @author Tab
* @date 2021/7/17 14:28
*/
public function getAvatarAttr($value)
{
if ($value) {
return trim($value) ? FileService::getFileUrl($value) : '';
}
}
/**
* @notes 获取器-性别描述
* @param $value
* @param $data
* @return string|string[]
* @author 乔峰
* @date 2022/9/7 15:15
*/
public function getSexTextAttr($value, $data)
{
return UserEnum::getSexDesc($data['sex']);
}
/**
* @notes 登录时间
* @param $value
* @return string
* @author 乔峰
* @date 2022/9/23 18:15
*/
public function getLoginTimeAttr($value)
{
return $value ? date('Y-m-d H:i:s', $value) : '';
}
/**
* @notes 生成用户编码
* @param string $prefix
* @param int $length
* @return string
* @throws \think\db\exception\DataNotFoundException
* @throws \think\db\exception\DbException
* @throws \think\db\exception\ModelNotFoundException
* @author 乔峰
* @date 2022/9/16 10:33
*/
public static function createUserSn($prefix = '', $length = 8)
{
// $rand_str = '';
// for ($i = 0; $i < $length; $i++) {
// $rand_str .= mt_rand(0, 9);
// }
$sn = $prefix . time();
// if (User::where(['sn' => $sn])->find()) {
// return self::createUserSn($prefix, $length);
// }
return $sn;
}
/**
* 获取用户统计数据
* @param $time
* @param $type
* @param $timeType
* @return mixed
*/
public function getTrendData($time, $where, $timeType, $create_time = 'create_time')
{
return $this->where($where)->where(function ($query) use ($time, $create_time) {
if ($time[0] == $time[1]) {
$query->whereDay($create_time, date('Y-m-d', $time[0]));
} else {
$time[1] = $time[1] + 86400;
$query->whereTime($create_time, 'between', $time);
}
})->field("FROM_UNIXTIME($create_time,'$timeType') as days,count(id) as num")->group('days')->select()->toArray();
}
public static function onAfterWrite($data){
try{
channelLog($data->getOrigin(),'user','更新前');
channelLog($data->toArray(),'user','更新后');
}catch(Throwable $e){
Log::error('user:'.$e->getMessage());
}
}
}