LinkController.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
  8. // +----------------------------------------------------------------------
  9. // | Author: 小夏 < 449134904@qq.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\admin\controller;
  12. use cmf\controller\AdminBaseController;
  13. use app\admin\model\LinkModel;
  14. use think\Db;
  15. class LinkController extends AdminBaseController
  16. {
  17. protected $targets = ["_blank" => "新标签页打开", "_self" => "本窗口打开"];
  18. /**
  19. * 友情链接管理
  20. * @adminMenu(
  21. * 'name' => '友情链接',
  22. * 'parent' => 'admin/Setting/default',
  23. * 'display'=> true,
  24. * 'hasView'=> true,
  25. * 'order' => 50,
  26. * 'icon' => '',
  27. * 'remark' => '友情链接管理',
  28. * 'param' => ''
  29. * )
  30. * @return mixed
  31. * @throws \think\db\exception\DataNotFoundException
  32. * @throws \think\db\exception\ModelNotFoundException
  33. * @throws \think\exception\DbException
  34. */
  35. public function index()
  36. {
  37. $content = hook_one('admin_link_index_view');
  38. if (!empty($content)) {
  39. return $content;
  40. }
  41. //分类
  42. $cates = Db::name('category')->where('parent_id',0)->order('list_order')->select();
  43. $catelist = array(
  44. 'index' => '首页',
  45. 'news' => '新闻页'
  46. );
  47. foreach($cates as $k=>$v){
  48. $catelist[$v['enname']] = $v['catname'];
  49. }
  50. $linkModel = new LinkModel();
  51. $links = $linkModel->select();
  52. $this->assign('links', $links);
  53. $this->assign('catelist',$catelist);
  54. return $this->fetch();
  55. }
  56. /**
  57. * 添加友情链接
  58. * @adminMenu(
  59. * 'name' => '添加友情链接',
  60. * 'parent' => 'index',
  61. * 'display'=> false,
  62. * 'hasView'=> true,
  63. * 'order' => 10000,
  64. * 'icon' => '',
  65. * 'remark' => '添加友情链接',
  66. * 'param' => ''
  67. * )
  68. */
  69. public function add()
  70. {
  71. $this->assign('targets', $this->targets);
  72. //分类
  73. $cates = Db::name('category')->where('parent_id',0)->order('list_order')->select();
  74. $this->assign('cates',$cates);
  75. return $this->fetch();
  76. }
  77. /**
  78. * 添加友情链接提交保存
  79. * @adminMenu(
  80. * 'name' => '添加友情链接提交保存',
  81. * 'parent' => 'index',
  82. * 'display'=> false,
  83. * 'hasView'=> false,
  84. * 'order' => 10000,
  85. * 'icon' => '',
  86. * 'remark' => '添加友情链接提交保存',
  87. * 'param' => ''
  88. * )
  89. */
  90. public function addPost()
  91. {
  92. $data = $this->request->param();
  93. $res = Db::name('link')->insert($data);
  94. if($res){
  95. $this->success("添加成功!", url("Link/index"));
  96. }else{
  97. $this->error('添加失败');
  98. }
  99. }
  100. /**
  101. * 编辑友情链接
  102. * @adminMenu(
  103. * 'name' => '编辑友情链接',
  104. * 'parent' => 'index',
  105. * 'display'=> false,
  106. * 'hasView'=> true,
  107. * 'order' => 10000,
  108. * 'icon' => '',
  109. * 'remark' => '编辑友情链接',
  110. * 'param' => ''
  111. * )
  112. * @return mixed
  113. * @throws \think\Exception\DbException
  114. */
  115. public function edit()
  116. {
  117. $id = $this->request->param('id', 0, 'intval');
  118. $linkModel = new LinkModel();
  119. $link = $linkModel->get($id);
  120. $this->assign('targets', $this->targets);
  121. $this->assign('link', $link);
  122. return $this->fetch();
  123. }
  124. /**
  125. * 编辑友情链接提交保存
  126. * @adminMenu(
  127. * 'name' => '编辑友情链接提交保存',
  128. * 'parent' => 'index',
  129. * 'display'=> false,
  130. * 'hasView'=> false,
  131. * 'order' => 10000,
  132. * 'icon' => '',
  133. * 'remark' => '编辑友情链接提交保存',
  134. * 'param' => ''
  135. * )
  136. */
  137. public function editPost()
  138. {
  139. $data = $this->request->param();
  140. $linkModel = new LinkModel();
  141. $result = $this->validate($data, 'Link');
  142. if ($result !== true) {
  143. $this->error($result);
  144. }
  145. $linkModel->allowField(true)->isUpdate(true)->save($data);
  146. $this->success("保存成功!", url("Link/index"));
  147. }
  148. /**
  149. * 删除友情链接
  150. * @adminMenu(
  151. * 'name' => '删除友情链接',
  152. * 'parent' => 'index',
  153. * 'display'=> false,
  154. * 'hasView'=> false,
  155. * 'order' => 10000,
  156. * 'icon' => '',
  157. * 'remark' => '删除友情链接',
  158. * 'param' => ''
  159. * )
  160. */
  161. public function delete()
  162. {
  163. $id = $this->request->param('id', 0, 'intval');
  164. LinkModel::destroy($id);
  165. $this->success("删除成功!", url("link/index"));
  166. }
  167. /**
  168. * 友情链接排序
  169. * @adminMenu(
  170. * 'name' => '友情链接排序',
  171. * 'parent' => 'index',
  172. * 'display'=> false,
  173. * 'hasView'=> false,
  174. * 'order' => 10000,
  175. * 'icon' => '',
  176. * 'remark' => '友情链接排序',
  177. * 'param' => ''
  178. * )
  179. */
  180. public function listOrder()
  181. {
  182. $linkModel = new LinkModel();
  183. parent::listOrders($linkModel);
  184. $this->success("排序更新成功!");
  185. }
  186. /**
  187. * 友情链接显示隐藏
  188. * @adminMenu(
  189. * 'name' => '友情链接显示隐藏',
  190. * 'parent' => 'index',
  191. * 'display'=> false,
  192. * 'hasView'=> false,
  193. * 'order' => 10000,
  194. * 'icon' => '',
  195. * 'remark' => '友情链接显示隐藏',
  196. * 'param' => ''
  197. * )
  198. */
  199. public function toggle()
  200. {
  201. $data = $this->request->param();
  202. $linkModel = new LinkModel();
  203. if (isset($data['ids']) && !empty($data["display"])) {
  204. $ids = $this->request->param('ids/a');
  205. $linkModel->where('id', 'in', $ids)->update(['status' => 1]);
  206. $this->success("更新成功!");
  207. }
  208. if (isset($data['ids']) && !empty($data["hide"])) {
  209. $ids = $this->request->param('ids/a');
  210. $linkModel->where('id', 'in', $ids)->update(['status' => 0]);
  211. $this->success("更新成功!");
  212. }
  213. }
  214. }