ImportAlbum.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace App\Admin\Forms;
  3. use App\Admin\Repositories\BaseProductCategory;
  4. use App\Admin\Repositories\RpcAlbum;
  5. use App\Libraries\CommonHelper;
  6. use App\Models\BaseProduct;
  7. use App\Distributor\Repositories\DistProductCategory;
  8. use App\Models\BaseProductImage;
  9. use App\Models\DistProduct;
  10. use App\Models\DistProductImage;
  11. use Dcat\Admin\Widgets\Form;
  12. class ImportAlbum extends Form
  13. {
  14. /**
  15. * Handle the form request.
  16. *
  17. * @param array $input
  18. *
  19. * @return mixed
  20. */
  21. public function handle(array $input)
  22. {
  23. $albumIds = explode(',', $input['album_ids']);
  24. $categoryId = $input['category_id'];
  25. // 检查 product_ids 是否为空
  26. if (empty($input['album_ids'])) {
  27. return $this
  28. ->response()
  29. ->error('请选择要导入的产品');
  30. }
  31. try {
  32. //RPC读取相册
  33. $rpcAlbum = new RpcAlbum();
  34. $albumResult = $rpcAlbum->getByids($albumIds);
  35. if ($albumResult['status']= false) {
  36. return $this
  37. ->response()
  38. ->error('RPC获取相册失败:'. $albumResult['msg']);
  39. }
  40. foreach ($albumResult['data'] as $item) {
  41. $title = isset($item['title']) ? $item['title'] : $item['model'];
  42. // 创建新的 DistProduct 记录
  43. $distProduct = BaseProduct::create([
  44. 'category_id' => $categoryId,
  45. 'title' => $title,
  46. 'sku' => $item['model'], // 假设 $baseProduct 也有 sku 字段
  47. 'issuance_date' => null, // 假设 $baseProduct 也有 issuance_date 字段
  48. 'order' => 0, // 假设 $baseProduct 也有 order 字段
  49. 'enabled' => 1, // 假设 $baseProduct 也有 enabled 字段
  50. 'content' => $this->changeToimagesHTML($item['en_detail']), // 假设 $baseProduct 也有 content 字段
  51. 'parameters' => $item['parameters'], // 假设 $baseProduct 也有 parameters 字段
  52. 'seo_title'=> $title,
  53. 'seo_keywords' => '',
  54. 'seo_description' => '',
  55. 'created_at' => now(), // 自动填充创建时间
  56. 'updated_at' => now(), // 自动填充更新时间
  57. ]);
  58. //DistProduct::where('id', $distProduct->id)->update(['slug' => $distProduct->id]);
  59. // 遍历 base_product_image 表中的记录,并插入到 dist_product_image 表中
  60. $cover = json_decode($item['cover'], true);
  61. foreach ($cover as $baseImage) {
  62. $i = 1;
  63. BaseProductImage::create([
  64. 'image_url' => $baseImage,
  65. 'product_id' => $distProduct->id, // 使用新创建的 DistProduct 的 ID
  66. 'order' => $i,
  67. 'created_at' => now(), // 自动填充创建时间
  68. 'updated_at' => now(), // 自动填充更新时间
  69. ]);
  70. $i++;
  71. }
  72. }
  73. return $this
  74. ->response()
  75. ->success('导入成功')
  76. ->refresh();
  77. } catch (\Exception $e) {
  78. throw $e;
  79. return $this
  80. ->response()
  81. ->error('导入失败: ' . $e->getMessage());
  82. }
  83. }
  84. public function changeToimagesHTML($content) {
  85. $content = json_decode($content, true);
  86. $html = '';
  87. foreach ($content as $item) {
  88. $item = CommonHelper::ossUrl($item);
  89. $html.= '<img src="'. $item. '">';
  90. }
  91. return $html;
  92. }
  93. /**
  94. * Build a form here.
  95. */
  96. public function form()
  97. {
  98. // 设置隐藏表单,传递用户id
  99. $this->select('category_id', admin_trans_label('category_name'))
  100. ->options(BaseProductCategory::selectOptions())
  101. ->required();
  102. $this->hidden('album_ids')->attribute('id', 'album_ids');
  103. }
  104. /**
  105. * The data of the form.
  106. *
  107. * @return array
  108. */
  109. public function default()
  110. {
  111. return [];
  112. }
  113. }