You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

310 lines
10 KiB

<?php
namespace backend\modules\goods\controllers;
use backend\modules\file\logic\file\FileManager;
use backend\modules\goods\models\ars\FilterAttr;
use backend\modules\goods\models\ars\GoodsAttr;
use backend\modules\shop\logic\ShopManager;
use Exception;
use Throwable;
use Yii;
use backend\modules\goods\models\ars\Goods;
use backend\modules\goods\models\searchs\GoodsSearch;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use backend\modules\goods\logic\goods\GoodsManager;
use backend\modules\file\models\ars\File;
use backend\modules\goods\models\ars\Attribute;
use iron\widget\Excel;
/**
* GoodsController implements the CRUD actions for Goods model.
*/
class GoodsController extends Controller
{
/**
* {@inheritdoc}
*/
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => ['POST'],
],
],
];
}
public function actions()
{
return [
'upload' => [
'class' => 'iron\actions\UploadAction',
'path' => 'xls/',
'maxSize' => 20480,
],
'ueditor' => [
'class' => 'common\widgets\ueditor\UeditorAction',
'config' => [
//上传图片配置
'imageUrlPrefix' => "", /* 图片访问路径前缀 */
'imagePathFormat' => "/uploads/origin/introduce/" . md5(time() . rand(000, 999)), /* 上传保存路径,可以自定义保存路径和文件名格式 */
]
],
];
}
/**
* Lists all Goods models.
* @return mixed
*/
public function actionIndex()
{
$searchModel = new GoodsSearch();
$dataProvider = $searchModel->search(Yii::$app->request->queryParams);
return $this->render('index', [
'searchModel' => $searchModel,
'dataProvider' => $dataProvider,
'columns' => $searchModel->columns()
]);
}
/**
* Displays a single Goods model.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionView($id)
{
return $this->render('view', [
'model' => $this->findModel($id),
]);
}
/**
* Creates a new Goods model.
* If creation is successful, the browser will be redirected to the 'view' page.
* @return mixed
* @throws Exception
*/
public function actionCreate()
{
$model = new Goods();
$model->is_sale = Goods::IS_SALE_YES;
$model->stock = -1;
$model->is_taking = Goods::IS_TAKING_NO;
$model->is_express = Goods::IS_EXPRESS_YES;
$model->express_type = Goods::EXPRESS_TYPE_EXPRESS_TEMPLATE;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//商品封面图和商品详情图上传保存处理
$res = GoodsManager::updateGoods(Yii::$app->request->post(), $model);
if ($res) {
return $this->redirect('index');
}
}
return $this->render('create', [
'model' => $model,
]);
}
/**
* Updates an existing Goods model.
* If update is successful, the browser will be redirected to the 'view' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
* @throws Exception
*/
public function actionUpdate($id)
{
$model = $this->findModel($id);
$model->coverImageId = $model->image;
$model->detailImageId = implode(',', File::find()->select('id')->where(['is_delete' => File::IS_DELETE_NO, 'own_id' => $model->id, 'own_type' => File::OWN_TYPE_GOODS_DETAILS])->column());
//记录已保存的商品图片id,用于修改
$cover_image_old_id_str = $model->image;
$detail_image_old_id_str = $model->detailImageId;
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
//商品封面图和商品详情图上传保存处理
$res = GoodsManager::updateGoods(Yii::$app->request->post(), $model, $cover_image_old_id_str, $detail_image_old_id_str);
if ($res) {
return $this->redirect('index');
}
}
$model->uniform_postage /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
$model->market_price /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
$model->price /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
$goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$attributeModel = GoodsManager::getAttribute($goodsAttributes);
$checkAttr = GoodsManager::getSkuInfo($id);
$goodsFilterAttributes = FilterAttr::find()->where(['goods_id' => $id, 'is_delete' => FilterAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
$filterAttributeModel = GoodsManager::getAttribute($goodsFilterAttributes);
$judgeGoodsCategory = GoodsManager::judgeGoodsCategory($model);
return $this->render('update', [
'model' => $model,
'attributeModel' => $attributeModel,
'attrValue' => $checkAttr,
'filterAttributeModel' => $filterAttributeModel,
'judgeGoodsCategory' => $judgeGoodsCategory,
]);
}
/**
* Deletes an existing Goods model.
* If deletion is successful, the browser will be redirected to the 'index' page.
* @param integer $id
* @return mixed
* @throws NotFoundHttpException if the model cannot be found
*/
public function actionDelete($id)
{
$model = $this->findModel($id);
$model->is_delete = Goods::IS_DELETE_YES;
$model->save();
return $this->redirect(['index']);
}
/**
* Finds the Goods model based on its primary key value.
* If the model is not found, a 404 HTTP exception will be thrown.
* @param integer $id
* @return Goods the loaded model
* @throws NotFoundHttpException if the model cannot be found
*/
protected function findModel($id)
{
if (($model = Goods::findOne($id)) !== null) {
return $model;
}
throw new NotFoundHttpException('The requested page does not exist.');
}
/**
* @author iron
* 文件导出
*/
public function actionExport()
{
$searchModel = new GoodsSearch();
$params = Yii::$app->request->queryParams;
if ($params['page-type'] == 'all') {
$dataProvider = $searchModel->allData($params);
} else {
$dataProvider = $searchModel->search($params);
}
Excel::export([
'models' => $dataProvider->getModels(),
'format' => 'Xlsx',
'asAttachment' => true,
'fileName' =>'Goods'. "-" .date('Y-m-d H/i/s', time()),
'columns' => $searchModel->columns()
]);
}
/**
* 处理文件上传成功后回调保存到临时文件表中,并返回临时文件id
*/
public function actionSaveFile()
{
$data = Yii::$app->request->get('data');
$fileName = Yii::$app->request->get('fileName')[0];
if ($data['status'] == true) {
return FileManager::saveTemFile($fileName, $data);
}
return false;
}
/**
* @return string
* 点击删除按钮时同时删除字符串中的id
*/
public function actionImgIdDel()
{
$data = Yii::$app->request->get();
return FileManager::dealFileIdStrInDel($data);
}
/**
* @return bool|false|string
* 加载已有的文件
*/
public function actionImageFile()
{
$fileIdStr = Yii::$app->request->get('fileIdStr');
return FileManager::loadExitFile($fileIdStr);
}
/**
* @param $id
* @return string
* 商品编辑sku
*/
public function actionEditSku($id)
{
$sku = GoodsManager::getCreatedSku($id);
$attributes = GoodsManager::getAttrs($id);
return $this->render('sku_edit', [
'attributes' => $attributes,
'sku' => $sku,]);
}
/**
* @return array
* @throws Throwable
* 添加sku
*/
public function actionAddSku()
{
$data = [];
Yii::$app->response->format = 'json';
$res = Yii::$app->request->post('sku');
$goodsId = Yii::$app->request->post('goodsId');
$tra = Yii::$app->db->beginTransaction();
try {
$data['originalIds'] = GoodsManager::getOriginalIds($res['type'], $goodsId);
$data['acceptIds'] = [];
foreach ($res['data'] as $sku) {
GoodsManager::AddOrUpdateData($sku, $res['type'], $goodsId);
if ($sku['id'] > 0) {
$data['acceptIds'][] = $sku['id'];
}
}
GoodsManager::deleteSku($res['type'], $data, $goodsId);
$tra->commit();
return ['status' => true];
} catch (Exception $e) {
$tra->rollBack();
return ['status' => false, 'info' => $e->getMessage()];
}
}
public function actionSwitch()
{
Yii::$app->response->format = 'json';
$data = [];
$type = Yii::$app->request->get('type');
$id = Yii::$app->request->get('goodsId');
$data['sku'] = GoodsManager::getCreatedSku($id, $type);
$data['attributes'] = GoodsManager::getAttrs($id);
return $data;
}
/**
* @return false|string
* 根据商品分类获取商品属性
*/
public function actionFilterAttribute()
{
$catId = Yii::$app->request->get('catId')??0;
$allAttr = Attribute::find()->where(['type' => Attribute::TYPE_ATTR, 'is_delete' => Attribute::IS_DELETE_NO])->andWhere(['cat_id' => [0,$catId]])->asArray()->all();
return json_encode($allAttr);
}
}