瀏覽代碼

增加红包折扣率

stanley-king 8 年之前
父節點
當前提交
e7cad5d124

+ 0 - 31
helper/account_helper.php

@@ -105,37 +105,6 @@ class account_helper
         }
     }
 
-    public static function share_bonus($member_id,$bonus_sn,&$msg)
-    {
-        $bonus = bonus\user_bonus::create_by_sn($bonus_sn);
-        $amount = $bonus->remain_amount();
-        if($bonus->spend_over()) {
-            $msg = "该红包现金已经花光了~";
-            return false;
-        }
-
-        $param = self::base_param($amount,1);
-
-        $minfo = new member_info($member_id);
-        $param['sender_id'] = $member_id;
-        $param['sender_mobile'] = $minfo->mobile();
-        $param['sender_name'] = $minfo->nickname();
-        $param['make_type'] = \bonus\type::MakeSendType;
-        $name = $minfo->nickname();
-        $param['type_name'] = "{$name}";
-
-        $ret = bonus_helper::make_bonus($param);
-        if($ret != false) {
-            $predo = new predeposit_helper($member_id);
-            $predo->handout_bonus($amount,$ret['type_sn'],$minfo->nickname(),"您分享了{$amount}元的红包.",\bonus\type::MakeSendType);
-            bonus_helper::withold_bonus($member_id,$bonus_sn);
-
-            return $ret;
-        } else {
-            return false;
-        }
-    }
-
     public static function gain_sysBonus($to_id,$amount)
     {
         $param = self::base_param($amount,1);

+ 47 - 41
helper/bonus/generator.php

@@ -51,6 +51,7 @@ class DirectGenerator extends IGenerator
 //发个任意人群的红包
 class GeneralGenerator extends IGenerator
 {
+    private $mDatas;
     public function __construct($type) {
         parent::__construct($type);
     }
@@ -61,6 +62,8 @@ class GeneralGenerator extends IGenerator
         $type_id = $this->mType->getType_id();
         $type_sn = $this->mType->getType_sn();
 
+        $this->mDatas = [];
+
         $min_val = -1;
         $max_val = -1;
         foreach ($rates as $item)
@@ -71,60 +74,75 @@ class GeneralGenerator extends IGenerator
 
             if($this->mType->isRandomAmount())
             {
-                $fSuccess = $this->random($type_id,$type_sn,$paramer['min_amount'],$amount,$num,$rate,$min_tmp,$max_tmp);
-                if($fSuccess == false) {
-                    return false;
+                $this->random($type_id,$type_sn,$paramer['min_amount'],$amount,$num,$rate,$min_tmp,$max_tmp);
+                if($min_val == -1) {
+                    $min_val = $min_tmp;
                 }
                 else
                 {
-                    if($min_val == -1) {
+                    if(intval($min_tmp * 100 + 0.5) < intval($min_val * 100 + 0.5)) {
                         $min_val = $min_tmp;
                     }
-                    else
-                    {
-                        if(intval($min_tmp * 100 + 0.5) < intval($min_val * 100 + 0.5)) {
-                            $min_val = $min_tmp;
-                        }
-                    }
+                }
 
-                    if($max_val == -1) {
+                if($max_val == -1) {
+                    $max_val = $max_tmp;
+                }
+                else
+                {
+                    if(intval($max_tmp * 100 + 0.5) > intval($max_val * 100 + 0.5)) {
                         $max_val = $max_tmp;
                     }
-                    else
-                    {
-                        if(intval($max_tmp * 100 + 0.5) > intval($max_val * 100 + 0.5)) {
-                            $max_val = $max_tmp;
-                        }
-                    }
                 }
             }
             else
             {
                 $fixed_money =  $paramer['fixed_money'];
-                $fSuccess = $this->general($type_id,$type_sn,$num,$fixed_money,$rate);
-                if($fSuccess == false) {
-                    return false;
-                }
+                $this->general($type_id,$type_sn,$num,$fixed_money,$rate);
             }
         }
 
+        if(empty($this->mDatas)) {
+            return false;
+        }
+
+        $bonus = $this->mislead();
+        $mod_bonus = Model('user_bonus');
+        $ret = $mod_bonus->insertAll($bonus);
+        if(!$ret) {
+            Log::record("insert user_bonus err: type_id = {$type_id}.");
+            return false;
+        }
 
         if($this->mType->isRandomAmount()) {
             Model('bonus_type')->edit(array('type_id' => $type_id),array('min_amount' => $min_val,'max_amount' => $max_val));
         }
-
         return true;
     }
 
+    private function mislead()
+    {
+        $ret = $this->mDatas;
+        $num = count($ret);
+        for($index = 0; $index < $num - 1; $index++) {
+            $pos_a = mt_rand(0, $num - 1);
+            $pos_b = mt_rand(0, $num - 1);
+
+            $tmp = $ret[$pos_a];
+            $ret[$pos_a] = $ret[$pos_b];
+            $ret[$pos_b] = $tmp;
+        }
+        return $ret;
+    }
+
     private function random($type_id,$type_sn,$min_amount,$amount,$num,$rate,&$min_val,&$max_val)
     {
-        $mod_bonus = Model('user_bonus');
         $allocator = new allocator();
         $moneys = $allocator->separate_money($min_amount,$amount,$num,$min_val,$max_val);
 
         foreach($moneys as $val)
         {
-            $datas[] = array('bonus_sn' => make_sn(),
+            $item = array('bonus_sn' => make_sn(),
                 'bonus_value' => $val,
                 'remain_amount' => $val,
                 'type_id' => $type_id,
@@ -132,37 +150,25 @@ class GeneralGenerator extends IGenerator
                 'bonus_rate' => $rate,
                 'type_sn' => $type_sn);
 
+            $this->mDatas[] = $item;
         }
-        $ret = $mod_bonus->insertAll($datas);
-        if(!$ret) {
-            Log::record("insert user_bonus err: type_id = {$type_id}.");
-            return false;
-        }
-        return true;
     }
 
     private function general($type_id,$type_sn,$total_num,$fixed_money,$rate)
     {
-        $mod_bonus = Model('user_bonus');
         $val =  $fixed_money;
-        for($i = 0; $i < $total_num; $i++) {
-            $datas[] = array( 'bonus_sn' => make_sn(),
+        for($i = 0; $i < $total_num; $i++)
+        {
+            $item = array( 'bonus_sn' => make_sn(),
                 'bonus_value' => $val,
                 'remain_amount' => $val,
                 'type_id' => $type_id,
                 'bonus_status' => 0,
                 'bonus_rate' => $rate,
                 'type_sn' => $type_sn);
+            $this->mDatas[] = $item;
         }
-        $ret = $mod_bonus->insertAll($datas);
-        if(!$ret) {
-            Log::record("insert user_bonus err: type_id = {$type_id}.");
-            return false;
-        }
-        return true;
     }
-
-
 }
 
 

+ 11 - 1
helper/bonus/type.php

@@ -207,7 +207,17 @@ class type
         $amount = intval($this->mParam['remain_amount'] * 100 + 0.5);
         return $amount / 100;
     }
-
+    public function bonus_rate() {
+        return intval($this->mParamer['bonus_rate']);
+    }
+    // 为了兼容老版本,提供设置红包折扣率接口
+    public function set_rate($rate)
+    {
+        if(empty($rate) && $rate > 0) {
+            $this->mParamer['bonus_rate'] = intval($rate);
+        }
+    }
+    
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     static public function crate_by_input($paramer)
     {

+ 0 - 2
helper/bonus/user_bonus.php

@@ -108,7 +108,6 @@ class user_bonus
             return mb_substr($user_comment,0,15) . '...';
         }
     }
-
     public function get_time()
     {
         return intval($this->mParamer['get_time']);
@@ -123,7 +122,6 @@ class user_bonus
     public function bonus_rate() {
         return intval($this->mParamer['bonus_rate']);
     }
-
     ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
     static public function create_by_param($param) {
         return new user_bonus($param);

+ 5 - 2
helper/bonus/witholder.php

@@ -71,9 +71,12 @@ class witholder
         return $this->usable_amount;
     }
 
-    public function withold_bonus($bonus_sn)
+    public function withold_bonus($rate,$bonus_sn)
     {
-        foreach ($this->mRateBonus as $bonus)
+        if(array_key_exists($rate,$this->mRateBonus) == false) return false;
+
+        $bounses = $this->mRateBonus[$rate]['bonus'];
+        foreach ($bounses as $bonus)
         {
             if($bonus_sn == $bonus->bonus_sn()) {
                 $param = $bonus->get_param();

+ 3 - 3
helper/bonus_helper.php

@@ -33,7 +33,7 @@ class bonus_helper
     static public function filter_type($type_info)
     {
         $type = \bonus\type::crate_by_paramer($type_info);
-        $fileds = 'type_sn,type_bless,send_type,sender_name,total_amount,remain_amount,total_num,max_amount,grabed_num,binded_num,send_start_date,send_end_date,binded_over,binded_period,grab_lastime,make_type';
+        $fileds = 'type_sn,type_bless,send_type,sender_name,total_amount,remain_amount,total_num,max_amount,bonus_rate,grabed_num,binded_num,send_start_date,send_end_date,binded_over,binded_period,grab_lastime,make_type';
         $ret = array();
         field_helper::copy_column($ret,$type_info,$fileds);
         $ret['time_out'] = \bonus\manager::grab_period_timeout;
@@ -264,10 +264,10 @@ class bonus_helper
         return $holder->withold($money);
     }
 
-    static public function withold_bonus($member_id,$bonus_sn)
+    static public function withold_bonus($member_id,$rate,$bonus_sn)
     {
         $holder = new \bonus\witholder($member_id);
-        return $holder->withold_bonus($bonus_sn);
+        return $holder->withold_bonus($rate,$bonus_sn);
     }
 
     static public function send($type_sn,$member_ids)

+ 133 - 154
helper/predeposit_helper.php

@@ -7,15 +7,21 @@
  */
 
 
-require_once(BASE_ROOT_PATH.'/helper/model_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/model_helper.php');
+require_once(BASE_ROOT_PATH . '/helper/bonus_helper.php');
+
 
 class RateMoney
 {
     const ASC  = 1;
     const DESC = 2;
     private $mRates;
+    private $mDirty;
+
     public function __construct($rates)
     {
+        $this->mDirty = false;
+
         $this->mRates = [];
         foreach ($rates as $key => $val)
         {
@@ -27,7 +33,7 @@ class RateMoney
         ksort($this->mRates);
     }
 
-    public function is_enough($amount,&$rate)
+    public function is_enough(&$rate,$amount)
     {
         $amount = intval($amount * 100 + 0.5);
         foreach ($this->mRates as $key => $val)
@@ -57,157 +63,31 @@ class RateMoney
         return false;
     }
 
+    public function with_hold($rate,$amount)
+    {
+        if(array_key_exists($rate,$this->mRates)) {
+            $this->mDirty = true;
+            $this->mRates[$rate] = $this->mRates[$rate] - $amount;
+        }
+    }
 
-//    public function separate($order_type,$amount,$num)
-//    {
-//        $rates =  $this->mRates;
-//        if($order_type == self::ASC) {
-//            ksort($rates);
-//        } else {
-//            krsort($rates);
-//        }
-//
-//        $moneys = $this->moneys($rates,$amount);
-//        return $this->numbers($moneys,$num);
-//    }
-//
-//    private function numbers($moneys,$num)
-//    {
-//        $count = count($moneys);
-//        if($count > $num)
-//        {
-//            $result = [];
-//
-//            $pos = 0;
-//            foreach ($moneys as $key => $val) {
-//                if($pos >= $num) break;
-//                $item['rate'] = $key;
-//                $item['amount'] = $val;
-//                $item['num'] = 1;
-//                $result[] = $item;
-//            }
-//
-//            return $result;
-//        }
-//        else if($count == $num)
-//        {
-//            $result = [];
-//            foreach ($moneys as $key => $val) {
-//                $item['rate'] = $key;
-//                $item['amount'] = $val;
-//                $item['num'] = 1;
-//                $result[] = $item;
-//            }
-//            return $result;
-//        }
-//        else
-//        {
-//            return $this->nor_num($moneys,$num);
-//        }
-//    }
-//
-//    private function nor_num($moneys,$num)
-//    {
-//        $amount = 0;
-//        $nums = [];
-//        foreach ($moneys as $key => $val) {
-//            $amount += intval($val * 100 + 0.5);
-//        }
-//
-//        foreach ($moneys as $key => $val)
-//        {
-//            $val = intval($val * 100 + 0.5);
-//            $n =  intval($val * $num / $amount + 0.5);
-//            if($n == 0) {
-//                $n = 1;
-//            }
-//            $nums[] = $n;
-//        }
-//
-//        $delta = array_sum($nums) - $num;
-//        if($delta > 0)
-//        {
-//            while($delta > 0)
-//            {
-//                foreach ($nums as &$n)
-//                {
-//                    if($delta == 0) break;
-//
-//                    if($n > 1) {
-//                        $n -= 1;
-//                        $delta -= 1;
-//                    }
-//                }
-//            }
-//        }
-//        elseif($delta < 0)
-//        {
-//            while($delta < 0)
-//            {
-//                foreach ($nums as &$n)
-//                {
-//                    if($delta == 0) break;
-//                    if($n > 1) {
-//                        $n += 1;
-//                        $delta += 1;
-//                    }
-//                }
-//            }
-//        }
-//
-//        $result = [];
-//        $pos = 0;
-//        foreach ($moneys as $key => $val) {
-//            $item['rate'] = $key;
-//            $item['amount'] = $val;
-//            $item['num'] = $nums[$pos];
-//            $pos++;
-//
-//            $result[] = $item;
-//        }
-//
-//        return $result;
-//    }
-//
-//    private function moneys($rates,$amount)
-//    {
-//        $result = [];
-//        $amount = intval($amount * 100 + 0.5);
-//        foreach ($rates as $key => $val)
-//        {
-//            $val = intval($val * 100 + 0.5);
-//            if($val >= $amount) {
-//                $result[$key] = $amount / 100;
-//                $amount = 0;
-//            } else {
-//                $result[$key] = $val / 100;
-//                $amount -= $val;
-//            }
-//
-//            if($amount == 0) break;
-//        }
-//
-//        return $result;
-//    }
-//    public function is_enough($amount)
-//    {
-//        $amount = intval($amount * 100 + 0.5);
-//        foreach ($this->mRates as $key => $val)
-//        {
-//            $val = intval($val * 100 + 0.5);
-//            if($val >= $amount) {
-//                $amount = 0;
-//            } else {
-//                $amount -= $val;
-//            }
-//
-//            if($amount == 0) return true;
-//        }
-//
-//        return false;
-//    }
-}
+    public function format()
+    {
+        $result = [];
+        foreach ($this->mRates as $key => $val)
+        {
+            $val = intval($val * 100 + 0.5);
+            if($val > 0) {
+                $result[$key] = $val / 100;
+            }
+        }
+        return $result;
+    }
 
+    public function dirty() {
+        return $this->mDirty;
+    }
+}
 
 class predeposit_helper
 {
@@ -227,12 +107,28 @@ class predeposit_helper
         $this->mFreeze_value = $pd_array['freeze_predeposit'];     // 当前预存款冻结
 
         $available_bonus = $pd_array['available_bonus'];
-        if (empty($available_bonus)) {
-            $this->mRates = null;
+        if (empty($available_bonus))
+        {
+            $total = intval($this->mTotal_value * 100 + 0.5);
+            if($total > 0) {
+                $this->mRates = new RateMoney(array(30 => $this->mTotal_value));
+            } else {
+                $this->mRates = null;
+            }
         } else {
             $this->mRates = new RateMoney(unserialize($available_bonus));
         }
     }
+
+    public function __destruct()
+    {
+        if($this->mRates != null && $this->mRates->dirty()) {
+            $data = $this->mRates->format();
+            $member = Model('member');
+            $member->editMember(array('member_id' => $this->member_id),array('available_bonus' => serialize($data)));
+        }
+    }
+
     public function total_pred() {
         return $this->mTotal_value;
     }
@@ -242,10 +138,93 @@ class predeposit_helper
     public function is_enough($money) {
         return intval($this->total_pred() * 100) >= intval($money * 100);
     }
+
+    public function person_enough($money,&$bonus_rate)
+    {
+        if($this->mRates == null) return false;
+        return $this->mRates->is_enough($bonus_rate,$money);
+    }
+
     public function rates() {
         return $this->mRates == null ? false : $this->mRates;
     }
 
+    public function make_bonus($param,$rate_moneys)
+    {
+        $result = bonus_helper::make_bonusex($param,$rate_moneys);
+        if($result == false) return false;
+
+        bonus_helper::witholdex($this->member_id,$rate_moneys);
+        $type_sn = $result['type_sn'];
+        $money = $result['money'];
+        $this->handout_bonus($money,$type_sn,session_helper::nickname(),"发送了{$money}元的红包.",\bonus\type::MakeSendType);
+
+        foreach ($rate_moneys as $item) {
+            $this->mRates->with_hold($item['rate'],$item['amount']);
+        }
+
+        return $result;
+    }
+
+    private function base_param($amount,$total_num)
+    {
+        $param = array();
+        $param['total_amount'] = $amount;
+        $param['total_num'] = $total_num;
+        $param['send_type'] = 1;
+        $param['use_type'] = 1;
+        $param['user_type'] = 2;
+
+        return $param;
+    }
+
+    public function share_bonus($bonus_sn,&$msg)
+    {
+        $bonus = bonus\user_bonus::create_by_sn($bonus_sn);
+        if($bonus->spend_over()) {
+            $msg = "该红包现金已经花光了~";
+            return false;
+        }
+
+        $amount = $bonus->remain_amount();
+        $param = $this->base_param($amount,1);
+
+        $minfo = new member_info($this->member_id);
+        $param['sender_id'] = $this->member_id;
+        $param['sender_mobile'] = $minfo->mobile();
+        $param['sender_name'] = $minfo->nickname();
+        $param['make_type'] = \bonus\type::MakeSendType;
+        $name = $minfo->nickname();
+        $param['type_name'] = "{$name}";
+
+        $type = \bonus\type::crate_by_input($param);
+
+        $rate_moneys = [];
+        $item['amount'] = $type->getTotal_amount();
+        $item['num'] = $type->getTotal_num();
+        $item['rate'] = $bonus->bonus_rate();
+        $rate_moneys[] = $item;
+
+        $result = bonus_helper::make_bonusex($param,$rate_moneys);
+        if($result == false) {
+            return false;
+        }
+        else
+        {
+            if(bonus_helper::withold_bonus($this->member_id,$bonus->bonus_rate(),$bonus_sn)) {
+                $type_sn = $result['type_sn'];
+                $money = $result['money'];
+                $this->handout_bonus($money,$type_sn,session_helper::nickname(),"发送了{$money}元的红包.",\bonus\type::MakeSendType);
+            }
+            return $result;
+        }
+    }
+
+    public function admin_make_bonus($param,$rate_moneys)
+    {
+        return bonus_helper::make_bonusex($param,$rate_moneys);
+    }
+
     public function bonus_expire($bouns)
     {
         try
@@ -327,7 +306,7 @@ class predeposit_helper
         $this->model_pd->changePd("bonus_add_money", $data);
     }
 
-    public function handout_bonus($amount, $type_sn, $sender_name, $info,$make_type = 0,$rate_moneys = array())
+    public function handout_bonus($amount, $type_sn, $sender_name, $info,$make_type = 0)
     {
         Log::record("handout_bonus {$amount} {$type_sn} {$sender_name}",Log::DEBUG);
         $minfo = new member_info($this->member_id);

+ 106 - 11
mobile/control/admin_oper.php

@@ -13,8 +13,83 @@ class admin_operControl extends mbMemberControl
     public function __construct()
     {
         parent::__construct();
-        if($_SESSION['member_id'] != 36490) {
-            throw new Exception("必须是管理员才能操作此项功能");
+//        if($_SESSION['member_id'] != 36429) {
+//            throw new Exception("必须是管理员才能操作此项功能");
+//        }
+    }
+
+    private function parse_rate($bonus_rate,&$total_num,&$toal_amount,&$max_rate)
+    {
+        $toal_amount = 0;
+        $total_num = 0;
+        $max_rate = 0;
+        $params = explode('#', urldecode($bonus_rate));
+        $result = [];
+        foreach ($params as $val)
+        {
+            if(preg_match_all('/^(\d{1,2})\|(\d{1,4})\|(\d{1,6})$/', $val, $match))
+            {
+                $item['rate'] = intval($match[1][0]);
+                $item['amount'] = intval($match[2][0]);
+                $item['num'] = intval($match[3][0]);
+
+                $toal_amount += $item['amount'];
+                $total_num += $item['num'];
+
+                if($item['rate'] > $max_rate) {
+                    $max_rate = $item['rate'];
+                }
+
+                $result[] = $item;
+            }
+        }
+        if(empty($result)) {
+            return false;
+        }
+        else {
+            return $result;
+        }
+    }
+
+    public function make_bonusOp()
+    {
+        if(empty($_GET['bonus_rate'])) {
+            return self::outerr(errcode::ErrParamter,"bonus_rate 参数为空");
+        }
+
+        $rate_moneys = $this->parse_rate($_GET['bonus_rate'],$total_num,$toal_amount,$max_rate);
+        if($rate_moneys == false) {
+            return self::outerr(errcode::ErrParamter,"bonus_rate 参数错误");
+        } else {
+            $_GET['total_amount'] = $toal_amount;
+            $_GET['total_num'] = $total_num;
+        }
+
+        $param_active = bonus_helper::check_activity($_GET,$ret);
+        if($param_active == false) {
+            return self::outerr($ret['code'],$ret['msg']);
+        }
+
+        $param_personal = bonus_helper::check_personal($_GET,$ret);
+        if($param_personal == false) {
+            return self::outerr($ret['code'],$ret['msg']);
+        }
+        $param = array_merge($param_active,$param_personal);
+        $param['sender_id'] = $_SESSION['member_id'];
+        $param['sender_mobile'] = $_SESSION['member_mobile'];
+        $param['sender_name'] = session_helper::nickname();
+
+        $type = \bonus\type::crate_by_input($param);
+        $type->set_rate($max_rate);
+
+        $pre_helper = new predeposit_helper($_SESSION['member_id']);
+        $ret = $pre_helper->admin_make_bonus($type->get_param(),$rate_moneys);
+        if($ret === false) {
+            return self::outerr(errcode::ErrBonus,"生成红包失败.");
+        } else {
+            $type_sn = $ret['type_sn'];
+            $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
+            return self::outsuccess(array('type_sn' => $ret,'url' => "{$url}"));
         }
     }
 
@@ -68,21 +143,41 @@ class admin_operControl extends mbMemberControl
             }
         }
     }
+
     public function reset_bounsOp()
     {
+        $err_user = [];
         $mod_member = Model('member');
-        $items = $mod_member->field('available_predeposit,available_bonus')->where(array('member_id' => array('in',array(ORDER_STATE_PAY))))->limit(false)->select();
-        foreach($items as $val)
+        $items = $mod_member->field('available_predeposit,available_bonus,member_id')->where(array('member_id' => array('gt',0)))->limit(false)->select();
+        foreach($items as $item)
         {
-            $pay_sn = $val['pay_sn'];
-            $trade_no = $val['trade_no'];
-
-            $logic_delivery = Logic('delivery');
-            $ret = $logic_delivery->putOrder($pay_sn, $trade_no);
-            if(!$ret) {
-                Log::record("putallOp pay_sn={$pay_sn} trade_no = {$trade_no}");
+            $bonus = [];
+            $bonus[30] = intval(doubleval($item['available_predeposit']) * 100 + 0.5) / 100;
+            $strbonus = serialize($bonus);
+            $member_id = intval($item['member_id']);
+            $ret = Model()->table('member')->where(array('member_id' => $member_id))->update(array('available_bonus' => $strbonus));
+            if($ret == false) {
+                $err_user[] = $member_id;
             }
         }
+
+        if(empty($err_user)) {
+            return self::outsuccess(null);
+        } else {
+            return self::outerr($err_user);
+        }
+    }
+
+    public function reset_bonus_typeOp()
+    {
+        $bonus_type = Model('bonus_type');
+        $ret = $bonus_type->where(array('type_id' => array('gt',0)))->update(array('bonus_rate' => 30));
+
+        if($ret == false) {
+            return self::outerr(errcode::ErrDB);
+        } else {
+            return self::outsuccess(null);
+        }
     }
 
 //    // 更新商品序列号

+ 4 - 3
mobile/control/control.php

@@ -118,7 +118,7 @@ class mobileControl
         {
             Tpl::output("error", $msg);
             if (!empty($page)) {
-                TPL::showpage($page);
+                Tpl::showpage($page);
             }
         }
         else if ($show_type == 'ajax')
@@ -148,6 +148,7 @@ class mobileControl
                 echo "{$sql}<br/>";
             }
         }
+        return true;
     }
 
     static public function outsuccess($data, $page = '',$type = NULL)
@@ -168,11 +169,11 @@ class mobileControl
         {
             if (is_array($data)) {
                 foreach ($data as $key => $val) {
-                    TPL::output($key, $val);
+                    Tpl::output($key, $val);
                 }
             }
             if (!empty($page)) {
-                TPL::showpage($page);
+                Tpl::showpage($page);
             }
         }
         else if ($show_type == 'ajax')

+ 40 - 59
mobile/control/member_bonus.php

@@ -28,12 +28,7 @@ class member_bonusControl extends mbMemberControl
             return self::outerr($ret['code'],$ret['msg']);
         }
 
-        $pred = new predeposit_helper($_SESSION['member_id']);
-        if(!$pred->is_enough($param['total_amount'])) {
-            return self::outerr(errcode::ErrBonusNotEnough,'余额不够发送红包');
-        }
-
-        if (!empty($_GET['bonus_rate']) && intval($_GET['bonus_rate']) > 0) {
+        if (intval($_GET['bonus_rate']) > 0) {
             $bonus_rate = intval($_GET['bonus_rate']);
         } else {
             $bonus_rate = -1;
@@ -44,10 +39,11 @@ class member_bonusControl extends mbMemberControl
         $param['sender_name'] = session_helper::nickname();
         $type = bonus_helper::create_type($param);
 
-        $rates = $pred->rates();
-        if($rates->is_enough($type->getTotal_amount(),$bonus_rate) == false) {
-            return self::outerr(errcode::ErrBonusNotEnough,'该折扣率的红包余额不够......');
+        $pred = new predeposit_helper($_SESSION['member_id']);
+        if(!$pred->person_enough($type->getTotal_amount(),$bonus_rate)) {
+            return self::outerr(errcode::ErrBonusNotEnough,'余额不够发送红包');
         }
+        $type->set_rate($bonus_rate);
 
         $rate_moneys = [];
         $item['amount'] = $type->getTotal_amount();
@@ -55,56 +51,11 @@ class member_bonusControl extends mbMemberControl
         $item['rate'] = $bonus_rate;
         $rate_moneys[] = $item;
 
-        $ret = bonus_helper::make_bonusex($param,$rate_moneys);
-        if($ret === false) {
-            return self::outerr(errcode::ErrBonus,"生成红包失败.");
-        }
-        else
-        {
-            $type_sn = $ret['type_sn'];
-            $money = $ret['money'];
-
-            bonus_helper::witholdex($_SESSION['member_id'],$rate_moneys);
-            $pred->handout_bonus($money,$type_sn,session_helper::nickname(),"发送了{$money}元的红包.");
-
-            $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
-            return self::outsuccess(array('type_sn' => $ret,'url' => "{$url}"));
-        }
-    }
-
-    public function makeactivityOp()
-    {
-        $param_active = bonus_helper::check_activity($_GET,$ret);
-        if($param_active == false) {
-            return self::outerr($ret['code'],$ret['msg']);
-        }
-
-        $param_personal = bonus_helper::check_personal($_GET,$ret);
-        if($param_personal == false) {
-            return self::outerr($ret['code'],$ret['msg']);
-        }
-
-        $param = array_merge($param_active,$param_personal);
-
-        $pre_helper = new predeposit_helper($_SESSION['member_id']);
-        if(!$pre_helper->is_enough($param['total_amount'])) {
-            return self::outerr(errcode::ErrBonusNotEnough,'余额不够发送红包');
-        }
-
-        $param['sender_id'] = $_SESSION['member_id'];
-        $param['sender_mobile'] = $_SESSION['member_mobile'];
-        $param['sender_name'] = session_helper::nickname();
-
-        $ret = bonus_helper::make_bonus($param);
+        $ret = $pred->make_bonus($type->get_param(),$rate_moneys);
         if($ret === false) {
             return self::outerr(errcode::ErrBonus,"生成红包失败.");
         } else {
             $type_sn = $ret['type_sn'];
-            $money = $ret['money'];
-
-            $pre_helper = new predeposit_helper($_SESSION['member_id']);
-            $pre_helper->handout_bonus($money,$type_sn,session_helper::nickname(),"发送了{$money}元的红包.");
-
             $url = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
             return self::outsuccess(array('type_sn' => $ret,'url' => "{$url}"));
         }
@@ -116,7 +67,8 @@ class member_bonusControl extends mbMemberControl
             return self::outerr(errcode::ErrParamter,"需要红包序列号.");
         }
         $bonus_sn = $_GET['bonus_sn'];
-        $ret = account_helper::share_bonus($_SESSION['member_id'],$bonus_sn,$msg);
+        $pred = new predeposit_helper($_SESSION['member_id']);
+        $ret = $pred->share_bonus($bonus_sn,$msg);
         if($ret === false) {
             return self::outerr(errcode::ErrBonus,empty($msg) ? "生成红包失败." : $msg);
         } else {
@@ -176,7 +128,9 @@ class member_bonusControl extends mbMemberControl
         $mod_type = Model('bonus_type');
 
         $cond = array();
-        $cond['sender_id|relayer_id'] = array('_multi'=>true,$_SESSION['member_id'],$_SESSION['member_id']);
+        //$cond['sender_id|relayer_id'] = array('_multi'=>true,$_SESSION['member_id'],$_SESSION['member_id']);
+        //$cond['sender_id'] = array('_multi'=>true,$_SESSION['member_id'],$_SESSION['member_id']);
+        $cond['sender_id'] = $_SESSION['member_id'];
         $cond['make_type'] = array('in',array(bonus\type::MakeSendType,bonus\type::MakePayType));
 
         $count = $mod_type->getTypeCount($cond);
@@ -195,12 +149,37 @@ class member_bonusControl extends mbMemberControl
         return self::outsuccess(array('type_infos' => $type_infos, 'mobile_page' => mobile_page($pages)));
     }
 
+    private function recv_cond()
+    {
+        static $stQuerys = array('usable','expiring','used','expired');
+        static $day_secs = 24 * 3600;
+
+        $cond = array('user_id' => $_SESSION['member_id'],'bonus_status' => 3);
+
+        $query_state = $_GET['query_state'];
+        if(!empty($query_state) && in_array($query_state,$stQuerys))
+        {
+            if($query_state == 'usable') {
+                $cond['remain_amount'] = array('gt','0.00');
+            } elseif ($query_state == 'expiring') {
+                $cond['usable_time'] = array('elt',time() + 5 * $day_secs);
+            } elseif ($query_state == 'used') {
+                $cond['remain_amount'] = '0.00';
+            } elseif ($query_state == 'expired') {
+                $cond['expired'] = 1;
+            } else {
+            }
+        }
+
+        return $cond;
+    }
+
     //我收到的红包
     public function recv_listOp()
     {
         $mod_bonus = Model('user_bonus');
 
-        $cond = array('user_id' => $_SESSION['member_id'],'bonus_status' => 3);
+        $cond = $this->recv_cond();
         $count = $mod_bonus->getBonusCount($cond);
         if($count == 0) {
             return self::outsuccess(array('bonuses' => array(), 'mobile_page' => mobile_page(0)));
@@ -235,6 +214,7 @@ class member_bonusControl extends mbMemberControl
             $item['expired'] = $user_bonus->expired();
             $item['spend_over'] = $user_bonus->spend_over();
             $item['get_time'] = $user_bonus->get_time();
+            $item['bonus_rate'] = $user_bonus->bonus_rate();
 
             $type = $types[$user_bonus->type_id()];
             $item['sender_name'] = $type->sender_name();
@@ -242,7 +222,8 @@ class member_bonusControl extends mbMemberControl
             $item['show_type'] = self::show_type($type->make_type());
 
             $type_sn = $type->getType_sn();
-            $item['url'] = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
+            $item['url']        = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=open&client_type=wap&type_sn={$type_sn}";
+            $item['detail_url'] = BASE_SITE_URL . "/mobile/index.php?act=bonusex&op=detail&client_type=wap&type_sn={$type_sn}";
 
             array_push($result,$item);
         }

+ 24 - 0
test/bonus_helperTest.php

@@ -86,6 +86,30 @@ class bonus_helperTest extends PHPUnit_Framework_TestCase
         }
     }
 
+    public function testRegex()
+    {
+        $src = '30|1980|165#60|10|20#50|10|5';
+
+        $params = explode('#', urldecode($src));
+        $result = [];
+        foreach ($params as $val)
+        {
+            if(preg_match_all('/^(\d{1,2})\|(\d{1,4})\|(\d{1,6})$/', $val, $match)) {
+                $item['rate'] = intval($match[1]);
+                $item['amount'] = intval($match[2]);
+                $item['num'] = intval($match[3]);
+
+                $result[] = $item;
+            }
+        }
+
+        //$regxp = '/([0-9^|#]*|[0-9^|#]*|[0-9^|#]*)[#]?/i';
+//        $regxp = '/(([0-9]*){3}[#]?/i';
+        $regxp = '/(([0-9]*)|([0-9]*)|([0-9]*)[^#]){1}[#]?/i';
+
+        $val = preg_match_all($regxp,$src,$match);
+    }
+
     public static function tearDownAfterClass()
     {