AiMeasurement.php 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909
  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 (POST)
  230. * @ApiParams (name="profile_id", type="integer", required=true, description="档案ID")
  231. * @ApiParams (name="photos", type="object", required=true, description="身体照片对象")
  232. * @ApiParams (name="photos.front", type="string", required=true, description="正面照片URL")
  233. * @ApiParams (name="photos.side", type="string", required=true, description="侧面照片URL")
  234. * @ApiParams (name="photos.back", type="string", required=true, description="背面照片URL")
  235. */
  236. public function measurementDirect()
  237. {
  238. $params = $this->request->post();
  239. // 验证必要参数
  240. if (empty($params['profile_id'])) {
  241. $this->error('档案ID不能为空');
  242. }
  243. // if (empty($params['photos']) || !is_array($params['photos'])) {
  244. // $this->error('请上传身体照片');
  245. // }
  246. // try {
  247. // 验证档案归属
  248. $profile = \app\common\model\BodyProfile::where('id', $params['profile_id'])
  249. ->where('user_id', $this->auth->id)
  250. ->find();
  251. if (!$profile) {
  252. $this->error('档案不存在');
  253. }
  254. // 验证照片格式
  255. // $requiredPhotos = ['front', 'side', 'back'];
  256. // foreach ($requiredPhotos as $angle) {
  257. // if (empty($params['photos'][$angle])) {
  258. // $this->error("请上传{$angle}角度的身体照片");
  259. // }
  260. // }
  261. // 直接使用档案的
  262. $photos = $profile->body_photos_text;
  263. // 安全调用第三方AI服务 - 确保身高为数字格式
  264. $heightCm = is_numeric($profile->height) ? floatval($profile->height) : 0;
  265. $measurements = $this->safeCallThirdPartyAiService(
  266. $photos,
  267. $heightCm
  268. );
  269. // echo "<pre>";
  270. // print_r($measurements);
  271. // echo "</pre>";
  272. // exit;
  273. // 处理结果
  274. // $result = [
  275. // 'measurements' => $measurements,
  276. // 'confidence' => $measurements['_confidence'] ?? 0.8,
  277. // 'warnings' => $measurements['_warnings'] ?? []
  278. // ];
  279. // 清理内部字段
  280. // unset($result['measurements']['_confidence']);
  281. // unset($result['measurements']['_warnings']);
  282. // 格式化结果用于展示
  283. //$formattedResult = $this->formatMeasurementResult($result, $params['profile_id']);
  284. $this->success('AI测量完成', $measurements);
  285. // } catch (\Exception $e) {
  286. // $this->error($e->getMessage());
  287. // }
  288. }
  289. /**
  290. * 调用第三方AI测量接口
  291. */
  292. private function callThirdPartyAiService($photos, $height)
  293. {
  294. // 第三方API配置
  295. $apiUrl = $this->thirdPartyApiConfig['url'];
  296. try {
  297. // 准备请求数据 - 确保身高为纯数字(厘米)
  298. $heightValue = is_numeric($height) ? floatval($height) : 0;
  299. $requestData = [
  300. 'height' => $heightValue
  301. ];
  302. // 处理照片数据 - 转换为base64格式
  303. if (isset($photos['front'])) {
  304. $requestData['image1'] = $this->convertImageToBase64($photos['front']);
  305. }
  306. if (isset($photos['side'])) {
  307. $requestData['image2'] = $this->convertImageToBase64($photos['side']);
  308. }
  309. if (isset($photos['back'])) {
  310. $requestData['image3'] = $this->convertImageToBase64($photos['back']);
  311. }
  312. // 记录请求日志(不包含图片数据)
  313. // $logData = [
  314. // 'url' => $apiUrl,
  315. // 'height' => $requestData['height'],
  316. // 'image_count' => count(array_filter([
  317. // isset($requestData['image1']),
  318. // isset($requestData['image2']),
  319. // isset($requestData['image3'])
  320. // ]))
  321. // ];
  322. // 记录请求日志(包含身高和图片base64数据的前50个字符)
  323. $logData = [
  324. 'url' => $apiUrl,
  325. 'height' => $heightValue . 'cm',
  326. 'image1_preview' => isset($requestData['image1']) ? substr($requestData['image1'], 0, 50) . '...' : null,
  327. 'image2_preview' => isset($requestData['image2']) ? substr($requestData['image2'], 0, 50) . '...' : null,
  328. 'image3_preview' => isset($requestData['image3']) ? substr($requestData['image3'], 0, 50) . '...' : null,
  329. 'request_data_size' => strlen(json_encode($requestData)) . ' bytes'
  330. ];
  331. \think\Log::info('Calling third party AI service: ' . json_encode($logData));
  332. // 发送POST请求
  333. $ch = curl_init();
  334. curl_setopt_array($ch, [
  335. CURLOPT_URL => $apiUrl,
  336. CURLOPT_POST => true,
  337. CURLOPT_POSTFIELDS => json_encode($requestData),
  338. CURLOPT_RETURNTRANSFER => true,
  339. CURLOPT_TIMEOUT => $this->thirdPartyApiConfig['timeout'],
  340. CURLOPT_CONNECTTIMEOUT => $this->thirdPartyApiConfig['connect_timeout'],
  341. CURLOPT_HTTPHEADER => [
  342. 'Content-Type: application/json',
  343. 'Accept: application/json'
  344. ],
  345. CURLOPT_SSL_VERIFYPEER => false,
  346. CURLOPT_SSL_VERIFYHOST => false
  347. ]);
  348. $response = curl_exec($ch);
  349. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  350. $error = curl_error($ch);
  351. curl_close($ch);
  352. if ($error) {
  353. throw new \Exception('请求第三方AI服务失败: ' . $error);
  354. }
  355. // 处理各种HTTP错误状态
  356. if ($httpCode >= 500) {
  357. throw new \Exception('第三方AI服务内部错误: HTTP ' . $httpCode);
  358. } elseif ($httpCode >= 400) {
  359. throw new \Exception('第三方AI服务请求错误: HTTP ' . $httpCode);
  360. } elseif ($httpCode !== 200) {
  361. throw new \Exception('第三方AI服务返回异常状态: HTTP ' . $httpCode);
  362. }
  363. // 检查响应内容
  364. if (empty($response)) {
  365. throw new \Exception('第三方AI服务返回空响应');
  366. }
  367. $result = json_decode($response, true);
  368. if (json_last_error() !== JSON_ERROR_NONE) {
  369. throw new \Exception('第三方AI服务返回数据格式错误');
  370. }
  371. // 记录API响应信息
  372. $responseLog = [
  373. 'http_code' => $httpCode,
  374. 'response_size' => strlen($response) . ' bytes',
  375. 'has_body_size' => isset($result['body_size']),
  376. 'body_size_fields' => isset($result['body_size']) ? array_keys($result['body_size']) : [],
  377. 'response_preview' => substr($response, 0, 200) . '...'
  378. ];
  379. \think\Log::info('Third party AI service response: ' . json_encode($responseLog));
  380. // 处理返回的测量数据
  381. // echo "<pre>";
  382. // print_r($result);
  383. // echo "</pre>";
  384. // exit;
  385. return $this->processMeasurementData($result);
  386. } catch (\Exception $e) {
  387. // 记录错误日志
  388. \think\Log::error('Third party AI service error: ' . $e->getMessage());
  389. throw $e;
  390. }
  391. // 如果执行到这里说明没有异常处理,直接返回处理结果
  392. }
  393. /**
  394. * 安全调用第三方AI服务(带异常处理和默认返回)
  395. */
  396. private function safeCallThirdPartyAiService($photos, $height)
  397. {
  398. try {
  399. return $this->callThirdPartyAiService($photos, $height);
  400. } catch (\Exception $e) {
  401. // 记录错误日志
  402. \think\Log::error('Third party AI service error, returning default data: ' . $e->getMessage());
  403. // 返回默认的空测量数据
  404. return $this->getDefaultMeasurementData();
  405. }
  406. }
  407. /**
  408. * 获取默认的空测量数据
  409. */
  410. private function getDefaultMeasurementData()
  411. {
  412. // 返回所有映射字段的空值(使用空字符串)
  413. return [
  414. 'chest'=>'', // 净胸围 → 胸围
  415. 'waist'=>'', // 净腰围 → 腰围
  416. 'hip'=>'', // 净臀围 → 实际臀围
  417. //'thigh', // 净腿根 → 大腿围
  418. 'knee'=>'', // 净膝围 → 膝围
  419. 'calf'=>'', // 净小腿围 → 小腿围
  420. 'arm_length'=>'', // 净手臂长 → 臂长
  421. 'wrist'=>'', // 净手腕围 → 手腕围
  422. 'pants_length'=>'', // 腿长 → 腿长
  423. 'belly_belt'=>'', // 净肚围 → 肚围
  424. 'shoulder_width'=>'', // 净肩宽 → 肩宽
  425. 'leg_root'=>'', // 净腿根 → 大腿围
  426. 'neck'=>'', // 净颈围 → 颈围
  427. 'inseam'=>'', // 内腿长 → 内腿长
  428. 'upper_arm'=>'', // 净上臂围 → 上臂围
  429. 'ankle'=>'', // 净脚踝围 → 脚踝围
  430. 'waist_lower'=>'', // 净小腹围 → 下腰围
  431. 'mid_waist'=>'', // 净中腰 → 中腰围
  432. '_confidence' => 0.0,
  433. '_warnings' => ['第三方AI服务暂时不可用,返回默认数据']
  434. ];
  435. }
  436. /**
  437. * 测试接口 - 返回模拟的第三方API测量数据
  438. * @ApiMethod (POST)
  439. * @ApiParams (name="profile_id", type="integer", required=true, description="档案ID")
  440. */
  441. public function testMeasurementData()
  442. {
  443. $params = $this->request->post();
  444. // 验证必要参数
  445. if (empty($params['profile_id'])) {
  446. $this->error('档案ID不能为空');
  447. }
  448. // 验证档案归属
  449. $profile = \app\common\model\BodyProfile::where('id', $params['profile_id'])
  450. ->where('user_id', $this->auth->id)
  451. ->find();
  452. if (!$profile) {
  453. $this->error('档案不存在');
  454. }
  455. // 模拟第三方API返回的数据
  456. $mockApiResult = [
  457. "body_size" => [
  458. "datuigen" => 56.973762220946064,
  459. "duwei" => 71.86294164495045,
  460. "jiankuan" => 44.99356951672863,
  461. "jiaohuai" => 20.995062499529606,
  462. "jingwei" => 36.973537078225604,
  463. "neitui" => 67.99506048261769,
  464. "shangbi" => 23.285375591374667,
  465. "shoubichang" => 61.1834335984307,
  466. "shouwanwei" => 16.0697059847192,
  467. "tuichang" => 73.9800462219755,
  468. "tunwei" => 90.08082593388505,
  469. "xiaofu" => 70.98010845587423,
  470. "xiaotuiwei" => 37.2761443409742,
  471. "xigai" => 34.990971006868364,
  472. "xiongwei" => 81.85738385794711,
  473. "yaowei" => 72.93800974219818,
  474. "zhongyao" => 70.99945416888724
  475. ],
  476. "confidence" => 0.85
  477. ];
  478. // 处理测量数据
  479. $measurements = $this->processMeasurementData($mockApiResult);
  480. // 处理结果
  481. $result = [
  482. 'measurements' => $measurements,
  483. 'confidence' => $measurements['_confidence'] ?? 0.8,
  484. 'warnings' => $measurements['_warnings'] ?? []
  485. ];
  486. // 清理内部字段
  487. unset($result['measurements']['_confidence']);
  488. unset($result['measurements']['_warnings']);
  489. // 格式化结果用于展示
  490. $formattedResult = $this->formatMeasurementResult($result, $params['profile_id']);
  491. $this->success('测试数据返回成功', [
  492. 'original_api_data' => $mockApiResult['body_size'],
  493. 'mapped_measurements' => $result['measurements'],
  494. 'formatted_result' => $formattedResult
  495. ]);
  496. }
  497. /**
  498. * 将图片URL转换为base64格式
  499. */
  500. private function convertImageToBase64($imageUrl)
  501. {
  502. try {
  503. // 如果已经是base64格式,直接返回
  504. if (strpos($imageUrl, 'data:image') === 0) {
  505. return $imageUrl;
  506. }
  507. // 如果是相对路径,转换为绝对路径
  508. if (strpos($imageUrl, 'http') !== 0) {
  509. $imageUrl = request()->domain() . $imageUrl;
  510. }
  511. // 获取图片数据
  512. $ch = curl_init();
  513. curl_setopt_array($ch, [
  514. CURLOPT_URL => $imageUrl,
  515. CURLOPT_RETURNTRANSFER => true,
  516. CURLOPT_TIMEOUT => 30,
  517. CURLOPT_CONNECTTIMEOUT => 10,
  518. CURLOPT_FOLLOWLOCATION => true,
  519. CURLOPT_SSL_VERIFYPEER => false,
  520. CURLOPT_SSL_VERIFYHOST => false,
  521. CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36',
  522. CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
  523. CURLOPT_HTTPHEADER => [
  524. 'Accept: image/webp,image/apng,image/*,*/*;q=0.8',
  525. 'Accept-Language: zh-CN,zh;q=0.9,en;q=0.8',
  526. 'Cache-Control: no-cache',
  527. ]
  528. ]);
  529. $imageData = curl_exec($ch);
  530. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  531. $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
  532. $curlError = curl_error($ch);
  533. $effectiveUrl = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
  534. curl_close($ch);
  535. // 详细的错误处理
  536. if ($curlError) {
  537. throw new \Exception("网络请求失败: {$curlError} (URL: {$imageUrl})");
  538. }
  539. if ($httpCode !== 200) {
  540. throw new \Exception("图片访问失败,HTTP状态码: {$httpCode} (URL: {$imageUrl})");
  541. }
  542. if (!$imageData || strlen($imageData) === 0) {
  543. throw new \Exception("图片数据为空 (URL: {$imageUrl})");
  544. }
  545. // 验证图片数据是否有效
  546. if (!@getimagesizefromstring($imageData)) {
  547. throw new \Exception("获取的数据不是有效的图片格式 (URL: {$imageUrl})");
  548. }
  549. // 确定MIME类型
  550. if (strpos($contentType, 'image/') === 0) {
  551. $mimeType = $contentType;
  552. } else {
  553. // 通过文件扩展名推断
  554. $extension = strtolower(pathinfo(parse_url($imageUrl, PHP_URL_PATH), PATHINFO_EXTENSION));
  555. $mimeTypes = [
  556. 'jpg' => 'image/jpeg',
  557. 'jpeg' => 'image/jpeg',
  558. 'png' => 'image/png',
  559. 'gif' => 'image/gif',
  560. 'webp' => 'image/webp',
  561. 'bmp' => 'image/bmp'
  562. ];
  563. $mimeType = $mimeTypes[$extension] ?? 'image/jpeg';
  564. }
  565. // 转换为base64
  566. $base64 = base64_encode($imageData);
  567. return "data:{$mimeType};base64,{$base64}";
  568. } catch (\Exception $e) {
  569. \think\Log::error('Convert image to base64 error: ' . $e->getMessage() . ' URL: ' . $imageUrl);
  570. throw new \Exception('图片转换失败: ' . $e->getMessage());
  571. }
  572. }
  573. /**
  574. * 处理第三方API返回的测量数据
  575. */
  576. private function processMeasurementData($apiResult)
  577. {
  578. // 根据第三方API的返回格式处理数据
  579. // 这里需要根据实际的API返回格式进行调整
  580. $measurements = [];
  581. try {
  582. // 假设API返回格式类似:
  583. // {
  584. // "status": "success",
  585. // "data": {
  586. // "chest": 95.5,
  587. // "waist": 75.2,
  588. // "hip": 98.7,
  589. // ...
  590. // },
  591. // "confidence": 0.85
  592. // }
  593. // 检查返回数据结构 - 可能直接包含body_size字段
  594. if (isset($apiResult['body_size'])) {
  595. $data = $apiResult['body_size'];
  596. $hasValidData = true;
  597. } elseif (isset($apiResult['status']) && $apiResult['status'] === 'success') {
  598. $data = $apiResult['data'] ?? [];
  599. $hasValidData = true;
  600. } else {
  601. // 尝试直接使用返回的数据
  602. $data = $apiResult;
  603. $hasValidData = !empty($data) && is_array($data);
  604. }
  605. if ($hasValidData) {
  606. // 映射字段名(根据第三方API返回的字段名进行映射)
  607. $fieldMapping = [
  608. 'xiongwei' => 'chest', // 净胸围 → 胸围
  609. 'yaowei' => 'waist', // 净腰围 → 腰围
  610. 'tunwei' => 'hip', // 净臀围 → 实际臀围
  611. //'datuigen' => 'thigh', // 净腿根 → 大腿围
  612. 'xigai' => 'knee', // 净膝围 → 膝围
  613. 'xiaotuiwei' => 'calf', // 净小腿围 → 小腿围
  614. 'shoubichang' => 'arm_length', // 净手臂长 → 臂长
  615. 'shouwanwei' => 'wrist', // 净手腕围 → 手腕围
  616. 'tuichang' => 'pants_length', // 腿长 → 腿长
  617. 'duwei' => 'belly_belt', // 净肚围 → 肚围
  618. 'jiankuan' => 'shoulder_width', // 净肩宽 → 肩宽
  619. 'datuigen' => 'leg_root', // 净腿根 → 大腿围
  620. 'jingwei' => 'neck', // 净颈围 → 颈围
  621. 'neitui' => 'inseam', // 内腿长 → 内腿长
  622. 'shangbi' => 'upper_arm', // 净上臂围 → 上臂围
  623. 'jiaohuai' => 'ankle', // 净脚踝围 → 脚踝围
  624. 'xiaofu' => 'waist_lower', // 净小腹围 → 下腰围
  625. 'zhongyao' => 'mid_waist', // 净中腰 → 中腰围
  626. ];
  627. foreach ($fieldMapping as $apiField => $localField) {
  628. if (isset($data[$apiField]) && is_numeric($data[$apiField])) {
  629. $measurements[$localField] = round(floatval($data[$apiField]), 1);
  630. }
  631. }
  632. // 设置置信度和警告信息
  633. $measurements['_confidence'] = $apiResult['confidence'] ?? 0.8;
  634. $measurements['_warnings'] = $apiResult['warnings'] ?? [];
  635. // 如果没有测量数据,添加默认警告
  636. if (count($measurements) === 2) { // 只有_confidence和_warnings
  637. $measurements['_warnings'][] = '第三方AI服务未返回有效的测量数据';
  638. }
  639. } else {
  640. // API返回错误
  641. $errorMsg = $apiResult['message'] ?? $apiResult['error'] ?? '第三方AI服务返回未知错误';
  642. throw new \Exception($errorMsg);
  643. }
  644. } catch (\Exception $e) {
  645. // 处理异常,返回错误信息
  646. $measurements['_confidence'] = 0;
  647. $measurements['_warnings'] = ['数据处理失败: ' . $e->getMessage()];
  648. }
  649. return $measurements;
  650. }
  651. /**
  652. * 处理AI测量任务
  653. */
  654. private function processTask($taskId)
  655. {
  656. try {
  657. // 更新任务状态为处理中
  658. \think\Db::table('fa_ai_measurement_task')
  659. ->where('id', $taskId)
  660. ->update([
  661. 'status' => 1,
  662. 'started_at' => time(),
  663. 'attempts' => \think\Db::raw('attempts + 1'),
  664. 'updatetime' => time()
  665. ]);
  666. // 获取任务详情
  667. $task = \think\Db::table('fa_ai_measurement_task')->where('id', $taskId)->find();
  668. $photos = json_decode($task['photos'], true);
  669. $params = json_decode($task['params'], true);
  670. // 安全调用第三方AI分析服务 - 确保身高为数字格式
  671. $heightCm = is_numeric($params['height']) ? floatval($params['height']) : 0;
  672. $measurements = $this->safeCallThirdPartyAiService(
  673. $photos,
  674. $heightCm
  675. );
  676. // 处理结果
  677. $result = [
  678. 'measurements' => $measurements,
  679. 'confidence' => $measurements['_confidence'] ?? 0.8,
  680. 'warnings' => $measurements['_warnings'] ?? []
  681. ];
  682. // 清理内部字段
  683. unset($result['measurements']['_confidence']);
  684. unset($result['measurements']['_warnings']);
  685. // 更新任务状态为完成
  686. \think\Db::table('fa_ai_measurement_task')
  687. ->where('id', $taskId)
  688. ->update([
  689. 'status' => 2,
  690. 'result' => json_encode($result),
  691. 'completed_at' => time(),
  692. 'updatetime' => time()
  693. ]);
  694. } catch (\Exception $e) {
  695. // 更新任务状态为失败
  696. \think\Db::table('fa_ai_measurement_task')
  697. ->where('id', $taskId)
  698. ->update([
  699. 'status' => 3,
  700. 'error_message' => $e->getMessage(),
  701. 'updatetime' => time()
  702. ]);
  703. }
  704. }
  705. /**
  706. * 估算处理进度
  707. */
  708. private function estimateProgress($task)
  709. {
  710. $startTime = $task['started_at'];
  711. $currentTime = time();
  712. $elapsedTime = $currentTime - $startTime;
  713. // 假设总处理时间为30秒
  714. $totalTime = 30;
  715. $progress = min(95, ($elapsedTime / $totalTime) * 100);
  716. return round($progress);
  717. }
  718. /**
  719. * 格式化测量结果用于展示
  720. */
  721. private function formatMeasurementResult($result, $profileId)
  722. {
  723. $profile = \app\common\model\BodyProfile::find($profileId);
  724. $measurements = $result['measurements'];
  725. // 获取显示配置
  726. $displayConfig = AiMeasurementService::getMeasurementDisplayConfig($profile->gender);
  727. // 格式化数据
  728. $formattedData = [
  729. 'profile' => [
  730. 'id' => $profile->id,
  731. 'name' => $profile->profile_name,
  732. 'gender' => $profile->gender,
  733. 'height' => $profile->height,
  734. 'weight' => $profile->weight
  735. ],
  736. 'measurements' => [],
  737. 'display_config' => $displayConfig,
  738. 'confidence' => $result['confidence'] ?? 0,
  739. 'warnings' => $result['warnings'] ?? []
  740. ];
  741. // 组织测量数据
  742. foreach ($displayConfig as $field => $config) {
  743. $value = isset($measurements[$field]) && $measurements[$field] > 0
  744. ? $measurements[$field]
  745. : null;
  746. $formattedData['measurements'][$field] = [
  747. 'label' => $config['label'],
  748. 'value' => $value,
  749. 'unit' => 'cm',
  750. 'position' => $config['position'],
  751. 'side' => $config['side']
  752. ];
  753. }
  754. // 添加基础数据表格
  755. $formattedData['basic_data'] = [
  756. ['label' => '身高', 'value' => $profile->height, 'unit' => 'cm'],
  757. ['label' => '体重', 'value' => $profile->weight, 'unit' => 'kg'],
  758. ];
  759. // 添加测量数据表格
  760. $tableData = [];
  761. $fields = array_keys($measurements);
  762. $chunks = array_chunk($fields, 2);
  763. foreach ($chunks as $chunk) {
  764. $row = [];
  765. foreach ($chunk as $field) {
  766. if (isset($displayConfig[$field])) {
  767. $row[] = [
  768. 'label' => $displayConfig[$field]['label'],
  769. 'value' => $measurements[$field] ?? null,
  770. 'unit' => 'cm'
  771. ];
  772. }
  773. }
  774. if (!empty($row)) {
  775. $tableData[] = $row;
  776. }
  777. }
  778. $formattedData['table_data'] = $tableData;
  779. return $formattedData;
  780. }
  781. }