dggjimai/application/common/model/User.php

119 lines
3.3 KiB
PHP
Executable File

<?php
/**
* 用户模型
*/
namespace app\common\model;
use think\model\concern\SoftDelete;
use think\db;
class User extends Model
{
use SoftDelete;
public $softDelete = true;
protected $name = 'user';
protected $autoWriteTimestamp = true;
//可搜索字段
protected $searchField = ['username', 'mobile', 'nickname',];
//是否启用获取器
public function getStatusTextAttr($value, $data)
{
return self::BOOLEAN_TEXT[$data['status']];
}
//用户等级
public function getUserLevelIdTextAttr($value, $data)
{
return self::LEVL_TEXT[$data['user_level_id']];
}
//关联用户等级
public function userLevel()
{
return $this->belongsTo(UserLevel::class);
}
public static function getUserInfo($userId)
{
$user = self::field('id,sn,nickname,avatar,mobile')->findOrEmpty($userId)->toArray();
if (empty($user)) {
return '系统';
}
return $user;
}
/**
* 填写邀请码
* @param $code
* @param $my_id
* @return bool|string
*/
public static function code($code, $my_id)
{
$my_leader = Db::name('user')
->field(['id', 'first_leader', 'second_leader', 'ancestor_relation'])
->where(['distribution_code' => $code])
->find();
//更新我的第一上级、第二上级、关系链
$my_leader_id = $my_leader['id'];
$my_first_leader = $my_leader['first_leader'];
$my_leader['ancestor_relation'] = boolval($my_leader['ancestor_relation']) ? $my_leader['ancestor_relation'] : '';
$my_ancestor_relation = trim("{$my_leader_id},{$my_leader['ancestor_relation']}", ',');
$user = User::findOrEmpty($my_id);
// 旧关系链
if (!empty($user->ancestor_relation)) {
$old_ancestor_relation = $user->id . ',' .$user->ancestor_relation;
} else {
$old_ancestor_relation = $user->id;
}
$data = [
'first_leader' => $my_leader_id,
'second_leader' => $my_first_leader,
'ancestor_relation' => $my_ancestor_relation,
];
Db::name('user')
->where(['id' => $my_id])
->update($data);
//更新我向下一级的第二上级
$data = [
'second_leader' => $my_leader_id,
];
Db::name('user')
->where(['first_leader' => $my_id])
->update($data);
//更新与我相关的所有关系链
Db::name('user')
->where("find_in_set({$my_id},ancestor_relation)")
->exp('ancestor_relation', "replace(ancestor_relation,'{$old_ancestor_relation}','" . trim("{$my_id},{$my_ancestor_relation}", ',') . "')")
->update();
}
//用户登录
public static function login($param)
{
$username = $param['username'];
$password = $param['password'];
$user = self::get(['username' => $username]);
if (!$user) {
exception('用户不存在');
}
if (!password_verify($password, $user->password)) {
exception('密码错误');
}
if ((int)$user->status !== 1) {
exception('用户被冻结');
}
return $user;
}
}