Adv.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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 Adv extends AdminControl
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/adv.lang.php');
  18. }
  19. /**
  20. *
  21. * 管理广告位
  22. */
  23. public function ap_manage()
  24. {
  25. $adv_model = model('adv');
  26. /**
  27. * 多选删除广告位
  28. */
  29. if (!request()->isPost()) {
  30. /**
  31. * 显示广告位管理界面
  32. */
  33. $condition = array();
  34. $orderby = '';
  35. $search_name = trim(input('get.search_name'));
  36. if ($search_name != '') {
  37. $condition[] = array('ap_name', 'like', "%" . $search_name . "%");
  38. }
  39. $ap_list = $adv_model->getAdvpositionList($condition, '10', $orderby);
  40. $adv_list = $adv_model->getAdvList();
  41. View::assign('ap_list', $ap_list);
  42. View::assign('adv_list', $adv_list);
  43. View::assign('showpage', $adv_model->page_info->render());
  44. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  45. $this->setAdminCurItem('ap_manage');
  46. return View::fetch('ap_manage');
  47. }
  48. }
  49. /**
  50. *
  51. * 修改广告位
  52. */
  53. public function ap_edit()
  54. {
  55. $ap_id = intval(input('param.ap_id'));
  56. $adv_model = model('adv');
  57. if (!request()->isPost()) {
  58. $condition = array();
  59. $condition[] = array('ap_id', '=', $ap_id);
  60. $ap = $adv_model->getOneAdvposition($condition);
  61. View::assign('ref_url', get_referer());
  62. View::assign('ap', $ap);
  63. return View::fetch('ap_form');
  64. } else {
  65. $param['ap_name'] = trim(input('post.ap_name'));
  66. $param['ap_intro'] = trim(input('post.ap_intro'));
  67. $param['ap_width'] = intval(trim(input('post.ap_width')));
  68. $param['ap_height'] = intval(trim(input('post.ap_height')));
  69. if (input('post.ap_isuse') != '') {
  70. $param['ap_isuse'] = intval(input('post.ap_isuse'));
  71. }
  72. $adv_validate = ds_validate('adv');
  73. if (!$adv_validate->scene('ap_edit')->check($param)) {
  74. $this->error($adv_validate->getError());
  75. }
  76. $result = $adv_model->editAdvposition($ap_id, $param);
  77. if ($result >= 0) {
  78. $this->log(lang('ap_change_succ') . '[' . input('post.ap_name') . ']', null);
  79. dsLayerOpenSuccess(lang('ap_change_succ'));
  80. } else {
  81. $this->error(lang('ap_change_fail'));
  82. }
  83. }
  84. }
  85. /**
  86. *
  87. * 新增广告位
  88. */
  89. public function ap_add()
  90. {
  91. if (!request()->isPost()) {
  92. $ap['ap_isuse'] = 1;
  93. View::assign('ap', $ap);
  94. return View::fetch('ap_form');
  95. } else {
  96. $adv_model = model('adv');
  97. $insert_array['ap_name'] = trim(input('post.ap_name'));
  98. $insert_array['ap_intro'] = trim(input('post.ap_intro'));
  99. $insert_array['ap_isuse'] = intval(input('post.ap_isuse'));
  100. $insert_array['ap_width'] = intval(input('post.ap_width'));
  101. $insert_array['ap_height'] = intval(input('post.ap_height'));
  102. $adv_validate = ds_validate('adv');
  103. if (!$adv_validate->scene('ap_add')->check($insert_array)) {
  104. $this->error($adv_validate->getError());
  105. }
  106. $result = $adv_model->addAdvposition($insert_array);
  107. if ($result) {
  108. $this->log(lang('ap_add_succ') . '[' . input('post.ap_name') . ']', null);
  109. dsLayerOpenSuccess(lang('ap_add_succ'));
  110. } else {
  111. $this->error(lang('ap_add_fail'));
  112. }
  113. }
  114. }
  115. /**
  116. *
  117. * 删除广告位
  118. */
  119. public function ap_del()
  120. {
  121. $adv_model = model('adv');
  122. /**
  123. * 删除一个广告
  124. */
  125. $ap_id = intval(input('param.ap_id'));
  126. $result = $adv_model->delAdvposition($ap_id);
  127. if (!$result) {
  128. ds_json_encode('10001', lang('ap_del_fail'));
  129. } else {
  130. $this->log(lang('ap_del_succ') . '[' . $ap_id . ']', null);
  131. ds_json_encode('10000', lang('ap_del_succ'));
  132. }
  133. }
  134. /**
  135. *
  136. * 广告管理
  137. */
  138. public function adv()
  139. {
  140. $adv_model = model('adv');
  141. $ap_id = intval(input('param.ap_id'));
  142. if (!request()->isPost()) {
  143. $condition = array();
  144. if ($ap_id) {
  145. $condition[] = array('ap_id', '=', $ap_id);
  146. }
  147. $adv_info = $adv_model->getAdvList($condition, 20, '', '');
  148. View::assign('adv_info', $adv_info);
  149. $ap_list = $adv_model->getAdvpositionList();
  150. View::assign('ap_list', $ap_list);
  151. if ($ap_id) {
  152. $ap_condition = array();
  153. $ap_condition['ap_id'] = $ap_id;
  154. $ap = $adv_model->getOneAdvposition($ap_condition);
  155. View::assign('ap_name', $ap['ap_name']);
  156. } else {
  157. View::assign('ap_name', '');
  158. }
  159. View::assign('show_page', $adv_model->page_info->render());
  160. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  161. $this->setAdminCurItem('adv');
  162. return View::fetch('adv_index');
  163. }
  164. }
  165. /**
  166. * 管理员添加广告
  167. */
  168. public function adv_add()
  169. {
  170. $adv_model = model('adv');
  171. if (!request()->isPost()) {
  172. $ap_list = $adv_model->getAdvpositionList();
  173. View::assign('ap_list', $ap_list);
  174. $adv = array(
  175. 'ap_id' => 0,
  176. 'adv_enabled' => '1',
  177. 'adv_startdate' => TIMESTAMP,
  178. 'adv_enddate' => TIMESTAMP + 24 * 3600 * 365,
  179. );
  180. View::assign('adv', $adv);
  181. return View::fetch('adv_form');
  182. } else {
  183. $insert_array['ap_id'] = intval(input('post.ap_id'));
  184. $insert_array['adv_title'] = trim(input('post.adv_name'));
  185. $insert_array['adv_link'] = input('post.adv_link');
  186. $insert_array['adv_bgcolor'] = input('post.adv_bgcolor');
  187. $insert_array['adv_sort'] = input('post.adv_sort');
  188. $insert_array['adv_enabled'] = input('post.adv_enabled');
  189. $insert_array['adv_startdate'] = $this->getunixtime(input('post.adv_startdate'));
  190. $insert_array['adv_enddate'] = $this->getunixtime(input('post.adv_enddate'));
  191. //上传文件保存路径
  192. if (!empty($_FILES['adv_code']['name'])) {
  193. $res = ds_upload_pic(ATTACH_ADV, 'adv_code');
  194. if ($res['code']) {
  195. $file_name = $res['data']['file_name'];
  196. $insert_array['adv_code'] = $file_name;
  197. } else {
  198. $this->error($res['msg']);
  199. }
  200. }
  201. $adv_validate = ds_validate('adv');
  202. if (!$adv_validate->scene('adv_add')->check($insert_array)) {
  203. $this->error($adv_validate->getError());
  204. }
  205. //广告信息入库
  206. $result = $adv_model->addAdv($insert_array);
  207. if ($result) {
  208. $this->log(lang('adv_add_succ') . '[' . input('post.adv_name') . ']', null);
  209. dsLayerOpenSuccess(lang('adv_add_succ'));
  210. // $this->success(lang('adv_add_succ'), (string)url('Adv/adv', ['ap_id' => input('post.ap_id')]));
  211. } else {
  212. $this->error(lang('adv_add_fail'));
  213. }
  214. }
  215. }
  216. /**
  217. *
  218. * 修改广告
  219. */
  220. public function adv_edit()
  221. {
  222. $adv_id = intval(input('param.adv_id'));
  223. $adv_model = model('adv');
  224. //获取指定广告
  225. $condition = array();
  226. $condition[] = array('adv_id', '=', $adv_id);
  227. $adv = $adv_model->getOneAdv($condition);
  228. if (!request()->isPost()) {
  229. //获取广告列表
  230. $ap_list = $adv_model->getAdvpositionList();
  231. View::assign('ap_list', $ap_list);
  232. View::assign('adv', $adv);
  233. View::assign('ref_url', get_referer());
  234. return View::fetch('adv_form');
  235. } else {
  236. $param['ap_id'] = intval(input('post.ap_id'));
  237. $param['adv_title'] = trim(input('post.adv_name'));
  238. $param['adv_link'] = input('post.adv_link');
  239. $param['adv_bgcolor'] = input('post.adv_bgcolor');
  240. $param['adv_sort'] = input('post.adv_sort');
  241. $param['adv_enabled'] = input('post.adv_enabled');
  242. $param['adv_startdate'] = $this->getunixtime(trim(input('post.adv_startdate')));
  243. $param['adv_enddate'] = $this->getunixtime(trim(input('post.adv_enddate')));
  244. if (!empty($_FILES['adv_code']['name'])) {
  245. //上传文件保存路径
  246. $upload_file = BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ADV;
  247. $res = ds_upload_pic(ATTACH_ADV, 'adv_code');
  248. if ($res['code']) {
  249. //还需删除原来图片
  250. if (!empty($adv['adv_code'])) {
  251. @unlink($upload_file . DIRECTORY_SEPARATOR . $adv['adv_code']);
  252. }
  253. $file_name = $res['data']['file_name'];
  254. $param['adv_code'] = $file_name;
  255. } else {
  256. $this->error($res['msg']);
  257. }
  258. }
  259. $adv_validate = ds_validate('adv');
  260. if (!$adv_validate->scene('adv_edit')->check($param)) {
  261. $this->error($adv_validate->getError());
  262. }
  263. $result = $adv_model->editAdv($adv_id, $param);
  264. if ($result >= 0) {
  265. $this->log(lang('adv_change_succ') . '[' . input('post.ap_name') . ']', null);
  266. dsLayerOpenSuccess(lang('adv_change_succ'));
  267. // $this->success(lang('adv_change_succ'), input('post.ref_url'));
  268. } else {
  269. $this->error(lang('adv_change_fail'));
  270. }
  271. }
  272. }
  273. /**
  274. *
  275. * 删除广告
  276. */
  277. public function adv_del()
  278. {
  279. $adv_model = model('adv');
  280. /**
  281. * 删除一个广告
  282. */
  283. $adv_id = intval(input('param.adv_id'));
  284. $result = $adv_model->delAdv($adv_id);
  285. if (!$result) {
  286. ds_json_encode('10001', lang('adv_del_fail'));
  287. } else {
  288. $this->log(lang('adv_del_succ') . '[' . $adv_id . ']', null);
  289. ds_json_encode('10000', lang('adv_del_succ'));
  290. }
  291. }
  292. /**
  293. *
  294. * 获取UNIX时间戳
  295. */
  296. public function getunixtime($time)
  297. {
  298. $array = explode("-", $time);
  299. $unix_time = mktime(0, 0, 0, $array[1], $array[2], $array[0]);
  300. return $unix_time;
  301. }
  302. public function ajax()
  303. {
  304. $adv_model = model('adv');
  305. switch (input('get.branch')) {
  306. case 'ap_branch':
  307. $column = trim(input('param.column'));
  308. $value = trim(input('param.value'));
  309. $ap_id = intval(input('param.id'));
  310. $param[$column] = trim($value);
  311. $result = $adv_model->editAdvposition($ap_id, $param);
  312. break;
  313. //ADV数据表更新
  314. case 'adv_branch':
  315. $column = trim(input('param.column'));
  316. $value = trim(input('param.value'));
  317. $adv_id = intval(input('param.id'));
  318. $param[$column] = trim($value);
  319. $result = $adv_model->editAdv($adv_id, $param);
  320. break;
  321. }
  322. if ($result >= 0) {
  323. echo 'true';
  324. } else {
  325. echo false;
  326. }
  327. }
  328. function adv_template()
  329. {
  330. $pages = $this->_get_editable_pages();
  331. View::assign('pages', $pages);
  332. $this->setAdminCurItem('adv_template');
  333. return View::fetch();
  334. }
  335. /**
  336. * 获取可以编辑的页面列表
  337. */
  338. function _get_editable_pages()
  339. {
  340. return array(
  341. lang('homepage') => (string)url('home/Index/index', ['edit_ad' => 1]),
  342. lang('flea') => (string)url('home/Flea/index', ['edit_ad' => 1]),
  343. );
  344. }
  345. /**
  346. * 获取卖家栏目列表,针对控制器下的栏目
  347. */
  348. protected function getAdminItemList()
  349. {
  350. $menu_array = array(
  351. array(
  352. 'name' => 'ap_manage',
  353. 'text' => lang('ap_manage'),
  354. 'url' => (string)url('Adv/ap_manage')
  355. ),
  356. );
  357. $menu_array[] = array(
  358. 'name' => 'adv',
  359. 'text' => lang('adv_manage'),
  360. 'url' => (string)url('Adv/adv')
  361. );
  362. $menu_array[] = array(
  363. 'name' => 'adv_add',
  364. 'text' => lang('adv_add'),
  365. 'url' => "javascript:dsLayerOpen('" . (string)url('Adv/adv_add', ['ap_id' => input('param.ap_id')]) . "','" . lang('adv_add') . "')"
  366. );
  367. return $menu_array;
  368. }
  369. }