Browse Source

处理商品图片上传回调函数逻辑

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
30112194d4
  1. 28
      vendor/antgoods/goods/src/controllers/GoodsController.php
  2. 13
      vendor/antgoods/goods/src/views/goods/_form.php
  3. 2
      vendor/iron/actions/UploadAction.php

28
vendor/antgoods/goods/src/controllers/GoodsController.php

@ -8,6 +8,9 @@ use antgoods\goods\models\searchs\GoodsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use common\models\ars\TemFile;
use common\models\ars\File;
use backend\logic\file\FileManager;
/**
* GoodsController implements the CRUD actions for Goods model.
@ -34,7 +37,8 @@ class GoodsController extends Controller
return [
'upload' => [
'class' => 'iron\actions\UploadAction',
'path' => 'xls/'
'path' => 'xls/',
'maxSize' => 20480,
]
];
}
@ -156,4 +160,26 @@ class GoodsController extends Controller
'columns' => $searchModel->columns()
]);
}
/**
* 处理文件上传成功后回调保存到临时文件表中
*/
public function actionSavefile()
{
$data = Yii::$app->request->get('data');
$fileName = Yii::$app->request->get('fileName')[0];
if ($data['status'] == true) {
$model = new TemFile();
$model->user_id = Yii::$app->user->identity->id;
$model->name = $fileName;
$type = Yii::$app->file->searchType(FileManager::$extension, pathinfo($data['path'])['extension']);
if ($type != -1) {
$model->type = $type;
}
$model->alias = $data['alias'];
$model->path = $data['path'];
$model->save();
}
}
}

13
vendor/antgoods/goods/src/views/goods/_form.php

@ -52,12 +52,6 @@ use antgoods\goods\models\ars\Supplier;
<?= $form->field($model, 'price')->textInput() ?>
<?= $form->field($model, 'image')->widget(\iron\widgets\Upload::className(), [
'url' => 'upload',
'deleteUrl' => 'imageDel',
'dragdropWidth'=> 800
]) ?>
<?= $form->field($model, 'brief')->textInput(['maxlength' => true]) ?>
<?= $form->field($model, 'description')->textarea(['rows' => 6]) ?>
@ -74,6 +68,13 @@ use antgoods\goods\models\ars\Supplier;
<?= $form->field($model, 'express_template')->textInput() ?>
<?= $form->field($model, 'image')->widget(\iron\widgets\Upload::className(), [
'url' => 'upload',
'deleteUrl' => 'imageDel',
'dragdropWidth'=> 800,
'afterSave' => 'savefile'
]) ?>
<div class="form-group">
<?= Html::submitButton('保存', ['class' => 'btn btn-success']) ?>
<?= Html::a('返回', ['index'], ['class' => 'btn btn-info']) ?>

2
vendor/iron/actions/UploadAction.php

@ -119,7 +119,7 @@ class UploadAction extends Action
// $filePath = "$savePath/$fileName.{$fileExtension}";
// file_put_contents($filePath, $bin);
}
return ['status' => true, 'path' => "uploads/$this->path$fileName.{$file->extension}"];
return ['status' => true, 'path' => "uploads/$this->path$fileName.{$file->extension}", 'alias' => $fileName . '.' . $file->extension];
} catch (\Error $e) {
throw new ServerErrorHttpException($e->getMessage());
// return ['status' => false, 'info' => $e->getMessage()];

Loading…
Cancel
Save