PublicAdmin.php 3.9 KB

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