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

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