2020-01-01 13:17:19 +08:00
|
|
|
|
<?php
|
|
|
|
|
namespace app\common\model;
|
|
|
|
|
|
|
|
|
|
use think\Model;
|
|
|
|
|
use think\model\concern\SoftDelete;
|
|
|
|
|
use think\facade\Db;
|
|
|
|
|
use think\facade\Session;
|
2020-03-11 20:20:59 +08:00
|
|
|
|
use think\facade\Log;
|
2020-03-11 14:23:25 +08:00
|
|
|
|
use taoler\com\Api;
|
2020-01-01 13:17:19 +08:00
|
|
|
|
|
|
|
|
|
class User extends Model
|
|
|
|
|
{
|
|
|
|
|
protected $pk = 'id'; //主键
|
|
|
|
|
protected $autoWriteTimestamp = true; //开启自动时间戳
|
|
|
|
|
protected $createTime = 'false';
|
|
|
|
|
protected $updateTime = 'update_time';
|
|
|
|
|
//protected $auto = ['password']; //定义自动处理的字段
|
|
|
|
|
//自动对password进行md5加密
|
|
|
|
|
protected function setPasswordAttr($value){
|
|
|
|
|
return md5($value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//软删除
|
|
|
|
|
use SoftDelete;
|
|
|
|
|
protected $deleteTime = 'delete_time';
|
|
|
|
|
protected $defaultSoftDelete = 0;
|
|
|
|
|
//只读字段,禁止更改
|
|
|
|
|
//protected $readonly = ['email'];
|
|
|
|
|
|
|
|
|
|
//用户关联评论
|
|
|
|
|
public function comments()
|
|
|
|
|
{
|
|
|
|
|
return $this->hasMany('Comment','user_id','id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//用户关联所属区域
|
|
|
|
|
public function userArea()
|
|
|
|
|
{
|
|
|
|
|
return $this->belongsTo('UserArea','user_raea_id','id');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//登陆校验
|
|
|
|
|
public function login($data)
|
|
|
|
|
{
|
|
|
|
|
//查询用户
|
|
|
|
|
$user = Db::name('user')->whereOr('email',$data['name'])->whereOr('name',$data['name'])->find();
|
|
|
|
|
//halt($user);
|
|
|
|
|
//对输入的密码字段进行MD5加密,再进行数据库的查询
|
|
|
|
|
$salt = substr(md5($user['create_time']),-6);
|
|
|
|
|
$pwd = substr_replace(md5($data['password']),$salt,0,6);
|
|
|
|
|
$data['password'] = md5($pwd);
|
|
|
|
|
|
|
|
|
|
if($user['password'] == $data['password']){
|
|
|
|
|
//将用户数据写入Session
|
|
|
|
|
Session::set('user_id',$user['id']);
|
|
|
|
|
Session::set('user_name',$user['name']);
|
2020-03-11 14:23:25 +08:00
|
|
|
|
$ip = request()->ip();
|
2020-03-18 21:30:30 +08:00
|
|
|
|
/*
|
2020-03-11 14:23:25 +08:00
|
|
|
|
$url = 'http://ip-api.com/json/'.$ip.'?lang=zh-CN';
|
|
|
|
|
//$url = 'http://ip-api.com/json/?lang=zh-CN';
|
2020-03-18 21:30:30 +08:00
|
|
|
|
$add = Api::urlGet($url);
|
2020-03-11 14:23:25 +08:00
|
|
|
|
if($add->status == 'success'){
|
|
|
|
|
$city = $add->city;
|
|
|
|
|
} else {
|
|
|
|
|
$city ='未知';
|
|
|
|
|
}
|
2020-03-18 21:30:30 +08:00
|
|
|
|
*/
|
2020-03-11 14:23:25 +08:00
|
|
|
|
Db::name('user')->where('id',$user['id'])->update(
|
|
|
|
|
[
|
2020-03-18 21:30:30 +08:00
|
|
|
|
//'city' => $city,
|
2020-03-11 14:23:25 +08:00
|
|
|
|
'last_login_ip' => $ip,
|
|
|
|
|
'last_login_time' => time()
|
|
|
|
|
]
|
|
|
|
|
);
|
2020-03-18 21:30:30 +08:00
|
|
|
|
Log::channel('login')->info('login:{user} {ip}',['user'=>$user['name'],'ip'=>$ip]);
|
2020-03-11 14:23:25 +08:00
|
|
|
|
|
2020-01-01 13:17:19 +08:00
|
|
|
|
//查询结果1表示有用户,用户名密码正确
|
|
|
|
|
return 1;
|
|
|
|
|
}else{
|
|
|
|
|
return '用户名或密码错误';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//注册校验
|
|
|
|
|
public function reg($data)
|
|
|
|
|
{
|
|
|
|
|
//随机存入默认头像
|
|
|
|
|
$code = mt_rand('1','11');
|
2020-01-08 17:37:41 +08:00
|
|
|
|
$data['user_img'] = "/static/res/images/avatar/$code.jpg";
|
2020-01-01 13:17:19 +08:00
|
|
|
|
$data['create_time'] = time();
|
|
|
|
|
$salt = substr(md5($data['create_time']),-6);
|
|
|
|
|
$data['password'] = substr_replace(md5($data['password']),$salt,0,6);
|
|
|
|
|
$result = $this->save($data);
|
|
|
|
|
if ($result) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else{
|
|
|
|
|
return '注册失败';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//重置密码
|
|
|
|
|
public function respass($data)
|
|
|
|
|
{ //halt($data);
|
|
|
|
|
$user = $this->where('id',$data['uid'])->find();
|
|
|
|
|
$salt = substr(md5($user['create_time']),-6);
|
|
|
|
|
$data['password'] = substr_replace(md5($data['password']),$salt,0,6);
|
|
|
|
|
$result = $user->save($data);
|
|
|
|
|
if ($result) {
|
|
|
|
|
return 1;
|
|
|
|
|
} else{
|
|
|
|
|
return '更改失败';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//更新设置
|
|
|
|
|
public function setNew($data)
|
|
|
|
|
{
|
|
|
|
|
$userId = $data['user_id'];
|
|
|
|
|
$user = User::where('id',$userId)->find();
|
2020-05-23 09:51:24 +08:00
|
|
|
|
$result = $user->allowField(['email','nickname','sex','city','area_id','sign'])->save($data);
|
2020-01-01 13:17:19 +08:00
|
|
|
|
if($result){
|
|
|
|
|
return 1;
|
|
|
|
|
}else{
|
|
|
|
|
return '修改失败';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//用户修改密码
|
|
|
|
|
public function setpass($data)
|
|
|
|
|
{
|
|
|
|
|
$user = $this->find($data['user_id']);
|
|
|
|
|
$salt = substr(md5($user['create_time']),-6);
|
|
|
|
|
$pwd = substr_replace(md5($data['nowpass']),$salt,0,6);
|
|
|
|
|
$data['nowpass'] = md5($pwd);
|
|
|
|
|
$result = $data['nowpass'] == $user['password'];
|
|
|
|
|
if(!$result){
|
|
|
|
|
return '当前密码不正确';
|
|
|
|
|
}
|
|
|
|
|
$data['password'] = substr_replace(md5($data['password']),$salt,0,6);
|
|
|
|
|
$user->password = $data['password'];
|
|
|
|
|
$result = $user->save();
|
|
|
|
|
if($result){
|
|
|
|
|
return 1;
|
|
|
|
|
}else{
|
|
|
|
|
return '修改失败,请改换密码';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|