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.

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