diff --git a/app/common/ApiController.php b/app/common/ApiController.php index 642ff4a..665ad3c 100644 --- a/app/common/ApiController.php +++ b/app/common/ApiController.php @@ -30,6 +30,11 @@ abstract class ApiController extends BaseController $this->currentUserInfo = $this->request->user; } + /** + * 获取当前请求api的用户信息, + * 前提为必须有参数open_id + * @return UserInfo + */ protected function getCurrentUserInfo() { if (empty($this->currentUserInfo)) { diff --git a/app/controller/Admin.php b/app/controller/Admin.php index d4ddc1e..3190af9 100644 --- a/app/controller/Admin.php +++ b/app/controller/Admin.php @@ -14,6 +14,7 @@ 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; @@ -95,6 +96,25 @@ class Admin extends BaseController return AdminInfo::find(4); } + 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() { diff --git a/app/controller/Index.php b/app/controller/Index.php index d9d94f3..eec2900 100644 --- a/app/controller/Index.php +++ b/app/controller/Index.php @@ -3,6 +3,7 @@ namespace app\controller; use app\BaseController; +use think\facade\Config; use think\response\Json; class Index extends BaseController @@ -14,6 +15,12 @@ class Index extends BaseController "message" => "running" ]); } + public function test($name = 'ThinkPHP6') + { + return \json( + Config::get("app.evaluation.subject") + ); + } public function hello($name = 'ThinkPHP6') { diff --git a/app/controller/User.php b/app/controller/User.php index fa44928..f89a9d7 100644 --- a/app/controller/User.php +++ b/app/controller/User.php @@ -10,6 +10,7 @@ namespace app\controller; use app\BaseController; +use app\common\ApiController; use app\model\UserInfo; use app\util\ErrorCode; use app\util\ErrorResponse; @@ -17,8 +18,12 @@ use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Csv; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; -class User extends BaseController +class User extends ApiController { + protected $middleware = [ + '\app\middleware\ApiCheck' => ['except' => ['create']], + ]; + public function search() { @@ -59,7 +64,7 @@ class User extends BaseController if ($dataType == 'xlsx') { $writer = new Xlsx($spreadsheet); $filename .= '.xlsx'; - }else { + } else { $filename .= '.csv'; } header('Content-Disposition: attachment;filename=' . $filename); @@ -67,31 +72,48 @@ class User extends BaseController exit; } - public function getExport() - { - - } - public function update() { } - public function detail(int $id) + public function info() { - if ($id < 1) { - return ErrorResponse::createError( - ErrorCode::ERROR_PARAM_ERROR, '用户编号错误' - ); - } - $user = UserInfo::find($id); - if (empty($user)) { - return ErrorResponse::createError( - ErrorCode::ERROR_USER_NOT_EXISTS, '用户不存在' - ); - } - $userData = $user->getData(); - $userData['detail'] = $user->getParsedDetail(); - return json($userData); +// if (empty($open_id)) { +// return ErrorResponse::createError( +// ErrorCode::ERROR_PARAM_ERROR, '用户open_id错误' +// ); +// } +// $user = UserInfo::where('open_id', $open_id)->find(); +// if (empty($user)) { +// return ErrorResponse::createError( +// ErrorCode::ERROR_USER_NOT_EXISTS, '用户不存在' +// ); +// } + + return $this->getCurrentUserInfo(); + } + + public function detail() + { +// if ($id < 1) { +// return ErrorResponse::createError( +// ErrorCode::ERROR_PARAM_ERROR, '用户编号错误' +// ); +// } +// $user = UserInfo::find($id); +// if (empty($user)) { +// return ErrorResponse::createError( +// ErrorCode::ERROR_USER_NOT_EXISTS, '用户不存在' +// ); +// } +// $userData = $this->getCurrentUser()->getData(); +// $userData['detail'] = $this->getCurrentUser()->getParsedDetail(); + return json( + array_merge( + $this->getCurrentUserInfo()->toArray(), + $this->getCurrentUserInfo()->getParsedDetail()->toArray() + ) + ); } } \ No newline at end of file diff --git a/app/middleware/ApiCheck.php b/app/middleware/ApiCheck.php index 27ffd7a..b521c40 100644 --- a/app/middleware/ApiCheck.php +++ b/app/middleware/ApiCheck.php @@ -29,7 +29,7 @@ class ApiCheck */ public function handle(Request $request, \Closure $next) { - $open_id = Env::get('app_debug') ? 'wxaffadsf31Dfaf93' : $request->param('open_id');//'wxaffadsf31Dfaf93'; + $open_id = Env::get('app_debug') ? 'wxaffadsf31Dfaf93wxaffadsf31Dfaf93' : $request->param('open_id');//'wxaffadsf31Dfaf93'; if (empty($open_id)) { return ErrorResponse::createError( diff --git a/app/model/UserInfo.php b/app/model/UserInfo.php index 04d94c7..08dcf4f 100644 --- a/app/model/UserInfo.php +++ b/app/model/UserInfo.php @@ -14,6 +14,13 @@ use app\BaseModel; class UserInfo extends BaseModel { protected $table = 'user_info'; + protected $globalScope = ['status']; + + + public function scopeStatus($query) + { + $query->where('status', 1); + } public function detail() { diff --git a/app/service/UserService.php b/app/service/UserService.php new file mode 100644 index 0000000..7e967f9 --- /dev/null +++ b/app/service/UserService.php @@ -0,0 +1,82 @@ +db()->alias('d') + ->join($userInfo->getTableName() . ' u', 'u.id = d.uid'); + + // 首次进藏 + if ($isFirst != self::AllData) { + $model->where('is_first_to_tibet', $isFirst); + } + // 性别 + if ($gender != self::AllData) { + $model->where('gender', $gender); + } + // 性别 + if ($province != self::AllData) { + $model->where('province', $province); + } + // 性别 + if ($city != self::AllData) { + $model->where('city', $city); + } + if (!empty($name)) { + //, ['name' => ""] + $model->where("(u.nickname LIKE '%{$name}%' OR d.realname LIKE '%{$name}%')"); + } + + $dataArray = $model->limit(($page - 1) & $pageSize, $pageSize)// 分页 + ->field('u.nickname,u.open_id,u.avatar,d.*')// 查询字段 + ->select()->toArray(); // 获取结果 + $count = $userInfo->count(); // 查询总数 + self::parseArray($dataArray); + + return ListCountData::Create($count, $dataArray); + } + + private static function parseArray(&$dataArray) + { + $userData = Config::get('app.user.subject'); + foreach ($dataArray as $key => &$item) { +// $item['is_first_to_tibet'] = $item['is_first_to_tibet'] == 1 ? '是' : '否'; + $item['smoke'] = $userData['smoke'][$item['smoke']]; + $item['medical_history'] = empty($item['medical_history']) ?[]: self::getDataFromArray( + $userData['medical_history'], + explode(',', $item['medical_history']) + ); + } + } + + private static function getDataFromArray(array $datas, array $keys) + { + foreach ($keys as $key => $v) { + $keys[$key] = $datas[$v]; + } + return $keys; + } +} \ No newline at end of file diff --git a/config/app.php b/config/app.php index 961104c..15299f4 100644 --- a/config/app.php +++ b/config/app.php @@ -48,6 +48,9 @@ return [ 'evaluation' => [ 'subject' => include(__DIR__.'/evaluation_subject.php'), ], + 'user' => [ + 'subject' => include(__DIR__.'/person_data.php'), + ], 'admin' =>[ 'multi_login' => Env::get('app.admin_multi_login', true), // 过期时间:1小时 diff --git a/config/person_data.php b/config/person_data.php new file mode 100644 index 0000000..1f5b6bb --- /dev/null +++ b/config/person_data.php @@ -0,0 +1,26 @@ + [ + '不吸烟', + '10支/天以下', + '10~20支/天', + '20支/天以上', + ], + 'drink' => [ + '否','是' + ], + 'medical_history'=>[ + '慢性支气管炎、支气管哮喘、支气管扩张病、肺心病', + '高血压、冠心病、心肌病、先天性心脏病、风湿性心脏病', + '反流性食道炎、慢性胃炎、胃溃疡、慢性胰腺炎、肠易激惹综合征、结肠炎', + '3个月内脑梗塞和/或脑出血、癫痫、脑炎、脑膜炎', + '特发性或继发性肺动脉高压症', + '其他疾病' + ] +]; \ No newline at end of file diff --git a/route/app.php b/route/app.php index 9e59225..e6c8711 100644 --- a/route/app.php +++ b/route/app.php @@ -13,4 +13,5 @@ use think\facade\Route; Route::get('hello', function () { return 'hello!'; }); -Route::get('user/detail/:id','User/detail'); \ No newline at end of file +//Route::get('user/detail/:id','User/detail'); +//Route::get('user/info/:open_id','User/info'); \ No newline at end of file