<?php
// +----------------------------------------------------------------------
// | SentCMS [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013 http://www.tensent.cn All rights reserved.
// +----------------------------------------------------------------------
// | Author: molong <molong@tensent.cn> <http://www.tensent.cn>
// +----------------------------------------------------------------------
namespace app\model\auth;

use app\model\BaseModel;
use think\facade\Config;

class Users extends BaseModel{

	public $pk = 'uid';
	
	/**
	 * @title 密码加密
	 *
	 * @param [type] $value
	 * @param [type] $data
	 * @return void
	 */
	public function setPasswordAttr($value, $data){
		return $value ? password_hash($value, PASSWORD_DEFAULT) : password_hash('123456', PASSWORD_DEFAULT);
	}

	public function getTokenAttr($value, $data){
		$token = \leruge\facade\JWT::builder($data, false);
		return $token;
	}

	public function scopeAuth($query, $where = []){
		$auth = request()->auth();
		$uid = request()->user['uid'];
		$map = [];
		
		// if(!in_array($uid, Config::get('auth.admin_root'))){
		// 	if($auth['data_range'] == 2){
		// 		$map[] = ['uid', '=', $uid];//只能看自己
		// 	}elseif($auth['data_range'] == 3){
		// 		$map[] = ['department_id', '=', $auth['department_id']];    //查自己所在部门
		// 	}elseif($auth['data_range'] == 4){
		// 		$map[] = ['department_id', 'IN', getDepartmentChild($auth['department_id'])];  //部门及以下数据
		// 	}
		// }
		$query->where($query)->where($where);
	}

	public function roles(){
		return $this->belongsToMany(Roles::class, RolesAccess::class, 'role_id', 'uid');
	}
	public function department(){
		return $this->hasOne(Departments::class, 'id', 'department_id')->bind([
			'department_name' => 'title'
		]);
	}
}