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.

287 lines
9.0 KiB

  1. <?php
  2. namespace goods\controllers;
  3. use goods\logic\goods\GoodsManager;
  4. use backend\models\ars\File;
  5. use Yii;
  6. use goods\models\ars\ShopCategory;
  7. use goods\models\searchs\ShopCategorySearch;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. /**
  12. * ShopcategoryController implements the CRUD actions for ShopCategory model.
  13. */
  14. class ShopCategoryController extends Controller
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function behaviors()
  20. {
  21. return [
  22. 'verbs' => [
  23. 'class' => VerbFilter::className(),
  24. 'actions' => [
  25. 'delete' => ['POST'],
  26. ],
  27. ],
  28. ];
  29. }
  30. public function actions()
  31. {
  32. return [
  33. 'upload' => [
  34. 'class' => 'iron\actions\UploadAction',
  35. 'path' => 'xls/',
  36. 'maxSize' => 20480,
  37. ]
  38. ];
  39. }
  40. /**
  41. * Lists all ShopCategory models.
  42. * @return mixed
  43. */
  44. public function actionIndex()
  45. {
  46. $searchModel = new ShopCategorySearch();
  47. $dataProvider = $searchModel->search(Yii::$app->request->queryParams);
  48. return $this->render('index', [
  49. 'searchModel' => $searchModel,
  50. 'dataProvider' => $dataProvider,
  51. 'columns' => $searchModel->columns()
  52. ]);
  53. }
  54. /**
  55. * Displays a single ShopCategory model.
  56. * @param integer $id
  57. * @return mixed
  58. * @throws NotFoundHttpException if the model cannot be found
  59. */
  60. public function actionView($id)
  61. {
  62. return $this->render('view', [
  63. 'model' => $this->findModel($id),
  64. ]);
  65. }
  66. /**
  67. * Creates a new ShopCategory model.
  68. * If creation is successful, the browser will be redirected to the 'view' page.
  69. * @return mixed
  70. */
  71. public function actionCreate()
  72. {
  73. $model = new ShopCategory();
  74. $model->is_show = ShopCategory::IS_SHOW_HIDE;
  75. $model->sort_order = 0;
  76. if ($model->load(Yii::$app->request->post())) {
  77. if ($model->filter_attr != null && is_array($model->filter_attr)) {
  78. $model->filter_attr = implode(',', $model->filter_attr);
  79. } else {
  80. $model->filter_attr = '';
  81. }
  82. //类目图片上传保存处理
  83. $icon_image_id_str = $model->iconImageId;
  84. $model->save();
  85. $save_icon_image_res = GoodsManager::saveFile(explode(',', $icon_image_id_str), $model, [], File::OWN_TYPE_CATEGORY_ICON);
  86. if($save_icon_image_res['status']){
  87. $model->icon = $save_icon_image_res['first_file_id'];
  88. $model->save();
  89. }
  90. return $this->redirect('index');
  91. }
  92. return $this->render('create', [
  93. 'model' => $model,
  94. ]);
  95. }
  96. /**
  97. * Updates an existing ShopCategory model.
  98. * If update is successful, the browser will be redirected to the 'view' page.
  99. * @param integer $id
  100. * @return mixed
  101. * @throws NotFoundHttpException if the model cannot be found
  102. */
  103. public function actionUpdate($id)
  104. {
  105. $model = $this->findModel($id);
  106. if ($model->filter_attr != null) {
  107. $model->filter_attr = explode(',', $model->filter_attr);
  108. }
  109. $model->iconImageId = $model->icon;
  110. $icon_image_old_id_arr = $model->icon;//记录已保存的类目图片id,用于修改
  111. if ($model->load(Yii::$app->request->post())) {
  112. if ($model->filter_attr != null && is_array($model->filter_attr)) {
  113. $model->filter_attr = implode(',', $model->filter_attr);
  114. } else {
  115. $model->filter_attr = '';
  116. }
  117. //类目图片上传保存处理
  118. $icon_image_id_str = $model->iconImageId;
  119. $model->save();
  120. $save_icon_image_res = GoodsManager::saveFile(explode(',', $icon_image_id_str), $model, explode(',', $icon_image_old_id_arr), File::OWN_TYPE_CATEGORY_ICON);
  121. if($save_icon_image_res['status'] && $save_icon_image_res['first_file_id'] !== 0){
  122. $model->icon = $save_icon_image_res['first_file_id'];
  123. $model->save();
  124. }
  125. return $this->redirect('index');
  126. }
  127. return $this->render('update', [
  128. 'model' => $model,
  129. ]);
  130. }
  131. /**
  132. * Deletes an existing ShopCategory model.
  133. * If deletion is successful, the browser will be redirected to the 'index' page.
  134. * @param integer $id
  135. * @return mixed
  136. * @throws NotFoundHttpException if the model cannot be found
  137. */
  138. public function actionDelete($id)
  139. {
  140. $model = $this->findModel($id);
  141. $model->is_delete = ShopCategory::IS_DELETE_YES;
  142. $model->save();
  143. return $this->redirect(['index']);
  144. }
  145. /**
  146. * Finds the ShopCategory model based on its primary key value.
  147. * If the model is not found, a 404 HTTP exception will be thrown.
  148. * @param integer $id
  149. * @return ShopCategory the loaded model
  150. * @throws NotFoundHttpException if the model cannot be found
  151. */
  152. protected function findModel($id)
  153. {
  154. if (($model = ShopCategory::findOne($id)) !== null) {
  155. return $model;
  156. }
  157. throw new NotFoundHttpException('The requested page does not exist.');
  158. }
  159. /**
  160. * @author iron
  161. * 文件导出
  162. */
  163. public function actionExport()
  164. {
  165. $searchModel = new ShopCategorySearch();
  166. $params = Yii::$app->request->queryParams;
  167. if ($params['page-type'] == 'all') {
  168. $dataProvider = $searchModel->allData($params);
  169. } else {
  170. $dataProvider = $searchModel->search($params);
  171. }
  172. \iron\widget\Excel::export([
  173. 'models' => $dataProvider->getModels(),
  174. 'format' => 'Xlsx',
  175. 'asAttachment' => true,
  176. 'fileName' =>'Shop Categories'. "-" .date('Y-m-d H/i/s', time()),
  177. 'columns' => $searchModel->columns()
  178. ]);
  179. }
  180. /**
  181. * 处理文件上传成功后回调保存到临时文件表中,并返回临时文件id
  182. */
  183. public function actionSaveFile()
  184. {
  185. if(!class_exists('\backend\models\ars\TemFile') || !class_exists('\backend\logic\file\FileManager')){
  186. return '';
  187. }
  188. $data = Yii::$app->request->get('data');
  189. $file_name = Yii::$app->request->get('fileName')[0];
  190. if ($data['status'] == true) {
  191. $model = new \backend\models\ars\TemFile();
  192. $model->user_id = Yii::$app->user->identity->id;
  193. $model->name = $file_name;
  194. $file_manager = new \backend\logic\file\FileManager();
  195. $type_res = $file_manager->searchType(\backend\logic\file\FileManager::$extension, pathinfo($data['path'])['extension']);
  196. if ($type_res['status']) {
  197. $model->type = $type_res['type'];
  198. }
  199. $model->alias = $data['alias'];
  200. $model->path = $data['path'];
  201. $model->save();
  202. return $model->id;
  203. }
  204. }
  205. /**
  206. * @return string
  207. * 点击删除按钮时同时删除字符串中的id
  208. */
  209. public function actionImgIdDel()
  210. {
  211. //判断该类是否存在
  212. if(!class_exists('\backend\models\ars\TemFile') || !class_exists('\backend\models\ars\File')){
  213. return '';
  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\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\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. //判断该类是否存在
  241. if(!class_exists('\backend\models\ars\File')){
  242. return false;
  243. }
  244. $file_id_str = Yii::$app->request->get('fileidstr');
  245. $file_id_arr = explode(',', $file_id_str);
  246. $data = \backend\models\ars\File::find()->where(['id' => $file_id_arr])->all();
  247. $res = array();
  248. if($data) {
  249. $i = 0;
  250. foreach ($data as $key => $value) {
  251. $res[$i]['name'] = $value->alias;
  252. $res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path;
  253. $res[$i]['size'] = filesize($value->path);
  254. $i++;
  255. }
  256. }
  257. return json_encode($res);
  258. }
  259. }