/** * 确认注册【设定密码】 * @method registerAction * @return [type] [description] * @author NewFuture */ public function registerAction() { $msg = '信息注册失败!'; if ($regInfo = Session::get('reg')) { Session::del('reg'); if (Input::post('password', $password, 'trim') === false) { /*密码未md5*/ $this->error('密码错误', '/'); } elseif (!$password) { /*未设置密码*/ $password = $regInfo['password']; } $regInfo['password'] = Encrypt::encryptPwd($password, $regInfo['number']); if ($id = UserModel::insert($regInfo)) { /*注册成功*/ $regInfo['id'] = $id; $token = Auth::token($regInfo); Cookie::set('token', [$id => $token]); unset($regInfo['password']); Session::set('user', $regInfo); $msg = '信息注册成功!'; } } $this->jump('/', $msg); }
/** * 打印店登录 * @method loginAction * @return [type] [description] * @author NewFuture */ public function POST_indexAction() { $response['status'] = 0; if (!Input::post('account', $account, Config::get('regex.account'))) { $response['info'] = '账号格式错误'; } elseif (!Input::post('password', $password, 'isMd5')) { $response['info'] = '密码未加密处理'; } elseif (!Safe::checkTry('printer_auth_' . $account)) { $response['info'] = '尝试次数过多账号临时封禁,稍后重试或者联系我们'; } elseif (!($Printer = PrinterModel::where('account', $account)->field('id,sch_id,password,status,name')->find())) { $response['info'] = '账号错误'; } elseif (Encrypt::encryptPwd($password, $account) != $Printer['password']) { $response['info'] = '密码错误'; } else { Safe::del('printer_auth_' . $account); unset($Printer['password']); $sid = Session::start(); Session::set('printer', ['id' => $Printer['id'], 'sch_id' => $Printer['sch_id']]); $response['status'] = 1; $response['info'] = ['sid' => $sid, 'printer' => $Printer]; } $this->response = $response; }
/** * 登录函数 * @method login * @access private * @author NewFuture[newfuture@yunyin.org] * @param [string] $password [md5密码] * @return [bool/int] [用户id] */ private function login($number, $password, $sch_id = null) { $conditon = ['number' => $number]; //指定学校 $sch_id and $conditon['sch_id'] = $sch_id; $users = UserModel::where($conditon)->select('id,password,sch_id,name'); if (empty($users)) { /*未注册*/ return null; } else { /*验证结果*/ $password = Encrypt::encryptPwd($password, $number); $reg_schools = []; foreach ($users as &$user) { if ($user['password'] == $password) { /*登录成功*/ $user['number'] = $number; $token = Auth::token($user); $sessionid = Session::start(); unset($user['password']); Session::set('user', $user); Cookie::set('token', $token); // $user['school'] = SchoolModel::getName($user['sch_id']); $result = ['sid' => $sessionid, 'user' => $user, 'msg' => '登录成功!', 'token' => $token]; $this->response(1, $result); return true; } else { /*验证失败*/ $sid = $user['sch_id']; $reg_schools[$sid] = School::getAbbr($sid); } } $this->reg_schools = $reg_schools; return false; } }
/** * 创建用户 * @method create * @param [type] $data [description] * @return [type] [description] * @author NewFuture */ public function create($data) { $userInfo = array(); /*姓名*/ if (!(isset($data['name']) && $data['name'])) { $this->error = '姓名有误!'; return false; } else { $userInfo['name'] = $data['name']; } /*学号*/ if (!(isset($data['number']) && is_numeric($data['number']))) { $this->error = '学号必须'; return false; } else { $userInfo['number'] = $data['number']; } /*密码*/ if (!isset($data['password'])) { $this->error = '密码必须'; return false; } else { $userInfo['password'] = Encrypt::encryptPwd($data['password'], $data['number']); } /*学校*/ if (isset($data['sch_id'])) { $userInfo['sch_id'] = intval($data['sch_id']); } /*存入数据库*/ if ($uid = parent::getModel()->insert($userInfo)) { return $uid; } else { $this->error = '保存失败'; return false; } }
/** * 修改用户信息[密码] * PUT /user/1 * @method PUT_infoAction * @param integer $id [description] * @author NewFuture */ public function PUT_infoAction($id = 0) { $id = $this->auth($id); $response['status'] = 0; if (!Input::put('password', $password, 'isMd5')) { $response['info'] = '新的密码格式不对'; } elseif (!Input::put('old', $old_pwd, 'isMd5')) { $response['info'] = '请输入原密码'; } else { /*数据库中读取用户数据*/ $user = UserModel::field('password,number')->find($id); $number = $user->number; if (!$user || Encrypt::encryptPwd($old_pwd, $number) != $user['password']) { $response['info'] = '原密码错误'; } elseif (UserModel::set('password', Encrypt::encryptPwd($password, $number))->save($id) >= 0) { $response['info'] = '修改成功'; $response['status'] = 1; } else { $response['info'] = '修改失败'; } } $this->response = $response; }
/** * 重置密码 * @method POST_printerAction * @author NewFuture */ public function PUT_indexAction($id) { $this->auth($id); $response['status'] = 0; if (!Input::put('password', $password, 'isMd5')) { $response['info'] = '新的密码格式不对'; } elseif (!Input::put('old', $old_pwd, 'isMd5')) { $response['info'] = '请输入原密码'; } else { /*数据库中读取用户数据*/ $printer = PrinterModel::field('password,account')->find($id); $account = $printer['account']; if (!$printer || Encrypt::encryptPwd($old_pwd, $account) != $printer['password']) { $response['info'] = '原密码错误'; } elseif ($printer->update(['password' => Encrypt::encryptPwd($password, $account)]) >= 0) { $response['info'] = '修改成功'; $response['status'] = 1; } else { $response['info'] = '修改失败'; } } $this->response = $response; }