ProductController.php 3.3 KB

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