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