<?php 
  
  /** 
  * 鼠笼前台登录接口
  * @author   YS
  * @version  1.0
  */ 
  class Api_LoginController extends Ctrl_Api{
        public $Usermodel;
        public $POST;
        public function init() {
            $this->Usermodel = new UserModel();
            $this->POST =json_decode(file_get_contents('php://input'),true);
        }
        /**
         * 鼠笼系统登录接口
         * @param $Username  用户名
         * @param $Password   密码
         * @param $type   用户类型
         * @return
         *  {
         *  "errorcode": "200",
         *  "message": "登陆成功",
         *  "data": {
         *          "username": "用户名",
         *          "type": "1",用户类型
         *          "realname": "真实姓名",
         *          "faculties": "1",院系
         *          "specialized": "1",专业
         *          "group": "1",课题组
         *          "teacher": "1",导师
         *          "phone": "13566987478",手机号
         *          "status": "1",审核状态
         *          "is_delete": "0"是否删除
         *      }
         *  }
         */
        public function indexAction(){
            //用户名
            $Username = empty($this->POST['Username'])||!isset($this->POST['Username'])? Tool_Fnc::apiMsg('账号密码错误', '500'):Tool_fnc::safe_string($this->POST['Username']);
            //密码
            $Password = empty($this->POST['Password'])||!isset($this->POST['Password'])? Tool_Fnc::apiMsg('账号密码错误', '500'):Tool_fnc::safe_string($this->POST['Password']);
            $Type = empty($this->POST['Type'])||!isset($this->POST['Type'])? Tool_Fnc::apiMsg('选择人员类型', '500'):Tool_fnc::safe_string($this->POST['Type']);
            $Userinfo = $this->Usermodel->field('*')->where("username='{$Username}' and type={$Type} and is_delete=0")->fRow();
            empty($Userinfo)?Tool_Fnc::apiMsg('账号密码错误', '500'):$Userinfo;
            $Userinfo['password']!=md5($Password.Yaf_Registry::get("config")->psd->default->appsecret)?Tool_Fnc::apiMsg('账号密码错误', '500'):$Userinfo['password'];
            unset($Userinfo['password']);
            $Userinfo['uid'] = $Userinfo['id'];
            unset($Userinfo['id']);
            if($Userinfo['status']!=1) Tool_Fnc::apiMsg('等待管理审核', '500');

            Tool_Fnc::apiMsg('登陆成功', '200',$Userinfo);
        }
        /**
            * 鼠笼系统注册接口
            * @param username  用户名
            * @param password   密码
            * @param type   用户类型
            * @param realname   用户类型
            * @param faculties   用户类型
            * @param specialized   用户类型
            * @param group   用户类型
            * @param teacher   用户类型
            * @return
            *  {
            *  "errorcode": "200",
            *  "message": "注册成功",
            *  "data": {
            *          "username": "用户名",
            *          "password": "1",密码
            *          "type": "用户类型",
            *          "realname": "1",真实姓名
            *          "faculties": "1",院系
            *          "specialized": "1",专业
            *          "group": "1",课题组
            *          "teacher": "13566987478",导师
            *      }
            *  }
         */
        public function registerAction(){
            //用户名
            $username = empty($this->POST['username'])||!isset($this->POST['username'])? Tool_Fnc::apiMsg('请输入用户名', '500'):Tool_fnc::safe_string($this->POST['username']);
            //密码
            $password = empty($this->POST['password'])||!isset($this->POST['password'])? Tool_Fnc::apiMsg('请输入密码', '500'):Tool_fnc::safe_string($this->POST['password']);
            //用户类型
            $type = empty($this->POST['type'])||!isset($this->POST['type'])? Tool_Fnc::apiMsg('请选择用户类型', '500'):Tool_fnc::safe_string($this->POST['type']);
            //真实姓名
            $realname = empty($this->POST['realname'])||!isset($this->POST['realname'])? Tool_Fnc::apiMsg('请输入真实姓名', '500'):Tool_fnc::safe_string($this->POST['realname']);
            //院系
            $faculties = empty($this->POST['faculties'])||!isset($this->POST['faculties'])? Tool_Fnc::apiMsg('请选择院系', '500'):Tool_fnc::safe_string($this->POST['faculties']);
            //专业
            $specialized = empty($this->POST['specialized'])||!isset($this->POST['specialized'])? Tool_Fnc::apiMsg('请选择专业', '500'):Tool_fnc::safe_string($this->POST['specialized']);
            //课题组
            $group = empty($this->POST['group'])||!isset($this->POST['group'])? Tool_Fnc::apiMsg('请选择课题组', '500'):Tool_fnc::safe_string($this->POST['group']);
            //导师
            $teacher = empty($this->POST['teacher'])||!isset($this->POST['teacher'])? Tool_Fnc::apiMsg('请选择导师', '500'):Tool_fnc::safe_string($this->POST['teacher']);
            //查询用户名是否存在
            $Userinfo = $this->Usermodel->field('*')->where("username='{$username}' and type={$type} and is_delete=0")->fRow();
            if(!empty($Userinfo)) Tool_Fnc::apiMsg('当前选择人员类型已存在此用户名，请重新填写用户名', '500');
            $data['username'] = $username;
            $data['password'] = md5($password.Yaf_Registry::get("config")->psd->default->appsecret);
            $data['realname'] = $realname;
            $data['type'] = $type;
            $data['faculties'] = $faculties;
            $data['specialized'] = $specialized;
            $data['group'] = $group;
            $data['teacher'] = $teacher;
            $ret = $this->Usermodel->save($data);
            if(!$ret) Tool_Fnc::apiMsg('注册失败', '500');
            Tool_Fnc::apiMsg('注册成功，等待管理员审核通过', '200');
        }
      public function editinfoAction(){
          $data['id'] = empty($this->POST['id'])?Tool_Fnc::apiMsg('请先登录', '500'):$this->POST['id'];
          //用户名
          $username = empty($this->POST['username'])||!isset($this->POST['username'])? Tool_Fnc::apiMsg('请输入用户名', '500'):Tool_fnc::safe_string($this->POST['username']);
          //密码
//          $password = empty($this->POST['password'])||!isset($this->POST['password'])? Tool_Fnc::apiMsg('请输入密码', '500'):Tool_fnc::safe_string($this->POST['password']);
          //用户类型
//          $type = empty($this->POST['type'])||!isset($this->POST['type'])? Tool_Fnc::apiMsg('请选择用户类型', '500'):Tool_fnc::safe_string($this->POST['type']);
          //真实姓名
          $realname = empty($this->POST['realname'])||!isset($this->POST['realname'])? Tool_Fnc::apiMsg('请输入真实姓名', '500'):Tool_fnc::safe_string($this->POST['realname']);
          //院系
          $faculties = empty($this->POST['faculties'])||!isset($this->POST['faculties'])? Tool_Fnc::apiMsg('请选择院系', '500'):Tool_fnc::safe_string($this->POST['faculties']);
          //专业
          $specialized = empty($this->POST['specialized'])||!isset($this->POST['specialized'])? Tool_Fnc::apiMsg('请选择专业', '500'):Tool_fnc::safe_string($this->POST['specialized']);
          //课题组
          $group = empty($this->POST['group'])||!isset($this->POST['group'])? Tool_Fnc::apiMsg('请选择课题组', '500'):Tool_fnc::safe_string($this->POST['group']);
          //导师
          $teacher = empty($this->POST['teacher'])||!isset($this->POST['teacher'])? Tool_Fnc::apiMsg('请选择导师', '500'):Tool_fnc::safe_string($this->POST['teacher']);
          //查询用户名是否存在
          $Userinfo = $this->Usermodel->field('*')->where("username='{$username}' and type={$type} and is_delete=0 and id!={$data['id']}")->fRow();
          if(!empty($Userinfo)) Tool_Fnc::apiMsg('当前选择人员类型已存在此用户名，请重新填写用户名', '500');
          $data['username'] = $username;
//          $data['password'] = md5($password.Yaf_Registry::get("config")->psd->default->appsecret);
          $data['realname'] = $realname;
//          $data['type'] = $type;
          $data['faculties'] = $faculties;
          $data['specialized'] = $specialized;
          $data['group'] = $group;
          $data['teacher'] = $teacher;
          $ret = $this->Usermodel->save($data);
          if(!$ret) Tool_Fnc::apiMsg('信息未变更', '500');
          Tool_Fnc::apiMsg('编辑成功', '200');
      }
  }

