Browse Source

增加antgoods逻辑层logic,删除修改attribute表的type字段数据迁移文件,创建antgoods模块的商品逻辑文件,在文件模块逻辑文件中统一type字段内容,在商品逻辑文件中开发将临时文件保存的文件中的方法

wechat_public_accounts
linyaostalker 5 years ago
parent
commit
6ea8813274
  1. 1
      backend/config/main.php
  2. 17
      backend/logic/file/FileManager.php
  3. 2
      common/models/ars/File.php
  4. 6
      common/models/ars/TemFile.php
  5. 5
      vendor/antgoods/goods/src/controllers/GoodsController.php
  6. 70
      vendor/antgoods/goods/src/logic/goods/GoodsManager.php
  7. 28
      vendor/antgoods/goods/src/migrations/m191121_111825_update_column_type_in_table_antgoods_attribute.php
  8. 7
      vendor/antgoods/goods/src/models/ars/Goods.php
  9. 2
      vendor/antgoods/goods/src/views/goods/_form.php

1
backend/config/main.php

@ -56,6 +56,7 @@ return [
],
],
'file' => ['class' => 'backend\logic\file\FileManager'],
'goods' => ['class' => 'antgoods\goods\logic\goods\GoodsManager'],
],
'params' => $params,
];

17
backend/logic/file/FileManager.php

@ -7,12 +7,19 @@ use common\models\ars\TemFile;
class FileManager
{
//数据表ats_file和ats_tem_file的类型字段type
const TYPE_IMAGE = 1;//图片
const TYPE_VIDEO = 2;//影视
const TYPE_EXCEL = 3;//excel表单
const TYPE_WORD = 4;//word文本
const TYPE_TXT = 5;//txt文本
public static $extension = [
TemFile::TYPE_IMAGE => ['jpg', 'png', 'jpeg'],
TemFile::TYPE_VIDEO => ['mp4'],
TemFile::TYPE_EXCEL => [],
TemFile::TYPE_WORD => ['docx'],
TemFile::TYPE_TXT => ['txt'],
self::TYPE_IMAGE => ['jpg', 'png', 'jpeg'],
self::TYPE_VIDEO => ['mp4'],
self::TYPE_EXCEL => [],
self::TYPE_WORD => ['docx'],
self::TYPE_TXT => ['txt'],
];
/**

2
common/models/ars/File.php

@ -22,6 +22,8 @@ use yii\behaviors\TimestampBehavior;
*/
class File extends \yii\db\ActiveRecord
{
const OWN_TYPE_GOODS_INDEX = 1;//商品首页
const OWN_TYPE_GOODS_DETAILS = 2;//商品详情
/**
* {@inheritdoc}
*/

6
common/models/ars/TemFile.php

@ -19,12 +19,6 @@ use yii\behaviors\TimestampBehavior;
*/
class TemFile extends \yii\db\ActiveRecord
{
//类型type
const TYPE_IMAGE = 1;//图片
const TYPE_VIDEO = 2;//影视
const TYPE_EXCEL = 3;//excel表单
const TYPE_WORD = 4;//word文本
const TYPE_TXT = 5;//txt文本
/**
* {@inheritdoc}

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

@ -11,6 +11,7 @@ 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.
@ -81,9 +82,7 @@ class GoodsController extends Controller
{
$model = new Goods();
if ($model->load(Yii::$app->request->post())) {
$img_id_arr = explode(',', $model->image);
$model->save();
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect('index');
}

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

@ -0,0 +1,70 @@
<?php
namespace antgoods\goods\logic\goods;
use common\models\ars\TemFile;
use common\models\ars\File;
class GoodsManager
{
/**
* @param $imgIdStr
* @param $ownId
* @param $ownType
* @return array
* 将临时文件保存在文件中
*/
public function saveTemFileToFile($imgIdStr, $ownId, $ownType)
{
if(!$imgIdStr || !$ownId) {
return ['status' => false, 'info' => '参数错误'];
}
$img_id_arr = explode(',', $imgIdStr);
$i = 1;
$first_file_id = 0;
foreach ($img_id_arr as $key => $value) {
$tem_file = TemFile::findOne($key);
if(!$tem_file) {
return ['status' => false, 'info' => '存在查找不到的文件'];
}
$res = self::saveNewFile($tem_file, $ownId, $ownType);
if(!$res['status']) {
return ['status' => false, 'info' => '存在文件保存失败'];
}
if($i == 1) {
$first_file_id = $res['file_id'];
}
$i++;
}
return ['status' => true, 'info' => '保存成功', 'first_file_id' => $first_file_id];
}
/**
* @param $temFile
* @param $ownId
* @param $ownType
* @return bool
* 创建新的文件
*/
private function saveNewFile($temFile, $ownId, $ownType)
{
$new_file = new File();
$new_file->name = $temFile->name;
$new_file->type = $temFile->type;
$new_file->own_id = $ownId;
$new_file->own_type = $ownType;
$new_file->alias = $temFile->alias;
$new_file->path = $temFile->path;
if($new_file->save()) {
return ['status' => true, 'file_id' => $new_file->id];
} else {
return ['status' => false];
}
}
}

28
vendor/antgoods/goods/src/migrations/m191121_111825_update_column_type_in_table_antgoods_attribute.php

@ -1,28 +0,0 @@
<?php
use yii\db\Migration;
/**
* Class m191121_111825_update_column_type_in_table_antgoods_attribute
*/
class m191121_111825_update_column_type_in_table_antgoods_attribute extends Migration
{
/**
* {@inheritdoc}
*/
public function up()
{
$this->dropColumn('antgoods_attribute', 'type');
$this->addColumn('antgoods_attribute', 'type', $this->smallInteger(2)->notNull()->defaultValue(1)->comment('类型:1:商品sku属性'));
}
/**
* {@inheritdoc}
*/
public function down()
{
$this->dropColumn('antgoods_attribute', 'type');
$this->addColumn('antgoods_attribute', 'type', $this->smallInteger(2)->defaultValue(null)->comment('类型'));
return true;
}
}

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

@ -45,6 +45,7 @@ use yii\behaviors\TimestampBehavior;
class Goods extends \yii\db\ActiveRecord
{
public $imagePath;
public $imageId;
//是否删除is_delete
const IS_DELETE_NO = 0;//未删除
const IS_DELETE_YES = 1;//已删除
@ -176,4 +177,10 @@ class Goods extends \yii\db\ActiveRecord
}
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
}
}

2
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, 'image')->hiddenInput()->label('') ?>
<?= $form->field($model, 'imageId')->hiddenInput()->label('') ?>
<?= $form->field($model, 'imagePath')->widget(\iron\widgets\Upload::className(), [
'url' => 'upload',
'deleteUrl' => 'imageDel',

Loading…
Cancel
Save