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.

359 lines
12 KiB

  1. <?php
  2. namespace goods\controllers;
  3. use goods\models\ars\GoodsAttr;
  4. use goods\models\ars\GoodsSku;
  5. use backend\models\ars\OrderGoods;
  6. use backend\models\ars\TemFile;
  7. use MongoDB\Driver\Manager;
  8. use Yii;
  9. use goods\models\ars\Goods;
  10. use goods\models\searchs\GoodsSearch;
  11. use yii\web\Controller;
  12. use yii\web\NotFoundHttpException;
  13. use yii\filters\VerbFilter;
  14. use goods\logic\goods\GoodsManager;
  15. use backend\models\ars\File;
  16. use yii\web\Response;
  17. use 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. if(!class_exists('\backend\models\ars\TemFile') || !class_exists('\backend\logic\file\FileManager')){
  193. return '';
  194. }
  195. $data = Yii::$app->request->get('data');
  196. $file_name = Yii::$app->request->get('fileName')[0];
  197. if ($data['status'] == true) {
  198. $model = new \backend\models\ars\TemFile();
  199. $model->user_id = Yii::$app->user->identity->id;
  200. $model->name = $file_name;
  201. $file_manager = new \backend\logic\file\FileManager();
  202. $type_res = $file_manager->searchType(\backend\logic\file\FileManager::$extension, pathinfo($data['path'])['extension']);
  203. if ($type_res['status']) {
  204. $model->type = $type_res['type'];
  205. }
  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. //判断该类是否存在
  219. if(!class_exists('\backend\models\ars\TemFile') || !class_exists('\backend\models\ars\File')){
  220. return '';
  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\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\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. //判断该类是否存在
  248. if(!class_exists('\backend\models\ars\File')){
  249. return false;
  250. }
  251. $rule_verify = Yii::$app->request->get('ruleverify');
  252. $file_id_str = Yii::$app->request->get('fileidstr');
  253. $file_id_arr = explode(',', $file_id_str);
  254. if ($rule_verify == 1) {
  255. $data = \backend\models\ars\TemFile::find()->where(['id' => $file_id_arr])->all();
  256. } else {
  257. $data = \backend\models\ars\File::find()->where(['id' => $file_id_arr])->all();
  258. }
  259. $res = array();
  260. if($data) {
  261. $i = 0;
  262. foreach ($data as $key => $value) {
  263. $res[$i]['name'] = $value->alias;
  264. $res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path;
  265. $res[$i]['size'] = filesize($value->path);
  266. $i++;
  267. }
  268. }
  269. return json_encode($res);
  270. }
  271. /**
  272. * @param $id
  273. * @return string
  274. * 商品编辑sku
  275. */
  276. public function actionEditSku($id)
  277. {
  278. $sku = GoodsManager::getCreatedSku($id);
  279. $attributes = GoodsManager::getAttrs($id);
  280. return $this->render('sku_edit', [
  281. 'attributes' => $attributes,
  282. 'sku' => $sku,]);
  283. }
  284. /**
  285. * @return array
  286. * @throws \Throwable
  287. * 添加sku
  288. */
  289. public function actionAddSku()
  290. {
  291. $data = [];
  292. Yii::$app->response->format = 'json';
  293. $res = Yii::$app->request->post('sku');
  294. $goodsId = Yii::$app->request->post('goodsId');
  295. $tra = Yii::$app->db->beginTransaction();
  296. try {
  297. $data['originalIds'] = GoodsManager::getOriginalIds($res['type'], $goodsId);
  298. $data['acceptIds'] = [];
  299. foreach ($res['data'] as $sku) {
  300. GoodsManager::AddOrUpdateData($sku, $res['type'], $goodsId);
  301. if ($sku['id'] > 0) {
  302. $data['acceptIds'][] = $sku['id'];
  303. }
  304. }
  305. GoodsManager::deleteSku($res['type'], $data, $goodsId);
  306. $tra->commit();
  307. return ['status' => true];
  308. } catch (\Exception $e) {
  309. $tra->rollBack();
  310. return ['status' => false, 'info' => $e->getMessage()];
  311. }
  312. }
  313. public function actionSwitch()
  314. {
  315. Yii::$app->response->format = 'json';
  316. $data = [];
  317. $type = Yii::$app->request->get('type');
  318. $id = Yii::$app->request->get('goodsId');
  319. $data['sku'] = GoodsManager::getCreatedSku($id, $type);
  320. $data['attributes'] = GoodsManager::getAttrs($id, $type);
  321. return $data;
  322. }
  323. /**
  324. * @return false|string
  325. * 根据商品分类获取商品属性
  326. */
  327. public function actionFilterAttribute()
  328. {
  329. $catId = Yii::$app->request->get('catId')??0;
  330. $goodsId = Yii::$app->request->get('goodsId')??0;
  331. $allAttr = Attribute::find()->where(['type' => Attribute::TYPE_ATTR])->andWhere(['cat_id' => [0,$catId]])->asArray()->all();
  332. return json_encode($allAttr);
  333. }
  334. }