<?php

/**
 * @Author: 思睿软件
 * @Date:   2020-01-04 17:55:34
 * @Last Modified by:   Administrator
 * @Last Modified time: 2020-01-30 16:10:52
 * 公用 user 服务用于用户查询等
 */
/**
* 
*/
namespace siruisoft\common\service;
use think\Db;

class User{
	
	
	/**
	 * 用户前端登录 web + H5
	 * @Author   思睿软件<363359328@qq.com>
	 * @DateTime 2020-01-06T15:40:31+0800
	 * @param    [type]                   $name     [description]
	 * @param    [type]                   $password [description]
	 * @return   [type]                             [description]
	 * 流程  判断用户 --唯一性判断 -- 日志 --session
	 */
	static function login($name,$password){
		$row=model('user')->getByName($name);
		if(empty($row)){
			return ['error'=>1,'msg'=>'用户不存在'];
		}
		//处理登录错误次数
        if ($row['locktime'] > time()) {
        	return ['error'=>1,'msg'=>'您的帐号已经被锁定，请稍后再登录'];
        }

		$pass_error_times = intval(cookie('pass_error_times'));
        if ($password && (password($password,$row['login_salt']) != $row['password'])) {
            $pass_error_times = $pass_error_times + 1;
            cookie('pass_error_times', $pass_error_times);
            return ['error'=>1,'msg'=>'密码输入错误，您还可以输入' . (6 - $pass_error_times) . '次'];
            if ($pass_error_times >= 6) {
            	//记录锁定时间
                Db::name('user')->where('uid',$uid)->update(['last_login_time'=>time() + 3600]);
                log_record($uid,$row['uname'],'密码错误被锁定',1);
                return ['error'=>1,'msg'=>'您输入的密码错误次数过多，帐号将被锁定1小时'];                
            }
		}
		$uid = $row['uid'];
		//登录成功
		unset($row['password'],$row['login_salt']);
		//唯一性判断等

		//记录日志并修改登录时间
		try{
			//获取头像
			//获取用户vip
			//判断是否teacher
			Db::name('user')->where('uid',$uid)->update(['last_login_time'=>time()]);
	        log_record($uid,$row['uname'],'登录成功',1);
	        cookie('pass_error_times',null);
	        session('user',$row);
	        return ['error'=>0,'msg'=>'登录成功','data'=>$row];
		}catch(Exception $e){
			return ['error'=>1,'msg'=>'操作失败'];
		}
	}



	
	
	
}