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

  1. <?php
  2. namespace backend\modules\goods\controllers;
  3. use backend\modules\file\logic\file\FileManager;
  4. use backend\modules\goods\models\ars\FilterAttr;
  5. use backend\modules\goods\models\ars\GoodsAttr;
  6. use backend\modules\shop\logic\ShopManager;
  7. use Exception;
  8. use Throwable;
  9. use Yii;
  10. use backend\modules\goods\models\ars\Goods;
  11. use backend\modules\goods\models\searchs\GoodsSearch;
  12. use yii\web\Controller;
  13. use yii\web\NotFoundHttpException;
  14. use yii\filters\VerbFilter;
  15. use backend\modules\goods\logic\goods\GoodsManager;
  16. use backend\modules\file\models\ars\File;
  17. use backend\modules\goods\models\ars\Attribute;
  18. use iron\widget\Excel;
  19. /**
  20. * GoodsController implements the CRUD actions for Goods model.
  21. */
  22. class GoodsController extends Controller
  23. {
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function behaviors()
  28. {
  29. return [
  30. 'verbs' => [
  31. 'class' => VerbFilter::className(),
  32. 'actions' => [
  33. 'delete' => ['POST'],
  34. ],
  35. ],
  36. ];
  37. }
  38. public function actions()
  39. {
  40. return [
  41. 'upload' => [
  42. 'class' => 'iron\actions\UploadAction',
  43. 'path' => 'xls/',
  44. 'maxSize' => 20480,
  45. ],
  46. 'ueditor' => [
  47. 'class' => 'common\widgets\ueditor\UeditorAction',
  48. 'config' => [
  49. //上传图片配置
  50. 'imageUrlPrefix' => "", /* 图片访问路径前缀 */
  51. 'imagePathFormat' => "/uploads/origin/introduce/" . md5(time() . rand(000, 999)), /* 上传保存路径,可以自定义保存路径和文件名格式 */
  52. ]
  53. ],
  54. ];
  55. }
  56. /**
  57. * Lists all Goods models.
  58. * @return mixed
  59. */
  60. public function actionIndex()
  61. {
  62. $searchModel = new GoodsSearch();
  63. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  64. return $this->render('index', [
  65. 'searchModel' => $searchModel,
  66. 'dataProvider' => $dataProvider,
  67. 'columns' => $searchModel->columns()
  68. ]);
  69. }
  70. /**
  71. * Displays a single Goods model.
  72. * @param integer $id
  73. * @return mixed
  74. * @throws NotFoundHttpException if the model cannot be found
  75. */
  76. public function actionView($id)
  77. {
  78. return $this->render('view', [
  79. 'model' => $this->findModel($id),
  80. ]);
  81. }
  82. /**
  83. * Creates a new Goods model.
  84. * If creation is successful, the browser will be redirected to the 'view' page.
  85. * @return mixed
  86. * @throws Exception
  87. */
  88. public function actionCreate()
  89. {
  90. $model = new Goods();
  91. $model->is_sale = Goods::IS_SALE_YES;
  92. $model->stock = -1;
  93. $model->is_taking = Goods::IS_TAKING_NO;
  94. $model->is_express = Goods::IS_EXPRESS_YES;
  95. $model->express_type = Goods::EXPRESS_TYPE_EXPRESS_TEMPLATE;
  96. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  97. //商品封面图和商品详情图上传保存处理
  98. $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model);
  99. if ($res) {
  100. return $this->redirect('index');
  101. }
  102. }
  103. return $this->render('create', [
  104. 'model' => $model,
  105. ]);
  106. }
  107. /**
  108. * Updates an existing Goods model.
  109. * If update is successful, the browser will be redirected to the 'view' page.
  110. * @param integer $id
  111. * @return mixed
  112. * @throws NotFoundHttpException if the model cannot be found
  113. * @throws Exception
  114. */
  115. public function actionUpdate($id)
  116. {
  117. $model = $this->findModel($id);
  118. $model->coverImageId = $model->image;
  119. $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());
  120. //记录已保存的商品图片id,用于修改
  121. $cover_image_old_id_str = $model->image;
  122. $detail_image_old_id_str = $model->detailImageId;
  123. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  124. //商品封面图和商品详情图上传保存处理
  125. $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model, $cover_image_old_id_str, $detail_image_old_id_str);
  126. if ($res) {
  127. return $this->redirect('index');
  128. }
  129. }
  130. $model->uniform_postage /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
  131. $model->market_price /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
  132. $model->price /= ShopManager::proportionalConversion(ShopManager::UNIT_TYPE_MONEY);
  133. $goodsAttributes = GoodsAttr::find()->where(['goods_id' => $id, 'is_delete' => GoodsAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
  134. $attributeModel = GoodsManager::getAttribute($goodsAttributes);
  135. $checkAttr = GoodsManager::getSkuInfo($id);
  136. $goodsFilterAttributes = FilterAttr::find()->where(['goods_id' => $id, 'is_delete' => FilterAttr::IS_DELETE_NO])->andWhere(['!=', 'attr_id', 0])->all();
  137. $filterAttributeModel = GoodsManager::getAttribute($goodsFilterAttributes);
  138. $judgeGoodsCategory = GoodsManager::judgeGoodsCategory($model);
  139. return $this->render('update', [
  140. 'model' => $model,
  141. 'attributeModel' => $attributeModel,
  142. 'attrValue' => $checkAttr,
  143. 'filterAttributeModel' => $filterAttributeModel,
  144. 'judgeGoodsCategory' => $judgeGoodsCategory,
  145. ]);
  146. }
  147. /**
  148. * Deletes an existing Goods model.
  149. * If deletion is successful, the browser will be redirected to the 'index' page.
  150. * @param integer $id
  151. * @return mixed
  152. * @throws NotFoundHttpException if the model cannot be found
  153. */
  154. public function actionDelete($id)
  155. {
  156. $model = $this->findModel($id);
  157. $model->is_delete = Goods::IS_DELETE_YES;
  158. $model->save();
  159. return $this->redirect(['index']);
  160. }
  161. /**
  162. * Finds the Goods model based on its primary key value.
  163. * If the model is not found, a 404 HTTP exception will be thrown.
  164. * @param integer $id
  165. * @return Goods the loaded model
  166. * @throws NotFoundHttpException if the model cannot be found
  167. */
  168. protected function findModel($id)
  169. {
  170. if (($model = Goods::findOne($id)) !== null) {
  171. return $model;
  172. }
  173. throw new NotFoundHttpException('The requested page does not exist.');
  174. }
  175. /**
  176. * @author iron
  177. * 文件导出
  178. */
  179. public function actionExport()
  180. {
  181. $searchModel = new GoodsSearch();
  182. $params = Yii::$app->request->queryParams;
  183. if ($params['page-type'] == 'all') {
  184. $dataProvider = $searchModel->allData($params);
  185. } else {
  186. $dataProvider = $searchModel->search($params);
  187. }
  188. Excel::export([
  189. 'models' => $dataProvider->getModels(),
  190. 'format' => 'Xlsx',
  191. 'asAttachment' => true,
  192. 'fileName' =>'Goods'. "-" .date('Y-m-d H/i/s', time()),
  193. 'columns' => $searchModel->columns()
  194. ]);
  195. }
  196. /**
  197. * 处理文件上传成功后回调保存到临时文件表中,并返回临时文件id
  198. */
  199. public function actionSaveFile()
  200. {
  201. $data = Yii::$app->request->get('data');
  202. $fileName = Yii::$app->request->get('fileName')[0];
  203. if ($data['status'] == true) {
  204. return FileManager::saveTemFile($fileName, $data);
  205. }
  206. return false;
  207. }
  208. /**
  209. * @return string
  210. * 点击删除按钮时同时删除字符串中的id
  211. */
  212. public function actionImgIdDel()
  213. {
  214. $data = Yii::$app->request->get();
  215. return FileManager::dealFileIdStrInDel($data);
  216. }
  217. /**
  218. * @return bool|false|string
  219. * 加载已有的文件
  220. */
  221. public function actionImageFile()
  222. {
  223. $fileIdStr = Yii::$app->request->get('fileIdStr');
  224. return FileManager::loadExitFile($fileIdStr);
  225. }
  226. /**
  227. * @param $id
  228. * @return string
  229. * 商品编辑sku
  230. */
  231. public function actionEditSku($id)
  232. {
  233. $sku = GoodsManager::getCreatedSku($id);
  234. $attributes = GoodsManager::getAttrs($id);
  235. return $this->render('sku_edit', [
  236. 'attributes' => $attributes,
  237. 'sku' => $sku,]);
  238. }
  239. /**
  240. * @return array
  241. * @throws Throwable
  242. * 添加sku
  243. */
  244. public function actionAddSku()
  245. {
  246. $data = [];
  247. Yii::$app->response->format = 'json';
  248. $res = Yii::$app->request->post('sku');
  249. $goodsId = Yii::$app->request->post('goodsId');
  250. $tra = Yii::$app->db->beginTransaction();
  251. try {
  252. $data['originalIds'] = GoodsManager::getOriginalIds($res['type'], $goodsId);
  253. $data['acceptIds'] = [];
  254. foreach ($res['data'] as $sku) {
  255. GoodsManager::AddOrUpdateData($sku, $res['type'], $goodsId);
  256. if ($sku['id'] > 0) {
  257. $data['acceptIds'][] = $sku['id'];
  258. }
  259. }
  260. GoodsManager::deleteSku($res['type'], $data, $goodsId);
  261. $tra->commit();
  262. return ['status' => true];
  263. } catch (Exception $e) {
  264. $tra->rollBack();
  265. return ['status' => false, 'info' => $e->getMessage()];
  266. }
  267. }
  268. public function actionSwitch()
  269. {
  270. Yii::$app->response->format = 'json';
  271. $data = [];
  272. $type = Yii::$app->request->get('type');
  273. $id = Yii::$app->request->get('goodsId');
  274. $data['sku'] = GoodsManager::getCreatedSku($id, $type);
  275. $data['attributes'] = GoodsManager::getAttrs($id);
  276. return $data;
  277. }
  278. /**
  279. * @return false|string
  280. * 根据商品分类获取商品属性
  281. */
  282. public function actionFilterAttribute()
  283. {
  284. $catId = Yii::$app->request->get('catId')??0;
  285. $allAttr = Attribute::find()->where(['type' => Attribute::TYPE_ATTR, 'is_delete' => Attribute::IS_DELETE_NO])->andWhere(['cat_id' => [0,$catId]])->asArray()->all();
  286. return json_encode($allAttr);
  287. }
  288. }