Link.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. *
  7. *
  8. * ----------------------------------------------------------------------------
  9. *
  10. * 控制器
  11. */
  12. class Link extends AdminControl
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/link.lang.php');
  18. }
  19. public function index()
  20. {
  21. $condition = array();
  22. $link_title = input('get.link_title');
  23. if ($link_title) {
  24. $condition[] = array('link_title', 'like', "%$link_title%");
  25. }
  26. $link_model = model('link');
  27. $link_list = $link_model->getLinkList($condition, 10);
  28. View::assign('link_list', $link_list);
  29. View::assign('show_page', $link_model->page_info->render());
  30. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  31. $this->setAdminCurItem('index');
  32. return View::fetch('');
  33. }
  34. /**
  35. * 新增友情链接
  36. * */
  37. public function add()
  38. {
  39. if (!(request()->isPost())) {
  40. $link = [
  41. 'link_id' => '',
  42. 'link_title' => '',
  43. 'link_pic' => '',
  44. 'link_url' => '',
  45. 'link_sort' => 255,
  46. ];
  47. View::assign('link', $link);
  48. return View::fetch('form');
  49. } else {
  50. //上传图片
  51. $link_pic = '';
  52. if ($_FILES['link_pic']['name'] != '') {
  53. $file_name = date('YmdHis') . rand(10000, 99999) . '.png';
  54. $res = ds_upload_pic(DIR_ADMIN . DIRECTORY_SEPARATOR . 'link', 'link_pic', $file_name);
  55. if ($res['code']) {
  56. $link_pic = $res['data']['file_name'];
  57. } else {
  58. $this->error($res['msg']);
  59. }
  60. }
  61. $data = array(
  62. 'link_title' => input('post.link_title'),
  63. 'link_pic' => $link_pic,
  64. 'link_url' => input('post.link_url'),
  65. 'link_sort' => input('post.link_sort'),
  66. );
  67. $link_validate = ds_validate('link');
  68. if (!$link_validate->scene('add')->check($data)) {
  69. $this->error($link_validate->getError());
  70. }
  71. $result = model('link')->addLink($data);
  72. if ($result) {
  73. dsLayerOpenSuccess(lang('ds_common_save_succ'), (string) url('Link/index'));
  74. } else {
  75. $this->error(lang('ds_common_save_fail'));
  76. }
  77. }
  78. }
  79. /**
  80. * 编辑友情链接
  81. * */
  82. public function edit()
  83. {
  84. $link_id = input('param.link_id');
  85. if (empty($link_id)) {
  86. $this->error(lang('param_error'));
  87. }
  88. $link = model('link')->getOneLink($link_id);
  89. if (!request()->isPost()) {
  90. View::assign('link', $link);
  91. return View::fetch('form');
  92. } else {
  93. $data = array(
  94. 'link_title' => input('post.link_title'),
  95. 'link_sort' => input('post.link_sort'),
  96. 'link_url' => input('post.link_url'),
  97. );
  98. //上传图片
  99. if ($_FILES['link_pic']['name'] != '') {
  100. $upload_file = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . DIR_ADMIN . DIRECTORY_SEPARATOR . 'link';
  101. $file_name = date('YmdHis') . rand(10000, 99999) . '.png';
  102. $res = ds_upload_pic(DIR_ADMIN . DIRECTORY_SEPARATOR . 'link', 'link_pic', $file_name);
  103. if ($res['code']) {
  104. $file_name = $res['data']['file_name'];
  105. $data['link_pic'] = $file_name;
  106. //删除原有友情链接图片
  107. @unlink($upload_file . DIRECTORY_SEPARATOR . $link['link_pic']);
  108. } else {
  109. $this->error($res['msg']);
  110. }
  111. }
  112. $link_validate = ds_validate('link');
  113. if (!$link_validate->scene('edit')->check($data)) {
  114. $this->error($link_validate->getError());
  115. }
  116. $result = model('link')->editLink($data, $link_id);
  117. if ($result >= 0) {
  118. dsLayerOpenSuccess(lang('ds_common_save_succ'), (string) url('Link/index'));
  119. } else {
  120. $this->error(lang('ds_common_save_fail'));
  121. }
  122. }
  123. }
  124. public function drop()
  125. {
  126. $link_id = intval(input('param.link_id'));
  127. if (empty($link_id)) {
  128. $this->error(lang('param_error'));
  129. }
  130. $result = model('link')->delLink($link_id);
  131. if ($result) {
  132. ds_json_encode(10000, lang('ds_common_op_succ'));
  133. } else {
  134. ds_json_encode(10001, lang('ds_common_op_fail'));
  135. }
  136. }
  137. /**
  138. * ajax操作
  139. */
  140. public function ajax()
  141. {
  142. $result = -1;
  143. switch (input('get.branch')) {
  144. case 'link':
  145. $model_link = model('link');
  146. $link_id = intval(input('get.id'));
  147. $update_array = array();
  148. $update_array[input('get.column')] = trim(input('get.value'));
  149. $result = $model_link->editLink($update_array, $link_id);
  150. break;
  151. }
  152. if ($result >= 0) {
  153. echo 'true';
  154. }
  155. }
  156. /**
  157. * 获取卖家栏目列表,针对控制器下的栏目
  158. */
  159. protected function getAdminItemList()
  160. {
  161. $menu_array = array(
  162. array(
  163. 'name' => 'index',
  164. 'text' => lang('ds_manage'),
  165. 'url' => (string) url('Link/index')
  166. ),
  167. array(
  168. 'name' => 'add',
  169. 'text' => lang('ds_add'),
  170. 'url' => "javascript:dsLayerOpen('" . (string) url('Link/add') . "','" . lang('ds_add') . "')"
  171. )
  172. );
  173. return $menu_array;
  174. }
  175. }