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.

348 lines
12 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. $model->is_taking = Goods::IS_TAKING_NO;
  92. $model->is_express = Goods::IS_EXPRESS_YES;
  93. $model->express_type = Goods::EXPRESS_TYPE_EXPRESS_TEMPLAGE;
  94. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  95. $model->uniform_postage *= 100;
  96. //商品封面图和商品详情图上传保存处理
  97. $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model);
  98. if ($res['status']) {
  99. return $this->redirect('index');
  100. }
  101. } else {
  102. $model->ruleVerify = 1;
  103. }
  104. return $this->render('create', [
  105. 'model' => $model,
  106. ]);
  107. }
  108. /**
  109. * Updates an existing Goods model.
  110. * If update is successful, the browser will be redirected to the 'view' page.
  111. * @param integer $id
  112. * @return mixed
  113. * @throws NotFoundHttpException if the model cannot be found
  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. $model->uniform_postage *= 100;
  125. //商品封面图和商品详情图上传保存处理
  126. $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model, $cover_image_old_id_str, $detail_image_old_id_str);
  127. if ($res['status']) {
  128. return $this->redirect('index');
  129. }
  130. }
  131. $model->uniform_postage /= 100;
  132. $attributeModel = GoodsManager::getAttribute($id);
  133. $checkAttr = GoodsManager::getSkuInfo($id);
  134. $filterAttributeModel = GoodsManager::getFilterAttribute($id);
  135. $judgeGoodsCategory = GoodsManager::judgeGoodsCategory($model);
  136. return $this->render('update', [
  137. 'model' => $model,
  138. 'attributeModel' => $attributeModel,
  139. 'attrValue' => $checkAttr,
  140. 'filterAttributeModel' => $filterAttributeModel,
  141. 'judgeGoodsCategory' => $judgeGoodsCategory,
  142. ]);
  143. }
  144. /**
  145. * Deletes an existing Goods model.
  146. * If deletion is successful, the browser will be redirected to the 'index' page.
  147. * @param integer $id
  148. * @return mixed
  149. * @throws NotFoundHttpException if the model cannot be found
  150. */
  151. public function actionDelete($id)
  152. {
  153. $model = $this->findModel($id);
  154. $model->is_delete = Goods::IS_DELETE_YES;
  155. $model->save();
  156. return $this->redirect(['index']);
  157. }
  158. /**
  159. * Finds the Goods model based on its primary key value.
  160. * If the model is not found, a 404 HTTP exception will be thrown.
  161. * @param integer $id
  162. * @return Goods the loaded model
  163. * @throws NotFoundHttpException if the model cannot be found
  164. */
  165. protected function findModel($id)
  166. {
  167. if (($model = Goods::findOne($id)) !== null) {
  168. return $model;
  169. }
  170. throw new NotFoundHttpException('The requested page does not exist.');
  171. }
  172. /**
  173. * @author iron
  174. * 文件导出
  175. */
  176. public function actionExport()
  177. {
  178. $searchModel = new GoodsSearch();
  179. $params = Yii::$app->request->queryParams;
  180. if ($params['page-type'] == 'all') {
  181. $dataProvider = $searchModel->allData($params);
  182. } else {
  183. $dataProvider = $searchModel->search($params);
  184. }
  185. \iron\widget\Excel::export([
  186. 'models' => $dataProvider->getModels(),
  187. 'format' => 'Xlsx',
  188. 'asAttachment' => true,
  189. 'fileName' =>'Goods'. "-" .date('Y-m-d H/i/s', time()),
  190. 'columns' => $searchModel->columns()
  191. ]);
  192. }
  193. /**
  194. * 处理文件上传成功后回调保存到临时文件表中,并返回临时文件id
  195. */
  196. public function actionSaveFile()
  197. {
  198. $data = Yii::$app->request->get('data');
  199. $file_name = Yii::$app->request->get('fileName')[0];
  200. if ($data['status'] == true) {
  201. $model = new \backend\modules\file\models\ars\TemFile();
  202. $model->user_id = Yii::$app->user->identity->id;
  203. $model->name = $file_name;
  204. $file_manager = new \backend\modules\file\logic\file\FileManager();
  205. $model->type = $file_manager->searchType(pathinfo($data['path'])['extension']);
  206. $model->alias = $data['alias'];
  207. $model->path = $data['path'];
  208. $model->save();
  209. return $model->id;
  210. }
  211. }
  212. /**
  213. * @return string
  214. * 点击删除按钮时同时删除字符串中的id
  215. */
  216. public function actionImgIdDel()
  217. {
  218. $img_id = Yii::$app->request->get('imgid');
  219. $img_id_arr = explode(',', $img_id);
  220. if(isset(Yii::$app->request->get('data')['alias'])) {
  221. $alias = Yii::$app->request->get('data')['alias'];
  222. $tem_file = \backend\modules\file\models\ars\TemFile::findOne(['alias' => $alias]);
  223. if ($tem_file) {
  224. $img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
  225. }
  226. }else{
  227. foreach (Yii::$app->request->get() as $key => $value) {
  228. $tem_file = \backend\modules\file\models\ars\File::findOne(['alias' => $value]);
  229. if ($tem_file) {
  230. $img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
  231. }
  232. }
  233. }
  234. $img_id_str = implode(',', $img_id_arr);
  235. return $img_id_str;
  236. }
  237. /**
  238. * @return bool|false|string
  239. * 加载已有的文件
  240. */
  241. public function actionImageFile()
  242. {
  243. $rule_verify = Yii::$app->request->get('ruleverify');
  244. $file_id_str = Yii::$app->request->get('fileidstr');
  245. $file_id_arr = explode(',', $file_id_str);
  246. if ($rule_verify == 1) {
  247. $data = \backend\modules\file\models\ars\TemFile::find()->where(['id' => $file_id_arr])->all();
  248. } else {
  249. $data = \backend\modules\file\models\ars\File::find()->where(['id' => $file_id_arr])->all();
  250. }
  251. $res = array();
  252. if($data) {
  253. $i = 0;
  254. foreach ($data as $key => $value) {
  255. $res[$i]['name'] = $value->alias;
  256. $res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path;
  257. $res[$i]['size'] = filesize($value->path);
  258. $i++;
  259. }
  260. }
  261. return json_encode($res);
  262. }
  263. /**
  264. * @param $id
  265. * @return string
  266. * 商品编辑sku
  267. */
  268. public function actionEditSku($id)
  269. {
  270. $sku = GoodsManager::getCreatedSku($id);
  271. $attributes = GoodsManager::getAttrs($id);
  272. return $this->render('sku_edit', [
  273. 'attributes' => $attributes,
  274. 'sku' => $sku,]);
  275. }
  276. /**
  277. * @return array
  278. * @throws \Throwable
  279. * 添加sku
  280. */
  281. public function actionAddSku()
  282. {
  283. $data = [];
  284. Yii::$app->response->format = 'json';
  285. $res = Yii::$app->request->post('sku');
  286. $goodsId = Yii::$app->request->post('goodsId');
  287. $tra = Yii::$app->db->beginTransaction();
  288. try {
  289. $data['originalIds'] = GoodsManager::getOriginalIds($res['type'], $goodsId);
  290. $data['acceptIds'] = [];
  291. foreach ($res['data'] as $sku) {
  292. GoodsManager::AddOrUpdateData($sku, $res['type'], $goodsId);
  293. if ($sku['id'] > 0) {
  294. $data['acceptIds'][] = $sku['id'];
  295. }
  296. }
  297. GoodsManager::deleteSku($res['type'], $data, $goodsId);
  298. $tra->commit();
  299. return ['status' => true];
  300. } catch (\Exception $e) {
  301. $tra->rollBack();
  302. return ['status' => false, 'info' => $e->getMessage()];
  303. }
  304. }
  305. public function actionSwitch()
  306. {
  307. Yii::$app->response->format = 'json';
  308. $data = [];
  309. $type = Yii::$app->request->get('type');
  310. $id = Yii::$app->request->get('goodsId');
  311. $data['sku'] = GoodsManager::getCreatedSku($id, $type);
  312. $data['attributes'] = GoodsManager::getAttrs($id, $type);
  313. return $data;
  314. }
  315. /**
  316. * @return false|string
  317. * 根据商品分类获取商品属性
  318. */
  319. public function actionFilterAttribute()
  320. {
  321. $catId = Yii::$app->request->get('catId')??0;
  322. $goodsId = Yii::$app->request->get('goodsId')??0;
  323. $allAttr = Attribute::find()->where(['type' => Attribute::TYPE_ATTR])->andWhere(['cat_id' => [0,$catId]])->asArray()->all();
  324. return json_encode($allAttr);
  325. }
  326. }