LinkController.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. // +—————————————————————————————————————————————————————————————————————
  3. // | Created by Yunbao
  4. // +—————————————————————————————————————————————————————————————————————
  5. // | Copyright (c) 2013~2022 http://www.yunbaokj.com All rights reserved.
  6. // +—————————————————————————————————————————————————————————————————————
  7. // | Author: https://gitee.com/yunbaokeji
  8. // +—————————————————————————————————————————————————————————————————————
  9. // | Date: 2022-04-30
  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. $data = $this->request->param();
  79. $linkModel = new LinkModel();
  80. $result = $this->validate($data, 'Link');
  81. if ($result !== true) {
  82. $this->error($result);
  83. }
  84. $linkModel->allowField(true)->save($data);
  85. $this->success("添加成功!", url("Link/index"));
  86. }
  87. /**
  88. * 编辑友情链接
  89. * @adminMenu(
  90. * 'name' => '编辑友情链接',
  91. * 'parent' => 'index',
  92. * 'display'=> false,
  93. * 'hasView'=> true,
  94. * 'order' => 10000,
  95. * 'icon' => '',
  96. * 'remark' => '编辑友情链接',
  97. * 'param' => ''
  98. * )
  99. * @return mixed
  100. * @throws \think\Exception\DbException
  101. */
  102. public function edit()
  103. {
  104. $id = $this->request->param('id', 0, 'intval');
  105. $linkModel = new LinkModel();
  106. $link = $linkModel->get($id);
  107. $this->assign('targets', $this->targets);
  108. $this->assign('link', $link);
  109. return $this->fetch();
  110. }
  111. /**
  112. * 编辑友情链接提交保存
  113. * @adminMenu(
  114. * 'name' => '编辑友情链接提交保存',
  115. * 'parent' => 'index',
  116. * 'display'=> false,
  117. * 'hasView'=> false,
  118. * 'order' => 10000,
  119. * 'icon' => '',
  120. * 'remark' => '编辑友情链接提交保存',
  121. * 'param' => ''
  122. * )
  123. */
  124. public function editPost()
  125. {
  126. $data = $this->request->param();
  127. $linkModel = new LinkModel();
  128. $result = $this->validate($data, 'Link');
  129. if ($result !== true) {
  130. $this->error($result);
  131. }
  132. $linkModel->allowField(true)->isUpdate(true)->save($data);
  133. $this->success("保存成功!", url("Link/index"));
  134. }
  135. /**
  136. * 删除友情链接
  137. * @adminMenu(
  138. * 'name' => '删除友情链接',
  139. * 'parent' => 'index',
  140. * 'display'=> false,
  141. * 'hasView'=> false,
  142. * 'order' => 10000,
  143. * 'icon' => '',
  144. * 'remark' => '删除友情链接',
  145. * 'param' => ''
  146. * )
  147. */
  148. public function delete()
  149. {
  150. $id = $this->request->param('id', 0, 'intval');
  151. LinkModel::destroy($id);
  152. $this->success("删除成功!", url("link/index"));
  153. }
  154. /**
  155. * 友情链接排序
  156. * @adminMenu(
  157. * 'name' => '友情链接排序',
  158. * 'parent' => 'index',
  159. * 'display'=> false,
  160. * 'hasView'=> false,
  161. * 'order' => 10000,
  162. * 'icon' => '',
  163. * 'remark' => '友情链接排序',
  164. * 'param' => ''
  165. * )
  166. */
  167. public function listOrder()
  168. {
  169. $linkModel = new LinkModel();
  170. parent::listOrders($linkModel);
  171. $this->success("排序更新成功!");
  172. }
  173. /**
  174. * 友情链接显示隐藏
  175. * @adminMenu(
  176. * 'name' => '友情链接显示隐藏',
  177. * 'parent' => 'index',
  178. * 'display'=> false,
  179. * 'hasView'=> false,
  180. * 'order' => 10000,
  181. * 'icon' => '',
  182. * 'remark' => '友情链接显示隐藏',
  183. * 'param' => ''
  184. * )
  185. */
  186. public function toggle()
  187. {
  188. $data = $this->request->param();
  189. $linkModel = new LinkModel();
  190. if (isset($data['ids']) && !empty($data["display"])) {
  191. $ids = $this->request->param('ids/a');
  192. $linkModel->where('id', 'in', $ids)->update(['status' => 1]);
  193. $this->success("更新成功!");
  194. }
  195. if (isset($data['ids']) && !empty($data["hide"])) {
  196. $ids = $this->request->param('ids/a');
  197. $linkModel->where('id', 'in', $ids)->update(['status' => 0]);
  198. $this->success("更新成功!");
  199. }
  200. }
  201. }