219 lines
6.4 KiB
PHP
Raw Normal View History

2025-05-26 16:02:00 +08:00
<?php
namespace app\common\model;
2025-05-27 18:33:00 +08:00
use app\common\library\Auth;
use app\common\model\score\Employee;
use app\common\model\score\Team;
use fast\Random;
2025-05-26 16:02:00 +08:00
use think\Db;
2025-05-27 18:33:00 +08:00
use think\Exception;
use think\Hook;
2025-05-26 16:02:00 +08:00
use think\Model;
/**
* 会员模型
* @method static mixed getByUsername($str) 通过用户名查询用户
* @method static mixed getByNickname($str) 通过昵称查询用户
* @method static mixed getByMobile($str) 通过手机查询用户
* @method static mixed getByEmail($str) 通过邮箱查询用户
*/
class User extends Model
{
// 开启自动写入时间戳字段
protected $autoWriteTimestamp = 'int';
// 定义时间戳字段名
protected $createTime = 'createtime';
protected $updateTime = 'updatetime';
// 追加属性
protected $append = [
'url',
];
/**
* 获取个人URL
* @param string $value
* @param array $data
* @return string
*/
public function getUrlAttr($value, $data)
{
return "/u/" . $data['id'];
}
/**
* 获取头像
* @param string $value
* @param array $data
* @return string
*/
public function getAvatarAttr($value, $data)
{
if (!$value) {
//如果不需要启用首字母头像,请使用
//$value = '/assets/img/avatar.png';
$value = letter_avatar($data['nickname']);
}
return $value;
}
/**
* 获取会员的组别
*/
public function getGroupAttr($value, $data)
{
return UserGroup::get($data['group_id']);
}
/**
* 获取验证字段数组值
* @param string $value
* @param array $data
* @return object
*/
public function getVerificationAttr($value, $data)
{
$value = array_filter((array)json_decode($value, true));
$value = array_merge(['email' => 0, 'mobile' => 0], $value);
return (object)$value;
}
/**
* 设置验证字段
* @param mixed $value
* @return string
*/
public function setVerificationAttr($value)
{
$value = is_object($value) || is_array($value) ? json_encode($value) : $value;
return $value;
}
/**
* 变更会员余额
* @param int $money 余额
* @param int $user_id 会员ID
* @param string $memo 备注
*/
public static function money($money, $user_id, $memo)
{
Db::startTrans();
try {
$user = self::lock(true)->find($user_id);
if ($user && $money != 0) {
$before = $user->money;
//$after = $user->money + $money;
$after = function_exists('bcadd') ? bcadd($user->money, $money, 2) : $user->money + $money;
//更新会员信息
$user->save(['money' => $after]);
//写入日志
MoneyLog::create(['user_id' => $user_id, 'money' => $money, 'before' => $before, 'after' => $after, 'memo' => $memo]);
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
}
/**
* 变更会员积分
* @param int $score 积分
* @param int $user_id 会员ID
* @param string $memo 备注
*/
public static function score($score, $user_id, $memo)
{
Db::startTrans();
try {
$user = self::lock(true)->find($user_id);
if ($user && $score != 0) {
$before = $user->score;
$after = $user->score + $score;
$level = self::nextlevel($after);
//更新会员信息
$user->save(['score' => $after, 'level' => $level]);
//写入日志
ScoreLog::create(['user_id' => $user_id, 'score' => $score, 'before' => $before, 'after' => $after, 'memo' => $memo]);
}
Db::commit();
} catch (\Exception $e) {
Db::rollback();
}
}
/**
* 根据积分获取等级
* @param int $score 积分
* @return int
*/
public static function nextlevel($score = 0)
{
$lv = array(1 => 0, 2 => 30, 3 => 100, 4 => 500, 5 => 1000, 6 => 2000, 7 => 3000, 8 => 5000, 9 => 8000, 10 => 10000);
$level = 1;
foreach ($lv as $key => $value) {
if ($score >= $value) {
$level = $key;
}
}
return $level;
}
2025-05-27 18:33:00 +08:00
/**添加用户:通过 手机号 和昵称
* @param $mobile
* @param $nickname
*/
public function addUserByMobile($mobile,$nickname,$password=null,$check=true){
//去除空格
$nickname = trim($nickname);
$mobile = trim($mobile);
if(!is_numeric($mobile))throw new \Exception("不是合法手机号!");
if(strlen($mobile)!=11)throw new \Exception("请输入11位手机号");
if($check){
if (!preg_match("/[\x7f-\xff]/", $nickname)) throw new \Exception("名称必须是汉字,请去除空格和其他特殊符号!");
preg_match_all("/[\x{4e00}-\x{9fa5}]/u","$nickname",$result);
if(!$result || join('',$result[0])!=$nickname)throw new \Exception("名称必须是汉字,请去除空格和其他特殊符号!");
}
//判断用户存不存在
$user = self::where("mobile",$mobile)->find();
if($user){
//如果存在,直接返回
return $user;
}else{
//如果不存在,创建并返回
//得到认证实例
$auth = Auth::instance();
//拼装提交参数
// $extend = $this->getUserDefaultFields();
$extend=[];
$extend['nickname'] = $nickname;
$extend['avatar'] = config("site.default_avatar");
if(!$password) $password = rand(100000,999999);
//调用注册方法
$user = $auth->registerNoLogin("{$mobile}_user", $password, "{$mobile}@user.com", $mobile,$extend);
if(!$user)throw new \Exception($auth->getError());
//更新已认证方式
$user = self::where("id", $user->id)->find();
$verification = $user->verification;
$verification->mobile = 1;
$user->verification = $verification;
$user->save();
//返回对象
return self::where("id", $user->id)->find();
}
}
public function employee()
{
return $this->hasOne(Employee::class, 'user_id', 'id');
}
2025-05-26 16:02:00 +08:00
}