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.

276 lines
8.4 KiB

  1. <?php
  2. namespace antgoods\controllers;
  3. use antgoods\logic\goods\GoodsManager;
  4. use common\models\ars\File;
  5. use Yii;
  6. use antgoods\models\ars\Category;
  7. use antgoods\models\searchs\CategorySearch;
  8. use yii\web\Controller;
  9. use yii\web\NotFoundHttpException;
  10. use yii\filters\VerbFilter;
  11. /**
  12. * CategoryController implements the CRUD actions for Category model.
  13. */
  14. class CategoryController 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 Category models.
  42. * @return mixed
  43. */
  44. public function actionIndex()
  45. {
  46. $searchModel = new CategorySearch();
  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 Category 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 Category 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 Category();
  74. $model->is_show = Category::IS_SHOW_DISPLAY;
  75. $model->sort_order = 0;
  76. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  77. //类目图片上传保存处理
  78. $icon_image_id_str = $model->iconImageId;
  79. $model->save();
  80. $save_icon_image_res = GoodsManager::saveFile(explode(',', $icon_image_id_str), $model, [], File::OWN_TYPE_CATEGORY_ICON);
  81. if($save_icon_image_res['status']){
  82. $model->icon = $save_icon_image_res['first_file_id'];
  83. $model->save();
  84. }
  85. return $this->redirect('index');
  86. }
  87. return $this->render('create', [
  88. 'model' => $model,
  89. ]);
  90. }
  91. /**
  92. * Updates an existing Category model.
  93. * If update is successful, the browser will be redirected to the 'view' page.
  94. * @param integer $id
  95. * @return mixed
  96. * @throws NotFoundHttpException if the model cannot be found
  97. */
  98. public function actionUpdate($id)
  99. {
  100. $model = $this->findModel($id);
  101. $model->iconImageId = $model->icon;
  102. //记录已保存的类目图片id,用于修改
  103. $icon_image_old_id_arr = $model->icon;
  104. if ($model->load(Yii::$app->request->post()) && $model->validate()) {
  105. //类目图片上传保存处理
  106. $icon_image_id_str = $model->iconImageId;
  107. $model->save();
  108. $save_icon_image_res = GoodsManager::saveFile(explode(',', $icon_image_id_str), $model, explode(',', $icon_image_old_id_arr), File::OWN_TYPE_CATEGORY_ICON);
  109. if($save_icon_image_res['status'] && $save_icon_image_res['first_file_id'] !== 0){
  110. $model->icon = $save_icon_image_res['first_file_id'];
  111. $model->save();
  112. }
  113. return $this->redirect('index');
  114. }
  115. return $this->render('update', [
  116. 'model' => $model,
  117. ]);
  118. }
  119. /**
  120. * Deletes an existing Category model.
  121. * If deletion is successful, the browser will be redirected to the 'index' page.
  122. * @param integer $id
  123. * @return mixed
  124. * @throws NotFoundHttpException if the model cannot be found
  125. */
  126. public function actionDelete($id)
  127. {
  128. $model = $this->findModel($id);
  129. $model->is_delete = Category::IS_DELETE_YES;
  130. $model->save();
  131. return $this->redirect(['index']);
  132. }
  133. /**
  134. * Finds the Category model based on its primary key value.
  135. * If the model is not found, a 404 HTTP exception will be thrown.
  136. * @param integer $id
  137. * @return Category the loaded model
  138. * @throws NotFoundHttpException if the model cannot be found
  139. */
  140. protected function findModel($id)
  141. {
  142. if (($model = Category::findOne($id)) !== null) {
  143. return $model;
  144. }
  145. throw new NotFoundHttpException('The requested page does not exist.');
  146. }
  147. /**
  148. * @author iron
  149. * 文件导出
  150. */
  151. public function actionExport()
  152. {
  153. $searchModel = new CategorySearch();
  154. $params = Yii::$app->request->queryParams;
  155. if ($params['page-type'] == 'all') {
  156. $dataProvider = $searchModel->allData($params);
  157. } else {
  158. $dataProvider = $searchModel->search($params);
  159. }
  160. \iron\widget\Excel::export([
  161. 'models' => $dataProvider->getModels(),
  162. 'format' => 'Xlsx',
  163. 'asAttachment' => true,
  164. 'fileName' =>'Categories'. "-" .date('Y-m-d H/i/s', time()),
  165. 'columns' => $searchModel->columns()
  166. ]);
  167. }
  168. /**
  169. * 处理文件上传成功后回调保存到临时文件表中,并返回临时文件id
  170. */
  171. public function actionSaveFile()
  172. {
  173. if(!class_exists('\common\models\ars\TemFile') || !class_exists('\backend\logic\file\FileManager')){
  174. return '';
  175. }
  176. $data = Yii::$app->request->get('data');
  177. $file_name = Yii::$app->request->get('fileName')[0];
  178. if ($data['status'] == true) {
  179. $model = new \common\models\ars\TemFile();
  180. $model->user_id = Yii::$app->user->identity->id;
  181. $model->name = $file_name;
  182. $file_manager = new \backend\logic\file\FileManager();
  183. $type_res = $file_manager->searchType(\backend\logic\file\FileManager::$extension, pathinfo($data['path'])['extension']);
  184. if ($type_res['status']) {
  185. $model->type = $type_res['type'];
  186. }
  187. $model->alias = $data['alias'];
  188. $model->path = $data['path'];
  189. $model->save();
  190. return $model->id;
  191. }
  192. }
  193. /**
  194. * @return string
  195. * 点击删除按钮时同时删除字符串中的id
  196. */
  197. public function actionImgIdDel()
  198. {
  199. //判断该类是否存在
  200. if(!class_exists('\common\models\ars\TemFile') || !class_exists('\common\models\ars\File')){
  201. return '';
  202. }
  203. $img_id = Yii::$app->request->get('imgid');
  204. $img_id_arr = explode(',', $img_id);
  205. if(isset(Yii::$app->request->get('data')['alias'])) {
  206. $alias = Yii::$app->request->get('data')['alias'];
  207. $tem_file = \common\models\ars\TemFile::findOne(['alias' => $alias]);
  208. if ($tem_file) {
  209. $img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
  210. }
  211. }else{
  212. foreach (Yii::$app->request->get() as $key => $value) {
  213. $tem_file = \common\models\ars\File::findOne(['alias' => $value]);
  214. if ($tem_file) {
  215. $img_id_arr = array_diff($img_id_arr, [$tem_file->id]);
  216. }
  217. }
  218. }
  219. $img_id_str = implode(',', $img_id_arr);
  220. return $img_id_str;
  221. }
  222. /**
  223. * @return bool|false|string
  224. * 加载已有的文件
  225. */
  226. public function actionImageFile()
  227. {
  228. //判断该类是否存在
  229. if(!class_exists('\common\models\ars\File')){
  230. return false;
  231. }
  232. $file_id_str = Yii::$app->request->get('fileidstr');
  233. $file_id_arr = explode(',', $file_id_str);
  234. $data = \common\models\ars\File::find()->where(['id' => $file_id_arr])->all();
  235. $res = array();
  236. if($data) {
  237. $i = 0;
  238. foreach ($data as $key => $value) {
  239. $res[$i]['name'] = $value->alias;
  240. $res[$i]['path'] = Yii::$app->request->hostInfo . '/' . $value->path;
  241. $res[$i]['size'] = filesize($value->path);
  242. $i++;
  243. }
  244. }
  245. return json_encode($res);
  246. }
  247. }