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\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\HttpException;
  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 yii\web\Response;
  18. use backend\modules\goods\models\ars\Attribute;
  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. */
  87. public function actionCreate()
  88. {
  89. $model = new Goods();
  90. $model->is_sale = Goods::IS_SALE_YES;
  91. $model->stock = -1;
  92. $model->is_taking = Goods::IS_TAKING_NO;
  93. $model->is_express = Goods::IS_EXPRESS_YES;
  94. $model->express_type = Goods::EXPRESS_TYPE_EXPRESS_TEMPLATE;
  95. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  96. if ($model->uniform_postage) {
  97. $model->uniform_postage *= 100;
  98. }
  99. //商品封面图和商品详情图上传保存处理
  100. $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model);
  101. if ($res['status']) {
  102. return $this->redirect('index');
  103. }
  104. } else {
  105. $model->ruleVerify = 1;
  106. }
  107. return $this->render('create', [
  108. 'model' => $model,
  109. ]);
  110. }
  111. /**
  112. * Updates an existing Goods model.
  113. * If update is successful, the browser will be redirected to the 'view' page.
  114. * @param integer $id
  115. * @return mixed
  116. * @throws NotFoundHttpException if the model cannot be found
  117. */
  118. public function actionUpdate($id)
  119. {
  120. $model = $this->findModel($id);
  121. $model->coverImageId = $model->image;
  122. $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());
  123. //记录已保存的商品图片id,用于修改
  124. $cover_image_old_id_str = $model->image;
  125. $detail_image_old_id_str = $model->detailImageId;
  126. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  127. if ($model->uniform_postage) {
  128. $model->uniform_postage *= 100;
  129. }
  130. //商品封面图和商品详情图上传保存处理
  131. $res = GoodsManager::updateGoods(Yii::$app->request->post(), $model, $cover_image_old_id_str, $detail_image_old_id_str);
  132. if ($res['status']) {
  133. return $this->redirect('index');
  134. }
  135. }
  136. $model->uniform_postage /= 100;
  137. $attributeModel = GoodsManager::getAttribute($id);
  138. $checkAttr = GoodsManager::getSkuInfo($id);
  139. $filterAttributeModel = GoodsManager::getFilterAttribute($id);
  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. }