Browse Source

开发创建商品时保存图片操作

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
a90a4c2a88
  1. 24
      vendor/antgoods/goods/src/controllers/GoodsController.php
  2. 22
      vendor/antgoods/goods/src/logic/goods/GoodsManager.php
  3. 8
      vendor/antgoods/goods/src/models/ars/Goods.php
  4. 12
      vendor/antgoods/goods/src/views/goods/_form.php

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

@ -2,16 +2,13 @@
namespace antgoods\goods\controllers; namespace antgoods\goods\controllers;
use common\models\ars\TemFile;
use Yii; use Yii;
use antgoods\goods\models\ars\Goods; use antgoods\goods\models\ars\Goods;
use antgoods\goods\models\searchs\GoodsSearch; use antgoods\goods\models\searchs\GoodsSearch;
use yii\web\Controller; use yii\web\Controller;
use yii\web\NotFoundHttpException; use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter; use yii\filters\VerbFilter;
use common\models\ars\TemFile;
use common\models\ars\File;
use backend\logic\file\FileManager;
use antgoods\goods\logic\goods\GoodsManager;
/** /**
* GoodsController implements the CRUD actions for Goods model. * GoodsController implements the CRUD actions for Goods model.
@ -82,7 +79,8 @@ class GoodsController extends Controller
{ {
$model = new Goods(); $model = new Goods();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
if ($model->load(Yii::$app->request->post())) {
$model->save();
return $this->redirect('index'); return $this->redirect('index');
} }
@ -167,14 +165,19 @@ class GoodsController extends Controller
*/ */
public function actionSaveFile() public function actionSaveFile()
{ {
if(!class_exists('\common\models\ars\TemFile') || !class_exists('\backend\logic\file\FileManager')){
return '';
}
$data = Yii::$app->request->get('data'); $data = Yii::$app->request->get('data');
$file_name = Yii::$app->request->get('fileName')[0]; $file_name = Yii::$app->request->get('fileName')[0];
if ($data['status'] == true) { if ($data['status'] == true) {
$model = new TemFile();
$model = new \common\models\ars\TemFile();
$model->user_id = Yii::$app->user->identity->id; $model->user_id = Yii::$app->user->identity->id;
$model->name = $file_name; $model->name = $file_name;
$type = Yii::$app->file->searchType(FileManager::$extension, pathinfo($data['path'])['extension']);
$file_manager = new \backend\logic\file\FileManager();
$type = $file_manager->searchType(\backend\logic\file\FileManager::$extension, pathinfo($data['path'])['extension']);
if ($type != -1) { if ($type != -1) {
$model->type = $type; $model->type = $type;
} }
@ -191,10 +194,15 @@ class GoodsController extends Controller
*/ */
public function actionImgIdDel() public function actionImgIdDel()
{ {
//判断该类是否存在
if(!class_exists('\common\models\ars\TemFile')){
return '';
}
$alias = Yii::$app->request->get('data')['alias']; $alias = Yii::$app->request->get('data')['alias'];
$img_id = Yii::$app->request->get('imgid'); $img_id = Yii::$app->request->get('imgid');
$img_id_arr = explode(',', $img_id); $img_id_arr = explode(',', $img_id);
$tem_file = TemFile::findOne(['alias' => $alias]);
$tem_file = \common\models\ars\TemFile::findOne(['alias' => $alias]);
if ($tem_file) { if ($tem_file) {
$img_id_arr = array_diff($img_id_arr, [$tem_file->id]); $img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
} }

22
vendor/antgoods/goods/src/logic/goods/GoodsManager.php

@ -6,5 +6,27 @@ namespace antgoods\goods\logic\goods;
class GoodsManager class GoodsManager
{ {
/**
* @param $fileIdStr
* @param $goodsModel
* @param int $filetype
* @return bool
* 判断file逻辑文件FileManager和file模型文件是否存在,若存在,则执行文件保存操作
*/
public function saveFile($fileIdStr, $goodsModel, $filetype = 1)
{
if(class_exists('\backend\logic\file\FileManager') || class_exists('\common\models\ars\File')) {
return false;
}
$class = new \backend\logic\file\FileManager();
$res = $class->saveTemFileToFile($fileIdStr, $goodsModel->id, $filetype);
if($res['status'] || $filetype == \common\models\ars\File::OWN_TYPE_GOODS_INDEX) {
$goodsModel->image = $res['first_file_id'];
$goodsModel->save();
}
return true;
}
} }

8
vendor/antgoods/goods/src/models/ars/Goods.php

@ -73,7 +73,7 @@ class Goods extends \yii\db\ActiveRecord
[['pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'market_price', 'price', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template'], 'integer'], [['pid', 'cat_id', 'brand_id', 'shop_cat_id', 'supplier_id', 'weight', 'length', 'width', 'height', 'diameter', 'sold_count', 'limit_count', 'stock', 'stock_warn', 'market_price', 'price', 'image', 'model_id', 'is_sale', 'sort_order', 'bouns_points', 'experience_points', 'is_delete', 'express_template'], 'integer'],
[['cat_id', 'brand_id', 'shop_cat_id', 'name'], 'required'], [['cat_id', 'brand_id', 'shop_cat_id', 'name'], 'required'],
[['sn'], 'checkExist'], [['sn'], 'checkExist'],
[['description'], 'string'],
[['description', 'imageId'], 'string'],
[['name'], 'string', 'max' => 120], [['name'], 'string', 'max' => 120],
[['sn'], 'string', 'max' => 60], [['sn'], 'string', 'max' => 60],
[['code'], 'string', 'max' => 50], [['code'], 'string', 'max' => 50],
@ -177,10 +177,4 @@ class Goods extends \yii\db\ActiveRecord
} }
return parent::beforeSave($insert); // TODO: Change the autogenerated stub return parent::beforeSave($insert); // TODO: Change the autogenerated stub
} }
public function afterSave($insert, $changedAttributes)
{
d($this->imageId);die();
parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
}
} }

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

@ -68,7 +68,7 @@ use antgoods\goods\models\ars\Supplier;
<?= $form->field($model, 'express_template')->textInput() ?> <?= $form->field($model, 'express_template')->textInput() ?>
<?= $form->field($model, 'imageId')->hiddenInput()->label('') ?>
<?= $form->field($model, 'imageId')->textInput()->label('') ?>
<?= $form->field($model, 'imagePath')->widget(\iron\widgets\Upload::className(), [ <?= $form->field($model, 'imagePath')->widget(\iron\widgets\Upload::className(), [
'url' => 'upload', 'url' => 'upload',
'deleteUrl' => 'imageDel', 'deleteUrl' => 'imageDel',
@ -78,21 +78,21 @@ use antgoods\goods\models\ars\Supplier;
'successScript' => " 'successScript' => "
success: function(data) success: function(data)
{ {
var imageval = $('#goods-image').val();
var imageval = $('#goods-imageid').val();
if(imageval == ''){ if(imageval == ''){
$('#goods-image').val(data);
$('#goods-imageid').val(data);
}else{ }else{
$('#goods-image').val($('#goods-image').val()+','+data);
$('#goods-imageid').val($('#goods-imageid').val()+','+data);
} }
} }
", ",
'deleteScript' => " 'deleteScript' => "
$.ajax({ $.ajax({
url: 'img-id-del', url: 'img-id-del',
data: {data: data, imgid: $('#goods-image').val()},
data: {data: data, imgid: $('#goods-imageid').val()},
success: function(data) success: function(data)
{ {
$('#goods-image').val(data);
$('#goods-imageid').val(data);
}, },
}); });
", ",

Loading…
Cancel
Save