<?php
namespace app\model;
use think\Model;
class User extends Model
{
const TYPE_USERNAME = 1;
const TYPE_EMAIL = 2;
const TYPE_MOBILE = 3;
const TYPE_UID = 4;
const TYPE_UNKNOWN = 5;
/* @type int 登陆时的参数错误 */
const ERROR_PARAM = 0;
/* @type int 用户不存在 */
const ERROR_USER_NOT_EXISTS = -1;
/* @type int 用户被冻结 */
const ERROR_FROZEN = -2;
/* @type int 密码错误 */
const ERROR_PASSWORD = -3;
/* @type int 未知错误 */
const ERROR_UNKNOWN = -4;
protected $auto = ['updated', 'status'];
protected $insert = ['registered', 'registerip', 'salt', 'password'];
protected function setRegisteripAttr()
{
return get_client_ip();
}
protected function setStatusAttr()
{
return true;
}
protected function setSaltAttr()
{
return str_random(6);
}
protected function setPasswordAttr($value)
{
return password($this->email, $value, $this->salt);
}
public function getStatus($uid, $type = null)
{
if (!is_array($map = $this->type2map($uid, $type))) {
return static::ERROR_PARAM;
}
$user = $this->where($map)->field('status')->find();
if (!is_array($user)) {
return static::ERROR_USER_NOT_EXISTS;
}
return $user['status'];
}
/**
* 注册一个新用户
*
* @param string $username - 用户名
* @param string $password - 用户密码
* @param string $email - 用户邮箱
* @param string $mobile - 用户手机号码
*
* @return integer - 注册成功-用户信息,注册失败-错误编号
*/
public function register($username, $password, $email, $mobile = null)
{
$data = [
'username' => $username,
'password' => $password,
'email' => $email,
'mobile' => $mobile,
];
if ($this->validate(true)->create($data)->save()) {
$uid = $this->id;
return $uid ? $uid : static::ERROR_UNKNOWN;
}
// 错误详情见自动验证注释
return $this->getError();
}
/**
* 用户登录认证
* @param string $username 用户名
* @param string $password 用户密码
* @param integer $type 用户名类型 (1-用户名,2-邮箱,3-手机,4-UID)
* @return int - 登录成功-用户ID,登录失败-错误编号
*/
public function login($username, $password, $type = null)
{
if (!is_array($map = $this->type2map($username, $type))) {
return static::ERROR_PARAM;
}
if (!($user = $this->get($map))) {
return static::ERROR_USER_NOT_EXISTS;
}
if (password($password, $user->email, $user->salt) !== $user->password) {
return static::ERROR_PASSWORD;
}
// 用户被冻结
if ($user->type != 1) {
return static::ERROR_FROZEN;
}
//更新用户登录信息
$this->updateLogin($user->id);
return $user->id;
}
/**
* 更新用户登录信息
* @param integer $uid 用户ID
*/
protected function updateLogin($uid)
{
$this->update([
'id' => $uid,
'signed' => (new \DateTime())->format('Y-m-d H:i:s'),
'signip' => get_client_ip(),
]);
}
/**
* 获取用户信息
*
* @param string $uid - 用户ID或用户名
* @param bool $type - 是否使用用户名查询
*
* @return array|int - 用户信息
*/
public function info($uid, $type=null)
{
if (!is_array($map = $this->type2map($uid, $type))) {
return static::ERROR_PARAM;
}
$user = $this->where($map)->field('id,username,email,mobile,status')->find();
if (!is_array($user)) {
return static::ERROR_USER_NOT_EXISTS;
}
if ($user['status'] != 1) {
return static::ERROR_FROZEN;
}
return [
$user['id'],
$user['username'],
$user['email'],
$user['mobile']
];
}
/**
* 检测用户信息
*
* @param string $field - 用户名
* @param int $type - 用户名类型 1-用户名,2-用户邮箱,3-用户电话
*
* @return int - 错误编号
*/
public function checkField($field, $type = null)
{
if (!is_array($data = $this->type2map($field, $type))) {
return static::ERROR_PARAM;
}
return $this->create($data) ? 1 : $this->getError();
}
public function updateUserFields($uid, $password, $data)
{
if (empty($uid) || empty($password) || empty($data)) {
$this->error = '参数错误!';
return false;
}
if (!$this->verifyUser($uid, $password)) {
$this->error = '验证出错:密码不正确!';
return false;
}
if ($this->validate(true)->save($data, ['id'=>$uid])) {
return true;
}
return false;
}
public function updateUserFieldsNotCheck($uid, $data)
{
if (empty($uid) || empty($data)) {
$this->error = '参数错误!';
return false;
}
if ($this->validate(true)->save($data, ['id'=>$uid])) {
return true;
}
return false;
}
protected function verifyUser($uid, $password_in)
{
$user = $this->get($uid);
return password($user->email, $password_in, $user->salt) === $user->passwor;
}
protected function type2map($uid, $type)
{
$map = [];
switch ($type ?: static::TYPE_USERNAME) {
case static::TYPE_USERNAME:
$map['username'] = $uid;
break;
case static::TYPE_EMAIL:
$map['email'] = $uid;
break;
case static::TYPE_MOBILE:
$map['mobile'] = $uid;
break;
case static::TYPE_UID:
$map['uid'] = $uid;
break;
default:
return static::ERROR_PARAM;
}
return $map;
}
}再注册用户常见密码时报错
最佳答案