<?php 
 | 
  
 | 
class Service_User extends System_Service_Base{ 
 | 
  
 | 
    public function __construct(){ 
 | 
        $this->user_model = new UserModel(); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据条件获取用户列表 
 | 
     * @param  array   $query  [description] 
 | 
     * @param  array   $fields [description] 
 | 
     * @param  array   $sort   [description] 
 | 
     * @param  integer $limit  [description] 
 | 
     * @param  integer $skip   [description] 
 | 
     * @return [type]          [description] 
 | 
     */ 
 | 
    public function getUserList($query=array(),$fields=array(),$sort=array(),$limit=0,$skip=0){ 
 | 
        return $this->user_model->getList($query, $fields, $sort, $limit, $skip); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据条件获取数量 
 | 
     * @param  array   $query [description] 
 | 
     * @param  integer $limit [description] 
 | 
     * @param  integer $skip  [description] 
 | 
     * @return [type]         [description] 
 | 
     */ 
 | 
    public function getUserListTotal($query=array(),$limit=0,$skip=0){ 
 | 
        return $this->user_model->count($query, $limit, $skip); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 添加用户 
 | 
     * @param [type] $data [description] 
 | 
     */ 
 | 
    public function add($data){ 
 | 
        return $this->user_model->add($data); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 更新用户信息 
 | 
     * @param  [type] $data [description] 
 | 
     * @return [type]       [description] 
 | 
     */ 
 | 
    public function update($data, $query = array()){ 
 | 
        return $this->user_model->update($data, $query); 
 | 
    } 
 | 
  
 | 
    /** 
 | 
     * 根据条件,获取用户信息 
 | 
     * @param  array  $query [description] 
 | 
     * @param  array  $field [description] 
 | 
     * @return [type]        [description] 
 | 
     */ 
 | 
    public function getUserInfo($query = array(), $field = array()){ 
 | 
        return $this->user_model->get($query, $field); 
 | 
    } 
 | 
  
 | 
     /** 
 | 
     * 后台用户登录 
 | 
     * @param  [type] $username [description] 
 | 
     * @param  [type] $password [description] 
 | 
     * @return [type]           [description] 
 | 
     */ 
 | 
     public function login($phone, $password){ 
 | 
         $user = $this->valid($phone, $password); 
 | 
         if(!$user){ 
 | 
             $this->error('用户名或密码不正确'); 
 | 
             return false; 
 | 
         } 
 | 
         else{ 
 | 
             $role_service = new Service_Role(); 
 | 
             $user_role = $role_service->getRoleInfo(array('_id' => $user['role'])); 
 | 
             $session = Yaf_Session::getInstance(); 
 | 
             $user_info = array( 
 | 
                 'nickname' => $user['nickname'], 
 | 
                 'user_id' => $user['_id'], 
 | 
                 'role_id' => $user['role'], 
 | 
                 'role_name' => $user_role['name'] 
 | 
             ); 
 | 
             $session->user = $user_info; 
 | 
             return true; 
 | 
         } 
 | 
  
 | 
     } 
 | 
  
 | 
     /** 
 | 
      * 检测用户名与密码 
 | 
      * @param  [type] $username [description] 
 | 
      * @param  [type] $password [description] 
 | 
      * @return [type]           [description] 
 | 
      */ 
 | 
     public function valid($phone, $password){ 
 | 
         $user = $this->user_model->getInfoByPhone($phone); 
 | 
         if($user && $user['password'] == md5(md5($password).$user['encrypt'])){ 
 | 
             return $user; 
 | 
         } 
 | 
         return false; 
 | 
     } 
 | 
} 
 |