bonus_helper.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 16/4/11
  6. * Time: 上午12:51
  7. */
  8. require_once (BASE_ROOT_PATH . '/helper/bonus/util.php');
  9. require_once (BASE_ROOT_PATH . '/helper/bonus/type.php');
  10. require_once (BASE_ROOT_PATH . '/helper/bonus/user_bonus.php');
  11. require_once (BASE_ROOT_PATH . '/helper/bonus/generator.php');
  12. require_once (BASE_ROOT_PATH . '/helper/bonus/grab.php');
  13. require_once (BASE_ROOT_PATH . '/helper/bonus/bind.php');
  14. require_once (BASE_ROOT_PATH . '/helper/bonus/factory.php');
  15. require_once (BASE_ROOT_PATH . '/helper/bonus/manager.php');
  16. require_once (BASE_ROOT_PATH . '/helper/field_helper.php');
  17. require_once (BASE_ROOT_PATH . '/helper/predeposit_helper.php');
  18. class bonus_helper
  19. {
  20. const def_bless = '恭喜发财大吉大利';
  21. const max_total_amount = 1000000;
  22. const max_total_num = 1000;
  23. static public function filter_type($type_info) {
  24. $type = \bonus\type::crate_by_paramer($type_info);
  25. $fileds = 'type_sn,type_bless,send_type,sender_name,total_amount,total_num,max_amount,grabed_num,binded_num,send_start_date,send_end_date,binded_over,binded_period,grab_lastime';
  26. $ret = array();
  27. field_helper::copy_column($ret,$type_info,$fileds);
  28. $ret['time_out'] = \bonus\manager::grab_period_timeout;
  29. if($type->binded_over()) {
  30. $ret['binded_over'] = 1;
  31. } else {
  32. $ret['binded_over'] = 0;
  33. }
  34. if($type->isEnd()) {
  35. $ret['is_end'] = 1;
  36. } else {
  37. $ret['is_end'] = 0;
  38. }
  39. $type_sn = $type->getType_sn();
  40. $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
  41. $ret['url'] = $url;
  42. return $ret;
  43. }
  44. static public function filter_bonus($bonus_info) {
  45. $fileds = 'bonus_sn,bonus_value,user_name,user_mobile,user_comment,get_time,bonus_status';
  46. $ret = array();
  47. field_helper::copy_column($ret,$bonus_info,$fileds);
  48. if(empty($bonus_info['user_name'])) {
  49. $ret['user_name'] = substr_replace($bonus_info['user_mobile'], '****', 3, 4);
  50. }
  51. return $ret;
  52. }
  53. static public function isFixed($send_type) {
  54. return ($send_type == \bonus\type::fixed_amount);
  55. }
  56. static public function isRandom($send_type) {
  57. return ($send_type == \bonus\type::random_amount);
  58. }
  59. static public function make_bonus($param)
  60. {
  61. $ret = \bonus\factory::make_bonus($param);
  62. return $ret;
  63. }
  64. static public function get_typeinfo($type_sn)
  65. {
  66. $manager = new \bonus\manager();
  67. return $manager->get_typeinfo($type_sn);
  68. }
  69. static public function get_mine_by_typesn($type_sn) {
  70. $manager = new \bonus\manager();
  71. return $manager->get_mine_by_typesn($type_sn);
  72. }
  73. static public function get_mine_by_bonussn($bonus_sn) {
  74. $manager = new \bonus\manager();
  75. return $manager->get_mine_by_bonussn($bonus_sn);
  76. }
  77. static public function grab_bonus($type_sn)
  78. {
  79. $manager = new \bonus\manager();
  80. return $manager->grab_bonus($type_sn);
  81. }
  82. static public function bind_bonus($bonus_sn,$session_id,$mobile,&$new_bonus_sn)
  83. {
  84. try
  85. {
  86. $manager = new \bonus\manager();
  87. return $manager->bind_bonus($bonus_sn,$session_id,$mobile,$new_bonus_sn);
  88. } catch (Exception $ex) {
  89. return array($ex->getCode(),$ex->getMessage());
  90. }
  91. }
  92. static public function comment($bonus_sn,$comment)
  93. {
  94. try
  95. {
  96. $manager = new \bonus\manager();
  97. return $manager->comment($bonus_sn,$comment);
  98. } catch (Exception $ex) {
  99. return false;
  100. }
  101. }
  102. static public function topup_bonus($mobile)
  103. {
  104. $mod_bonus = Model('user_bonus');
  105. $bind_bonus = $mod_bonus->getBinded($mobile);
  106. if(empty($bind_bonus)) {
  107. return false;
  108. }
  109. $manager = new \bonus\manager();
  110. $pd_helper = new predeposit_helper();
  111. $bonusex = array();
  112. foreach($bind_bonus as $val)
  113. {
  114. $type_id = $val['type_id'];
  115. $type = \bonus\type::create_by_id($type_id);
  116. try
  117. {
  118. $info = sprintf("来自%s的红包.",$val['sender_name']);
  119. Db::beginTransaction();
  120. $pd_helper->bonus_add_money($val['bonus_value'],$val['bonus_sn'],$type->sender_name(),$info);
  121. if($manager->topup($mod_bonus,$val) === false) {
  122. throw new Exception();
  123. } else {
  124. $bonus['mine_bonus'] = $val;
  125. array_push($bonusex,$val);
  126. }
  127. Db::commit();
  128. } catch (Exception $ex) {
  129. Db::rollback();
  130. }
  131. }
  132. if(empty($bonusex)) {
  133. return false;
  134. } else {
  135. return $bonusex;
  136. }
  137. }
  138. //检查个人发送红包参数
  139. static public function check_personal($input,&$ret)
  140. {
  141. $param = array();
  142. $send_type = intval($input['send_type']);
  143. if(!in_array($send_type,array(1,2))) {
  144. $ret = array('code' => errcode::ErrParamter,'msg' => "请输入正确的红包类型.");
  145. return false;
  146. }
  147. $param['send_type'] = $send_type; // '红包类型,1为随机红包,2为固定额度红包'
  148. $type_bless = isset($input['type_bless']) && !empty($input['type_bless']) ? $input['type_bless'] : self::def_bless;
  149. $type_bless = urldecode($type_bless);
  150. $type_bless = text_filter::filter_input($type_bless);
  151. $param['type_bless'] = $type_bless;
  152. $param['total_num'] = intval($input['total_num']);
  153. if($param['total_num'] <= 0) {
  154. $ret = array('code' => errcode::ErrParamter,'msg' => "红包个数不能小于1.");
  155. return false;
  156. }
  157. if(bonus_helper::isFixed($send_type))
  158. {
  159. $fixed_mondey = floatval($input['fixed_money']);
  160. if ($fixed_mondey * 100 < 1) {
  161. $ret = array('code' => errcode::ErrParamter,'msg' => "红包额度不能小于一分钱.");
  162. return false;
  163. }
  164. $param['total_amount'] = $fixed_mondey * $param['total_num'];
  165. $param['fixed_money'] = $fixed_mondey;
  166. }
  167. else if (bonus_helper::isRandom($send_type))
  168. {
  169. $total_amount = floatval($input['total_amount']);
  170. if($total_amount * 100 < $param['total_num']) {
  171. $ret = array('code' => errcode::ErrParamter,'msg' => "金额不够.");
  172. return false;
  173. }
  174. $param['total_amount'] = $total_amount;
  175. }
  176. if($total_amount > self::max_total_amount || $param['total_num'] > self::max_total_num) {
  177. $ret = array('code' => errcode::ErrParamter,'msg' => sprintf("红包个数不能大于%d,总金额不能大于%.2f元.",self::max_total_num,self::max_total_amount));
  178. return false;
  179. }
  180. $param['use_type'] = 1;
  181. $param['user_type'] = 2;
  182. return $param;
  183. }
  184. //检查活动红包参数
  185. static public function check_activity($input,&$ret)
  186. {
  187. if(!isset($input['start_time']) || !isset($input['end_time'])) {
  188. $ret = array('code' => errcode::ErrParamter,'msg' => "请输入活动开始和结束时间.");
  189. return false;
  190. }
  191. $start_tm = strtotime($input['start_time']);
  192. if($start_tm == false) {
  193. $ret = array('code' => errcode::ErrParamter,'msg' => "错误的活动开始时间格式,请参考,2016-06-04 10:00:00.");
  194. return false;
  195. }
  196. if($start_tm < time()) {
  197. $cur_time = date("Y-m-d H:i:s");
  198. $ret = array('code' => errcode::ErrParamter,'msg' => "活动时间不能小于服务器当前时间:{$cur_time}");
  199. return false;
  200. }
  201. $end_tm = strtotime($input['end_time']);
  202. if($end_tm == false) {
  203. $ret = array('code' => errcode::ErrParamter,'msg' => "错误的活动结束时间格式,请参考,2016-06-04 10:00:00.");
  204. return false;
  205. }
  206. if($end_tm <= $start_tm) {
  207. $ret = array('code' => errcode::ErrParamter,'msg' => "活动结束时间必须大于开始时间");
  208. return false;
  209. }
  210. $param = array();
  211. $param['send_start_date'] = $start_tm;
  212. $param['send_end_date'] = $end_tm;
  213. return $param;
  214. }
  215. }