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.

353 lines
12 KiB

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