BodyProfile.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. * 获取体型推荐
  191. */
  192. public function getBodyTypeRecommendation()
  193. {
  194. $profile_id = $this->request->get('profile_id/d');
  195. if (!$profile_id) {
  196. $this->error('档案ID不能为空');
  197. }
  198. try {
  199. $recommendations = BodyProfileService::getBodyTypeRecommendation($profile_id, $this->auth->id);
  200. $this->success('获取成功', $recommendations);
  201. } catch (\Exception $e) {
  202. $this->error($e->getMessage());
  203. }
  204. }
  205. /**
  206. * 生成AI测试报告
  207. */
  208. public function generateAiReport()
  209. {
  210. $profile_id = $this->request->post('profile_id/d');
  211. $report_type = $this->request->post('report_type', 'comprehensive');
  212. if (!$profile_id) {
  213. $this->error('档案ID不能为空');
  214. }
  215. try {
  216. $report = BodyProfileService::generateAiReport($profile_id, $this->auth->id, $report_type);
  217. $this->success('报告生成成功', ['report_id' => $report->id]);
  218. } catch (\Exception $e) {
  219. $this->error($e->getMessage());
  220. }
  221. }
  222. /**
  223. * 获取AI报告详情
  224. */
  225. public function getAiReport()
  226. {
  227. $report_id = $this->request->get('report_id/d');
  228. if (!$report_id) {
  229. $this->error('报告ID不能为空');
  230. }
  231. try {
  232. $report = BodyProfileService::getAiReport($report_id, $this->auth->id);
  233. $this->success('获取成功', $report->toArray());
  234. } catch (\Exception $e) {
  235. $this->error($e->getMessage());
  236. }
  237. }
  238. /**
  239. * 获取AI报告列表
  240. */
  241. public function getAiReportList()
  242. {
  243. $profile_id = $this->request->get('profile_id/d');
  244. $page = $this->request->get('page/d', 1);
  245. $limit = $this->request->get('limit/d', 10);
  246. if (!$profile_id) {
  247. $this->error('档案ID不能为空');
  248. }
  249. try {
  250. $result = BodyProfileService::getAiReportList($profile_id, $this->auth->id, $page, $limit);
  251. $this->success('获取成功', $result);
  252. } catch (\Exception $e) {
  253. $this->error($e->getMessage());
  254. }
  255. }
  256. /**
  257. * 获取体型选择建议报告
  258. */
  259. public function getSelectionReport()
  260. {
  261. $profile_id = $this->request->get('profile_id/d');
  262. if (!$profile_id) {
  263. $this->error('档案ID不能为空');
  264. }
  265. try {
  266. $report = BodyProfileService::getSelectionReport($profile_id, $this->auth->id);
  267. $this->success('获取成功', $report);
  268. } catch (\Exception $e) {
  269. $this->error($e->getMessage());
  270. }
  271. }
  272. /**
  273. * 获取体型选择统计数据
  274. */
  275. public function getSelectionStatistics()
  276. {
  277. $category = $this->request->get('category', '');
  278. $gender = $this->request->get('gender/d', 0);
  279. try {
  280. $statistics = BodyTypeService::getSelectionStatistics($category, $gender);
  281. $this->success('获取成功', $statistics);
  282. } catch (\Exception $e) {
  283. $this->error($e->getMessage());
  284. }
  285. }
  286. }