Appadv.php 13 KB

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