| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 | <?phpnamespace app\common\api;use app\admin\model\User;use app\admin\model\Enterprise;use app\admin\model\Person;use app\admin\model\Role;const MAX_ERROR_TIMES = 5;const FREEZETIME = 5;const FREEZE_NO = 1;const FREEZE_YES = 2;/** * Description of UserApi * * @author sgq */class UserApi {    protected $username, $password, $usertype, $info;    public function __construct($username, $password, $usertype) {        $this->username = $username;        $this->password = $password;        $this->usertype = $usertype;        switch ($usertype) {            case 2:                //企业                $user = Enterprise::where(['username' => $username])->find();                break;            case 3:                //个人                $user = Person::where('username', $username)->find();                break;            default:                //管理员                $user = User::where(['account' => $username])->find();                break;        }        $this->info = $user;        return $this;    }    public function getRole() {        $role = Role::find($this->info["roleid"]);        return $role->toArray();    }    /**     * 返回用户信息     * @return type     */    public function getUserInfo() {        return $this->info->toArray();    }    /**     * 检查密码     * @return type     */    public function checkPwd() {        switch ($this->usertype) {            case 1:                $salt = hash("md5", $this->info["salt"], true);                $password = simple_hash("md5", $this->password, $salt, 1024);                break;            case 2:            case 3:                $password = hash("md5", $this->password);                break;        }        return $password == $this->info["password"];    }    /**     * 设置冻结与否     * @param type $freezetype     */    public function setFreeze($freezetype = FREEZE_NO) {        $this->info->freeze = $freezetype;        if ($freezetype == FREEZE_NO) {            $this->info->errorCount = null;            $this->info->freezeTime = null;        } else {            $this->info->freezeTime = strtotime(sprintf("+%d minutes", FREEZETIME));        }        $this->info->save();    }    public function setSession() {        $user = $this->getUserInfo();        switch ($this->usertype) {            case 1:                session("user", [                    "uid" => $user["id"],                    "roleid" => $user["roleid"],                    "account" => $user["account"],                    "name" => $user["name"],                    "avatar" => $user["avatar"],                    "sex" => $user["sex"],                    "rolename" => $this->getRole()["name"],                    "usertype" => $this->usertype                ]);                break;            case 2:                session("user", [                    "uid" => $user["id"],                    "account" => $user["username"],                    "name" => $user["name"],                    "avatar" => $user["headPortrait"],                    "rolename" => "企业用户",                    "usertype" => $this->usertype,                    "type" => $user["type"]                ]);                break;            case 3:                session("user", [                    "uid" => $user["id"],                    "account" => $user["username"],                    "name" => $user["name"],                    "avatar" => $user["headPortrait"],                    "sex" => $user["sex"],                    "rolename" => "个人用户",                    "usertype" => $this->usertype,                    "type" => $user["type"]                ]);                break;        }    }}
 |