LinkController.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. namespace backend\modules\admin\controllers;
  3. use backend\controllers\AdminController;
  4. use common\enums\StatusEnum;
  5. use common\models\common\AddonsLink;
  6. use common\models\diy\DiyPage;
  7. use common\models\goods\Goods;
  8. use common\models\goods\GoodsCate;
  9. use common\models\link\Link;
  10. use common\models\link\LinkCategory;
  11. use \Yii;
  12. class LinkController extends AdminController
  13. {
  14. /**
  15. *
  16. * 链接分类管理页
  17. * @return mixed|string|void
  18. */
  19. public function actionCateIndex()
  20. {
  21. if (Yii::$app->request->isAjax) {
  22. if (Yii::$app->request->isGet) {
  23. $params = Yii::$app->request->get();
  24. // 检查提交的参数
  25. $res = Yii::$app->services->validate->validate($params,[
  26. [['page', 'limit'], 'safe'],
  27. ]);
  28. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  29. $where = [];
  30. $where[] = ['=', 'status', StatusEnum::ENABLED];
  31. $order = 'created_at desc';
  32. $params = array('where' => $where, 'order'=>$order, 'limit' => $params['limit']);
  33. $list = LinkCategory::listPage($params, false, true);
  34. foreach ($list['list'] as &$item) {
  35. $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
  36. }
  37. $this->success('获取成功', $list);
  38. }
  39. } else {
  40. return $this->render($this->action->id);
  41. }
  42. }
  43. /**
  44. * 链接分类编辑页
  45. * @return mixed|string|void
  46. */
  47. public function actionCateEdit()
  48. {
  49. $retuest = Yii::$app->request;
  50. if ($retuest->isAjax) {
  51. if ($retuest->isPost) {
  52. $params = Yii::$app->request->post('detail');
  53. // 检查提交的参数
  54. $res = Yii::$app->services->validate->validate($params,[
  55. [['title','name'], 'required'],
  56. [['pid','is_list'], 'integer'],
  57. [['title','name'], 'string'],
  58. [['id'], 'safe'],
  59. ]);
  60. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  61. // 保存数据
  62. $category = new LinkCategory();
  63. $res = $category::setData($params);
  64. if ($res == false) return $this->error('保存失败,msg=' . $category->getError());
  65. $this->success('编辑成功');
  66. } else {
  67. $params = Yii::$app->request->get();
  68. // 检查提交的参数
  69. $res = Yii::$app->services->validate->validate($params,[
  70. [['id'], 'integer'],
  71. [['id'], 'safe'],
  72. ]);
  73. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  74. // 获取所有分类
  75. $cate = LinkCategory::getLinkCategory([], [], true, 'id,name,title,pid,is_list,remark');
  76. // 对分类进行树形拼接
  77. $menu = LinkCategory::getTreeMenu($cate);
  78. // 获取数据
  79. $detail = (object) array();
  80. if (!empty($params['id'])) {
  81. $cate = array_column($cate, null, 'id');
  82. $detail = $cate[$params['id']] ?? (object)[];
  83. }
  84. $this->success('获取成功', compact('detail', 'menu'));
  85. }
  86. } else {
  87. return $this->render($this->action->id);
  88. }
  89. }
  90. /**
  91. * 链接分类删除
  92. * @return mixed|void
  93. */
  94. public function actionCateDelete()
  95. {
  96. $request = Yii::$app->request;
  97. if ($request->isPost) {
  98. try{
  99. $params = $request->post();
  100. $res = Yii::$app->services->validate->validate($params,[
  101. [['id'], 'required'],
  102. ]);
  103. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  104. $detail = LinkCategory::findOne(['id' => $params['id'], 'status' => StatusEnum::ENABLED]);
  105. if (empty($detail)) throw new \Exception("页面不存在");
  106. $has_link = Link::findOne(['cat_id' => $detail->id]);
  107. if ($has_link) throw new \Exception("该分类下存在链接,不可删除");
  108. $detail->status = StatusEnum::DELETE;
  109. if (!$detail->save()) throw new \Exception("删除失败");
  110. $this->success('删除成功');
  111. } catch (\Exception $e) {
  112. $this->error($e->getMessage());
  113. }
  114. }
  115. }
  116. /**
  117. * 链接管理页
  118. * @return mixed|string|void
  119. */
  120. public function actionIndex()
  121. {
  122. if (Yii::$app->request->isAjax) {
  123. if (Yii::$app->request->isPost) {
  124. $params = Yii::$app->request->post();
  125. // 检查提交的参数
  126. $res = Yii::$app->services->validate->validate($params,[
  127. [['cat_id' ,'page', 'limit'], 'safe'],
  128. ]);
  129. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  130. $where = [];
  131. $where[] = ['=', 'status', StatusEnum::ENABLED];
  132. $order = 'id asc';
  133. $params = array('where' => $where, 'order'=>$order, 'limit' => $params['limit']);
  134. $list = Link::listPage($params, false, true);
  135. if (empty($list)) $this->success('获取成功', $list);
  136. // 获取分类
  137. $catName = LinkCategory::getCategoryName();
  138. foreach ($list['list'] as &$item) {
  139. $item['cat_name'] = $catName[$item['cat_id']];
  140. $item['h5_route'] = Yii::$app->request->hostInfo . '/h5/#' . $item['route'];
  141. }
  142. $this->success('获取成功', $list);
  143. }
  144. } else {
  145. return $this->render($this->action->id);
  146. }
  147. }
  148. /**
  149. * 链接编辑页
  150. * @return mixed|string|void
  151. */
  152. public function actionEdit()
  153. {
  154. $retuest = Yii::$app->request;
  155. if ($retuest->isAjax) {
  156. if ($retuest->isPost) {
  157. $params = Yii::$app->request->post('detail');
  158. // 检查提交的参数
  159. $res = Yii::$app->services->validate->validate($params,[
  160. [['title','cat_id','open_type'], 'required'],
  161. [['title','open_type','route'], 'string'],
  162. [['id','route'], 'safe'],
  163. ]);
  164. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  165. // 判断是编辑还是新增数据
  166. if (empty($params['id'])) {
  167. $detail = new Link();
  168. } else {
  169. $detail = Link::findOne(['id' => $params['id'], 'status' => StatusEnum::ENABLED]);
  170. if (empty($detail)) $this->error('无法查询到分类数据');
  171. }
  172. // 数据赋值
  173. $detail->attributes = $params;
  174. if (!$detail->save()) $this->error('编辑失败');
  175. $this->success('编辑成功');
  176. } else {
  177. $params = Yii::$app->request->get();
  178. // 检查提交的参数
  179. $res = Yii::$app->services->validate->validate($params,[
  180. [['id'], 'integer'],
  181. [['id'], 'safe'],
  182. ]);
  183. if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
  184. // 获取数据
  185. $detail = [];
  186. if (!empty($params['id'])) {
  187. $detail = Link::find()->where(['id' => $params['id'], 'status' => StatusEnum::ENABLED])->asArray()->one();
  188. }
  189. // 获取所有分类
  190. $cate = LinkCategory::getLinkCategory([], [], true, 'id,name,title,pid');
  191. // 对分类进行树形拼接
  192. $menu = LinkCategory::getTreeMenu($cate);
  193. // 类型
  194. $open_type_list = Link::OPEN_TYPE;
  195. $this->success('获取成功', compact('detail', 'menu' ,'open_type_list'));
  196. }
  197. } else {
  198. return $this->render($this->action->id);
  199. }
  200. }
  201. }