LinkController.php 6.5 KB

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