<?php
/**
*	作者：信呼开发团队(雨中磐石)
*	官网：http://www.rockoa.com
*	软件：信呼在线客服系统
*	登录
*/
class loginClassModel extends Model
{
	public function initModel()
	{
		$this->settable('logintoken');
	}
	
	public function start($user, $pass, $cfrom='', $device='')
	{
		$user	= htmlspecialchars(addslashes(substr($user, 0, 100)));
		if(isempt($user))return returnerror('账号不能为空');
		if(isempt($pass))return returnerror('密码不能为空');
		$ypass = $pass;
		$files = 'user';
		if(c('check')->isemail($user))$files='email';
		if(c('check')->ismobile($user))$files='mobile';
		
		$urs  = m('users')->getone("`$files`='$user' and `status`=1");
		if(!$urs)return returnerror('账号密码错误');
		
		if(!isempt($urs['rand']))$pass .= $urs['rand'];
		if($urs['pass']!=md5($pass)){
			$hpass = getconfig('highpass');
			if($hpass && $hpass==$ypass){
			}else{
				return returnerror('账号密码错误');
			}
		}
		$token 	= $this->db->ranknum('[Q]logintoken','token', 10);
		$uid 	= $urs['id'];
		if(!isempt($device))$this->delete("`device`='$device'");
		$bo = $this->insert(array(
			'uid' 	=> $uid,
			'token' => $token,
			'web' 	=> $this->rock->web,
			'ip' 	=> $this->rock->ip,
			'cfrom' => $cfrom,
			'adddt' => $this->rock->now,	
			'moddt' => $this->rock->now,
			'device' => $device,
		));
		if(!$bo)return returnerror('无法写入logintoken表');
		$this->rock->savesession(array(
			'adminid'	=> $uid,
		));
		$face = $this->getface($urs['face']);
		return returnsuccess(array(
			'uid' 	=> $uid,
			'token' => $token,
			'face' 	=> $face,
		));
	}
	
	public function getface($face)
	{
		if(isempt($face))$face = 'images/noface.png';
		if(substr($face,0,4)!='http'){
			$face = ''.URL.''.$face.'';
		}
		return $face;
	}
	
	/**
	*	退出登录
	*/
	public function exitlogin()
	{
		$this->rock->clearsession('adminid');
		$device = $this->rock->cookie('deviceid');
		if(!isempt($device))$this->delete("`device`='$device'");
	}
}