123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511 |
- <?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());
- // }
- }
- /**
- * 生成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());
- }
- }
- /**
- * 保存测量数据和体型选择(合并接口)
- */
- public function saveMeasurementAndBodyType()
- {
- $params = $this->request->post();
-
- // 验证基本参数
- if (!isset($params['profile_id']) || !$params['profile_id']) {
- $this->error('档案ID不能为空');
- }
- // 分离测量数据和体型选择数据
- $measurementData = [];
- $bodyTypeSelections = [];
-
- // 测量数据字段
- $measurementFields = [
- 'chest', 'waist', 'hip', 'thigh', 'calf', 'upper_arm', 'forearm',
- 'neck', 'shoulder_width', 'bust', 'underbust', 'inseam', 'outseam',
- 'knee', 'arm_length', 'wrist', 'pants_length','belly_belt','leg_root',
- 'ankle', 'waist_lower', 'mid_waist',
- 'shoe_size', 'measurement_date'
- ];
-
- // 提取测量数据
- foreach ($measurementFields as $field) {
- if (isset($params[$field]) && $params[$field] !== '') {
- $measurementData[$field] = $params[$field];
- }
- }
-
- // 提取体型选择数据
- if (isset($params['selections']) && is_array($params['selections'])) {
- $bodyTypeSelections = $params['selections'];
- }
- // 提取档案更新数据(身高体重)
- $profileUpdateData = [];
- if (isset($params['height']) && $params['height'] !== '') {
- $profileUpdateData['height'] = floatval($params['height']);
- }
- if (isset($params['weight']) && $params['weight'] !== '') {
- $profileUpdateData['weight'] = floatval($params['weight']);
- }
- // 检查是否至少有一种数据
- if (empty($measurementData) && empty($bodyTypeSelections) && empty($profileUpdateData)) {
- $this->error('请至少提供测量数据、体型选择数据或档案更新数据');
- }
- // 验证测量数据(如果有)
- if (!empty($measurementData)) {
- $measurementData['profile_id'] = $params['profile_id'];
- $measurementValidate = new BodyMeasurementsValidate();
- if (!$measurementValidate->scene('add')->check($measurementData)) {
- $this->error('测量数据验证失败:' . $measurementValidate->getError());
- }
- }
- // 验证体型选择数据(如果有)
- if (!empty($bodyTypeSelections)) {
- $selectionData = [
- 'profile_id' => $params['profile_id'],
- 'selections' => $bodyTypeSelections
- ];
- $selectionValidate = new BodyTypeSelectionValidate();
- if (!$selectionValidate->scene('save')->check($selectionData)) {
- $this->error('体型选择数据验证失败:' . $selectionValidate->getError());
- }
- }
- // 验证档案更新数据(如果有)
- if (!empty($profileUpdateData)) {
- // 简单的身高体重验证
- if (isset($profileUpdateData['height']) && ($profileUpdateData['height'] <= 0 || $profileUpdateData['height'] > 300)) {
- $this->error('身高必须在0-300厘米之间');
- }
- if (isset($profileUpdateData['weight']) && ($profileUpdateData['weight'] <= 0 || $profileUpdateData['weight'] > 1000)) {
- $this->error('体重必须在0-1000公斤之间');
- }
- }
- // try {
- $result = BodyProfileService::saveMeasurementAndBodyType(
- $params['profile_id'],
- $this->auth->id,
- $measurementData ?: [], // 确保不是null
- $bodyTypeSelections ?: [], // 确保不是null
- $profileUpdateData ?: [] // 档案更新数据
- );
-
- $this->success('保存成功', $result);
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- /**
- * 编辑测量数据和体型选择(合并接口)
- */
- public function updateMeasurementAndBodyType()
- {
- $params = $this->request->post();
-
- // 验证基本参数
- if (!isset($params['profile_id']) || !$params['profile_id']) {
- $this->error('档案ID不能为空');
- }
- // 分离测量数据和体型选择数据
- $measurementData = [];
- $bodyTypeSelections = [];
-
- // 测量数据字段
- $measurementFields = [
- 'chest', 'waist', 'hip', 'thigh', 'calf', 'upper_arm', 'forearm',
- 'neck', 'shoulder_width', 'bust', 'underbust', 'inseam', 'outseam',
- 'knee', 'arm_length', 'wrist', 'pants_length','belly_belt','leg_root',
- 'shoe_size', 'measurement_date'
- ];
-
- // 提取测量数据
- foreach ($measurementFields as $field) {
- if (isset($params[$field]) && $params[$field] !== '') {
- $measurementData[$field] = $params[$field];
- }
- }
-
- // 提取体型选择数据
- if (isset($params['selections']) && is_array($params['selections'])) {
- $bodyTypeSelections = $params['selections'];
- }
- // 提取档案更新数据(身高体重)
- $profileUpdateData = [];
- if (isset($params['height']) && $params['height'] !== '') {
- $profileUpdateData['height'] = floatval($params['height']);
- }
- if (isset($params['weight']) && $params['weight'] !== '') {
- $profileUpdateData['weight'] = floatval($params['weight']);
- }
- // 检查是否至少有一种数据
- if (empty($measurementData) && empty($bodyTypeSelections) && empty($profileUpdateData)) {
- $this->error('请至少提供测量数据、体型选择数据或档案更新数据');
- }
- // 验证测量数据(如果有)
- if (!empty($measurementData)) {
- $measurementData['profile_id'] = $params['profile_id'];
- $measurementValidate = new BodyMeasurementsValidate();
- if (!$measurementValidate->scene('add')->check($measurementData)) {
- $this->error('测量数据验证失败:' . $measurementValidate->getError());
- }
- }
- // 验证体型选择数据(如果有)
- if (!empty($bodyTypeSelections)) {
- $selectionData = [
- 'profile_id' => $params['profile_id'],
- 'selections' => $bodyTypeSelections
- ];
- $selectionValidate = new BodyTypeSelectionValidate();
- if (!$selectionValidate->scene('save')->check($selectionData)) {
- $this->error('体型选择数据验证失败:' . $selectionValidate->getError());
- }
- }
- // 验证档案更新数据(如果有)
- if (!empty($profileUpdateData)) {
- // 简单的身高体重验证
- if (isset($profileUpdateData['height']) && ($profileUpdateData['height'] <= 0 || $profileUpdateData['height'] > 300)) {
- $this->error('身高必须在0-300厘米之间');
- }
- if (isset($profileUpdateData['weight']) && ($profileUpdateData['weight'] <= 0 || $profileUpdateData['weight'] > 1000)) {
- $this->error('体重必须在0-1000公斤之间');
- }
- }
- // try {
- $result = BodyProfileService::updateMeasurementAndBodyType(
- $params['profile_id'],
- $this->auth->id,
- $measurementData ?: [],
- $bodyTypeSelections ?: [],
- $profileUpdateData ?: []
- );
-
- $this->success('更新成功', $result);
- // } catch (\Exception $e) {
- // $this->error($e->getMessage());
- // }
- }
- }
|