146 lines
4.6 KiB
PHP
146 lines
4.6 KiB
PHP
<?php
|
|
/**
|
|
* Created by PhpStorm.
|
|
* User: yancheng<cheng@love.xiaoyan.me>
|
|
* Date: 2019/6/18
|
|
* Time: 1:42 PM
|
|
*/
|
|
|
|
namespace app\controller;
|
|
|
|
|
|
use app\BaseController;
|
|
use app\model\AdminInfo;
|
|
use app\Request;
|
|
use app\service\AdminService;
|
|
use app\service\EvaluationService;
|
|
use app\util\ErrorCode;
|
|
use app\util\ErrorResponse;
|
|
use app\util\StringUtil;
|
|
use app\util\SuccessResponse;
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Csv;
|
|
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
|
|
use think\response\Json;
|
|
|
|
class Admin extends BaseController
|
|
{
|
|
protected $middleware = [
|
|
'\app\middleware\AdminApiCheck' => ['except' => ['login']],
|
|
];
|
|
|
|
public function login()
|
|
{
|
|
$username = $this->request->param("username");
|
|
$password = $this->request->param("password");
|
|
if (empty($username) || empty($password)) {
|
|
return ErrorResponse::createError(ErrorCode::ERROR_PARAM_REQUIRED, '请提交正确的参数');
|
|
}
|
|
usleep(500000);
|
|
$user = AdminInfo::where('username', $username)->find();
|
|
|
|
if (empty($user) || $user->isEmpty()) {
|
|
return ErrorResponse::createError(ErrorCode::ERROR_ADMIN_LOGIN_PWD, '用户名或者密码错误(1)');
|
|
}
|
|
if ($user->password != md5($username.$password . $user->salt)) {
|
|
return ErrorResponse::createError(ErrorCode::ERROR_ADMIN_LOGIN_PWD, '用户名或者密码错误(2)');;
|
|
}
|
|
$data = $user->getPartData(['id', 'username', 'email', 'avatar', 'last_login', 'sex']);
|
|
$user->save(['last_login' => time()]);
|
|
// 登录的token
|
|
$token = AdminService::createAdminToken($user);
|
|
$data['token'] = $token->token;
|
|
return Json::create($data);
|
|
}
|
|
|
|
public function updatePwd()
|
|
{
|
|
$originPwd = $this->request->post('origin');
|
|
$newPwd = $this->request->post('new_pwd');
|
|
$newPwd2 = $this->request->post('new_pwd2');
|
|
|
|
if ($newPwd != $newPwd2) {
|
|
return ErrorResponse::createError(
|
|
ErrorCode::ERROR_ADMIN_LOGIN_PWD, '输入密码不一致'
|
|
);
|
|
}
|
|
$admin = $this->getCurrentLoginAdmin();
|
|
if (!$this->passwordIsCorrect($admin, $originPwd)) {
|
|
return ErrorResponse::createError(
|
|
ErrorCode::ERROR_ADMIN_PWD_ERROR, '原始密码不正确'
|
|
);
|
|
}
|
|
$salt = StringUtil::generateRandom(6);
|
|
$admin->save([
|
|
'password' => StringUtil::getEncryptPassword($originPwd, $salt),
|
|
'salt' => $salt
|
|
]);
|
|
return \json(['code' => 0]);
|
|
}
|
|
|
|
|
|
private function passwordIsCorrect(AdminInfo $admin, string $originPwd)
|
|
{
|
|
return $admin->password == md5($originPwd . $admin->salt);
|
|
}
|
|
|
|
/**
|
|
* @return AdminInfo
|
|
* @throws \think\db\exception\DataNotFoundException
|
|
* @throws \think\db\exception\ModelNotFoundException
|
|
* @throws \think\exception\DbException
|
|
*/
|
|
private function getCurrentLoginAdmin()
|
|
{
|
|
return AdminInfo::find(4);
|
|
}
|
|
|
|
|
|
public function searchEvaluation()
|
|
{
|
|
$resultType = $this->request->get('resultType');
|
|
if ($resultType == null) $resultType = EvaluationService::ResultAll;
|
|
list($page,$size) = $this->getPageParam();
|
|
$data = EvaluationService::search(
|
|
intval($resultType),
|
|
$this->request->get('name'),
|
|
$this->request->get('sort', EvaluationService::SortByTime),
|
|
$page,$size
|
|
);
|
|
return SuccessResponse::create($data->toArray());
|
|
}
|
|
|
|
public function downloadEvaluation(){
|
|
$resultType = $this->request->get('resultType');
|
|
if ($resultType == null) $resultType = EvaluationService::ResultAll;
|
|
list($page,$size) = $this->getPageParam();
|
|
|
|
$data = EvaluationService::search(
|
|
intval($resultType),
|
|
$this->request->get('name'),
|
|
$this->request->get('sort', EvaluationService::SortByTime),
|
|
$page,$size);
|
|
|
|
|
|
$dataType = $this->request->post('type');
|
|
|
|
$excel = new Spreadsheet();
|
|
$sheet = $excel->getActiveSheet();
|
|
// $rowNumber =
|
|
$sheet->setCellValue('A1', 'Hello World !');
|
|
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
$filename = urlencode("评估记录_") . date('mdHi');
|
|
$writer = new Csv($excel);
|
|
if ($dataType == 'xlsx') {
|
|
$writer = new Xlsx($excel);
|
|
$filename .= '.xlsx';
|
|
}else {
|
|
$filename .= '.csv';
|
|
}
|
|
header('Content-Disposition: attachment;filename=' . $filename);
|
|
$writer->save('php://output');
|
|
exit;
|
|
}
|
|
|
|
} |