bonus_helper.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  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/field_helper.php');
  9. require_once (BASE_ROOT_PATH . '/helper/predeposit_helper.php');
  10. require_once (BASE_ROOT_PATH . '/helper/ranklist_helper.php');
  11. require_once (BASE_ROOT_PATH . '/helper/text_filter.php');
  12. require_once (BASE_ROOT_PATH . '/helper/bonus/util.php');
  13. require_once (BASE_ROOT_PATH . '/helper/bonus/type.php');
  14. require_once (BASE_ROOT_PATH . '/helper/bonus/user_bonus.php');
  15. require_once (BASE_ROOT_PATH . '/helper/bonus/generator.php');
  16. require_once (BASE_ROOT_PATH . '/helper/bonus/grab.php');
  17. require_once (BASE_ROOT_PATH . '/helper/bonus/factory.php');
  18. require_once (BASE_ROOT_PATH . '/helper/bonus/manager.php');
  19. require_once (BASE_ROOT_PATH . '/helper/bonus/shaker.php');
  20. require_once (BASE_ROOT_PATH . '/helper/bonus/allocator.php');
  21. require_once (BASE_ROOT_PATH . '/helper/bonus/witholder.php');
  22. require_once (BASE_ROOT_PATH . '/helper/bonus/open_sharer.php');
  23. require_once (BASE_ROOT_PATH . '/helper/bonus/scaler.php');
  24. class bonus_helper
  25. {
  26. const def_bless = '熊猫美妆与你分享美丽的秘密~';
  27. const send_bonus_withold = 1;
  28. const pay_order_withold = 2;
  29. static public function filter_type($type_info)
  30. {
  31. $type = \bonus\type::create_by_paramer($type_info);
  32. $ret = $type->get_param();
  33. $ret['time_out'] = \bonus\manager::grab_period_timeout;
  34. if($type->binded_over()) {
  35. $ret['binded_over'] = 1;
  36. } else {
  37. $ret['binded_over'] = 0;
  38. }
  39. if($type->isEnd()) {
  40. $ret['is_end'] = 1;
  41. } else {
  42. $ret['is_end'] = 0;
  43. }
  44. $type_sn = $type->getType_sn();
  45. $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
  46. $ret['url'] = $url;
  47. $detail_url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=detail&client_type=wap&type_sn={$type_sn}";
  48. $ret['detail_url'] = $detail_url;
  49. return $ret;
  50. }
  51. static public function filter_bonus($bonus_info)
  52. {
  53. if(empty($bonus_info['user_name'])) {
  54. $bonus_info['user_name'] = substr_replace($bonus_info['user_mobile'], '****', 3, 4);
  55. }
  56. return $bonus_info;
  57. }
  58. static public function isFixed($send_type) {
  59. return ($send_type == \bonus\type::SendType_Fixed);
  60. }
  61. static public function isRandom($send_type) {
  62. return ($send_type == \bonus\type::SendType_Random);
  63. }
  64. static public function create_type_input($param) {
  65. return \bonus\type::crate_by_input($param);
  66. }
  67. static public function create_by_paramer($param) {
  68. return \bonus\type::create_by_paramer($param);
  69. }
  70. static public function create_type_sn($type_sn) {
  71. return \bonus\type::create_by_sn($type_sn);
  72. }
  73. static public function make_bonus($param, $rate_moneys)
  74. {
  75. $ret = \bonus\factory::make_bonus($param,$rate_moneys);
  76. return $ret;
  77. }
  78. static public function last_invite_type($member_id)
  79. {
  80. $mod = Model('bonus_type');
  81. $items = $mod->get(array('relayer_id' => $member_id, 'make_type' => \bonus\type::MakeInviteType));
  82. if(empty($items)) {
  83. return null;
  84. } else {
  85. return $items[0];
  86. }
  87. }
  88. static public function get_typeinfo($type_sn)
  89. {
  90. $manager = new \bonus\manager();
  91. return $manager->get_typeinfo($type_sn);
  92. }
  93. static public function get_mine_by_typesn($type_sn) {
  94. $manager = new \bonus\manager();
  95. return $manager->get_mine_by_typesn($type_sn);
  96. }
  97. static public function get_mine_by_bonussn($bonus_sn) {
  98. $manager = new \bonus\manager();
  99. return $manager->get_mine_by_bonussn($bonus_sn);
  100. }
  101. static public function grab_bonus($type_sn)
  102. {
  103. $manager = new \bonus\manager();
  104. return $manager->grab_bonus($type_sn);
  105. }
  106. static public function bind_bonus($bonus_sn,$session_id,$mobile,&$new_bonus_sn)
  107. {
  108. try
  109. {
  110. $manager = new \bonus\manager();
  111. return $manager->bind_bonus($bonus_sn,$session_id,$mobile,$new_bonus_sn);
  112. } catch (Exception $ex) {
  113. return array($ex->getCode(),$ex->getMessage());
  114. }
  115. }
  116. static public function shake($bonus_sn,$strength,$direction)
  117. {
  118. $manager = new \bonus\manager();
  119. return $manager->shake($bonus_sn,$strength,$direction);
  120. }
  121. static public function comment($bonus_sn,$comment)
  122. {
  123. try
  124. {
  125. $manager = new \bonus\manager();
  126. return $manager->comment($bonus_sn,$comment);
  127. } catch (Exception $ex) {
  128. return false;
  129. }
  130. }
  131. static public function topup_bonus(predeposit_helper $pred,$mobile)
  132. {
  133. $mod_bonus = Model('user_bonus');
  134. $bind_bonus = $mod_bonus->getBinded($mobile);
  135. if(empty($bind_bonus)) {
  136. return false;
  137. }
  138. $manager = new \bonus\manager();
  139. $bonusex = array();
  140. foreach($bind_bonus as $val)
  141. {
  142. $type_id = $val['type_id'];
  143. $type = \bonus\type::create_by_id($type_id);
  144. $bonus = \bonus\user_bonus::create_by_param($val);
  145. try
  146. {
  147. if($manager->topup($type,$mod_bonus,$val) == true) {
  148. array_push($bonusex,$val);
  149. ranklist_helper::add_money($_SESSION['member_id'],$bonus->bonus_value());
  150. $pred->add_bonus($bonus,$type);
  151. }
  152. } catch (Exception $ex) {
  153. }
  154. }
  155. if(empty($bonusex)) {
  156. return false;
  157. } else {
  158. return $bonusex;
  159. }
  160. }
  161. static public function direct_asc() {
  162. return \bonus\shaker::direct_asc;
  163. }
  164. static public function direct_dec() {
  165. return \bonus\shaker::direct_dec;
  166. }
  167. static public function get_direction($first)
  168. {
  169. if($first) {
  170. return self::direct_asc();
  171. }
  172. else
  173. {
  174. $direct = mt_rand(1,100);
  175. if($direct % 2 == 0) {
  176. return self::direct_asc();
  177. } else {
  178. return self::direct_dec();
  179. }
  180. }
  181. }
  182. static public function check_shake($bonus_sn,&$first)
  183. {
  184. static $shake_expire = 5;
  185. static $shake_maxcount = 5;
  186. if(!isset($_SESSION['bonus_shake'])) {
  187. $_SESSION['bonus_shake'] = array();
  188. }
  189. if(isset($_SESSION['bonus_shake'][$bonus_sn]))
  190. {
  191. $first = false;
  192. $bonus_shake = &$_SESSION['bonus_shake'][$bonus_sn];
  193. $tmout = time() - $bonus_shake['time'];
  194. if($tmout <= $shake_expire) {
  195. return array('code' => errcode::ErrBonus, 'msg' => '请不要摇得太快~');
  196. } elseif($bonus_shake['count'] > $shake_maxcount) {
  197. ++$bonus_shake['count'];
  198. return array('code' => errcode::ErrBonus, 'msg' => '该红包只允许被摇' . $shake_maxcount .'次~');
  199. } else {
  200. return true;
  201. }
  202. }
  203. else
  204. {
  205. $_SESSION['bonus_shake'][$bonus_sn] = array();
  206. $bonus_shake = &$_SESSION['bonus_shake'][$bonus_sn];
  207. $bonus_shake['count'] = 1;
  208. $bonus_shake['time'] = time();
  209. $first = true;
  210. return true;
  211. }
  212. }
  213. static public function withold($member_id,$rates,$used_type)
  214. {
  215. $fsuccess = true;
  216. $total = 0.00;
  217. $holder = new \bonus\witholder($member_id,$used_type);
  218. foreach ($rates as $rate => $money)
  219. {
  220. if($holder->withold($rate,$money) == true) {
  221. $total += $money;
  222. } else {
  223. $fsuccess = false;
  224. }
  225. }
  226. if($fsuccess) {
  227. return true;
  228. }
  229. else {
  230. return $total;
  231. }
  232. }
  233. static public function withold_money($member_id,$rate,$money,$used_type)
  234. {
  235. $holder = new \bonus\witholder($member_id,$used_type);
  236. if($holder->withold($rate,$money) == false) {
  237. Log::record("withold user member_id={$member_id} money={$money}.",Log::ERR);
  238. }
  239. return true;
  240. }
  241. static public function withold_bonus($member_id,$bonus_sn,$rate,$money,$used_type)
  242. {
  243. $holder = new \bonus\witholder($member_id,$used_type);
  244. return $holder->withold_bonus($rate,$bonus_sn,$money);
  245. }
  246. static public function send($type_sn,$member_ids)
  247. {
  248. try
  249. {
  250. $manager = new \bonus\manager();
  251. return $manager->send($type_sn,$member_ids);
  252. } catch (Exception $ex) {
  253. return false;
  254. }
  255. }
  256. static public function get_share($share_id = 0) {
  257. return \bonus\open_sharer::instance()->get($share_id);
  258. }
  259. static public function can_grab(\bonus\type $type,$member_id,$mobile)
  260. {
  261. $grab_type = $type->grab_type();
  262. if($grab_type == \bonus\type::GrabType_All) {
  263. return true;
  264. }
  265. elseif($grab_type == \bonus\type::GrabType_EXSENDER)
  266. {
  267. if($type->sender_id() == $member_id) {
  268. return false;
  269. }
  270. if($type->sender_mobile() == $mobile) {
  271. return false;
  272. }
  273. return true;
  274. }
  275. else
  276. {
  277. $member_id = intval($member_id);
  278. if($member_id > 0 && !empty($mobile)) {
  279. $cond['member_id|member_mobile'] = array('_multi'=>true,$member_id,$mobile);
  280. }
  281. elseif($member_id > 0) {
  282. $cond['member_id'] = $member_id;
  283. }
  284. elseif(!empty($mobile)) {
  285. $cond['member_mobile'] = $mobile;
  286. }
  287. else {
  288. return true;
  289. }
  290. $items = Model('member')->getMemberInfo($cond);
  291. return (empty($items) == true);
  292. }
  293. }
  294. static public function match_price($rate_moneys)
  295. {
  296. if(empty($rate_moneys)) return false;
  297. $scaler = new \bonus\scaler($rate_moneys);
  298. return $scaler->calc();
  299. }
  300. }