123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335 |
- <?php
- namespace app\api\controller;
- use app\common\controller\Api;
- use app\common\model\BodyTypeConfig;
- use app\common\Service\BodyProfileService;
- use app\common\Service\BodyTypeService;
- use app\api\validate\BodyProfile as BodyProfileValidate;
- use app\api\validate\BodyMeasurements as BodyMeasurementsValidate;
- use app\api\validate\BodyTypeSelection as BodyTypeSelectionValidate;
- /**
- * 身体档案API
- */
- class BodyProfile extends Api
- {
- protected $noNeedLogin = [];
- protected $noNeedRight = '*';
- /**
- * 获取用户的档案列表
- */
- public function index()
- {
- // try {
- $profiles = BodyProfileService::getUserProfiles($this->auth->id);
- $this->success('获取成功', $profiles);
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- /**
- * 创建新档案
- */
- public function create()
- {
- $params = $this->request->post();
-
- // 验证数据
- $validate = new BodyProfileValidate();
- if (!$validate->scene('create')->check($params)) {
- $this->error($validate->getError());
- }
- // try {
- // 设置用户ID
- $params['user_id'] = $this->auth->id;
-
- $profile = BodyProfileService::createProfile($params);
- $this->success('创建成功', ['profile_id' => $profile->id]);
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- /**
- * 获取档案详情
- */
- public function detail()
- {
- $profile_id = $this->request->get('profile_id/d');
-
- if (!$profile_id) {
- $this->error('档案ID不能为空');
- }
- // try {
- $profileData = BodyProfileService::getProfileDetail($profile_id, $this->auth->id);
- $this->success('获取成功', $profileData);
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- /**
- * 更新档案基础信息
- */
- public function update()
- {
- $profile_id = $this->request->post('profile_id/d');
- $params = $this->request->post();
- if (!$profile_id) {
- $this->error('档案ID不能为空');
- }
- // 过滤允许更新的字段
- $allowedFields = ['profile_name', 'relation', 'age', 'height', 'weight', 'profile_photo', 'body_photos'];
- $updateData = [];
-
- foreach ($allowedFields as $field) {
- if (isset($params[$field])) {
- $updateData[$field] = $params[$field];
- }
- }
- if (empty($updateData)) {
- $this->error('没有需要更新的数据');
- }
- // 验证数据
- $validate = new BodyProfileValidate();
- if (!$validate->scene('update')->check($updateData)) {
- $this->error($validate->getError());
- }
- // try {
- BodyProfileService::updateProfile($profile_id, $this->auth->id, $updateData);
- $this->success('更新成功');
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- /**
- * 删除档案
- */
- public function delete()
- {
- $profile_id = $this->request->post('profile_id/d');
- if (!$profile_id) {
- $this->error('档案ID不能为空');
- }
- // try {
- BodyProfileService::deleteProfile($profile_id, $this->auth->id);
- $this->success('删除成功');
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- /**
- * 添加测量数据
- */
- public function addMeasurement()
- {
- $params = $this->request->post();
-
- // 验证数据
- $validate = new BodyMeasurementsValidate();
- if (!$validate->scene('add')->check($params)) {
- $this->error($validate->getError());
- }
- // try {
- $measurement = BodyProfileService::addMeasurement(
- $params['profile_id'],
- $this->auth->id,
- $params
- );
- $this->success('添加成功', ['measurement_id' => $measurement->id]);
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- /**
- * 获取测量历史
- */
- public function measurementHistory()
- {
- $profile_id = $this->request->get('profile_id/d');
- $page = $this->request->get('page/d', 1);
- $limit = $this->request->get('limit/d', 10);
- if (!$profile_id) {
- $this->error('档案ID不能为空');
- }
- try {
- $result = BodyProfileService::getMeasurementHistory($profile_id, $this->auth->id, $page, $limit);
- $this->success('获取成功', $result);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 获取体型配置选项
- */
- public function getBodyTypeOptions()
- {
- $gender = $this->request->get('gender/d', 0);
-
- // try {
- $categories = BodyTypeService::getTypeSelectionConfig($gender);
- $this->success('获取成功', $categories);
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- /**
- * 保存体型选择
- */
- public function saveBodyTypeSelection()
- {
- $params = $this->request->post();
-
- // 验证数据
- $validate = new BodyTypeSelectionValidate();
- if (!$validate->scene('save')->check($params)) {
- $this->error($validate->getError());
- }
- // try {
- BodyProfileService::saveBodyTypeSelection(
- $params['profile_id'],
- $this->auth->id,
- $params['selections']
- );
- $this->success('保存成功');
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- /**
- * 获取体型推荐
- */
- public function getBodyTypeRecommendation()
- {
- $profile_id = $this->request->get('profile_id/d');
- if (!$profile_id) {
- $this->error('档案ID不能为空');
- }
- try {
- $recommendations = BodyProfileService::getBodyTypeRecommendation($profile_id, $this->auth->id);
- $this->success('获取成功', $recommendations);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 生成AI测试报告
- */
- public function generateAiReport()
- {
- $profile_id = $this->request->post('profile_id/d');
- $report_type = $this->request->post('report_type', 'comprehensive');
- if (!$profile_id) {
- $this->error('档案ID不能为空');
- }
- try {
- $report = BodyProfileService::generateAiReport($profile_id, $this->auth->id, $report_type);
- $this->success('报告生成成功', ['report_id' => $report->id]);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 获取AI报告详情
- */
- public function getAiReport()
- {
- $report_id = $this->request->get('report_id/d');
- if (!$report_id) {
- $this->error('报告ID不能为空');
- }
- try {
- $report = BodyProfileService::getAiReport($report_id, $this->auth->id);
- $this->success('获取成功', $report->toArray());
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 获取AI报告列表
- */
- public function getAiReportList()
- {
- $profile_id = $this->request->get('profile_id/d');
- $page = $this->request->get('page/d', 1);
- $limit = $this->request->get('limit/d', 10);
- if (!$profile_id) {
- $this->error('档案ID不能为空');
- }
- try {
- $result = BodyProfileService::getAiReportList($profile_id, $this->auth->id, $page, $limit);
- $this->success('获取成功', $result);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 获取体型选择建议报告
- */
- public function getSelectionReport()
- {
- $profile_id = $this->request->get('profile_id/d');
- if (!$profile_id) {
- $this->error('档案ID不能为空');
- }
- try {
- $report = BodyProfileService::getSelectionReport($profile_id, $this->auth->id);
- $this->success('获取成功', $report);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- /**
- * 获取体型选择统计数据
- */
- public function getSelectionStatistics()
- {
- $category = $this->request->get('category', '');
- $gender = $this->request->get('gender/d', 0);
- try {
- $statistics = BodyTypeService::getSelectionStatistics($category, $gender);
- $this->success('获取成功', $statistics);
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- }
|