| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246 |
- <?php
- namespace backend\modules\admin\controllers;
- use backend\controllers\AdminController;
- use common\enums\StatusEnum;
- use common\models\common\AddonsLink;
- use common\models\diy\DiyPage;
- use common\models\goods\Goods;
- use common\models\goods\GoodsCate;
- use common\models\link\Link;
- use common\models\link\LinkCategory;
- use \Yii;
- class LinkController extends AdminController
- {
- /**
- *
- * 链接分类管理页
- * @return mixed|string|void
- */
- public function actionCateIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isGet) {
- $params = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['page', 'limit'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'created_at desc';
- $params = array('where' => $where, 'order'=>$order, 'limit' => $params['limit']);
- $list = LinkCategory::listPage($params, false, true);
- foreach ($list['list'] as &$item) {
- $item['created_at'] = date('Y-m-d H:i:s', $item['created_at']);
- }
- $this->success('获取成功', $list);
- }
- } else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 链接分类编辑页
- * @return mixed|string|void
- */
- public function actionCateEdit()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax) {
- if ($retuest->isPost) {
- $params = Yii::$app->request->post('detail');
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['title','name'], 'required'],
- [['pid','is_list'], 'integer'],
- [['title','name'], 'string'],
- [['id'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 保存数据
- $category = new LinkCategory();
- $res = $category::setData($params);
- if ($res == false) return $this->error('保存失败,msg=' . $category->getError());
- $this->success('编辑成功');
- } else {
- $params = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['id'], 'integer'],
- [['id'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 获取所有分类
- $cate = LinkCategory::getLinkCategory([], [], true, 'id,name,title,pid,is_list,remark');
- // 对分类进行树形拼接
- $menu = LinkCategory::getTreeMenu($cate);
- // 获取数据
- $detail = (object) array();
- if (!empty($params['id'])) {
- $cate = array_column($cate, null, 'id');
- $detail = $cate[$params['id']] ?? (object)[];
- }
- $this->success('获取成功', compact('detail', 'menu'));
- }
- } else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 链接分类删除
- * @return mixed|void
- */
- public function actionCateDelete()
- {
- $request = Yii::$app->request;
- if ($request->isPost) {
- try{
- $params = $request->post();
- $res = Yii::$app->services->validate->validate($params,[
- [['id'], 'required'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $detail = LinkCategory::findOne(['id' => $params['id'], 'status' => StatusEnum::ENABLED]);
- if (empty($detail)) throw new \Exception("页面不存在");
- $has_link = Link::findOne(['cat_id' => $detail->id]);
- if ($has_link) throw new \Exception("该分类下存在链接,不可删除");
- $detail->status = StatusEnum::DELETE;
- if (!$detail->save()) throw new \Exception("删除失败");
- $this->success('删除成功');
- } catch (\Exception $e) {
- $this->error($e->getMessage());
- }
- }
- }
- /**
- * 链接管理页
- * @return mixed|string|void
- */
- public function actionIndex()
- {
- if (Yii::$app->request->isAjax) {
- if (Yii::$app->request->isPost) {
- $params = Yii::$app->request->post();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['cat_id' ,'page', 'limit'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- $where = [];
- $where[] = ['=', 'status', StatusEnum::ENABLED];
- $order = 'id asc';
- $params = array('where' => $where, 'order'=>$order, 'limit' => $params['limit']);
- $list = Link::listPage($params, false, true);
- if (empty($list)) $this->success('获取成功', $list);
- // 获取分类
- $catName = LinkCategory::getCategoryName();
- foreach ($list['list'] as &$item) {
- $item['cat_name'] = $catName[$item['cat_id']];
- $item['h5_route'] = Yii::$app->request->hostInfo . '/h5/#' . $item['route'];
- }
- $this->success('获取成功', $list);
- }
- } else {
- return $this->render($this->action->id);
- }
- }
- /**
- * 链接编辑页
- * @return mixed|string|void
- */
- public function actionEdit()
- {
- $retuest = Yii::$app->request;
- if ($retuest->isAjax) {
- if ($retuest->isPost) {
- $params = Yii::$app->request->post('detail');
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['title','cat_id','open_type'], 'required'],
- [['title','open_type','route'], 'string'],
- [['id','route'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 判断是编辑还是新增数据
- if (empty($params['id'])) {
- $detail = new Link();
- } else {
- $detail = Link::findOne(['id' => $params['id'], 'status' => StatusEnum::ENABLED]);
- if (empty($detail)) $this->error('无法查询到分类数据');
- }
- // 数据赋值
- $detail->attributes = $params;
- if (!$detail->save()) $this->error('编辑失败');
- $this->success('编辑成功');
- } else {
- $params = Yii::$app->request->get();
- // 检查提交的参数
- $res = Yii::$app->services->validate->validate($params,[
- [['id'], 'integer'],
- [['id'], 'safe'],
- ]);
- if($res === false) return $this->error(Yii::$app->services->validate->getErrorMessage());
- // 获取数据
- $detail = [];
- if (!empty($params['id'])) {
- $detail = Link::find()->where(['id' => $params['id'], 'status' => StatusEnum::ENABLED])->asArray()->one();
- }
- // 获取所有分类
- $cate = LinkCategory::getLinkCategory([], [], true, 'id,name,title,pid');
- // 对分类进行树形拼接
- $menu = LinkCategory::getTreeMenu($cate);
- // 类型
- $open_type_list = Link::OPEN_TYPE;
- $this->success('获取成功', compact('detail', 'menu' ,'open_type_list'));
- }
- } else {
- return $this->render($this->action->id);
- }
- }
- }
|