Adv.php 14 KB

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