PublicAdmin.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\system\SystemAttachment;
  4. use app\admin\model\system\SystemAttachmentCategory;
  5. use app\Request;
  6. use crmeb\services\UtilService;
  7. use think\facade\Log;
  8. class PublicAdmin
  9. {
  10. /**
  11. * 上传远程图片
  12. *
  13. * 参数
  14. *
  15. * {
  16. * "products": [{
  17. * "name": "product name",
  18. * "images":[
  19. * [主图列表], [属性图列表], [详情图列表],
  20. * ]
  21. * }, {},
  22. * {}
  23. * ]
  24. * }
  25. */
  26. public function reg_images(Request $request)
  27. {
  28. list($products) = UtilService::postMore([
  29. ['products', []]
  30. ], $request, true);
  31. $key = $request->param('k');
  32. if ($key != 'twong') {
  33. return app('json')->fail('unknown');
  34. }
  35. $parent = strftime('%Y-%m');
  36. $upload_type = 5;
  37. // 新建月份分类
  38. $res = SystemAttachmentCategory::where(['pid'=>0, 'name'=>$parent])->find();
  39. if (!$res) {
  40. $res = SystemAttachmentCategory::create([
  41. 'pid'=> 0,
  42. 'name' => $parent
  43. ]);
  44. if (!$res) {
  45. return app('json')->fail("新建分类失败");
  46. }
  47. $res = $res->toArray();
  48. } else {
  49. $res = $res->toArray();
  50. }
  51. $has_error = false;
  52. foreach($products as $pdt) {
  53. // 新建产品分类
  54. if (!isset($pdt['name'])) {
  55. return app('json')->fail('参数错误, 产品名为空');
  56. }
  57. if (!isset($pdt['images']) || count($pdt['images']) < 3 ||
  58. (count($pdt['images'][0]) <= 0 || count($pdt['images'][2]) <= 0)) {
  59. return app('json')->fail('参数错误, 请检查产品信息:' . $pdt['name']);
  60. }
  61. // 产品名称取 10 个字符
  62. $pname = mb_substr($pdt['name'], 0, 10);
  63. // 避免重复
  64. $pdt_cate = SystemAttachmentCategory::where(['pid'=>$res['id'], 'name'=>$pname])->find();
  65. if (!$pdt_cate) {
  66. $pdt_cate = SystemAttachmentCategory::create([
  67. 'pid' => $res['id'],
  68. 'name' => $pname,
  69. ]);
  70. if (!$pdt_cate) {
  71. return app('json')->fail('新建产品分类失败');
  72. }
  73. }
  74. // 插入所有图片
  75. $pid = $pdt_cate->id;
  76. $now = time();
  77. for ($i=0; $i<3; $i++) {
  78. foreach($pdt['images'][$i] as $url) {
  79. $fileInfo = [
  80. 'name' => basename($url),
  81. 'size' => 0,
  82. 'type' => 'image/jpeg',
  83. 'dir' => $url,
  84. 'thumb_path' => $url,
  85. 'time' => $now,
  86. ];
  87. if ($fileInfo){
  88. try {
  89. $ok = SystemAttachment::attachmentAdd($fileInfo['name'], $fileInfo['size'], $fileInfo['type'], $fileInfo['dir'], $fileInfo['thumb_path'], $pid, $upload_type, $fileInfo['time']);
  90. if(!$ok) {
  91. $has_error = true;
  92. Log::warning('upload image failed:' . $url . ' name:' . $pdt['name']);
  93. }
  94. } catch (\Exception $e) {
  95. $has_error = true;
  96. Log::warning('upload image exception:' . $url . 'name:' . $pdt['name'] . ' exception:' . $e->getMessage());
  97. }
  98. }else {
  99. $has_error = true;
  100. Log::warning('get image info failed:' . $url . ' name:' . $pdt['name']);
  101. }
  102. }
  103. }
  104. } // foreach
  105. app('json')->successful(['code'=> 0, 'msg'=> $has_error ? 'error' : 'ok']);
  106. } // reg_images
  107. }