bonus_helper.php 9.7 KB

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