ProductController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * ThinkPHP5 文件上传控制器
  4. * 图片上传接口
  5. */
  6. namespace app\api\controller;
  7. use app\api\controller\Common;
  8. use think\Controller;
  9. use think\Request;
  10. use think\facade\Env;
  11. class ProductController extends Controller
  12. {
  13. /**
  14. * 图片上传接口
  15. * POST /api/product/uploadImage
  16. *
  17. * @return \think\response\Json
  18. */
  19. public function uploadImage()
  20. {
  21. $file = $this->request->file('file');
  22. if (empty($file)) {
  23. return json([
  24. 'success' => false,
  25. 'message' => '没有接收到文件'
  26. ]);
  27. }
  28. if (is_array($file)) {
  29. $file = $file[0];
  30. }
  31. $originalName = $file->getInfo('name');
  32. $suffix = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
  33. $allowedSuffix = ['jpg', 'png', 'gif', 'jpeg', 'webp'];
  34. if (!in_array($suffix, $allowedSuffix)) {
  35. return json([
  36. 'success' => false,
  37. 'message' => '不支持的图片格式'
  38. ]);
  39. }
  40. $fileSize = $file->getSize();
  41. if ($fileSize > 10485760) {
  42. return json([
  43. 'success' => false,
  44. 'message' => '文件大小超过10MB限制'
  45. ]);
  46. }
  47. $year = date('Y');
  48. $month = date('m');
  49. $day = date('d');
  50. $uploadPath = ROOT_PATH . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $year . $month . $day . DIRECTORY_SEPARATOR;
  51. if (!is_dir($uploadPath)) {
  52. mkdir($uploadPath, 0755, true);
  53. }
  54. $fileName = time() . '.' . $suffix;
  55. $info = $file->move($uploadPath, $fileName);
  56. if ($info) {
  57. $relativePath = 'uploads/' . $year . $month . $day . '/' . $fileName;
  58. // $fileUrl = $this->request->domain() . '/' . str_replace('\\', '/', $relativePath);
  59. return json([
  60. 'success' => true,
  61. 'message' => '上传成功',
  62. 'data' => [
  63. // 'url' => $fileUrl
  64. 'url' => $relativePath
  65. ]
  66. ]);
  67. } else {
  68. return json([
  69. 'success' => false,
  70. 'message' => $file->getError()
  71. ]);
  72. }
  73. }
  74. /**
  75. * 添加产品接口(示例)
  76. * POST /api/product/productAdd
  77. *
  78. * @return \think\response\Json
  79. */
  80. public function productAdd()
  81. {
  82. $data = $this->request->param();
  83. if (empty($data['code'])) {
  84. return json([
  85. 'success' => false,
  86. 'message' => '产品编码不能为空'
  87. ]);
  88. }
  89. $productImgPath = '';
  90. if (!empty($data['image'])) {
  91. $base64Data = $data['image'];
  92. if (preg_match('/data:image\/(png|jpg|jpeg);base64,([A-Za-z0-9+\/=]+)/i', $base64Data, $m)) {
  93. $imageType = strtolower($m[1]);
  94. $imageData = base64_decode($m[2]);
  95. if ($imageData !== false && strlen($imageData) >= 100) {
  96. $productCode = $data['code'];
  97. $prefix = strlen($productCode) >= 4 ? substr($productCode, 0, -4) : $productCode;
  98. $safeName = preg_replace('/[\\\\\/:*?"<>|]/u', '_', $data['name']);
  99. $ext = ($imageType === 'jpeg') ? 'jpg' : $imageType;
  100. $fileName = $safeName . '.' . $ext;
  101. $saveDir = str_replace('\\', '/', ROOT_PATH . 'public/uploads/merchant/' . $prefix . '/' . $productCode . '/oldimg/');
  102. if (!is_dir($saveDir)) {
  103. mkdir($saveDir, 0755, true);
  104. }
  105. if (file_put_contents($saveDir . $fileName, $imageData)) {
  106. $productImgPath = 'uploads/merchant/' . $prefix . '/' . $productCode . '/oldimg/' . $fileName;
  107. Common::uploadLocalFileToOss($saveDir . $fileName, $productImgPath);
  108. }
  109. }
  110. }
  111. }
  112. $res = [
  113. 'product_name' => $data['name'],
  114. 'product_code' => $data['code'],
  115. 'product_img' => $productImgPath,
  116. 'createTime' => date('Y-m-d H:i:s'),
  117. 'create_name' => '系统摄像头',
  118. ];
  119. $insertResult = db('product')->insert($res);
  120. if (!$insertResult) {
  121. return json([
  122. 'success' => false,
  123. 'message' => '添加失败'
  124. ]);
  125. }
  126. return json([
  127. 'success' => true,
  128. 'message' => '添加成功',
  129. 'data' => $data
  130. ]);
  131. }
  132. }
  133. ?>