Activity.php 17 KB

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