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.

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