<?php
|
|
namespace app\common\model\agent;
|
|
use app\common\model\BaseModel;
|
|
/**
|
* 商家用户模型
|
*/
|
class User extends BaseModel
|
{
|
protected $name = 'dlagent_user';
|
protected $pk = 'dlagent_user_id';
|
|
/**
|
* 关联应用表
|
*/
|
public function agent()
|
{
|
return $this->belongsTo('app\\common\\model\\agent\\Agent', 'agent_id', 'agent_id');
|
}
|
|
/**
|
* 关联用户角色表表
|
*/
|
public function role()
|
{
|
return $this->belongsToMany('app\\common\\model\\auth\\Role', 'app\\common\\model\\auth\\UserRole');
|
}
|
|
public function userRole()
|
{
|
return $this->hasMany('app\\common\\model\\agent\\UserRole', 'dlagent_user_id', 'dlagent_user_id');
|
}
|
|
/**
|
* 验证用户名是否重复
|
*/
|
public static function checkExist($user_name)
|
{
|
return !!static::withoutGlobalScope()
|
->where('user_name', '=', $user_name)
|
->value('dlagent_user_id');
|
}
|
|
/**
|
* 商家用户详情
|
*/
|
public static function detail($where, $with = [])
|
{
|
!is_array($where) && $where = ['dlagent_user_id' => (int)$where];
|
return (new static())->where(array_merge(['is_delete' => 0], $where))->with($with)->find();
|
}
|
|
/**
|
* 保存登录状态
|
*/
|
public function loginState($user)
|
{
|
$app = $user['agent'];
|
// 保存登录状态
|
$session = array(
|
'user' => [
|
'dlagent_user_id' => $user['dlagent_user_id'],
|
'user_name' => $user['user_name'],
|
],
|
'agent' => $app->toArray(),
|
'is_login' => true,
|
);
|
session('jjjshop_dlagent', $session);
|
}
|
|
/**
|
* 新增代理商用户记录
|
*/
|
public function dladd($agent_id, $data)
|
{
|
if (self::checkExist($data['user_name'])) {
|
$this->error = '代理商用户名已存在';
|
return false;
|
}
|
return $this->save([
|
'user_name' => $data['user_name'],
|
'password' => salt_hash($data['password']),
|
'agent_id' => $agent_id,
|
'is_super' => 1
|
]);
|
}
|
/**
|
* 更新当前管理员信息
|
*/
|
public function renew($data)
|
{
|
if ($data['pass'] !== $data['checkPass']) {
|
$this->error = '确认密码不正确';
|
return false;
|
}
|
return $this->save([
|
'password' => salt_hash($data['pass']),
|
]);
|
}
|
}
|