| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <?php
- /**
- * ThinkPHP5 文件上传控制器
- * 图片上传接口
- */
- namespace app\api\controller;
- use app\api\controller\Common;
- use think\Controller;
- use think\Request;
- use think\facade\Env;
- class ProductController extends Controller
- {
- /**
- * 图片上传接口
- * POST /api/product/uploadImage
- *
- * @return \think\response\Json
- */
- public function uploadImage()
- {
- $file = $this->request->file('file');
- if (empty($file)) {
- return json([
- 'success' => false,
- 'message' => '没有接收到文件'
- ]);
- }
- if (is_array($file)) {
- $file = $file[0];
- }
- $originalName = $file->getInfo('name');
- $suffix = strtolower(pathinfo($originalName, PATHINFO_EXTENSION));
- $allowedSuffix = ['jpg', 'png', 'gif', 'jpeg', 'webp'];
- if (!in_array($suffix, $allowedSuffix)) {
- return json([
- 'success' => false,
- 'message' => '不支持的图片格式'
- ]);
- }
- $fileSize = $file->getSize();
- if ($fileSize > 10485760) {
- return json([
- 'success' => false,
- 'message' => '文件大小超过10MB限制'
- ]);
- }
- $year = date('Y');
- $month = date('m');
- $day = date('d');
- $uploadPath = ROOT_PATH . 'public' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . $year . $month . $day . DIRECTORY_SEPARATOR;
- if (!is_dir($uploadPath)) {
- mkdir($uploadPath, 0755, true);
- }
- $fileName = time() . '.' . $suffix;
- $info = $file->move($uploadPath, $fileName);
- if ($info) {
- $relativePath = 'uploads/' . $year . $month . $day . '/' . $fileName;
- // $fileUrl = $this->request->domain() . '/' . str_replace('\\', '/', $relativePath);
- return json([
- 'success' => true,
- 'message' => '上传成功',
- 'data' => [
- // 'url' => $fileUrl
- 'url' => $relativePath
- ]
- ]);
- } else {
- return json([
- 'success' => false,
- 'message' => $file->getError()
- ]);
- }
- }
-
- /**
- * 添加产品接口(示例)
- * POST /api/product/productAdd
- *
- * @return \think\response\Json
- */
- public function productAdd()
- {
- $data = $this->request->param();
-
- if (empty($data['code'])) {
- return json([
- 'success' => false,
- 'message' => '产品编码不能为空'
- ]);
- }
- $productImgPath = '';
- if (!empty($data['image'])) {
- $base64Data = $data['image'];
- if (preg_match('/data:image\/(png|jpg|jpeg);base64,([A-Za-z0-9+\/=]+)/i', $base64Data, $m)) {
- $imageType = strtolower($m[1]);
- $imageData = base64_decode($m[2]);
- if ($imageData !== false && strlen($imageData) >= 100) {
- $productCode = $data['code'];
- $prefix = strlen($productCode) >= 4 ? substr($productCode, 0, -4) : $productCode;
- $safeName = preg_replace('/[\\\\\/:*?"<>|]/u', '_', $data['name']);
- $ext = ($imageType === 'jpeg') ? 'jpg' : $imageType;
- $fileName = $safeName . '.' . $ext;
- $saveDir = str_replace('\\', '/', ROOT_PATH . 'public/uploads/merchant/' . $prefix . '/' . $productCode . '/oldimg/');
- if (!is_dir($saveDir)) {
- mkdir($saveDir, 0755, true);
- }
- if (file_put_contents($saveDir . $fileName, $imageData)) {
- $productImgPath = 'uploads/merchant/' . $prefix . '/' . $productCode . '/oldimg/' . $fileName;
- Common::uploadLocalFileToOss($saveDir . $fileName, $productImgPath);
- }
- }
- }
- }
-
- $res = [
- 'product_name' => $data['name'],
- 'product_code' => $data['code'],
- 'product_img' => $productImgPath,
- 'createTime' => date('Y-m-d H:i:s'),
- 'create_name' => '系统摄像头',
- ];
-
- $insertResult = db('product')->insert($res);
- if (!$insertResult) {
- return json([
- 'success' => false,
- 'message' => '添加失败'
- ]);
- }
-
- return json([
- 'success' => true,
- 'message' => '添加成功',
- 'data' => $data
- ]);
- }
- }
- ?>
|