bonus_helper.php 12 KB

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