192 lines
6.4 KiB
PHP
192 lines
6.4 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\service\UserService;
|
|
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);
|
|
}
|
|
|
|
/**
|
|
* 后台用户搜索
|
|
* @return \think\Response
|
|
*/
|
|
public function searchUser()
|
|
{
|
|
// 获取参数
|
|
$isFirst = $this->request->get('is_first', UserService::AllData);
|
|
$gender = $this->request->get('gender', UserService::AllData);
|
|
$province = $this->request->get('province', UserService::AllData);
|
|
$city = $this->request->get('city', UserService::AllData);
|
|
list($page, $size) = $this->getPageParam();
|
|
|
|
$data = UserService::search(
|
|
intval($isFirst),
|
|
intval($gender),
|
|
intval($province),
|
|
intval($city),
|
|
$this->request->get('name'),
|
|
$page, $size
|
|
);
|
|
return SuccessResponse::create($data->toArray());
|
|
}
|
|
|
|
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 = 1;
|
|
$titles = [
|
|
'A' => ['text' => '自评时间', 'key' => 'create_time'],
|
|
'B' => ['text' => '姓名', 'key' => 'realname'],
|
|
'C' => ['text' => '头痛症状', 'key' => 'headache'],
|
|
'D' => ['text' => '胃肠道症状', 'key' => 'gastrointestinal'],
|
|
'E' => ['text' => '劳累或疲劳', 'key' => 'tired'],
|
|
'F' => ['text' => '头晕或眩晕', 'key' => 'dizzy'],
|
|
'G' => ['text' => '总分', 'key' => 'score'],
|
|
'H' => ['text' => '评估结果', 'key' => 'resultType'],
|
|
'I' => ['text' => '性别', 'key' => 'gender'],
|
|
'J' => ['text' => '年龄', 'key' => 'age']
|
|
];
|
|
$fields = explode(',', 'A,B,C,D,E,F,G.H');
|
|
foreach ($fields as $f) {
|
|
$sheet->setCellValue($f . $rowNumber, $titles[$f]);
|
|
}
|
|
foreach ($data->dataList as $item) {
|
|
$rowNumber++;
|
|
foreach ($fields as $f) {
|
|
$sheet->setCellValue($f . $rowNumber, $item[$f]);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
|
|
} |