AiMeasurement.php 33 KB

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