Scenefestival.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  1. <?php
  2. /**
  3. * Niushop商城系统 - 团队十年电商经验汇集巨献!
  4. * =========================================================
  5. * Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
  6. * ----------------------------------------------
  7. * 官方网址: https://www.niushop.com
  8. * =========================================================
  9. */
  10. namespace addon\scenefestival\shop\controller;
  11. use addon\coupon\model\CouponType;
  12. use addon\scenefestival\model\SceneFestival as Festival;
  13. use app\model\member\MemberLevel;
  14. use app\shop\controller\BaseShop;
  15. use think\facade\Cache;
  16. /**
  17. * 节日有礼控制器
  18. */
  19. class Scenefestival extends BaseShop
  20. {
  21. //活动类型
  22. private $game_type = 'scenefestival';
  23. private $game_type_name = '节日有礼';
  24. private $festival_url = '/pages_promotion/game/turntable';
  25. /*
  26. * 节日有礼活动列表
  27. */
  28. public function lists()
  29. {
  30. $model = new Festival();
  31. if (request()->isAjax()) {
  32. $condition = [
  33. [ 'site_id', '=', $this->site_id ],
  34. [ 'festival_type', '=', $this->game_type ]
  35. ];
  36. $status = input('status', '');//节日有礼状态
  37. if ($status !== '') {
  38. $condition[] = [ 'status', '=', $status ];
  39. }
  40. //活动名称
  41. $activity_name = input('activity_name', '');
  42. if ($activity_name) {
  43. $condition[] = [ 'activity_name', 'like', '%' . $activity_name . '%' ];
  44. }
  45. $page = input('page', 1);
  46. $page_size = input('page_size', PAGE_LIST_ROWS);
  47. $start_time = input('start_time', '');
  48. $end_time = input('end_time', '');
  49. if ($start_time && !$end_time) {
  50. $condition[] = [ 'end_time', '>=', date_to_time($start_time) ];
  51. } elseif (!$start_time && $end_time) {
  52. $condition[] = [ 'start_time', '<=', date_to_time($end_time) ];
  53. } elseif ($start_time && $end_time) {
  54. $start_timestamp = date_to_time($start_time);
  55. $end_timestamp = date_to_time($end_time);
  56. $sql = "start_time between {$start_timestamp} and {$end_timestamp}";
  57. $sql .= " or end_time between {$start_timestamp} and {$end_timestamp}";
  58. $sql .= " or (start_time <= {$start_timestamp} and end_time >= {$end_timestamp})";
  59. $condition[] = [ '', 'exp', \think\facade\Db::raw($sql) ];
  60. }
  61. $list = $model->getGamesPageList($condition, $page, $page_size, 'festival_id desc');
  62. return $list;
  63. } else {
  64. return $this->fetch("scenefestival/lists");
  65. }
  66. }
  67. /**
  68. * 添加活动
  69. */
  70. public function add()
  71. {
  72. $model = new Festival();
  73. if (request()->isAjax()) {
  74. $holiday_time = input('holiday_time', '');
  75. $days = input('join_frequency', '1');
  76. $join_type = input('join_type', 1);
  77. $join_time = input('join_time', '0'); //1传统节日 0自定义节日
  78. if ($join_time == 1) {
  79. $holiday_name = input('holiday_name', '');
  80. $date = substr($holiday_time, '0', '4') . '-' . substr($holiday_time, '4', '2') . '-' . substr($holiday_time, '6', '2');
  81. $end_time = strtotime($date . ' ' . '23:59:59');
  82. if ($join_type == 1) {
  83. $start_time = strtotime($date . ' ' . '00:00:00');
  84. $push_time = $start_time;
  85. } else {
  86. $start_time = strtotime($date . ' ' . '00:00:00');
  87. $push_time = $start_time - ( 86400 * $days );
  88. }
  89. } else {
  90. $holiday_name = input('festival_name', '');
  91. if (input('join_type') == 1) {
  92. $start_time = strtotime(input('start_time', ''));
  93. $push_time = $start_time;
  94. } else if (input('join_type') == 0) {
  95. $start_time = strtotime(input('start_time', ''));
  96. $push_time = $start_time - ( 86400 * $days );
  97. }
  98. $end_time = strtotime(input('end_time', ''));
  99. }
  100. //获取节日信息
  101. $festival_data = [
  102. 'site_id' => $this->site_id,
  103. 'activity_name' => input('activity_name', ''),
  104. 'festival_type' => $this->game_type,
  105. 'festival_type_name' => $this->game_type_name,
  106. 'festival_name' => $holiday_name,
  107. 'level_id' => input('level_id', ''),
  108. 'level_name' => input('level_name', ''),
  109. 'start_time' => $start_time,
  110. 'end_time' => $end_time,
  111. 'remark' => input('remark', ''),
  112. 'join_time' => input('join_time', ''),//0自定义节日 1传统节日
  113. 'join_type' => input('join_type', ''), //奖励发放时间类型(1节日当天 0节日前某天)
  114. 'join_frequency' => $days,//天数
  115. 'push_time' => $push_time//活动推送时间
  116. ];
  117. if (strpos(input('type', ''), 'point') !== false) {
  118. $point = input('point', 0);
  119. } else {
  120. $point = 0;
  121. }
  122. if (strpos(input('type', ''), 'balance') !== false) {
  123. if (input('balance_type') == 0) {
  124. $balance = input('balance', '0.00');
  125. $balance_money = '0.00';
  126. } else {
  127. $balance_money = input('balance_money', '0.00');
  128. $balance = '0.00';
  129. }
  130. } else {
  131. $balance = '0.00';
  132. $balance_money = '0.00';
  133. }
  134. if (strpos(input('type', ''), 'coupon') !== false) {
  135. $coupon = input('coupon', '');
  136. } else {
  137. $coupon = 0;
  138. }
  139. $award_arr = [
  140. 'award_type' => input('type', ''),
  141. 'point' => $point,
  142. 'balance' => $balance,
  143. 'balance_type' => input('balance_type', '0'),
  144. 'balance_money' => $balance_money,
  145. 'coupon' => $coupon
  146. ];
  147. return $model->addFestival($festival_data, $award_arr);
  148. } else {
  149. //获取优惠券列表
  150. $coupon_model = new CouponType();
  151. $condition = [
  152. [ 'promotion_type', '=', 0 ],
  153. [ 'status', '=', 1 ],
  154. [ 'site_id', '=', $this->site_id ],
  155. ];
  156. //优惠券字段
  157. $coupon_field = 'coupon_type_id,type,coupon_name,image,money,discount,validity_type,fixed_term,status,is_limit,at_least,count,lead_count,end_time,goods_type,max_fetch';
  158. $coupon_list = $coupon_model->getCouponTypeList($condition, $coupon_field);
  159. if (!empty($coupon_list[ 'data' ])) {
  160. foreach ($coupon_list[ 'data' ] as $k => $v) {
  161. if ($v[ 'count' ] < 0 || $v[ 'count' ] - $v[ 'lead_count' ] > 0) {
  162. $coupon_list[ 'data' ][ $k ][ 'coupon_flag' ] = true;
  163. } else {
  164. $coupon_list[ 'data' ][ $k ][ 'coupon_flag' ] = false;
  165. }
  166. }
  167. }
  168. $this->assign('coupon_list', $coupon_list);
  169. $jieri_list = Cache::get('jieri_list');
  170. if (empty($jieri_list)) {
  171. $jieri_data = file_get_contents('https://api.apihubs.cn/holiday/get?field=year,month,date,lunar_year,lunar_month,lunar_date,holiday&year=' . date('Y') . '&holiday=22,15,11,16,17,19,44,55,58,62,66,70,77,88,89,97&holiday_today=1&order_by=1&cn=1&size=31');
  172. $jieri_data = json_decode($jieri_data, true);
  173. $jieri_list = $jieri_data[ 'data' ][ 'list' ];
  174. Cache::set('jieri_list', $jieri_list);
  175. }
  176. $this->assign('jieri_list', $jieri_list);
  177. //会员等级
  178. $member_level_model = new MemberLevel();
  179. $member_level_list = $member_level_model->getMemberLevelList([ [ 'site_id', '=', $this->site_id ] ], 'level_id, level_name', 'growth asc');
  180. $this->assign('member_level_list', $member_level_list[ 'data' ]);
  181. return $this->fetch("scenefestival/add");
  182. }
  183. }
  184. /**
  185. * 编辑活动
  186. */
  187. public function edit()
  188. {
  189. $model = new Festival();
  190. $festival_id = input('festival_id');
  191. if (request()->isAjax()) {
  192. $holiday_time = input('holiday_time', '');
  193. $days = input('join_frequency', '1');
  194. $join_type = input('join_type', 1);
  195. $join_time = input('join_time', '0'); //1传统节日 0自定义节日
  196. if ($join_time == 1) {
  197. $holiday_name = input('holiday_name', '');
  198. $date = substr($holiday_time, '0', '4') . '-' . substr($holiday_time, '4', '2') . '-' . substr($holiday_time, '6', '2');
  199. $end_time = strtotime($date . ' ' . '23:59:59');
  200. if ($join_type == 1) {
  201. $start_time = strtotime($date . ' ' . '00:00:00');
  202. $push_time = $start_time;
  203. } else {
  204. $start_time = strtotime($date . ' ' . '00:00:00');
  205. $push_time = $start_time - ( 86400 * $days );
  206. }
  207. } else {
  208. $holiday_name = input('festival_name', '');
  209. if (input('join_type') == 1) {
  210. $start_time = strtotime(input('start_time', ''));
  211. $push_time = $start_time;
  212. } else if (input('join_type') == 0) {
  213. $start_time = strtotime(input('start_time', ''));
  214. $push_time = $start_time - ( 86400 * $days );
  215. }
  216. $end_time = strtotime(input('end_time', ''));
  217. }
  218. //获取活动信息
  219. $festival_data = [
  220. 'site_id' => $this->site_id,
  221. 'activity_name' => input('activity_name', ''),
  222. 'festival_type' => $this->game_type,
  223. 'festival_type_name' => $this->game_type_name,
  224. 'festival_name' => $holiday_name,
  225. 'level_id' => input('level_id', ''),
  226. 'level_name' => input('level_name', ''),
  227. 'start_time' => $start_time,
  228. 'end_time' => $end_time,
  229. 'remark' => input('remark', ''),
  230. 'join_time' => input('join_time', ''),//0自定义节日 1传统节日
  231. 'join_type' => input('join_type', ''), //奖励发放时间类型(1节日当天 0节日前某天)
  232. 'join_frequency' => $days,//天数
  233. 'push_time' => $push_time//活动推送时间
  234. ];
  235. if (strpos(input('type', ''), 'point') !== false) {
  236. $point = input('point', 0);
  237. } else {
  238. $point = 0;
  239. }
  240. if (strpos(input('type', ''), 'balance') !== false) {
  241. if (input('balance_type') == 0) {
  242. $balance = input('balance', '0.00');
  243. $balance_money = '0.00';
  244. } else {
  245. $balance_money = input('balance_money', '0.00');
  246. $balance = '0.00';
  247. }
  248. } else {
  249. $balance = '0.00';
  250. $balance_money = '0.00';
  251. }
  252. if (strpos(input('type', ''), 'coupon') !== false) {
  253. $coupon = input('coupon', '');
  254. } else {
  255. $coupon = 0;
  256. }
  257. $award_arr = [
  258. 'award_type' => input('type', ''),
  259. 'point' => $point,
  260. 'balance' => $balance,
  261. 'balance_type' => input('balance_type', '0'),
  262. 'balance_money' => $balance_money,
  263. 'coupon' => $coupon
  264. ];
  265. return $model->editGames([ [ 'site_id', '=', $this->site_id ], [ 'festival_id', '=', $festival_id ] ], $festival_data, $award_arr);
  266. } else {
  267. //获取优惠券列表
  268. $coupon_model = new CouponType();
  269. $condition = [
  270. [ 'promotion_type', '=', 0 ],
  271. [ 'status', '=', 1 ],
  272. [ 'site_id', '=', $this->site_id ],
  273. ];
  274. //优惠券字段
  275. $coupon_field = 'coupon_type_id,type,coupon_name,image,money,discount,validity_type,fixed_term,status,is_limit,at_least,count,lead_count,end_time,goods_type,max_fetch';
  276. $coupon_list = $coupon_model->getCouponTypeList($condition, $coupon_field);
  277. if (!empty($coupon_list[ 'data' ])) {
  278. foreach ($coupon_list[ 'data' ] as $k => $v) {
  279. if ($v[ 'count' ] < 0 || $v[ 'count' ] - $v[ 'lead_count' ] > 0) {
  280. $coupon_list[ 'data' ][ $k ][ 'coupon_flag' ] = true;
  281. } else {
  282. $coupon_list[ 'data' ][ $k ][ 'coupon_flag' ] = false;
  283. }
  284. }
  285. }
  286. $this->assign('coupon_list', $coupon_list);
  287. //会员等级
  288. $member_level_model = new MemberLevel();
  289. $member_level_list = $member_level_model->getMemberLevelList([ [ 'site_id', '=', $this->site_id ] ], 'level_id, level_name', 'growth asc');
  290. $this->assign('member_level_list', $member_level_list[ 'data' ]);
  291. $jieri_list = Cache::get('jieri_list');
  292. if (empty($jieri_list)) {
  293. $jieri_data = file_get_contents('https://api.apihubs.cn/holiday/get?field=year,month,date,lunar_year,lunar_month,lunar_date,holiday&year=' . date('Y') . '&holiday=22,15,11,16,17,19,44,55,58,62,66,70,77,88,89,97&holiday_today=1&order_by=1&cn=1&size=31');
  294. $jieri_data = json_decode($jieri_data, true);
  295. $jieri_list = $jieri_data[ 'data' ][ 'list' ];
  296. Cache::set('jieri_list', $jieri_list);
  297. }
  298. $this->assign('jieri_list', $jieri_list);
  299. //获取详情
  300. $info = $model->getFestivalDetail([ [ 'a.festival_id', '=', $festival_id ], [ 'a.site_id', '=', $this->site_id ] ], 'a.*,pfa.award_type,pfa.coupon,pfa.point,pfa.balance,pfa.balance_type,pfa.balance_money', 'a', [ [ 'promotion_festival_award pfa', 'pfa.festival_id = a.festival_id', 'left' ] ]);
  301. $info[ 'data' ][ 'time' ] = time();
  302. $this->assign('info', $info[ 'data' ]);
  303. if (empty($info[ 'data' ])) $this->error('未获取到活动数据', addon_url('scenefestival://shop/scenefestival/lists'));
  304. return $this->fetch("scenefestival/edit");
  305. }
  306. }
  307. /*
  308. * 节日有礼详情
  309. */
  310. public function detail()
  311. {
  312. $festival_model = new Festival();
  313. $festival_id = input('festival_id', '');
  314. //获取详情
  315. $info = $festival_model->getFestivalDetail([ [ 'a.festival_id', '=', $festival_id ], [ 'a.site_id', '=', $this->site_id ] ], 'a.*,pfa.award_type,pfa.coupon,pfa.point,pfa.balance,pfa.balance_type,pfa.balance_money', 'a', [ [ 'promotion_festival_award pfa', 'pfa.festival_id = a.festival_id', 'left' ] ])[ 'data' ] ?? [];
  316. if (empty($info)) $this->error('未获取到活动数据', addon_url('scenefestival://shop/scenefestival/lists'));
  317. $info[ 'status_name' ] = $festival_model->status[ $info[ 'status' ] ] ?? '';
  318. $this->assign('info', $info);
  319. return $this->fetch("scenefestival/detail");
  320. }
  321. /*
  322. * 删除节日有礼活动
  323. */
  324. public function delete()
  325. {
  326. $festival_id = input('festival_id', '0');
  327. $site_id = $this->site_id;
  328. $festival_model = new Festival();
  329. return $festival_model->deleteGames($site_id, $festival_id);
  330. }
  331. /*
  332. * 结束节日有礼活动
  333. */
  334. public function finish()
  335. {
  336. $festival_id = input('festival_id', '0');
  337. $site_id = $this->site_id;
  338. $festival_model = new Festival();
  339. return $festival_model->finishGames($site_id, $festival_id);
  340. }
  341. /*
  342. * 重启节日有礼活动
  343. */
  344. public function start()
  345. {
  346. $festival_id = input('festival_id', '0');
  347. $festival_model = new Festival();
  348. return $festival_model->startGames($festival_id);
  349. }
  350. /**
  351. * 推广
  352. * return
  353. */
  354. public function gameUrl()
  355. {
  356. $festival_id = input('festival_id', '0');
  357. $model = new Festival();
  358. $festival_info_data = $model->getGamesInfo([ [ 'festival_id', '=', $festival_id ] ], 'festival_id,activity_name');
  359. $festival_info = $festival_info_data[ 'data' ];
  360. $res = $model->qrcode($festival_info[ 'festival_id' ], $festival_info[ 'activity_name' ], $this->festival_url, $this->site_id);
  361. return $res;
  362. }
  363. }