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.
345 lines
11 KiB
345 lines
11 KiB
<?php
|
|
|
|
namespace backend\modules\goods\controllers;
|
|
|
|
use backend\modules\goods\models\ars\GoodsAttr;
|
|
use backend\modules\goods\models\ars\GoodsSku;
|
|
use backend\models\ars\OrderGoods;
|
|
use backend\modules\file\models\ars\TemFile;
|
|
use MongoDB\Driver\Manager;
|
|
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 yii\web\Response;
|
|
use backend\modules\goods\models\ars\Attribute;
|
|
|
|
/**
|
|
* 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
|
|
*/
|
|
public function actionCreate()
|
|
{
|
|
$model = new Goods();
|
|
$model->is_sale = Goods::IS_SALE_YES;
|
|
$model->stock = -1;
|
|
if ($model->load(Yii::$app->request->post()) && $model->validate()) {
|
|
//商品封面图和商品详情图上传保存处理
|
|
$res = GoodsManager::updateGoods(Yii::$app->request->post(), $model);
|
|
if ($res['status']) {
|
|
return $this->redirect('index');
|
|
}
|
|
} else {
|
|
$model->ruleVerify = 1;
|
|
}
|
|
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
|
|
*/
|
|
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['status']) {
|
|
return $this->redirect('index');
|
|
}
|
|
}
|
|
$attributeModel = GoodsManager::getAttribute($id);
|
|
$checkAttr = GoodsManager::getSkuInfo($id);
|
|
$filterAttributeModel = GoodsManager::getFilterAttribute($id);
|
|
$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);
|
|
}
|
|
\iron\widget\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');
|
|
$file_name = Yii::$app->request->get('fileName')[0];
|
|
|
|
if ($data['status'] == true) {
|
|
$model = new \backend\modules\file\models\ars\TemFile();
|
|
$model->user_id = Yii::$app->user->identity->id;
|
|
$model->name = $file_name;
|
|
$file_manager = new \backend\modules\file\logic\file\FileManager();
|
|
$type_res = $file_manager->searchType(\backend\modules\file\logic\file\FileManager::$extension, pathinfo($data['path'])['extension']);
|
|
if ($type_res['status']) {
|
|
$model->type = $type_res['type'];
|
|
}
|
|
$model->alias = $data['alias'];
|
|
$model->path = $data['path'];
|
|
$model->save();
|
|
return $model->id;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @return string
|
|
* 点击删除按钮时同时删除字符串中的id
|
|
*/
|
|
public function actionImgIdDel()
|
|
{
|
|
$img_id = Yii::$app->request->get('imgid');
|
|
$img_id_arr = explode(',', $img_id);
|
|
if(isset(Yii::$app->request->get('data')['alias'])) {
|
|
$alias = Yii::$app->request->get('data')['alias'];
|
|
$tem_file = \backend\modules\file\models\ars\TemFile::findOne(['alias' => $alias]);
|
|
if ($tem_file) {
|
|
$img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
|
|
}
|
|
}else{
|
|
foreach (Yii::$app->request->get() as $key => $value) {
|
|
$tem_file = \backend\modules\file\models\ars\File::findOne(['alias' => $value]);
|
|
if ($tem_file) {
|
|
$img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
|
|
}
|
|
}
|
|
}
|
|
$img_id_str = implode(',', $img_id_arr);
|
|
return $img_id_str;
|
|
}
|
|
|
|
/**
|
|
* @return bool|false|string
|
|
* 加载已有的文件
|
|
*/
|
|
public function actionImageFile()
|
|
{
|
|
$rule_verify = Yii::$app->request->get('ruleverify');
|
|
$file_id_str = Yii::$app->request->get('fileidstr');
|
|
$file_id_arr = explode(',', $file_id_str);
|
|
if ($rule_verify == 1) {
|
|
$data = \backend\modules\file\models\ars\TemFile::find()->where(['id' => $file_id_arr])->all();
|
|
} else {
|
|
$data = \backend\modules\file\models\ars\File::find()->where(['id' => $file_id_arr])->all();
|
|
}
|
|
$res = array();
|
|
if($data) {
|
|
$i = 0;
|
|
foreach ($data as $key => $value) {
|
|
$res[$i]['name'] = $value->alias;
|
|
$res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path;
|
|
$res[$i]['size'] = filesize($value->path);
|
|
$i++;
|
|
}
|
|
}
|
|
return json_encode($res);
|
|
}
|
|
|
|
/**
|
|
* @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, $type);
|
|
return $data;
|
|
}
|
|
|
|
/**
|
|
* @return false|string
|
|
* 根据商品分类获取商品属性
|
|
*/
|
|
public function actionFilterAttribute()
|
|
{
|
|
$catId = Yii::$app->request->get('catId')??0;
|
|
$goodsId = Yii::$app->request->get('goodsId')??0;
|
|
$allAttr = Attribute::find()->where(['type' => Attribute::TYPE_ATTR])->andWhere(['cat_id' => [0,$catId]])->asArray()->all();
|
|
return json_encode($allAttr);
|
|
}
|
|
}
|