Activity.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. <?php
  2. namespace app\index\controller;
  3. use app\common\controller\Frontend;
  4. use app\common\library\Redis;
  5. use app\common\service\CardFlipService;
  6. use app\main\constants\ActivityConstants;
  7. use app\main\constants\KafkaDotConstants;
  8. use app\main\constants\UrlConstants;
  9. use app\main\constants\UserConstants;
  10. use app\main\model\object\AnalysisObject;
  11. use app\main\service\AdminService;
  12. use app\main\service\KafkaDotService;
  13. use app\main\service\SubscripService;
  14. use app\main\service\UrlService;
  15. use app\main\service\UserService;
  16. use think\Config;
  17. use think\Cookie;
  18. use think\Request;
  19. class Activity extends Frontend
  20. {
  21. protected $layout = '';
  22. /**
  23. * @return mixed
  24. * @throws \think\Exception
  25. */
  26. public function pay()
  27. {
  28. $id = intval($_GET['id']);
  29. $aid = intval($_GET['aid'] ?? 0);
  30. UserService::instance()->setUserCache(Request::instance()->get('user_id'));
  31. $is_range = 0;
  32. $user_id = UserService::instance()->getUserInfo()->id;
  33. $admin_id = UserService::instance()->getAdminId()->data;
  34. $channel_id = UserService::instance()->getUserInfo()->channel_id;
  35. if ($this->request->has('isrange') || $this->request->has('isRange')) {
  36. $is_range = 1;
  37. }
  38. if ($this->request->has('visitor')) {
  39. Cookie::set('visitor', $this->request->get('visitor'));
  40. }
  41. /**
  42. * 获取渠道商/代理商资料
  43. */
  44. $adminExtend = AdminService::instance()->getAdminExtendModel()->getInfo($admin_id);
  45. if (!$adminExtend) {
  46. $this->error('管理员信息获取失败');
  47. }
  48. $activityInfo = model('Activity')->getInfo($aid);
  49. $isGiveAct = $activityInfo['type'] == ActivityConstants::ACTIVITY_TYPE_GIVE;
  50. if ($activityInfo && in_array($activityInfo['type'],
  51. [ActivityConstants::ACTIVITY_TYPE_CUSTOM, ActivityConstants::ACTIVITY_TYPE_GIVE])) {
  52. $resource_info = (model('Resource')->getInfo($id));
  53. $res['starttime'] = $activityInfo['starttime'];
  54. $res['endtime'] = $activityInfo['endtime'];
  55. $res['astatus'] = $activityInfo['status'];
  56. $res['aname'] = $activityInfo['name'];
  57. $res = array_merge($res, $resource_info);
  58. $res['activity_id'] = $aid;
  59. if (intval($activityInfo['limited'])) {
  60. $redis = Redis::instance();
  61. //用户在渠道自定义的活动充值次数统计
  62. if ($activityInfo['type'] == ActivityConstants::ACTIVITY_TYPE_CUSTOM) {
  63. $pay_count = intval($redis->hGet("ATPU:{$aid}", $user_id));
  64. if (intval($activityInfo['limited']) <= $pay_count) {
  65. $this->error("该活动限购 {$activityInfo['limited']} 次");
  66. }
  67. }
  68. //用户在赠币的活动充值次数统计
  69. if ($isGiveAct) {
  70. $this->assign('the_max_time', $activityInfo['limited']);
  71. $pay_count = intval($redis->hGet("ATGP:{$aid}", $user_id));
  72. if (intval($activityInfo['limited']) <= $pay_count) {
  73. $this->assign('is_max_end',1);
  74. }
  75. }
  76. }
  77. } else {
  78. $res = model('resource')->join('activity a', 'a.id=resource.activity_id')->where('resource.id', $id)
  79. ->field('a.starttime,a.endtime,a.status as astatus,a.name as aname,resource.*')->find();
  80. }
  81. if(!$res){
  82. $this->error('活动不存在');
  83. }
  84. $recent_url = getCurrentDomain($channel_id, '/index/user/recent');
  85. $this->assign('recent_url', $recent_url);
  86. if ($res['activity_id'] == 100) {
  87. if (UserService::instance()->getUserInfo()->is_pay == UserConstants::USER_IS_PAY_YES) {
  88. $is_range = 1;
  89. }
  90. }
  91. if($res['endtime'] < $this->time||empty($res['astatus'])){
  92. $this->assign('isend',1);
  93. }
  94. if($res['starttime'] > $this->time && !empty($res['astatus'])){
  95. $this->assign('needWait',1);
  96. $lastTime = $this->lastTime($res['starttime']);
  97. $this->assign('lastTime',$lastTime);
  98. }
  99. $arr = [];
  100. array_push($arr,"\r\n");
  101. array_push($arr,"\r");
  102. array_push($arr,"\n");
  103. $pay_url = '';
  104. $res['warning'] = str_replace($arr,"<br/>",$res['warning']);
  105. $to_url = getCurrentDomain($channel_id,'/index/user/recent');
  106. if ($admin_id) {
  107. $admin_info = AdminService::instance()->getAdminConfigModel()->getAdminInfoAll($admin_id);
  108. if (!empty($admin_info) && $admin_info['appid']) {
  109. $to_url = getCurrentDomain($admin_id, '/index/user/recent');
  110. }
  111. if ($admin_info) {
  112. $menuophost = $info['menuophost'] ?? null;
  113. $menuwxpay_host = $info['menuwxpay_host'] ?? null;
  114. if ($menuophost && $menuwxpay_host && $menuophost == getCurrentOphost()) {
  115. $pay_url = Config::get('site.scheme') . '://' . $admin_info['menuwxpay_host'] . '/index/recharge/pay?from=wechat&admin_id=' . $admin_id . '&channel_id=' . $channel_id . '&user_id=' . $user_id;
  116. } else {
  117. $pay_url = Config::get('site.scheme') . '://' . $admin_info['wxpay_pay_host'] . '/index/recharge/pay?from=wechat&admin_id=' . $admin_id . '&channel_id=' . $channel_id . '&user_id=' . $user_id;
  118. }
  119. }
  120. }
  121. $payment_method = empty($admin_info['payment_method']) ? '0' : '1';
  122. $this->assign('res', $res);
  123. $this->assign('isGiveAct', $isGiveAct);
  124. $this->assign('payment_method', $payment_method);
  125. $this->assign('aid', $res['activity_id']);
  126. $this->assign('payUrl', $pay_url);
  127. $this->assign('toUrl', $to_url);
  128. $this->assign('isRange', $is_range);
  129. return $this->fetch();
  130. }
  131. /**
  132. * 获取赠送书币
  133. */
  134. public function getActGiveMoney()
  135. {
  136. $redis = Redis::instance();
  137. $act_id = $this->request->get('act_id');
  138. $res_id = $this->request->get('res_id');
  139. $user_id = UserService::instance()->getUserInfo()->id;
  140. if (empty($user_id)) {
  141. $this->error("获取用户信息失败");
  142. }
  143. $activity_give_config = Config::get('site.activity_give_config');
  144. if (!$act_id || !$res_id || !is_numeric($act_id) || !is_numeric($res_id)) {
  145. $this->error("参数错误", 'Index/User/recent');
  146. }
  147. //如果是赠币活动在处理
  148. if (model('Activity')->checkIsGiveActivity($act_id)) {
  149. $activity_info = model('Activity')->getInfo($act_id);
  150. $resource_info = model('Resource')->getInfo($res_id);
  151. $res['starttime'] = $activity_info['starttime'];
  152. $res['endtime'] = $activity_info['endtime'];
  153. $res['astatus'] = $activity_info['status'];
  154. if (!$activity_info || !$resource_info) {
  155. $this->error('活动不存在');
  156. } else {
  157. //检查活动是否结束
  158. if ($activity_info['endtime'] < $this->time|| empty($activity_info['status'])) {
  159. $this->error("活动已结束", 'Index/User/recent');
  160. }
  161. //检查活动开始
  162. if ($activity_info['starttime'] > $this->time && !empty($activity_info['status'])) {
  163. $this->error("活动还未开始");
  164. }
  165. //检查限购
  166. if (intval($activity_info['limited'])) {
  167. $pay_count = intval($redis->hGet("ATGP:{$act_id}", $user_id));
  168. if (intval($activity_info['limited']) <= $pay_count) {
  169. $this->error("该活动限购 {$activity_info['limited']} 次");
  170. }
  171. }
  172. //写入Recharge表
  173. $recharge_data = [
  174. 'user_id' => $user_id,
  175. 'type' => 6,
  176. 'free_kandian' => $resource_info['free_kandian'],
  177. 'remain_free_kandian' => $resource_info['free_kandian'],
  178. 'free_endtime' => intval($resource_info['free_day']) * 86400 + time(),
  179. 'createtime' => time(),
  180. 'updatetime' => time()
  181. ];
  182. if (!$recharge_id = model('Recharge')->setConnect($user_id)->insertGetId($recharge_data)) {
  183. $this->error("写入数据失败");
  184. }
  185. $oAna = new AnalysisObject();
  186. $oAna->type = KafkaDotConstants::TYPE_ACTIVITY;
  187. $oAna->event_time = Request::instance()->time();
  188. $oAna->user_from = [
  189. 'activity_id' => $act_id,
  190. ];
  191. $oAna->data = [
  192. 'stage' => 'start'
  193. ];
  194. KafkaDotService::instance()->sendMsg($user_id, $oAna);
  195. $oAna->data = [
  196. 'stage' => 'end'
  197. ];
  198. KafkaDotService::instance()->sendMsg($user_id, $oAna);
  199. //赠送的免费书币存到redis
  200. $zkey = 'ZR:' . $user_id;
  201. $redis->del($zkey);
  202. //写入次数限制
  203. $pay_count_key = "ATGP:{$act_id}";
  204. $redis->hIncrBy($pay_count_key, $user_id, 1);
  205. $redis->expire($pay_count_key, (intval($activity_give_config['time_interval']) * 86400) * 2);
  206. //成功
  207. $this->success("操作成功", 'Index/User/recent');
  208. }
  209. } else {
  210. $this->error("错误的请求", 'Index/User/recent');
  211. }
  212. }
  213. /**
  214. * 剩余时间
  215. * @param int $unixEndTime
  216. * @return string
  217. */
  218. function lastTime($unixEndTime=0)
  219. {
  220. if ($unixEndTime <= $this->time) { // 如果过了活动终止日期
  221. return '0天0时0分';
  222. }
  223. // 使用当前日期时间到活动截至日期时间的毫秒数来计算剩余天时分
  224. $time = $unixEndTime - $this->time;
  225. $days = 0;
  226. if ($time >= 86400) { // 如果大于1天
  227. $days = (int)($time / 86400);
  228. $time = $time % 86400; // 计算天后剩余的毫秒数
  229. }
  230. $xiaoshi = 0;
  231. if ($time >= 3600) { // 如果大于1小时
  232. $xiaoshi = (int)($time / 3600);
  233. $time = $time % 3600; // 计算小时后剩余的毫秒数
  234. }
  235. $fen = (int)($time / 60); // 剩下的毫秒数都算作分
  236. if(empty($fen)){
  237. $fen = 1;
  238. }
  239. return $days.'天'.$xiaoshi.'时'.$fen.'分';
  240. }
  241. /**
  242. * 订阅活动阅读记录页面
  243. * @return mixed
  244. */
  245. public function subscribe()
  246. {
  247. $userInfo = UserService::instance()->getUserInfo();
  248. $actList = SubscripService::instance()->getUserActList($userInfo->id);
  249. $this->assign('actList',$actList);
  250. $this->view->assign('adminconfig',$this->adminconfig);
  251. return $this->fetch('record');
  252. }
  253. /**
  254. * 订阅活动打卡记录页面
  255. */
  256. public function UserActLog()
  257. {
  258. $actId = $this->request->request('actId',0);
  259. $userInfo = UserService::instance()->getUserInfo();
  260. $activity = SubscripService::instance()->getActivityById($actId);
  261. $state = SubscripService::instance()->checkUserState($userInfo->id,$actId);
  262. $this->view->assign('adminconfig',$this->adminconfig);
  263. if ( $state == 3 ){//如果活动时间已经结束,用户还没有被标记为成功,直接标记为失败,跳转到失败页面
  264. return $this->fetch('fail',$activity);
  265. }
  266. if ($state == 2){
  267. return $this->fetch('success',$activity);
  268. }
  269. if ( $activity['end_date'] < date('Ymd',time()) && $state == 1 ){
  270. SubscripService::instance()->updateUserState($userInfo->id,$actId,3);
  271. return $this->fetch('fail',$activity);
  272. }
  273. $actLog = SubscripService::instance()->getUserActLog($userInfo,$actId,$activity);
  274. return $this->fetch('record_info',$actLog);
  275. }
  276. /**
  277. * 订阅活动成功页面
  278. */
  279. public function actSuccess()
  280. {
  281. $actId = $this->request->request('actId',0);
  282. $activity = SubscripService::instance()->getActivityById($actId);
  283. $this->view->assign('adminconfig',$this->adminconfig);
  284. return $this->fetch('success',$activity);
  285. }
  286. /**
  287. * 订阅活动失败页面
  288. */
  289. public function actFail()
  290. {
  291. $actId = $this->request->request('actId',0);
  292. $activity = SubscripService::instance()->getActivityById($actId);
  293. $this->view->assign('adminconfig',$this->adminconfig);
  294. return $this->fetch('fail',$activity);
  295. }
  296. /**
  297. * 消耗活动-翻牌活动
  298. */
  299. public function cardFlip()
  300. {
  301. //跳转到业务域名
  302. if(UrlService::instance()->checkDomainIsShare()) { //是活动域名
  303. $userInfo = UserService::instance()->getUserInfo();
  304. $actId = $this->request->request('actId',0);
  305. $params['from'] = 'wechat';
  306. $params['user_id'] = $userInfo->id;
  307. $params['actId'] = $actId;
  308. $host = getCurrentDomain($userInfo->channel_id,'/index/activity/cardFlip').'?';
  309. $host .= http_build_query($params);
  310. $this -> redirect($host);
  311. }
  312. $actId = $this->request->request('actId',0);
  313. $cardInfo = CardFlipService::instance()->getCardInfo($actId);
  314. if (empty($cardInfo)){
  315. return $this->fetch('index@activity/noactive');
  316. }
  317. $userInfo = UserService::instance()->getUserInfo();
  318. $isHave = CardFlipService::instance()->checkUserAuth($cardInfo['group_id'],$userInfo);
  319. if (!$isHave){
  320. return $this->fetch('index@activity/noactive');
  321. }
  322. //kafka打点
  323. CardFlipService::instance()->sendShow($actId,$userInfo);
  324. //活动是否有抽奖次数
  325. $drawsNum = CardFlipService::instance()->getDrawNum($cardInfo['id'],$userInfo->id);
  326. $cardInfo['draw_text'] = $drawsNum ? '本次免费' : '每次消耗'.$cardInfo['kandian'].'书币';
  327. $this->assign('log_host', Config::get('site.loghost')); //设置打点域名
  328. return $this->fetch('card',$cardInfo);
  329. }
  330. }