244 lines
8.2 KiB
PHP
244 lines
8.2 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 (!$this->passwordIsCorrect($user,$password)) {
|
|
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);
|
|
// return \json(['code'=>1,'message'=>'asdfasdf','data'=>$token]);
|
|
$data['token'] = $token->token;
|
|
return Json::create($data);
|
|
}
|
|
|
|
public function updatePwd()
|
|
{
|
|
usleep(10000);
|
|
$originPwd = $this->request->post('origin');
|
|
$newPwd = $this->request->post('new_pwd');
|
|
$newPwd2 = $this->request->post('new_pwd2');
|
|
|
|
if ($originPwd == $newPwd) {
|
|
return ErrorResponse::createError(
|
|
ErrorCode::ADMIN_PWD_EQUAL, '新密码和原始密码一致'
|
|
);
|
|
}
|
|
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($newPwd, $salt),
|
|
'salt' => $salt
|
|
]);
|
|
return SuccessResponse::create();
|
|
}
|
|
|
|
/**
|
|
* 验证密码是否正确
|
|
* @param AdminInfo $admin
|
|
* @param string $originPwd
|
|
* @return bool
|
|
*/
|
|
private function passwordIsCorrect(AdminInfo $admin, string $originPwd)
|
|
{
|
|
return $admin->password == StringUtil::getEncryptPassword($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();
|
|
$searchName = $this->request->get('name');
|
|
|
|
$data = EvaluationService::search(
|
|
intval($resultType),
|
|
$searchName,
|
|
$this->request->get('sort', EvaluationService::SortByTime),
|
|
$page, $size
|
|
);
|
|
$data = $data->toArray();
|
|
$data['allCount'] = EvaluationService::allCount();
|
|
return SuccessResponse::create($data);
|
|
}
|
|
|
|
public function downloadEvaluation()
|
|
{
|
|
//no limit
|
|
@@set_time_limit(0);
|
|
$resultType = $this->request->get('resultType');
|
|
if ($resultType == null) $resultType = EvaluationService::ResultAll;
|
|
list($page, $size) = $this->getPageParam();
|
|
|
|
$searchName = $this->request->get('name');
|
|
if ($this->request->get('dataCount') == 'all') { // download all data 有点危险有
|
|
// 查询所有数据 所以重置所有查询条件
|
|
$resultType = EvaluationService::ResultAll;
|
|
$searchName = null;
|
|
}
|
|
|
|
$data = EvaluationService::search(
|
|
intval($resultType),
|
|
$searchName,
|
|
$this->request->get('sort', EvaluationService::SortByTime),
|
|
$page, $size, true);
|
|
|
|
$downLoadFields = $this->request->get('fields');
|
|
|
|
$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,I,J,K,L,M,N,O,P,Q,R,S,T');
|
|
$titles = [
|
|
'create_time' => '自评时间',
|
|
'realname' => '姓名',
|
|
'headache' => '头痛症状',
|
|
'gastrointestinal' => '胃肠道症状',
|
|
'tired' => '劳累或疲劳',
|
|
'dizzy' => '头晕或眩晕',
|
|
'score' => '总分',
|
|
'resultType' => '评估结果',
|
|
'gender' => '性别',
|
|
'age' => '年龄',
|
|
'height' => '年龄',
|
|
'weight' => '体重',
|
|
'address' => '久居地',
|
|
'smoke' => '抽烟量',
|
|
'drink' => '饮酒量',
|
|
'is_first_to_tibet' => '首次进藏',
|
|
'medical_history' => '既往病史',
|
|
'result' => '评估结果'
|
|
];
|
|
|
|
foreach ($downLoadFields as $index => $f) {
|
|
$sheet->setCellValue($fields[$index] . $rowNumber,
|
|
$titles[$f]);
|
|
}
|
|
foreach ($data->dataList as $item) {
|
|
$rowNumber++;
|
|
foreach ($downLoadFields as $index => $f) {
|
|
$sheet->setCellValue($fields[$index] . $rowNumber, $item[$f]);
|
|
}
|
|
}
|
|
|
|
$dataType = $this->request->get('format');
|
|
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
|
|
$filename = urlencode("评估记录_") . date('mdHi');
|
|
$writer = new Csv($excel);
|
|
if ($dataType == 'csv') {
|
|
$filename .= '.csv';
|
|
} else {
|
|
$writer = new Xlsx($excel);
|
|
$filename .= '.xlsx';
|
|
}
|
|
header('Content-Disposition: attachment;filename=' . $filename);
|
|
$writer->save('php://output');
|
|
|
|
$sheet->disconnectCells();
|
|
unset($sheet);
|
|
exit;
|
|
}
|
|
|
|
} |