Activity.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  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 Activity extends AdminControl
  13. {
  14. public function initialize()
  15. {
  16. parent::initialize();
  17. Lang::load(base_path() . 'admin/lang/' . config('lang.default_lang') . '/activity.lang.php');
  18. }
  19. /**
  20. * 活动列表/删除活动
  21. */
  22. public function index()
  23. {
  24. $activity_model = model('activity');
  25. //条件
  26. $condition = array();
  27. $condition[] = array('activity_type', '=', '1'); //只显示商品活动
  28. //状态
  29. if ((input('param.searchstate'))) {
  30. $state = intval(input('param.searchstate')) - 1;
  31. $condition[] = array('activity_state', '=', "$state");
  32. }
  33. //标题
  34. if ((input('param.searchtitle'))) {
  35. $condition[] = array('activity_title', 'like', "%" . input('param.searchtitle') . "%");
  36. }
  37. //有效期范围
  38. if ((input('param.searchstartdate')) && (input('param.searchenddate'))) {
  39. $startdate = strtotime(input('param.searchstartdate'));
  40. $enddate = strtotime(input('param.searchenddate'));
  41. if ($enddate > 0) {
  42. $enddate += 86400;
  43. }
  44. $condition[] = array('activity_enddate', '>=', $startdate);
  45. $condition[] = array('activity_startdate', '<=', $enddate);
  46. }
  47. //活动列表
  48. $activity_list = $activity_model->getActivityList($condition, 10, 'activity_sort asc');
  49. //输出
  50. View::assign('show_page', $activity_model->page_info->render());
  51. View::assign('activity_list', $activity_list);
  52. View::assign('filtered', $condition ? 1 : 0); //是否有查询条件
  53. $this->setAdminCurItem('index');
  54. return View::fetch();
  55. }
  56. /**
  57. * 新建活动/保存新建活动
  58. */
  59. public function add()
  60. {
  61. if (request()->isPost()) {
  62. //提交表单
  63. $data = [
  64. 'activity_title' => input('post.activity_title'),
  65. 'activity_startdate' => strtotime(input('post.activity_startdate')),
  66. 'activity_enddate' => strtotime(input('post.activity_enddate')),
  67. 'activity_type' => input('post.activity_type'),
  68. 'activity_banner' => $_FILES['activity_banner']['name'],
  69. 'activity_banner_mobile' => $_FILES['activity_banner_mobile']['name'],
  70. 'activity_sort' => intval(input('post.activity_sort'))
  71. ];
  72. $activity_validate = ds_validate('activity');
  73. if (!$activity_validate->scene('add')->check($data)) {
  74. $this->error($activity_validate->getError());
  75. }
  76. $file_name = '';
  77. if (!empty($_FILES['activity_banner']['name'])) {
  78. $res = ds_upload_pic(ATTACH_ACTIVITY, 'activity_banner');
  79. if ($res['code']) {
  80. $file_name = $res['data']['file_name'];
  81. } else {
  82. $this->error($res['msg']);
  83. }
  84. }
  85. //保存
  86. $data['activity_banner'] = $file_name;
  87. $file_name_mobile = '';
  88. if (!empty($_FILES['activity_banner_mobile']['name'])) {
  89. $res = ds_upload_pic(ATTACH_ACTIVITY, 'activity_banner_mobile');
  90. if ($res['code']) {
  91. $file_name_mobile = $res['data']['file_name'];
  92. } else {
  93. $this->error($res['msg']);
  94. }
  95. }
  96. //保存
  97. $data['activity_banner_mobile'] = $file_name_mobile;
  98. $data['activity_desc'] = trim(input('post.activity_desc'));
  99. $data['activity_state'] = intval(input('post.activity_state'));
  100. $activity_model = model('activity');
  101. $result = $activity_model->addActivity($data);
  102. if ($result) {
  103. $this->log(lang('ds_add') . lang('activity_index') . '[' . input('post.activity_title') . ']', null);
  104. dsLayerOpenSuccess(lang('ds_common_op_succ'));
  105. } else {
  106. //添加失败则删除刚刚上传的图片,节省空间资源
  107. @unlink($upload_file . DIRECTORY_SEPARATOR . $file_name);
  108. @unlink($upload_file . DIRECTORY_SEPARATOR . $file_name_mobile);
  109. $this->error(lang('ds_common_op_fail'));
  110. }
  111. } else {
  112. $activity = array(
  113. 'activity_type' => '1',
  114. 'activity_startdate' => TIMESTAMP,
  115. 'activity_enddate' => TIMESTAMP,
  116. 'activity_banner' => '',
  117. 'activity_desc' => '',
  118. 'activity_state' => '1',
  119. );
  120. View::assign('activity', $activity);
  121. return View::fetch('form');
  122. }
  123. }
  124. /**
  125. * 异步修改
  126. */
  127. public function ajax()
  128. {
  129. if (in_array(input('param.branch'), array('activity_title', 'activity_sort'))) {
  130. $activity_model = model('activity');
  131. $update_array = array();
  132. switch (input('param.branch')) {
  133. /**
  134. * 活动主题
  135. */
  136. case 'activity_title':
  137. if (trim(input('param.value')) == '')
  138. exit;
  139. break;
  140. /**
  141. * 排序
  142. */
  143. case 'activity_sort':
  144. if (preg_match('/^\d+$/', trim(input('param.value'))) <= 0 or intval(trim(input('param.value'))) < 0 or intval(trim(input('param.value'))) > 255)
  145. exit;
  146. break;
  147. default:
  148. exit;
  149. }
  150. $update_array[input('param.column')] = trim(input('param.value'));
  151. if ($activity_model->editActivity($update_array, intval(input('param.id'))))
  152. echo 'true';
  153. } elseif (in_array(input('param.branch'), array('activitydetail_sort'))) {
  154. $activitydetail_model = model('activitydetail');
  155. $update_array = array();
  156. switch (input('param.branch')) {
  157. /**
  158. * 排序
  159. */
  160. case 'activitydetail_sort':
  161. if (preg_match('/^\d+$/', trim(input('param.value'))) <= 0 or intval(trim(input('param.value'))) < 0 or intval(trim(input('param.value'))) > 255)
  162. exit;
  163. break;
  164. default:
  165. exit;
  166. }
  167. $update_array[input('param.column')] = trim(input('param.value'));
  168. if ($activitydetail_model->editActivitydetail($update_array, array(array('activitydetail_id', '=', intval(input('param.id'))))))
  169. echo 'true';
  170. }
  171. }
  172. /**
  173. * 删除活动
  174. */
  175. public function del()
  176. {
  177. $id = intval(input('param.activity_id'));
  178. if ($id <= 0) {
  179. ds_json_encode(10001, lang('param_error'));
  180. }
  181. $activity_model = model('activity');
  182. $activitydetail_model = model('activitydetail');
  183. //获取可以删除的数据
  184. $activity_info = $activity_model->getOneActivityById($id);
  185. if (empty($activity_info) || ($activity_info['activity_state'] && $activity_info['activity_enddate'] > TIMESTAMP)) { //没有符合条件的活动信息直接返回成功信息
  186. ds_json_encode(10001, lang('activity_index_help3'));
  187. }
  188. $id_arr = array($activity_info['activity_id']);
  189. $condition = array();
  190. $condition[] = array('activity_id', 'in', $id_arr);
  191. //只有关闭或者过期的活动,能删除
  192. if ($activitydetail_model->getActivitydetailList($condition)) {
  193. if (!$activitydetail_model->delActivitydetail($condition)) {
  194. ds_json_encode(10001, lang('activity_del_fail'));
  195. }
  196. }
  197. try {
  198. //删除数据先删除横幅图片,节省空间资源
  199. foreach ($id_arr as $v) {
  200. $this->delBanner(intval($v));
  201. }
  202. } catch (Exception $e) {
  203. ds_json_encode(10001, $e->getMessage());
  204. }
  205. if ($activity_model->delActivity($condition)) {
  206. $this->log(lang('ds_del') . lang('activity_index') . '[ID:' . $id . ']', null);
  207. ds_json_encode(10000, lang('ds_common_del_succ'));
  208. }
  209. ds_json_encode(10001, lang('activity_del_fail'));
  210. }
  211. /**
  212. * 编辑活动/保存编辑活动
  213. */
  214. public function edit()
  215. {
  216. $activity_id = intval(input('param.activity_id'));
  217. if ($activity_id <= 0) {
  218. $this->error(lang('miss_argument'));
  219. }
  220. $activity_model = model('activity');
  221. $activity = $activity_model->getOneActivityById($activity_id);
  222. if (!request()->isPost()) {
  223. View::assign('activity', $activity);
  224. return View::fetch('form');
  225. } else {
  226. //提交表单
  227. $data = [
  228. 'activity_title' => input('post.activity_title'),
  229. 'activity_startdate' => strtotime(input('post.activity_startdate')),
  230. 'activity_enddate' => strtotime(input('post.activity_enddate')),
  231. 'activity_type' => input('post.activity_type'),
  232. 'activity_sort' => intval(input('post.activity_sort'))
  233. ];
  234. $activity_validate = ds_validate('activity');
  235. if (!$activity_validate->scene('edit')->check($data)) {
  236. $this->error($activity_validate->getError());
  237. }
  238. //构造更新内容
  239. $file_name = '';
  240. if ($_FILES['activity_banner']['name'] != '') {
  241. $res = ds_upload_pic(ATTACH_ACTIVITY, 'activity_banner');
  242. if ($res['code']) {
  243. $file_name = $res['data']['file_name'];
  244. $data['activity_banner'] = $file_name;
  245. } else {
  246. $this->error($res['msg']);
  247. }
  248. }
  249. $file_name_mobile = '';
  250. if ($_FILES['activity_banner_mobile']['name'] != '') {
  251. $res = ds_upload_pic(ATTACH_ACTIVITY, 'activity_banner_mobile');
  252. if ($res['code']) {
  253. $file_name_mobile = $res['data']['file_name'];
  254. $data['activity_banner_mobile'] = $file_name_mobile;
  255. } else {
  256. $this->error($res['msg']);
  257. }
  258. }
  259. $data['activity_desc'] = trim(input('post.activity_desc'));
  260. $data['activity_state'] = intval(input('post.activity_state'));
  261. $result = $activity_model->editActivity($data, $activity_id);
  262. if ($result) {
  263. //删除图片
  264. @unlink($upload_file . DIRECTORY_SEPARATOR . $activity['activity_banner']);
  265. @unlink($upload_file . DIRECTORY_SEPARATOR . $activity['activity_banner_mobile']);
  266. $this->log(lang('ds_edit') . lang('activity_index') . '[ID:' . $activity_id . ']', null);
  267. dsLayerOpenSuccess(lang('ds_common_save_succ'));
  268. } else {
  269. if ($_FILES['activity_banner']['name'] != '') {
  270. @unlink($upload_file . DIRECTORY_SEPARATOR . $file_name);
  271. }
  272. if ($_FILES['activity_banner_mobile']['name'] != '') {
  273. @unlink($upload_file . DIRECTORY_SEPARATOR . $file_name_mobile);
  274. }
  275. $this->error(lang('ds_common_save_fail'));
  276. }
  277. }
  278. }
  279. /**
  280. * 活动细节列表
  281. */
  282. public function detail()
  283. {
  284. $activity_id = intval(input('param.id'));
  285. if ($activity_id <= 0) {
  286. $this->error(lang('miss_argument'));
  287. }
  288. //条件
  289. $condition_arr = array();
  290. $condition_arr[] = array('activity_id', '=', $activity_id);
  291. //审核状态
  292. if ((input('param.searchstate'))) {
  293. $state = intval(input('param.searchstate')) - 1;
  294. $condition_arr[] = array('activitydetail_state', '=', "$state");
  295. }
  296. //店铺名称
  297. if ((input('param.searchstore'))) {
  298. $condition_arr[] = array('store_name', 'like', "%" . input('param.searchstore') . "%");
  299. }
  300. //商品名称
  301. if ((input('param.searchgoods'))) {
  302. $condition_arr[] = array('item_name', 'like', "%" . input('param.searchgoods') . "%");
  303. }
  304. $activitydetail_model = model('activitydetail');
  305. $activitydetail_list = $activitydetail_model->getActivitydetailList($condition_arr, 10);
  306. //输出到模板
  307. View::assign('show_page', $activitydetail_model->page_info->render());
  308. View::assign('activitydetail_list', $activitydetail_list);
  309. $this->setAdminCurItem('detail');
  310. return View::fetch();
  311. }
  312. /**
  313. * 活动内容处理
  314. */
  315. public function deal()
  316. {
  317. $activitydetail_id = input('param.activitydetail_id');
  318. $activitydetail_id_array = ds_delete_param($activitydetail_id);
  319. if ($activitydetail_id_array == FALSE) {
  320. ds_json_encode('10001', lang('param_error'));
  321. }
  322. $condition = array();
  323. $condition[] = array('activitydetail_id', 'in', $activitydetail_id_array);
  324. //创建活动内容对象
  325. $activitydetail_state = intval(input('param.state'));
  326. $result = model('activitydetail')->editActivitydetail(array('activitydetail_state' => $activitydetail_state), $condition);
  327. if ($result >= 0) {
  328. $this->log(lang('ds_edit') . lang('activity_index') . '[ID:' . $activitydetail_id . ']', null);
  329. if (input('param.ajax')) {
  330. ds_json_encode(10000, lang('ds_common_op_succ'));
  331. } else {
  332. $this->success(lang('ds_common_op_succ'));
  333. }
  334. } else {
  335. if (input('param.ajax')) {
  336. ds_json_encode(10001, lang('ds_common_op_fail'));
  337. } else {
  338. $this->error(lang('ds_common_op_fail'));
  339. }
  340. }
  341. }
  342. /**
  343. * 删除活动内容
  344. */
  345. public function del_detail()
  346. {
  347. $activitydetail_id = input('param.activitydetail_id');
  348. $activitydetail_id_array = ds_delete_param($activitydetail_id);
  349. if ($activitydetail_id_array == FALSE) {
  350. ds_json_encode('10001', lang('param_error'));
  351. }
  352. $activitydetail_model = model('activitydetail');
  353. //条件
  354. $condition_arr = array();
  355. $condition_arr[] = array('activitydetail_id', 'in', $activitydetail_id_array);
  356. $condition_arr[] = array('activitydetail_state', 'in', array('0', '2')); //未审核和已拒绝
  357. if ($activitydetail_model->delActivitydetail($condition_arr)) {
  358. $this->log(lang('ds_del') . lang('activity_index_content') . '[ID:' . implode(',', $activitydetail_id_array) . ']', null);
  359. ds_json_encode(10000, lang('ds_common_del_succ'));
  360. } else {
  361. ds_json_encode(10001, lang('ds_common_del_fail'));
  362. }
  363. }
  364. /**
  365. * 根据活动编号删除横幅图片
  366. *
  367. * @param int $id
  368. */
  369. private function delBanner($id)
  370. {
  371. $activity_model = model('activity');
  372. $row = $activity_model->getOneActivityById($id);
  373. //删除图片文件
  374. @unlink(BASE_UPLOAD_PATH . DIRECTORY_SEPARATOR . ATTACH_ACTIVITY . DIRECTORY_SEPARATOR . $row['activity_banner']);
  375. }
  376. /**
  377. * 获取卖家栏目列表,针对控制器下的栏目
  378. */
  379. protected function getAdminItemList()
  380. {
  381. $menu_array = array(
  382. array(
  383. 'name' => 'index', 'text' => lang('ds_manage'), 'url' => (string)url('Activity/index')
  384. ), array(
  385. 'name' => 'add',
  386. 'text' => lang('ds_new'),
  387. 'url' => "javascript:dsLayerOpen('" . (string)url('Activity/add') . "','" . lang('ds_new') . "')"
  388. ),
  389. );
  390. if (request()->action() == 'detail') {
  391. $menu_array[] = array(
  392. 'name' => 'detail', 'text' => lang('processing_application'), 'url' => 'javascript:void(0)'
  393. );
  394. }
  395. return $menu_array;
  396. }
  397. }