BodyProfile.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\model\BodyTypeConfig;
  5. use app\common\Service\BodyProfileService;
  6. use app\common\Service\BodyTypeService;
  7. use app\api\validate\BodyProfile as BodyProfileValidate;
  8. use app\api\validate\BodyMeasurements as BodyMeasurementsValidate;
  9. use app\api\validate\BodyTypeSelection as BodyTypeSelectionValidate;
  10. /**
  11. * 身体档案API
  12. */
  13. class BodyProfile extends Api
  14. {
  15. protected $noNeedLogin = [];
  16. protected $noNeedRight = '*';
  17. /**
  18. * 获取用户的档案列表
  19. */
  20. public function index()
  21. {
  22. // try {
  23. $profiles = BodyProfileService::getUserProfiles($this->auth->id);
  24. $this->success('获取成功', $profiles);
  25. // } catch (\Exception $e) {
  26. // $this->error($e->getMessage());
  27. // }
  28. }
  29. /**
  30. * 创建新档案
  31. */
  32. public function create()
  33. {
  34. $params = $this->request->post();
  35. // 验证数据
  36. $validate = new BodyProfileValidate();
  37. if (!$validate->scene('create')->check($params)) {
  38. $this->error($validate->getError());
  39. }
  40. // try {
  41. // 设置用户ID
  42. $params['user_id'] = $this->auth->id;
  43. $profile = BodyProfileService::createProfile($params);
  44. $this->success('创建成功', ['profile_id' => $profile->id]);
  45. // } catch (\Exception $e) {
  46. // $this->error($e->getMessage());
  47. // }
  48. }
  49. /**
  50. * 获取档案详情
  51. */
  52. public function detail()
  53. {
  54. $profile_id = $this->request->get('profile_id/d');
  55. if (!$profile_id) {
  56. $this->error('档案ID不能为空');
  57. }
  58. // try {
  59. $profileData = BodyProfileService::getProfileDetail($profile_id, $this->auth->id);
  60. $this->success('获取成功', $profileData);
  61. // } catch (\Exception $e) {
  62. // $this->error($e->getMessage());
  63. // }
  64. }
  65. /**
  66. * 更新档案基础信息
  67. */
  68. public function update()
  69. {
  70. $profile_id = $this->request->post('profile_id/d');
  71. $params = $this->request->post();
  72. if (!$profile_id) {
  73. $this->error('档案ID不能为空');
  74. }
  75. // 过滤允许更新的字段
  76. $allowedFields = ['profile_name', 'relation', 'age', 'height', 'weight', 'profile_photo', 'body_photos'];
  77. $updateData = [];
  78. foreach ($allowedFields as $field) {
  79. if (isset($params[$field])) {
  80. $updateData[$field] = $params[$field];
  81. }
  82. }
  83. if (empty($updateData)) {
  84. $this->error('没有需要更新的数据');
  85. }
  86. // 验证数据
  87. $validate = new BodyProfileValidate();
  88. if (!$validate->scene('update')->check($updateData)) {
  89. $this->error($validate->getError());
  90. }
  91. // try {
  92. BodyProfileService::updateProfile($profile_id, $this->auth->id, $updateData);
  93. $this->success('更新成功');
  94. // } catch (\Exception $e) {
  95. // $this->error($e->getMessage());
  96. // }
  97. }
  98. /**
  99. * 删除档案
  100. */
  101. public function delete()
  102. {
  103. $profile_id = $this->request->post('profile_id/d');
  104. if (!$profile_id) {
  105. $this->error('档案ID不能为空');
  106. }
  107. // try {
  108. BodyProfileService::deleteProfile($profile_id, $this->auth->id);
  109. $this->success('删除成功');
  110. // } catch (\Exception $e) {
  111. // $this->error($e->getMessage());
  112. // }
  113. }
  114. /**
  115. * 添加测量数据
  116. */
  117. public function addMeasurement()
  118. {
  119. $params = $this->request->post();
  120. // 验证数据
  121. $validate = new BodyMeasurementsValidate();
  122. if (!$validate->scene('add')->check($params)) {
  123. $this->error($validate->getError());
  124. }
  125. // try {
  126. $measurement = BodyProfileService::addMeasurement(
  127. $params['profile_id'],
  128. $this->auth->id,
  129. $params
  130. );
  131. $this->success('添加成功', ['measurement_id' => $measurement->id]);
  132. // } catch (\Exception $e) {
  133. // $this->error($e->getMessage());
  134. // }
  135. }
  136. /**
  137. * 获取测量历史
  138. */
  139. public function measurementHistory()
  140. {
  141. $profile_id = $this->request->get('profile_id/d');
  142. $page = $this->request->get('page/d', 1);
  143. $limit = $this->request->get('limit/d', 10);
  144. if (!$profile_id) {
  145. $this->error('档案ID不能为空');
  146. }
  147. try {
  148. $result = BodyProfileService::getMeasurementHistory($profile_id, $this->auth->id, $page, $limit);
  149. $this->success('获取成功', $result);
  150. } catch (\Exception $e) {
  151. $this->error($e->getMessage());
  152. }
  153. }
  154. /**
  155. * 获取体型配置选项
  156. */
  157. public function getBodyTypeOptions()
  158. {
  159. $gender = $this->request->get('gender/d', 0);
  160. // try {
  161. $categories = BodyTypeService::getTypeSelectionConfig($gender);
  162. $this->success('获取成功', $categories);
  163. // } catch (\Exception $e) {
  164. // $this->error($e->getMessage());
  165. // }
  166. }
  167. /**
  168. * 保存体型选择
  169. */
  170. public function saveBodyTypeSelection()
  171. {
  172. $params = $this->request->post();
  173. // 验证数据
  174. $validate = new BodyTypeSelectionValidate();
  175. if (!$validate->scene('save')->check($params)) {
  176. $this->error($validate->getError());
  177. }
  178. // try {
  179. BodyProfileService::saveBodyTypeSelection(
  180. $params['profile_id'],
  181. $this->auth->id,
  182. $params['selections']
  183. );
  184. $this->success('保存成功');
  185. // } catch (\Exception $e) {
  186. // $this->error($e->getMessage());
  187. // }
  188. }
  189. /**
  190. * 生成AI测试报告
  191. */
  192. public function generateAiReport()
  193. {
  194. $profile_id = $this->request->post('profile_id/d');
  195. $report_type = $this->request->post('report_type', 'comprehensive');
  196. if (!$profile_id) {
  197. $this->error('档案ID不能为空');
  198. }
  199. try {
  200. $report = BodyProfileService::generateAiReport($profile_id, $this->auth->id, $report_type);
  201. $this->success('报告生成成功', ['report_id' => $report->id]);
  202. } catch (\Exception $e) {
  203. $this->error($e->getMessage());
  204. }
  205. }
  206. /**
  207. * 获取AI报告详情
  208. */
  209. public function getAiReport()
  210. {
  211. $report_id = $this->request->get('report_id/d');
  212. if (!$report_id) {
  213. $this->error('报告ID不能为空');
  214. }
  215. try {
  216. $report = BodyProfileService::getAiReport($report_id, $this->auth->id);
  217. $this->success('获取成功', $report->toArray());
  218. } catch (\Exception $e) {
  219. $this->error($e->getMessage());
  220. }
  221. }
  222. /**
  223. * 获取AI报告列表
  224. */
  225. public function getAiReportList()
  226. {
  227. $profile_id = $this->request->get('profile_id/d');
  228. $page = $this->request->get('page/d', 1);
  229. $limit = $this->request->get('limit/d', 10);
  230. if (!$profile_id) {
  231. $this->error('档案ID不能为空');
  232. }
  233. try {
  234. $result = BodyProfileService::getAiReportList($profile_id, $this->auth->id, $page, $limit);
  235. $this->success('获取成功', $result);
  236. } catch (\Exception $e) {
  237. $this->error($e->getMessage());
  238. }
  239. }
  240. /**
  241. * 获取体型选择建议报告
  242. */
  243. public function getSelectionReport()
  244. {
  245. $profile_id = $this->request->get('profile_id/d');
  246. if (!$profile_id) {
  247. $this->error('档案ID不能为空');
  248. }
  249. try {
  250. $report = BodyProfileService::getSelectionReport($profile_id, $this->auth->id);
  251. $this->success('获取成功', $report);
  252. } catch (\Exception $e) {
  253. $this->error($e->getMessage());
  254. }
  255. }
  256. /**
  257. * 获取体型选择统计数据
  258. */
  259. public function getSelectionStatistics()
  260. {
  261. $category = $this->request->get('category', '');
  262. $gender = $this->request->get('gender/d', 0);
  263. try {
  264. $statistics = BodyTypeService::getSelectionStatistics($category, $gender);
  265. $this->success('获取成功', $statistics);
  266. } catch (\Exception $e) {
  267. $this->error($e->getMessage());
  268. }
  269. }
  270. /**
  271. * 保存测量数据和体型选择(合并接口)
  272. */
  273. public function saveMeasurementAndBodyType()
  274. {
  275. $params = $this->request->post();
  276. // 验证基本参数
  277. if (!isset($params['profile_id']) || !$params['profile_id']) {
  278. $this->error('档案ID不能为空');
  279. }
  280. // 分离测量数据和体型选择数据
  281. $measurementData = [];
  282. $bodyTypeSelections = [];
  283. // 测量数据字段
  284. $measurementFields = [
  285. 'chest', 'waist', 'hip', 'thigh', 'calf', 'upper_arm', 'forearm',
  286. 'neck', 'shoulder_width', 'bust', 'underbust', 'inseam', 'outseam',
  287. 'knee', 'arm_length', 'wrist', 'pants_length','belly_belt','leg_root',
  288. 'ankle', 'waist_lower', 'mid_waist',
  289. 'shoe_size', 'measurement_date'
  290. ];
  291. // 提取测量数据
  292. foreach ($measurementFields as $field) {
  293. if (isset($params[$field]) && $params[$field] !== '') {
  294. $measurementData[$field] = $params[$field];
  295. }
  296. }
  297. // 提取体型选择数据
  298. if (isset($params['selections']) && is_array($params['selections'])) {
  299. $bodyTypeSelections = $params['selections'];
  300. }
  301. // 提取档案更新数据(身高体重)
  302. $profileUpdateData = [];
  303. if (isset($params['height']) && $params['height'] !== '') {
  304. $profileUpdateData['height'] = floatval($params['height']);
  305. }
  306. if (isset($params['weight']) && $params['weight'] !== '') {
  307. $profileUpdateData['weight'] = floatval($params['weight']);
  308. }
  309. // 检查是否至少有一种数据
  310. if (empty($measurementData) && empty($bodyTypeSelections) && empty($profileUpdateData)) {
  311. $this->error('请至少提供测量数据、体型选择数据或档案更新数据');
  312. }
  313. // 验证测量数据(如果有)
  314. if (!empty($measurementData)) {
  315. $measurementData['profile_id'] = $params['profile_id'];
  316. $measurementValidate = new BodyMeasurementsValidate();
  317. if (!$measurementValidate->scene('add')->check($measurementData)) {
  318. $this->error('测量数据验证失败:' . $measurementValidate->getError());
  319. }
  320. }
  321. // 验证体型选择数据(如果有)
  322. if (!empty($bodyTypeSelections)) {
  323. $selectionData = [
  324. 'profile_id' => $params['profile_id'],
  325. 'selections' => $bodyTypeSelections
  326. ];
  327. $selectionValidate = new BodyTypeSelectionValidate();
  328. if (!$selectionValidate->scene('save')->check($selectionData)) {
  329. $this->error('体型选择数据验证失败:' . $selectionValidate->getError());
  330. }
  331. }
  332. // 验证档案更新数据(如果有)
  333. if (!empty($profileUpdateData)) {
  334. // 简单的身高体重验证
  335. if (isset($profileUpdateData['height']) && ($profileUpdateData['height'] <= 0 || $profileUpdateData['height'] > 300)) {
  336. $this->error('身高必须在0-300厘米之间');
  337. }
  338. if (isset($profileUpdateData['weight']) && ($profileUpdateData['weight'] <= 0 || $profileUpdateData['weight'] > 1000)) {
  339. $this->error('体重必须在0-1000公斤之间');
  340. }
  341. }
  342. // try {
  343. $result = BodyProfileService::saveMeasurementAndBodyType(
  344. $params['profile_id'],
  345. $this->auth->id,
  346. $measurementData ?: [], // 确保不是null
  347. $bodyTypeSelections ?: [], // 确保不是null
  348. $profileUpdateData ?: [] // 档案更新数据
  349. );
  350. $this->success('保存成功', $result);
  351. // } catch (\Exception $e) {
  352. // $this->error($e->getMessage());
  353. // }
  354. }
  355. /**
  356. * 编辑测量数据和体型选择(合并接口)
  357. */
  358. public function updateMeasurementAndBodyType()
  359. {
  360. $params = $this->request->post();
  361. // 验证基本参数
  362. if (!isset($params['profile_id']) || !$params['profile_id']) {
  363. $this->error('档案ID不能为空');
  364. }
  365. // 分离测量数据和体型选择数据
  366. $measurementData = [];
  367. $bodyTypeSelections = [];
  368. // 测量数据字段
  369. $measurementFields = [
  370. 'chest', 'waist', 'hip', 'thigh', 'calf', 'upper_arm', 'forearm',
  371. 'neck', 'shoulder_width', 'bust', 'underbust', 'inseam', 'outseam',
  372. 'knee', 'arm_length', 'wrist', 'pants_length','belly_belt','leg_root',
  373. 'shoe_size', 'measurement_date'
  374. ];
  375. // 提取测量数据
  376. foreach ($measurementFields as $field) {
  377. if (isset($params[$field]) && $params[$field] !== '') {
  378. $measurementData[$field] = $params[$field];
  379. }
  380. }
  381. // 提取体型选择数据
  382. if (isset($params['selections']) && is_array($params['selections'])) {
  383. $bodyTypeSelections = $params['selections'];
  384. }
  385. // 提取档案更新数据(身高体重)
  386. $profileUpdateData = [];
  387. if (isset($params['height']) && $params['height'] !== '') {
  388. $profileUpdateData['height'] = floatval($params['height']);
  389. }
  390. if (isset($params['weight']) && $params['weight'] !== '') {
  391. $profileUpdateData['weight'] = floatval($params['weight']);
  392. }
  393. // 检查是否至少有一种数据
  394. if (empty($measurementData) && empty($bodyTypeSelections) && empty($profileUpdateData)) {
  395. $this->error('请至少提供测量数据、体型选择数据或档案更新数据');
  396. }
  397. // 验证测量数据(如果有)
  398. if (!empty($measurementData)) {
  399. $measurementData['profile_id'] = $params['profile_id'];
  400. $measurementValidate = new BodyMeasurementsValidate();
  401. if (!$measurementValidate->scene('add')->check($measurementData)) {
  402. $this->error('测量数据验证失败:' . $measurementValidate->getError());
  403. }
  404. }
  405. // 验证体型选择数据(如果有)
  406. if (!empty($bodyTypeSelections)) {
  407. $selectionData = [
  408. 'profile_id' => $params['profile_id'],
  409. 'selections' => $bodyTypeSelections
  410. ];
  411. $selectionValidate = new BodyTypeSelectionValidate();
  412. if (!$selectionValidate->scene('save')->check($selectionData)) {
  413. $this->error('体型选择数据验证失败:' . $selectionValidate->getError());
  414. }
  415. }
  416. // 验证档案更新数据(如果有)
  417. if (!empty($profileUpdateData)) {
  418. // 简单的身高体重验证
  419. if (isset($profileUpdateData['height']) && ($profileUpdateData['height'] <= 0 || $profileUpdateData['height'] > 300)) {
  420. $this->error('身高必须在0-300厘米之间');
  421. }
  422. if (isset($profileUpdateData['weight']) && ($profileUpdateData['weight'] <= 0 || $profileUpdateData['weight'] > 1000)) {
  423. $this->error('体重必须在0-1000公斤之间');
  424. }
  425. }
  426. // try {
  427. $result = BodyProfileService::updateMeasurementAndBodyType(
  428. $params['profile_id'],
  429. $this->auth->id,
  430. $measurementData ?: [],
  431. $bodyTypeSelections ?: [],
  432. $profileUpdateData ?: []
  433. );
  434. $this->success('更新成功', $result);
  435. // } catch (\Exception $e) {
  436. // $this->error($e->getMessage());
  437. // }
  438. }
  439. }