Activity.php 17 KB

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