<?php
|
|
namespace app\common\model\region;
|
|
use app\common\model\BaseModel;
|
|
/**
|
* 区域代理管理员模型
|
*/
|
class Admin extends BaseModel
|
{
|
protected $name = 'region_user_login';
|
protected $pk = 'shop_user_id';
|
|
/**
|
* 关联应用表
|
*/
|
public function app()
|
{
|
return $this->belongsTo('app\\common\\model\\app\\App', 'app_id', 'app_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\\region\\UserRole', 'shop_user_id', 'shop_user_id');
|
}
|
|
/**
|
* 验证用户名是否重复
|
*/
|
public static function checkExist($user_name)
|
{
|
return !!static::withoutGlobalScope()
|
->where('user_name', '=', $user_name)
|
->value('shop_user_id');
|
}
|
|
/**
|
* 区域代理管理员详情
|
*/
|
public static function detail($where, $with = [])
|
{
|
!is_array($where) && $where = ['shop_user_id' => (int)$where];
|
return (new static())->where(array_merge(['is_delete' => 0], $where))->with($with)->find();
|
}
|
|
/**
|
* 保存登录状态
|
*/
|
public function loginState($user)
|
{
|
$app = $user['app'];
|
// 保存登录状态
|
$session = array(
|
'user' => [
|
'shop_user_id' => $user['shop_user_id'],
|
'user_name' => $user['user_name'],
|
],
|
'app' => $app->toArray(),
|
'is_login' => true,
|
);
|
session('jjjshop_region_admin', $session);
|
}
|
|
/**
|
* 新增区域代理管理员记录
|
*/
|
public function addAdmin($client_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']),
|
'client_id' => $client_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']),
|
]);
|
}
|
}
|