AiMeasurement.php 49 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  1. <?php
  2. namespace app\api\controller;
  3. use app\common\controller\Api;
  4. use app\common\service\AiMeasurementService;
  5. use app\common\service\BodyProfileService;
  6. use app\api\validate\BodyProfile as BodyProfileValidate;
  7. use think\Cache;
  8. /**
  9. * AI测量API控制器
  10. */
  11. class AiMeasurement extends Api
  12. {
  13. protected $noNeedLogin = [];
  14. protected $noNeedRight = '*';
  15. // 第三方AI服务配置
  16. private $thirdPartyApiConfig = [
  17. 'url' => 'http://85cg744gf528.vicp.fun:25085/get_bodysize',
  18. 'timeout' => 120,
  19. 'connect_timeout' => 30
  20. ];
  21. /**
  22. * 开始AI身体测量分析
  23. */
  24. public function startAnalysis()
  25. {
  26. $params = $this->request->post();
  27. // 验证必要参数
  28. if (empty($params['profile_id'])) {
  29. $this->error('档案ID不能为空');
  30. }
  31. if (empty($params['photos']) || !is_array($params['photos'])) {
  32. $this->error('请上传身体照片');
  33. }
  34. try {
  35. // 验证档案归属
  36. $profile = \app\common\model\BodyProfile::where('id', $params['profile_id'])
  37. ->where('user_id', $this->auth->id)
  38. ->find();
  39. if (!$profile) {
  40. $this->error('档案不存在');
  41. }
  42. // 检查是否有正在处理的任务
  43. $existingTask = \think\Db::table('fa_ai_measurement_task')
  44. ->where('profile_id', $params['profile_id'])
  45. ->where('status', 'in', [0, 1]) // 待处理或处理中
  46. ->find();
  47. if ($existingTask) {
  48. $this->error('该档案已有正在处理的AI测量任务,请稍后再试');
  49. }
  50. // 验证照片格式
  51. $requiredPhotos = ['front', 'side', 'back'];
  52. foreach ($requiredPhotos as $angle) {
  53. if (empty($params['photos'][$angle])) {
  54. $this->error("请上传{$angle}角度的身体照片");
  55. }
  56. }
  57. // 创建AI测量任务
  58. $taskData = [
  59. 'profile_id' => $params['profile_id'],
  60. 'user_id' => $this->auth->id,
  61. 'photos' => json_encode($params['photos']),
  62. 'params' => json_encode([
  63. 'gender' => $profile->gender,
  64. 'height' => $profile->height,
  65. 'weight' => $profile->weight
  66. ]),
  67. 'priority' => $params['priority'] ?? 5,
  68. 'status' => 0, // 待处理
  69. 'createtime' => time(),
  70. 'updatetime' => time()
  71. ];
  72. $taskId = \think\Db::table('fa_ai_measurement_task')->insertGetId($taskData);
  73. // 立即处理任务(也可以放到队列中异步处理)
  74. $this->processTask($taskId);
  75. $this->success('AI测量分析已开始', [
  76. 'task_id' => $taskId,
  77. 'estimated_time' => 30 // 预计处理时间(秒)
  78. ]);
  79. } catch (\Exception $e) {
  80. $this->error($e->getMessage());
  81. }
  82. }
  83. /**
  84. * 获取AI测量结果
  85. */
  86. public function getResult()
  87. {
  88. $taskId = $this->request->get('task_id/d');
  89. $profileId = $this->request->get('profile_id/d');
  90. if (!$taskId && !$profileId) {
  91. $this->error('任务ID或档案ID不能为空');
  92. }
  93. try {
  94. $query = \think\Db::table('fa_ai_measurement_task')
  95. ->where('user_id', $this->auth->id);
  96. if ($taskId) {
  97. $query->where('id', $taskId);
  98. } else {
  99. $query->where('profile_id', $profileId)->order('id DESC');
  100. }
  101. $task = $query->find();
  102. if (!$task) {
  103. $this->error('任务不存在');
  104. }
  105. // 根据任务状态返回不同结果
  106. switch ($task['status']) {
  107. case 0: // 待处理
  108. $this->success('任务排队中', [
  109. 'status' => 'pending',
  110. 'message' => '任务正在排队等待处理'
  111. ]);
  112. break;
  113. case 1: // 处理中
  114. $this->success('正在分析中', [
  115. 'status' => 'processing',
  116. 'message' => 'AI正在分析您的身体照片,请稍候...',
  117. 'progress' => $this->estimateProgress($task)
  118. ]);
  119. break;
  120. case 2: // 完成
  121. $result = json_decode($task['result'], true);
  122. $this->success('分析完成', [
  123. 'status' => 'completed',
  124. 'data' => $this->formatMeasurementResult($result, $task['profile_id'])
  125. ]);
  126. break;
  127. case 3: // 失败
  128. $this->success('分析失败', [
  129. 'status' => 'failed',
  130. 'message' => $task['error_message'] ?: '分析过程中出现错误',
  131. 'can_retry' => $task['attempts'] < $task['max_attempts']
  132. ]);
  133. break;
  134. default:
  135. $this->error('未知的任务状态');
  136. }
  137. } catch (\Exception $e) {
  138. $this->error($e->getMessage());
  139. }
  140. }
  141. /**
  142. * 保存AI测量结果
  143. */
  144. public function saveResult()
  145. {
  146. $params = $this->request->post();
  147. if (empty($params['task_id'])) {
  148. $this->error('任务ID不能为空');
  149. }
  150. try {
  151. // 获取任务信息
  152. $task = \think\Db::table('fa_ai_measurement_task')
  153. ->where('id', $params['task_id'])
  154. ->where('user_id', $this->auth->id)
  155. ->where('status', 2) // 只有完成的任务才能保存
  156. ->find();
  157. if (!$task) {
  158. $this->error('任务不存在或未完成');
  159. }
  160. $result = json_decode($task['result'], true);
  161. if (!$result || !isset($result['measurements'])) {
  162. $this->error('测量结果数据异常');
  163. }
  164. // 保存测量数据
  165. $measurement = AiMeasurementService::saveMeasurementResult(
  166. $task['profile_id'],
  167. $result['measurements'],
  168. json_decode($task['photos'], true),
  169. $result['confidence'] ?? null
  170. );
  171. $this->success('测量结果已保存', [
  172. 'measurement_id' => $measurement->id
  173. ]);
  174. } catch (\Exception $e) {
  175. $this->error($e->getMessage());
  176. }
  177. }
  178. /**
  179. * 重新分析
  180. */
  181. public function retryAnalysis()
  182. {
  183. $taskId = $this->request->post('task_id/d');
  184. if (!$taskId) {
  185. $this->error('任务ID不能为空');
  186. }
  187. try {
  188. $task = \think\Db::table('fa_ai_measurement_task')
  189. ->where('id', $taskId)
  190. ->where('user_id', $this->auth->id)
  191. ->where('status', 3) // 失败的任务
  192. ->find();
  193. if (!$task) {
  194. $this->error('任务不存在或不允许重试');
  195. }
  196. if ($task['attempts'] >= $task['max_attempts']) {
  197. $this->error('重试次数已达上限');
  198. }
  199. // 重置任务状态
  200. \think\Db::table('fa_ai_measurement_task')
  201. ->where('id', $taskId)
  202. ->update([
  203. 'status' => 0,
  204. 'error_message' => '',
  205. 'updatetime' => time()
  206. ]);
  207. // 重新处理任务
  208. $this->processTask($taskId);
  209. $this->success('已重新开始分析');
  210. } catch (\Exception $e) {
  211. $this->error($e->getMessage());
  212. }
  213. }
  214. /**
  215. * 获取测量字段配置
  216. */
  217. public function getMeasurementConfig()
  218. {
  219. $gender = $this->request->get('gender/d', 1);
  220. try {
  221. $config = AiMeasurementService::getMeasurementDisplayConfig($gender);
  222. $this->success('获取成功', $config);
  223. } catch (\Exception $e) {
  224. $this->error($e->getMessage());
  225. }
  226. }
  227. /**
  228. * 获取AI测量素材配置
  229. * @ApiMethod (GET)
  230. * @ApiParams (name="mode", type="string", required=false, description="模式:selfie(自拍)、helper(帮拍)、common(通用)、all(全部)")
  231. */
  232. public function getMaterialConfig()
  233. {
  234. $mode = $this->request->get('mode', 'all');
  235. // try {
  236. $config = [];
  237. switch ($mode) {
  238. case 'selfie':
  239. $config = $this->getSelfieConfig();
  240. break;
  241. case 'helper':
  242. $config = $this->getHelperConfig();
  243. break;
  244. case 'common':
  245. $config = $this->getCommonConfig();
  246. break;
  247. case 'all':
  248. default:
  249. $config = [
  250. 'selfie' => $this->getSelfieConfig(),
  251. 'helper' => $this->getHelperConfig(),
  252. 'common' => $this->getCommonConfig()
  253. ];
  254. break;
  255. }
  256. $this->success('获取成功', $config);
  257. // } catch (\Exception $e) {
  258. // $this->error($e->getMessage());
  259. // }
  260. }
  261. /**
  262. * 获取自拍模式配置
  263. */
  264. private function getSelfieConfig()
  265. {
  266. return [
  267. 'enabled' => config('site.ai_measure_selfie_enabled'),
  268. 'intro_image' => $this->formatFileUrl(config('site.ai_measure_selfie_intro_image')),
  269. // 引导教程
  270. 'tutorial' => [
  271. 'images' => $this->parseImages(config('site.ai_measure_selfie_tutorial_images')),
  272. 'video' => $this->formatFileUrl(config('site.ai_measure_selfie_tutorial_video'))
  273. ],
  274. // 陀螺仪检测
  275. 'gyroscope' => [
  276. 'voice' => $this->formatFileUrl(config('site.ai_measure_selfie_gyro_voice')),
  277. 'example' => $this->formatFileUrl(config('site.ai_measure_selfie_gyro_example'))
  278. ],
  279. // 拍摄正面
  280. 'front_shooting' => [
  281. 'frame' => $this->formatFileUrl(config('site.ai_measure_selfie_front_frame')),
  282. 'demo' => $this->formatFileUrl(config('site.ai_measure_selfie_front_demo')),
  283. 'text' => config('site.ai_measure_selfie_front_text'),
  284. 'voice' => $this->formatFileUrl(config('site.ai_measure_selfie_front_voice'))
  285. ],
  286. // 拍摄侧面
  287. 'side_shooting' => [
  288. 'frame' => $this->formatFileUrl(config('site.ai_measure_selfie_side_frame')),
  289. 'demo' => $this->formatFileUrl(config('site.ai_measure_selfie_side_demo')),
  290. 'text' => config('site.ai_measure_selfie_side_text'),
  291. 'voice' => $this->formatFileUrl(config('site.ai_measure_selfie_side_voice'))
  292. ],
  293. // 拍摄正面侧平举
  294. 'arms_shooting' => [
  295. 'frame' => $this->formatFileUrl(config('site.ai_measure_selfie_arms_frame')),
  296. 'demo' => $this->formatFileUrl(config('site.ai_measure_selfie_arms_demo')),
  297. 'text' => config('site.ai_measure_selfie_arms_text'),
  298. 'voice' => $this->formatFileUrl(config('site.ai_measure_selfie_arms_voice'))
  299. ],
  300. // 拍摄过程素材
  301. 'process_materials' => [
  302. 'countdown_voice' => $this->formatFileUrl(config('site.ai_measure_selfie_countdown_voice')),
  303. 'timer_sound' => $this->formatFileUrl(config('site.ai_measure_selfie_timer_sound')),
  304. 'complete_sound' => $this->formatFileUrl(config('site.ai_measure_selfie_complete_sound')),
  305. 'next_voice' => $this->formatFileUrl(config('site.ai_measure_selfie_next_voice')),
  306. 'finish_voice' => $this->formatFileUrl(config('site.ai_measure_selfie_finish_voice'))
  307. ]
  308. ];
  309. }
  310. /**
  311. * 获取帮拍模式配置
  312. */
  313. private function getHelperConfig()
  314. {
  315. return [
  316. 'enabled' => config('site.ai_measure_helper_enabled'),
  317. 'intro_image' => $this->formatFileUrl(config('site.ai_measure_helper_intro_image')),
  318. // 引导教程
  319. 'tutorial' => [
  320. 'images' => $this->parseImages(config('site.ai_measure_helper_tutorial_images')),
  321. 'video' => $this->formatFileUrl(config('site.ai_measure_helper_tutorial_video'))
  322. ],
  323. // 陀螺仪检测
  324. 'gyroscope' => [
  325. 'voice' => $this->formatFileUrl(config('site.ai_measure_helper_gyro_voice')),
  326. 'example' => $this->formatFileUrl(config('site.ai_measure_helper_gyro_example'))
  327. ],
  328. // 拍摄正面
  329. 'front_shooting' => [
  330. 'frame' => $this->formatFileUrl(config('site.ai_measure_helper_front_frame')),
  331. 'demo' => $this->formatFileUrl(config('site.ai_measure_helper_front_demo')),
  332. 'text' => config('site.ai_measure_helper_front_text'),
  333. 'voice' => $this->formatFileUrl(config('site.ai_measure_helper_front_voice'))
  334. ],
  335. // 拍摄侧面
  336. 'side_shooting' => [
  337. 'frame' => $this->formatFileUrl(config('site.ai_measure_helper_side_frame')),
  338. 'demo' => $this->formatFileUrl(config('site.ai_measure_helper_side_demo')),
  339. 'text' => config('site.ai_measure_helper_side_text'),
  340. 'voice' => $this->formatFileUrl(config('site.ai_measure_helper_side_voice'))
  341. ],
  342. // 拍摄正面侧平举
  343. 'arms_shooting' => [
  344. 'frame' => $this->formatFileUrl(config('site.ai_measure_helper_arms_frame')),
  345. 'demo' => $this->formatFileUrl(config('site.ai_measure_helper_arms_demo')),
  346. 'text' => config('site.ai_measure_helper_arms_text'),
  347. 'voice' => $this->formatFileUrl(config('site.ai_measure_helper_arms_voice'))
  348. ],
  349. // 拍摄过程素材
  350. 'process_materials' => [
  351. 'countdown_voice' => cdnurl(config('site.ai_measure_helper_countdown_voice')),
  352. 'timer_sound' => $this->formatFileUrl(config('site.ai_measure_helper_timer_sound')),
  353. 'complete_sound' => $this->formatFileUrl(config('site.ai_measure_helper_complete_sound')),
  354. 'next_voice' => $this->formatFileUrl(config('site.ai_measure_helper_next_voice')),
  355. 'finish_voice' => $this->formatFileUrl(config('site.ai_measure_helper_finish_voice'))
  356. ]
  357. ];
  358. }
  359. /**
  360. * 获取通用配置
  361. */
  362. private function getCommonConfig()
  363. {
  364. return [
  365. 'welcome_notice' => config('site.ai_measure_welcome_notice'),
  366. 'privacy_notice' => config('site.ai_measure_privacy_notice'),
  367. 'accuracy_disclaimer' => config('site.ai_measure_accuracy_disclaimer'),
  368. 'demo_images' => [
  369. 'front_demo' => $this->formatFileUrl(config('site.ai_measure_common_front_demo')),
  370. 'side_demo' => $this->formatFileUrl(config('site.ai_measure_common_side_demo')),
  371. 'arms_demo' => $this->formatFileUrl(config('site.ai_measure_common_arms_demo'))
  372. ]
  373. ];
  374. }
  375. /**
  376. * 格式化文件URL
  377. */
  378. private function formatFileUrl($url)
  379. {
  380. if (empty($url)) {
  381. return null;
  382. }
  383. return cdnurl($url);
  384. }
  385. /**
  386. * 解析图片集合
  387. */
  388. private function parseImages($images)
  389. {
  390. if (empty($images)) {
  391. return [];
  392. }
  393. // 如果是JSON格式的字符串,解析为数组
  394. if (is_string($images)) {
  395. $imageArray = json_decode($images, true);
  396. if (json_last_error() === JSON_ERROR_NONE && is_array($imageArray)) {
  397. $images = $imageArray;
  398. } else {
  399. // 如果不是JSON,可能是逗号分隔的字符串
  400. $images = explode(',', $images);
  401. }
  402. }
  403. if (!is_array($images)) {
  404. return [];
  405. }
  406. // 格式化每个图片URL
  407. return array_map(function($url) {
  408. return $this->formatFileUrl(trim($url));
  409. }, array_filter($images));
  410. }
  411. /**
  412. * 直接调用第三方AI测量服务
  413. * @ApiMethod (POST)
  414. * @ApiParams (name="profile_id", type="integer", required=true, description="档案ID")
  415. * @ApiParams (name="photos", type="object", required=true, description="身体照片对象")
  416. * @ApiParams (name="photos.front", type="string", required=true, description="正面照片URL")
  417. * @ApiParams (name="photos.side", type="string", required=true, description="侧面照片URL")
  418. * @ApiParams (name="photos.back", type="string", required=true, description="背面照片URL")
  419. */
  420. public function measurementDirect()
  421. {
  422. $params = $this->request->post();
  423. // 验证必要参数
  424. if (empty($params['profile_id'])) {
  425. $this->error('档案ID不能为空');
  426. }
  427. // if (empty($params['photos']) || !is_array($params['photos'])) {
  428. // $this->error('请上传身体照片');
  429. // }
  430. // try {
  431. // 验证档案归属
  432. $profile = \app\common\model\BodyProfile::where('id', $params['profile_id'])
  433. ->where('user_id', $this->auth->id)
  434. ->find();
  435. if (!$profile) {
  436. $this->error('档案不存在');
  437. }
  438. // 验证照片格式
  439. // $requiredPhotos = ['front', 'side', 'back'];
  440. // foreach ($requiredPhotos as $angle) {
  441. // if (empty($params['photos'][$angle])) {
  442. // $this->error("请上传{$angle}角度的身体照片");
  443. // }
  444. // }
  445. // 直接使用档案的
  446. $photos = $profile->body_photos_text;
  447. // 安全调用第三方AI服务 - 确保身高为数字格式
  448. $heightCm = is_numeric($profile->height) ? floatval($profile->height) : 0;
  449. $measurements = $this->safeCallThirdPartyAiService(
  450. $photos,
  451. $heightCm
  452. );
  453. // echo "<pre>";
  454. // print_r($measurements);
  455. // echo "</pre>";
  456. // exit;
  457. // 处理结果
  458. // $result = [
  459. // 'measurements' => $measurements,
  460. // 'confidence' => $measurements['_confidence'] ?? 0.8,
  461. // 'warnings' => $measurements['warnings'] ?? []
  462. // ];
  463. // 清理内部字段
  464. // unset($result['measurements']['_confidence']);
  465. // unset($result['measurements']['warnings']);
  466. // 格式化结果用于展示
  467. //$formattedResult = $this->formatMeasurementResult($result, $params['profile_id']);
  468. $measurements['height'] = $profile->height;
  469. $measurements['weight'] = $profile->weight;
  470. // 检查是否有错误状态
  471. $hasErrors = isset($measurements['warnings']) && !empty($measurements['warnings']);
  472. $state = $measurements['_state'] ?? 0;
  473. $message = $measurements['_message'] ?? 'AI测量完成';
  474. // 构建返回数据
  475. $responseData = $measurements;
  476. // 根据状态和错误情况返回不同的消息
  477. if ($state === 0 && !$hasErrors) {
  478. $this->success('AI测量完成', $responseData);
  479. } else {
  480. // 有错误直接返回错误状态
  481. $errorMsg = $hasErrors ? implode(', ', $measurements['warnings']) : $message;
  482. $this->error($errorMsg, $responseData);
  483. }
  484. // } catch (\Exception $e) {
  485. // $this->error($e->getMessage());
  486. // }
  487. }
  488. /**
  489. * 调用第三方AI测量接口
  490. */
  491. private function callThirdPartyAiService($photos, $height)
  492. {
  493. // 第三方API配置
  494. $apiUrl = $this->thirdPartyApiConfig['url'];
  495. try {
  496. // 准备请求数据 - 确保身高为纯数字(厘米)
  497. $heightValue = is_numeric($height) ? floatval($height) : 0;
  498. $requestData = [
  499. 'height' => $heightValue
  500. ];
  501. // 处理照片数据 - 转换为base64格式
  502. if (isset($photos['front'])) {
  503. $requestData['image1'] = $this->convertImageToBase64($photos['front']);
  504. }
  505. if (isset($photos['side'])) {
  506. $requestData['image2'] = $this->convertImageToBase64($photos['side']);
  507. }
  508. if (isset($photos['back'])) {
  509. $requestData['image3'] = $this->convertImageToBase64($photos['back']);
  510. }
  511. // 记录请求日志(不包含图片数据)
  512. // $logData = [
  513. // 'url' => $apiUrl,
  514. // 'height' => $requestData['height'],
  515. // 'image_count' => count(array_filter([
  516. // isset($requestData['image1']),
  517. // isset($requestData['image2']),
  518. // isset($requestData['image3'])
  519. // ]))
  520. // ];
  521. // 记录请求日志(包含身高和图片base64数据的前50个字符)
  522. $logData = [
  523. 'url' => $apiUrl,
  524. 'height' => $heightValue . 'cm',
  525. 'image1_preview' => isset($requestData['image1']) ? substr($requestData['image1'], 0, 50) . '...' : null,
  526. 'image2_preview' => isset($requestData['image2']) ? substr($requestData['image2'], 0, 50) . '...' : null,
  527. 'image3_preview' => isset($requestData['image3']) ? substr($requestData['image3'], 0, 50) . '...' : null,
  528. 'request_data_size' => strlen(json_encode($requestData)) . ' bytes'
  529. ];
  530. \think\Log::info('Calling third party AI service: ' . json_encode($logData));
  531. // 发送POST请求
  532. $ch = curl_init();
  533. curl_setopt_array($ch, [
  534. CURLOPT_URL => $apiUrl,
  535. CURLOPT_POST => true,
  536. CURLOPT_POSTFIELDS => json_encode($requestData),
  537. CURLOPT_RETURNTRANSFER => true,
  538. CURLOPT_TIMEOUT => $this->thirdPartyApiConfig['timeout'],
  539. CURLOPT_CONNECTTIMEOUT => $this->thirdPartyApiConfig['connect_timeout'],
  540. CURLOPT_HTTPHEADER => [
  541. 'Content-Type: application/json',
  542. 'Accept: application/json'
  543. ],
  544. CURLOPT_SSL_VERIFYPEER => false,
  545. CURLOPT_SSL_VERIFYHOST => false
  546. ]);
  547. $response = curl_exec($ch);
  548. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  549. $error = curl_error($ch);
  550. curl_close($ch);
  551. if ($error) {
  552. throw new \Exception('请求第三方AI服务失败: ' . $error);
  553. }
  554. \think\Log::info('Third party AI service response: ' .$response);
  555. // 检查响应内容
  556. if (empty($response)) {
  557. throw new \Exception('第三方AI服务返回空响应');
  558. }
  559. $result = json_decode($response, true);
  560. if (json_last_error() !== JSON_ERROR_NONE) {
  561. // 对于非200状态码,尝试返回原始响应内容作为错误信息
  562. if ($httpCode !== 200) {
  563. throw new \Exception("第三方AI服务返回异常(HTTP {$httpCode}): " . substr($response, 0, 200));
  564. } else {
  565. throw new \Exception('第三方AI服务返回数据格式错误');
  566. }
  567. }
  568. // 处理各种HTTP错误状态,但先检查是否有有效的JSON响应
  569. if ($httpCode !== 200) {
  570. // 如果返回了JSON格式的错误信息,优先使用
  571. if (isset($result['state']) && $result['state'] == 201) {
  572. // 传参错误,通过processMeasurementData处理
  573. return $this->processMeasurementData($result);
  574. } elseif (isset($result['message'])) {
  575. throw new \Exception("第三方AI服务错误(HTTP {$httpCode}): " . $result['message']);
  576. } else {
  577. // 传统的HTTP错误处理
  578. if ($httpCode >= 500) {
  579. throw new \Exception('第三方AI服务内部错误: HTTP ' . $httpCode);
  580. } elseif ($httpCode >= 400) {
  581. throw new \Exception('第三方AI服务请求错误: HTTP ' . $httpCode);
  582. } else {
  583. throw new \Exception('第三方AI服务返回异常状态: HTTP ' . $httpCode);
  584. }
  585. }
  586. }
  587. // 记录API响应信息
  588. // $responseLog = [
  589. // 'http_code' => $httpCode,
  590. // 'response_size' => strlen($response) . ' bytes',
  591. // 'has_body_size' => isset($result['body_size']),
  592. // 'body_size_fields' => isset($result['body_size']) ? array_keys($result['body_size']) : [],
  593. // 'response_preview' => substr($response, 0, 200) . '...'
  594. // ];
  595. // 处理返回的测量数据
  596. // echo "<pre>";
  597. // print_r($result);
  598. // echo "</pre>";
  599. // exit;
  600. return $this->processMeasurementData($result);
  601. } catch (\Exception $e) {
  602. // 记录错误日志
  603. \think\Log::error('Third party AI service error: ' . $e->getMessage());
  604. throw $e;
  605. }
  606. // 如果执行到这里说明没有异常处理,直接返回处理结果
  607. }
  608. /**
  609. * 安全调用第三方AI服务(带异常处理和默认返回)
  610. */
  611. private function safeCallThirdPartyAiService($photos, $height)
  612. {
  613. try {
  614. return $this->callThirdPartyAiService($photos, $height);
  615. } catch (\Exception $e) {
  616. // 记录错误日志
  617. \think\Log::error('Third party AI service error, returning default data: ' . $e->getMessage());
  618. // 返回默认的空测量数据
  619. return $this->getDefaultMeasurementData();
  620. }
  621. }
  622. /**
  623. * 获取默认的空测量数据
  624. */
  625. private function getDefaultMeasurementData()
  626. {
  627. // 返回所有映射字段的空值(使用空字符串)
  628. return [
  629. 'chest'=>'', // 净胸围 → 胸围
  630. 'waist'=>'', // 净腰围 → 腰围
  631. 'hip'=>'', // 净臀围 → 实际臀围
  632. //'thigh', // 净腿根 → 大腿围
  633. 'knee'=>'', // 净膝围 → 膝围
  634. 'calf'=>'', // 净小腿围 → 小腿围
  635. 'arm_length'=>'', // 净手臂长 → 臂长
  636. 'wrist'=>'', // 净手腕围 → 手腕围
  637. 'pants_length'=>'', // 腿长 → 腿长
  638. 'belly_belt'=>'', // 净肚围 → 肚围
  639. 'shoulder_width'=>'', // 净肩宽 → 肩宽
  640. 'leg_root'=>'', // 净腿根 → 大腿围
  641. 'neck'=>'', // 净颈围 → 颈围
  642. 'inseam'=>'', // 内腿长 → 内腿长
  643. 'upper_arm'=>'', // 净上臂围 → 上臂围
  644. 'ankle'=>'', // 净脚踝围 → 脚踝围
  645. 'waist_lower'=>'', // 净小腹围 → 下腰围
  646. 'mid_waist'=>'', // 净中腰 → 中腰围
  647. '_confidence' => 0.0,
  648. 'warnings' => ['第三方AI服务暂时不可用,返回默认数据']
  649. ];
  650. }
  651. /**
  652. * 根据状态码获取错误信息
  653. */
  654. private function getStateErrorMessage($state)
  655. {
  656. $stateMessages = [
  657. 1 => '颈围计算异常',
  658. 2 => '腰围计算异常',
  659. 3 => '大腿围计算异常',
  660. 4 => '膝盖围计算异常',
  661. 5 => '胸围计算异常',
  662. 6 => '小腿围计算异常',
  663. 7 => '肚围计算异常',
  664. 8 => '臀围计算异常',
  665. 9 => '手臂长计算异常',
  666. 10 => '手腕围计算异常',
  667. 11 => '腿长计算异常、裤长计算异常、内腿长计算异常',
  668. 12 => '肩宽计算异常',
  669. 13 => '脚踝计算异常',
  670. 14 => '中腰计算异常',
  671. 15 => '小腹围计算异常',
  672. 16 => '上臂围计算异常',
  673. 101 => '图片1提取尺寸异常',
  674. 102 => '图片2提取尺寸异常',
  675. 103 => '图片3提取尺寸异常',
  676. 201 => '传参错误'
  677. ];
  678. return $stateMessages[$state] ?? "未知错误(状态码: {$state})";
  679. }
  680. /**
  681. * 测试接口 - 返回模拟的第三方API测量数据
  682. * @ApiMethod (POST)
  683. * @ApiParams (name="profile_id", type="integer", required=true, description="档案ID")
  684. */
  685. public function testMeasurementData()
  686. {
  687. $params = $this->request->post();
  688. // 验证必要参数
  689. if (empty($params['profile_id'])) {
  690. $this->error('档案ID不能为空');
  691. }
  692. // 验证档案归属
  693. $profile = \app\common\model\BodyProfile::where('id', $params['profile_id'])
  694. ->where('user_id', $this->auth->id)
  695. ->find();
  696. if (!$profile) {
  697. $this->error('档案不存在');
  698. }
  699. // 模拟第三方API返回的数据
  700. $mockApiResult = [
  701. "body_size" => [
  702. "datuigen" => 56.973762220946064,
  703. "duwei" => 71.86294164495045,
  704. "jiankuan" => 44.99356951672863,
  705. "jiaohuai" => 20.995062499529606,
  706. "jingwei" => 36.973537078225604,
  707. "neitui" => 67.99506048261769,
  708. "shangbi" => 23.285375591374667,
  709. "shoubichang" => 61.1834335984307,
  710. "shouwanwei" => 16.0697059847192,
  711. "tuichang" => 73.9800462219755,
  712. "tunwei" => 90.08082593388505,
  713. "xiaofu" => 70.98010845587423,
  714. "xiaotuiwei" => 37.2761443409742,
  715. "xigai" => 34.990971006868364,
  716. "xiongwei" => 81.85738385794711,
  717. "yaowei" => 72.93800974219818,
  718. "zhongyao" => 70.99945416888724
  719. ],
  720. "image1" => "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAME",
  721. "image2" => "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAME",
  722. "image3" => "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAME",
  723. "message" => "sucessed",
  724. "state" => 0
  725. ];
  726. // 处理测量数据
  727. $measurements = $this->processMeasurementData($mockApiResult);
  728. // 处理结果
  729. $result = [
  730. 'measurements' => $measurements,
  731. 'confidence' => $measurements['_confidence'] ?? 0.8,
  732. 'warnings' => $measurements['warnings'] ?? []
  733. ];
  734. // 清理内部字段
  735. unset($result['measurements']['_confidence']);
  736. unset($result['measurements']['warnings']);
  737. // 格式化结果用于展示
  738. $formattedResult = $this->formatMeasurementResult($result, $params['profile_id']);
  739. $this->success('测试数据返回成功', [
  740. 'original_api_data' => $mockApiResult,
  741. 'mapped_measurements' => $result['measurements'],
  742. 'formatted_result' => $formattedResult
  743. ]);
  744. }
  745. /**
  746. * 测试错误状态处理 - 返回不同错误状态的模拟数据
  747. * @ApiMethod (POST)
  748. * @ApiParams (name="profile_id", type="integer", required=true, description="档案ID")
  749. * @ApiParams (name="test_state", type="integer", required=false, description="测试状态码(1-16, 101-103, 201)")
  750. */
  751. public function testErrorStates()
  752. {
  753. $params = $this->request->post();
  754. // 验证必要参数
  755. if (empty($params['profile_id'])) {
  756. $this->error('档案ID不能为空');
  757. }
  758. // 验证档案归属
  759. $profile = \app\common\model\BodyProfile::where('id', $params['profile_id'])
  760. ->where('user_id', $this->auth->id)
  761. ->find();
  762. if (!$profile) {
  763. $this->error('档案不存在');
  764. }
  765. // 获取测试状态码
  766. $testState = intval($params['test_state'] ?? 1);
  767. // 模拟不同错误状态的API返回
  768. $mockApiResults = [
  769. // 部位计算异常(1-16)
  770. 1 => [
  771. "body_size" => [],
  772. "image1" => "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAME",
  773. "image2" => "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAME",
  774. "image3" => "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAME",
  775. "message" => "neck calculation error",
  776. "state" => 1
  777. ],
  778. 5 => [
  779. "body_size" => [
  780. "yaowei" => 72.93800974219818,
  781. "tunwei" => 90.08082593388505
  782. ],
  783. "image1" => "data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAIBAQEBAQIBAQECAgICAgQDAgICAgUEBAME",
  784. "message" => "chest calculation error",
  785. "state" => 5
  786. ],
  787. // 图片提取异常(101-103)
  788. 101 => [
  789. "body_size" => [],
  790. "message" => "image1 size extraction error",
  791. "state" => 101
  792. ],
  793. // 传参错误(201)
  794. 201 => [
  795. "body_size" => [],
  796. "message" => "parameter error",
  797. "state" => 201
  798. ]
  799. ];
  800. // 获取对应的模拟数据,如果没有则使用默认的状态1
  801. $mockApiResult = $mockApiResults[$testState] ?? $mockApiResults[1];
  802. $mockApiResult['state'] = $testState; // 确保状态码正确
  803. try {
  804. // 处理测量数据
  805. $measurements = $this->processMeasurementData($mockApiResult);
  806. $testData = [
  807. 'test_state' => $testState,
  808. 'error_message' => $this->getStateErrorMessage($testState),
  809. 'mock_api_result' => $mockApiResult,
  810. 'processed_measurements' => $measurements
  811. ];
  812. // 检查是否有错误状态
  813. $hasErrors = isset($measurements['warnings']) && !empty($measurements['warnings']);
  814. $state = $measurements['_state'] ?? 0;
  815. if ($state === 0 && !$hasErrors) {
  816. $this->success('错误状态测试成功', $testData);
  817. } else {
  818. // 有错误直接返回错误状态
  819. $errorMsg = $this->getStateErrorMessage($testState);
  820. $this->error($errorMsg, $testData);
  821. }
  822. } catch (\Exception $e) {
  823. // 对于201状态(传参错误),会抛出异常
  824. $this->error('测试异常: ' . $e->getMessage(), [
  825. 'test_state' => $testState,
  826. 'mock_api_result' => $mockApiResult
  827. ]);
  828. }
  829. }
  830. /**
  831. * 将图片URL转换为base64格式
  832. */
  833. private function convertImageToBase64($imageUrl)
  834. {
  835. try {
  836. // 如果已经是base64格式,直接返回
  837. if (strpos($imageUrl, 'data:image') === 0) {
  838. return $imageUrl;
  839. }
  840. // 如果是相对路径,转换为绝对路径
  841. if (strpos($imageUrl, 'http') !== 0) {
  842. $imageUrl = request()->domain() . $imageUrl;
  843. }
  844. // 获取图片数据
  845. $ch = curl_init();
  846. curl_setopt_array($ch, [
  847. CURLOPT_URL => $imageUrl,
  848. CURLOPT_RETURNTRANSFER => true,
  849. CURLOPT_TIMEOUT => 30,
  850. CURLOPT_CONNECTTIMEOUT => 10,
  851. CURLOPT_FOLLOWLOCATION => true,
  852. CURLOPT_SSL_VERIFYPEER => false,
  853. CURLOPT_SSL_VERIFYHOST => false,
  854. CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
  855. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  856. CURLOPT_HTTPHEADER => [
  857. 'Accept: image/webp,image/apng,image/*,*/*;q=0.8',
  858. 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
  859. 'Cache-Control: no-cache',
  860. ]
  861. ]);
  862. $imageData = curl_exec($ch);
  863. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  864. $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  865. $curlError = curl_error($ch);
  866. $effectiveUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  867. curl_close($ch);
  868. // 详细的错误处理
  869. if ($curlError) {
  870. throw new \Exception("网络请求失败: {$curlError} (URL: {$imageUrl})");
  871. }
  872. if ($httpCode !== 200) {
  873. throw new \Exception("图片访问失败,HTTP状态码: {$httpCode} (URL: {$imageUrl})");
  874. }
  875. if (!$imageData || strlen($imageData) === 0) {
  876. throw new \Exception("图片数据为空 (URL: {$imageUrl})");
  877. }
  878. // 验证图片数据是否有效
  879. if (!@getimagesizefromstring($imageData)) {
  880. throw new \Exception("获取的数据不是有效的图片格式 (URL: {$imageUrl})");
  881. }
  882. // 确定MIME类型
  883. if (strpos($contentType, 'image/') === 0) {
  884. $mimeType = $contentType;
  885. } else {
  886. // 通过文件扩展名推断
  887. $extension = strtolower(pathinfo(parse_url($imageUrl, PHP_URL_PATH), PATHINFO_EXTENSION));
  888. $mimeTypes = [
  889. 'jpg' => 'image/jpeg',
  890. 'jpeg' => 'image/jpeg',
  891. 'png' => 'image/png',
  892. 'gif' => 'image/gif',
  893. 'webp' => 'image/webp',
  894. 'bmp' => 'image/bmp'
  895. ];
  896. $mimeType = $mimeTypes[$extension] ?? 'image/jpeg';
  897. }
  898. // 转换为base64
  899. $base64 = base64_encode($imageData);
  900. return "data:{$mimeType};base64,{$base64}";
  901. } catch (\Exception $e) {
  902. \think\Log::error('Convert image to base64 error: ' . $e->getMessage() . ' URL: ' . $imageUrl);
  903. throw new \Exception('图片转换失败: ' . $e->getMessage());
  904. }
  905. }
  906. /**
  907. * 处理第三方API返回的测量数据
  908. */
  909. private function processMeasurementData($apiResult)
  910. {
  911. $measurements = [];
  912. try {
  913. // 首先检查是否有state字段(错误状态码)
  914. if (isset($apiResult['state'])) {
  915. $state = intval($apiResult['state']);
  916. // state=0为正常,其他为异常
  917. if ($state !== 0) {
  918. $errorMsg = $this->getStateErrorMessage($state);
  919. // 如果是严重错误(传参错误),直接抛出异常
  920. if ($state == 201) {
  921. throw new \Exception($errorMsg);
  922. }
  923. // 对于部位计算异常或图片提取异常,返回部分数据和警告
  924. $measurements = $this->getDefaultMeasurementData();
  925. $measurements['_confidence'] = 0.3; // 低置信度
  926. $measurements['warnings'] = [$errorMsg];
  927. $measurements['_state'] = $state;
  928. $measurements['_message'] = $apiResult['message'] ?? '';
  929. return $measurements;
  930. }
  931. }
  932. // state=0 或没有state字段,处理正常数据
  933. // 检查返回数据结构 - 可能直接包含body_size字段
  934. if (isset($apiResult['body_size'])) {
  935. $data = $apiResult['body_size'];
  936. $hasValidData = true;
  937. } elseif (isset($apiResult['status']) && $apiResult['status'] === 'success') {
  938. $data = $apiResult['data'] ?? [];
  939. $hasValidData = true;
  940. } else {
  941. // 尝试直接使用返回的数据
  942. $data = $apiResult;
  943. $hasValidData = !empty($data) && is_array($data);
  944. }
  945. if ($hasValidData) {
  946. // 映射字段名(根据第三方API返回的字段名进行映射)
  947. $fieldMapping = [
  948. 'xiongwei' => 'chest', // 净胸围 → 胸围
  949. 'yaowei' => 'waist', // 净腰围 → 腰围
  950. 'tunwei' => 'hip', // 净臀围 → 实际臀围
  951. //'datuigen' => 'thigh', // 净腿根 → 大腿围
  952. 'xigai' => 'knee', // 净膝围 → 膝围
  953. 'xiaotuiwei' => 'calf', // 净小腿围 → 小腿围
  954. 'shoubichang' => 'arm_length', // 净手臂长 → 臂长
  955. 'shouwanwei' => 'wrist', // 净手腕围 → 手腕围
  956. 'tuichang' => 'pants_length', // 腿长 → 腿长
  957. 'duwei' => 'belly_belt', // 净肚围 → 肚围
  958. 'jiankuan' => 'shoulder_width', // 净肩宽 → 肩宽
  959. 'datuigen' => 'leg_root', // 净腿根 → 大腿围
  960. 'jingwei' => 'neck', // 净颈围 → 颈围
  961. 'neitui' => 'inseam', // 内腿长 → 内腿长
  962. 'shangbi' => 'upper_arm', // 净上臂围 → 上臂围
  963. 'jiaohuai' => 'ankle', // 净脚踝围 → 脚踝围
  964. 'xiaofu' => 'waist_lower', // 净小腹围 → 下腰围
  965. 'zhongyao' => 'mid_waist', // 净中腰 → 中腰围
  966. ];
  967. foreach ($fieldMapping as $apiField => $localField) {
  968. if (isset($data[$apiField]) && is_numeric($data[$apiField])) {
  969. $measurements[$localField] = round(floatval($data[$apiField]), 1);
  970. }
  971. }
  972. // 设置置信度和警告信息
  973. $measurements['_confidence'] = $apiResult['confidence'] ?? 0.8;
  974. $measurements['warnings'] = $apiResult['warnings'] ?? [];
  975. $measurements['_state'] = isset($apiResult['state']) ? intval($apiResult['state']) : 0;
  976. $measurements['_message'] = $apiResult['message'] ?? 'sucessed';
  977. // 如果没有测量数据,添加默认警告
  978. if (count($measurements) <= 4) { // 只有_confidence, warnings, _state, _message
  979. $measurements['warnings'][] = '第三方AI服务未返回有效的测量数据';
  980. }
  981. } else {
  982. // API返回错误
  983. $errorMsg = $apiResult['message'] ?? $apiResult['error'] ?? '第三方AI服务返回未知错误';
  984. throw new \Exception($errorMsg);
  985. }
  986. } catch (\Exception $e) {
  987. // 处理异常,返回错误信息
  988. $measurements['_confidence'] = 0;
  989. $measurements['warnings'] = ['数据处理失败: ' . $e->getMessage()];
  990. }
  991. return $measurements;
  992. }
  993. /**
  994. * 处理AI测量任务
  995. */
  996. private function processTask($taskId)
  997. {
  998. try {
  999. // 更新任务状态为处理中
  1000. \think\Db::table('fa_ai_measurement_task')
  1001. ->where('id', $taskId)
  1002. ->update([
  1003. 'status' => 1,
  1004. 'started_at' => time(),
  1005. 'attempts' => \think\Db::raw('attempts + 1'),
  1006. 'updatetime' => time()
  1007. ]);
  1008. // 获取任务详情
  1009. $task = \think\Db::table('fa_ai_measurement_task')->where('id', $taskId)->find();
  1010. $photos = json_decode($task['photos'], true);
  1011. $params = json_decode($task['params'], true);
  1012. // 安全调用第三方AI分析服务 - 确保身高为数字格式
  1013. $heightCm = is_numeric($params['height']) ? floatval($params['height']) : 0;
  1014. $measurements = $this->safeCallThirdPartyAiService(
  1015. $photos,
  1016. $heightCm
  1017. );
  1018. // 处理结果
  1019. $result = [
  1020. 'measurements' => $measurements,
  1021. 'confidence' => $measurements['_confidence'] ?? 0.8,
  1022. 'warnings' => $measurements['warnings'] ?? []
  1023. ];
  1024. // 清理内部字段
  1025. unset($result['measurements']['_confidence']);
  1026. unset($result['measurements']['warnings']);
  1027. // 更新任务状态为完成
  1028. \think\Db::table('fa_ai_measurement_task')
  1029. ->where('id', $taskId)
  1030. ->update([
  1031. 'status' => 2,
  1032. 'result' => json_encode($result),
  1033. 'completed_at' => time(),
  1034. 'updatetime' => time()
  1035. ]);
  1036. } catch (\Exception $e) {
  1037. // 更新任务状态为失败
  1038. \think\Db::table('fa_ai_measurement_task')
  1039. ->where('id', $taskId)
  1040. ->update([
  1041. 'status' => 3,
  1042. 'error_message' => $e->getMessage(),
  1043. 'updatetime' => time()
  1044. ]);
  1045. }
  1046. }
  1047. /**
  1048. * 估算处理进度
  1049. */
  1050. private function estimateProgress($task)
  1051. {
  1052. $startTime = $task['started_at'];
  1053. $currentTime = time();
  1054. $elapsedTime = $currentTime - $startTime;
  1055. // 假设总处理时间为30秒
  1056. $totalTime = 30;
  1057. $progress = min(95, ($elapsedTime / $totalTime) * 100);
  1058. return round($progress);
  1059. }
  1060. /**
  1061. * 格式化测量结果用于展示
  1062. */
  1063. private function formatMeasurementResult($result, $profileId)
  1064. {
  1065. $profile = \app\common\model\BodyProfile::find($profileId);
  1066. $measurements = $result['measurements'];
  1067. // 获取显示配置
  1068. $displayConfig = AiMeasurementService::getMeasurementDisplayConfig($profile->gender);
  1069. // 格式化数据
  1070. $formattedData = [
  1071. 'profile' => [
  1072. 'id' => $profile->id,
  1073. 'name' => $profile->profile_name,
  1074. 'gender' => $profile->gender,
  1075. 'height' => $profile->height,
  1076. 'weight' => $profile->weight
  1077. ],
  1078. 'measurements' => [],
  1079. 'display_config' => $displayConfig,
  1080. 'confidence' => $result['confidence'] ?? 0,
  1081. 'warnings' => $result['warnings'] ?? []
  1082. ];
  1083. // 组织测量数据
  1084. foreach ($displayConfig as $field => $config) {
  1085. $value = isset($measurements[$field]) && $measurements[$field] > 0
  1086. ? $measurements[$field]
  1087. : null;
  1088. $formattedData['measurements'][$field] = [
  1089. 'label' => $config['label'],
  1090. 'value' => $value,
  1091. 'unit' => 'cm',
  1092. 'position' => $config['position'],
  1093. 'side' => $config['side']
  1094. ];
  1095. }
  1096. // 添加基础数据表格
  1097. $formattedData['basic_data'] = [
  1098. ['label' => '身高', 'value' => $profile->height, 'unit' => 'cm'],
  1099. ['label' => '体重', 'value' => $profile->weight, 'unit' => 'kg'],
  1100. ];
  1101. // 添加测量数据表格
  1102. $tableData = [];
  1103. $fields = array_keys($measurements);
  1104. $chunks = array_chunk($fields, 2);
  1105. foreach ($chunks as $chunk) {
  1106. $row = [];
  1107. foreach ($chunk as $field) {
  1108. if (isset($displayConfig[$field])) {
  1109. $row[] = [
  1110. 'label' => $displayConfig[$field]['label'],
  1111. 'value' => $measurements[$field] ?? null,
  1112. 'unit' => 'cm'
  1113. ];
  1114. }
  1115. }
  1116. if (!empty($row)) {
  1117. $tableData[] = $row;
  1118. }
  1119. }
  1120. $formattedData['table_data'] = $tableData;
  1121. return $formattedData;
  1122. }
  1123. }