Link.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace app\admin\controller;
  3. use think\facade\View;
  4. use think\facade\Lang;
  5. /**
  6. * ============================================================================
  7. *
  8. * ============================================================================
  9. * 版权所有 2014-2028 浙江惠利玛产业互联网有限公司,并保留所有权利。
  10. * 网站地址: https://www.valimart.net/
  11. * ----------------------------------------------------------------------------
  12. *
  13. * ============================================================================
  14. * 控制器
  15. */
  16. class Link extends AdminControl {
  17. public function initialize() {
  18. parent::initialize();
  19. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/link.lang.php');
  20. }
  21. public function index() {
  22. $condition = array();
  23. $link_title = input('get.link_title');
  24. if ($link_title) {
  25. $condition[] = array('link_title', 'like', "%$link_title%");
  26. }
  27. $link_model = model('link');
  28. $link_list = $link_model->getLinkList($condition, 10);
  29. View::assign('link_list', $link_list);
  30. View::assign('show_page', $link_model->page_info->render());
  31. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  32. $this->setAdminCurItem('index');
  33. return View::fetch('');
  34. }
  35. /**
  36. * 新增友情链接
  37. * */
  38. public function add() {
  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. $link_id = input('param.link_id');
  84. if (empty($link_id)) {
  85. $this->error(lang('param_error'));
  86. }
  87. $link = model('link')->getOneLink($link_id);
  88. if (!request()->isPost()) {
  89. View::assign('link', $link);
  90. return View::fetch('form');
  91. } else {
  92. $data = array(
  93. 'link_title' => input('post.link_title'),
  94. 'link_sort' => input('post.link_sort'),
  95. 'link_url' => input('post.link_url'),
  96. );
  97. //上传图片
  98. if ($_FILES['link_pic']['name'] != '') {
  99. $upload_file = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . DIR_ADMIN . DIRECTORY_SEPARATOR . 'link';
  100. $file_name = date('YmdHis') . rand(10000, 99999).'.png';
  101. $res=ds_upload_pic(DIR_ADMIN . DIRECTORY_SEPARATOR . 'link','link_pic',$file_name);
  102. if($res['code']){
  103. $file_name=$res['data']['file_name'];
  104. $data['link_pic'] = $file_name;
  105. //删除原有友情链接图片
  106. @unlink($upload_file . DIRECTORY_SEPARATOR . $link['link_pic']);
  107. }else{
  108. $this->error($res['msg']);
  109. }
  110. }
  111. $link_validate = ds_validate('link');
  112. if (!$link_validate->scene('edit')->check($data)) {
  113. $this->error($link_validate->getError());
  114. }
  115. $result = model('link')->editLink($data, $link_id);
  116. if ($result >= 0) {
  117. dsLayerOpenSuccess(lang('ds_common_save_succ'), (string) url('Link/index'));
  118. } else {
  119. $this->error(lang('ds_common_save_fail'));
  120. }
  121. }
  122. }
  123. public function drop() {
  124. $link_id = intval(input('param.link_id'));
  125. if (empty($link_id)) {
  126. $this->error(lang('param_error'));
  127. }
  128. $result = model('link')->delLink($link_id);
  129. if ($result) {
  130. ds_json_encode(10000, lang('ds_common_op_succ'));
  131. } else {
  132. ds_json_encode(10001, lang('ds_common_op_fail'));
  133. }
  134. }
  135. /**
  136. * ajax操作
  137. */
  138. public function ajax() {
  139. $result = -1;
  140. switch (input('get.branch')) {
  141. case 'link':
  142. $model_link = model('link');
  143. $link_id = intval(input('get.id'));
  144. $update_array = array();
  145. $update_array[input('get.column')] = trim(input('get.value'));
  146. $result = $model_link->editLink($update_array, $link_id);
  147. break;
  148. }
  149. if ($result >= 0) {
  150. echo 'true';
  151. }
  152. }
  153. /**
  154. * 获取卖家栏目列表,针对控制器下的栏目
  155. */
  156. protected function getAdminItemList() {
  157. $menu_array = array(
  158. array(
  159. 'name' => 'index',
  160. 'text' => lang('ds_manage'),
  161. 'url' => (string) url('Link/index')
  162. ),
  163. array(
  164. 'name' => 'add',
  165. 'text' => lang('ds_add'),
  166. 'url' => "javascript:dsLayerOpen('" . (string) url('Link/add') . "','" . lang('ds_add') . "')"
  167. )
  168. );
  169. return $menu_array;
  170. }
  171. }