activity_bonus.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2018/10/15
  6. * Time: 下午5:55
  7. */
  8. namespace bonus;
  9. use bonus_helper;
  10. use errcode;
  11. use Log;
  12. class activity_bonus
  13. {
  14. const admin_member_id = 36429;
  15. private $mErrMessage;
  16. public function err()
  17. {
  18. return $this->mErrMessage;
  19. }
  20. private function parse_rate($bonus_rate)
  21. {
  22. $params = explode('#', urldecode($bonus_rate));
  23. $result = [];
  24. foreach ($params as $val) {
  25. if (preg_match_all('/^(\d{1,3})\|(\d{1,7})\|(\d{1,4})$/', $val, $match)) {
  26. $item['rate'] = intval($match[1][0]);
  27. $item['amount'] = intval($match[2][0]);
  28. $item['num'] = intval($match[3][0]);
  29. $result[] = $item;
  30. }
  31. }
  32. if (empty($result)) {
  33. return false;
  34. } else {
  35. return $result;
  36. }
  37. }
  38. private function check_param($input, &$ret)
  39. {
  40. if (!isset($input['start_time'])) {
  41. $ret = array('code' => errcode::ErrParamter, 'msg' => "请输入活动开始和结束时间.");
  42. return false;
  43. }
  44. $start_tm = strtotime($input['start_time']);
  45. if ($start_tm == false || $start_tm < time()) {
  46. $start_tm = time();
  47. }
  48. if (isset($input['end_time'])) {
  49. $end_tm = strtotime($input['end_time']);
  50. } else {
  51. $end_tm = $start_tm + 24 * 3600;
  52. }
  53. if ($end_tm <= $start_tm) {
  54. $end_tm = $start_tm + 24 * 3600;
  55. }
  56. $param = [];
  57. $param['send_start_date'] = $start_tm;
  58. $param['send_end_date'] = $end_tm;
  59. return $param;
  60. }
  61. public function make($input)
  62. {
  63. if (empty($input['bonus_rate'])) {
  64. $this->mErrMessage = "bonus_rate 参数为空";
  65. return false;
  66. }
  67. $result = [];
  68. $result['type_name'] = $input['type_name'];
  69. $result['type_bless'] = $input['type_bless'];
  70. $result['sender_id'] = self::admin_member_id;
  71. $result['sender_name'] = "熊猫美妆";
  72. $result['send_type'] = $input['send_type'];
  73. $result['usable_days'] = $input['usable_days'];
  74. $rate_moneys = $this->parse_rate($input['bonus_rate']);
  75. if ($rate_moneys == false) {
  76. $this->mErrMessage = "bonus_rate 或者 send_type 参数错误";
  77. return false;
  78. } else {
  79. $result['rate_money'] = $rate_moneys;
  80. }
  81. $param = $this->check_param($input, $ret);
  82. if ($param == false) {
  83. $this->mErrMessage = $ret['msg'];
  84. return false;
  85. }
  86. $result = array_merge($param, $result);
  87. $ret = bonus_helper::make_bonus($result, $rate_moneys);
  88. if ($ret === false) {
  89. return false;
  90. } else {
  91. $type_sn = $ret['type_sn'];
  92. if (BASE_SITE_URL == 'https://manager.lrlz.com') {
  93. $url = "https://passport.lrlz.com/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
  94. } else {
  95. $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
  96. }
  97. return array('type_sn' => $type_sn, 'url' => "{$url}");
  98. }
  99. }
  100. public static function daliy_bonus_url()
  101. {
  102. $url = rkcache('daliy_bonus_url');
  103. if (!empty($url)) {
  104. return $url;
  105. } else {
  106. return "";
  107. }
  108. }
  109. public static function gen_daily_bonus()
  110. {
  111. $now = time();
  112. $today = date('Y-m-d', $now);
  113. $maker = new activity_bonus();
  114. $input['bonus_rate'] = "30|200|100"; //红包参数
  115. $input['type_name'] = "每日签到红包"; //红包名称
  116. $input['type_bless'] = "每天10点 试试手气"; //红包祝福语
  117. $input['usable_days'] = 30; //使用时间
  118. $input['send_type'] = 1; //1:随机红包
  119. $input['start_time'] = $today . ' 10:00:00';
  120. $input['end_time'] = $today . ' 23:59:59';
  121. $input['can_share'] = 0;
  122. $ret = $maker->make($input);
  123. if ($ret == false) {
  124. Log::record("daliy_bonus_url failed", Log::DEBUG);
  125. return "";
  126. } else {
  127. $expire = strtotime($input['end_time']) - $now + 86400;
  128. wkcache('daliy_bonus_url', $ret['url'], $expire);
  129. return $ret['url'];
  130. }
  131. }
  132. }