stanley-king 8 lat temu
rodzic
commit
3d811d9a01

+ 35 - 0
data/model/member_relation.model.php

@@ -0,0 +1,35 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/6/23
+ * Time: 下午9:58
+ */
+class member_relationModel extends Model
+{
+    public function __construct()
+    {
+        parent::__construct('member_relation');
+    }
+
+    public function findByID($member_id)
+    {
+        return $this->field('*')->where(array('member_id' => $member_id))->find();
+    }
+
+    public function findByMobile($mobile)
+    {
+        return $this->field('*')->where(array('member_mobile' => $mobile))->find();
+    }
+
+    public function replace($data)
+    {
+        $this->insert($data,true);
+    }
+
+    public function replaceAll($datas)
+    {
+        $this->insertAll($datas,array(),true);
+    }
+}

+ 31 - 0
helper/algorithm_helper.php

@@ -0,0 +1,31 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/6/24
+ * Time: 上午1:18
+ */
+class algorithm_helper
+{
+    static public function bsearch($needle, $haystack)
+    {
+        $high = count($haystack);
+        $low = 0;
+
+        while ($high - $low > 1){
+            $probe = ($high + $low) / 2;
+            if ($haystack[$probe] < $needle){
+                $low = $probe;
+            }else{
+                $high = $probe;
+            }
+        }
+
+        if ($high == count($haystack) || $haystack[$high] != $needle) {
+            return false;
+        }else {
+            return $high;
+        }
+    }
+}

+ 4 - 2
helper/bonus/manager.php

@@ -177,9 +177,11 @@ class manager
 
     public function topup($mod_user_bonus,&$bonus)
     {
+        $day_secs = 24 * 3600;
         $datas = array('bonus_status' => 3,
             'user_id' => $_SESSION['member_id'],
-            'user_name' => user_helper::nickname());
+            'user_name' => user_helper::nickname(),
+            'usable_time' => time() + $day_secs);
 
         $ret = $mod_user_bonus->where(array('bonus_id' => $bonus['bonus_id']))->update($datas);
         $affect_rows = $mod_user_bonus->affected_rows();
@@ -246,7 +248,7 @@ class manager
 
         return true;
     }
-    
+
     public function shake($bonus_sn,$strength,$direction)
     {
         $bonus_info = self::read_session('',$bonus_sn);

+ 6 - 0
helper/bonus/user_bonus.php

@@ -27,6 +27,7 @@ class user_bonus
     const GrabStatus = 1;
     const BindStatus = 2;
     const TopupStatus = 3;
+    const ExpiredStatus = 4;
 
     private $mParamer;
 
@@ -65,6 +66,11 @@ class user_bonus
         $status = intval($this->mParamer['bonus_status']);
         return ($status == self::GrabStatus || $status == self::BindStatus);
     }
+    public function expired() {
+        $status = intval($this->mParamer['bonus_status']);
+        return ($status == self::ExpiredStatus);
+    }
+
     public function remain_amount() {
         $val = intval(floatval($this->mParamer['remain_amount']) * 100 + 0.5);
         return floatval($val) / 100;

+ 265 - 0
helper/relation/mem_relation.php

@@ -0,0 +1,265 @@
+<?php
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/6/23
+ * Time: 下午10:28
+ */
+
+namespace relation;
+
+use \Exception;
+use \user_helper;
+use \algorithm_helper;
+
+class mem_relation
+{
+    //member_mobile VARCHAR(11) PRIMARY KEY NOT NULL COMMENT '用户手机号',
+    //member_id INT(11) DEFAULT '0' COMMENT '已经注册用户id',
+    //contact_name VARCHAR(50) DEFAULT '',
+    //subscriber TEXT COMMENT '订阅我为好友的人。',
+    //follower TEXT COMMENT '我订阅别人成为好友通过的人。',
+    //build_mobiles TEXT COMMENT '已经与我建立好友关系的手机号',
+    //unbuild_mobiles TEXT COMMENT '还未与我建立好友关系的手机号'
+
+    private $member_id;
+    private $member_mobile;
+    private $contact_name;
+    private $subscriber;
+    private $new_subscriber;
+    private $follower;
+    private $build_mobiles;
+    private $unbuild_mobiles;
+
+    private $mod_relation;
+    private $dirty;
+
+    public function __construct($member_id,$mobile,$name)
+    {
+        $this->dirty = false;
+        $this->member_id = 0;
+
+        if(isset($name) && !empty($name)) {
+            $this->contact_name = $name;
+        } else {
+            $this->contact_name = '';
+        }
+
+        $this->mod_relation = Model('member_relation');
+
+        if(isset($member_id) && intval($member_id) > 0)
+        {
+            $info = $this->mod_relation->findByID($member_id);
+            if(!empty($info)) {
+                $this->init($info);
+                return;
+            } else {
+                $this->member_id = $member_id;
+            }
+        }
+
+        if(isset($mobile) && !empty($mobile))
+        {
+            $info = $this->mod_relation->findByMobile($mobile);
+            if(!empty($info)) {
+                $this->init($info);
+                return;
+            }
+            else
+            {
+                $mobile = user_helper::mobile_valid($mobile);
+                if($mobile == false) {
+                    throw new Exception();
+                } else {
+                    $this->member_mobile = $mobile;
+                }
+            }
+        }
+        else {
+            throw new Exception();
+        }
+
+        $this->dirty = true;
+        $this->subscriber = array();
+        $this->new_subscriber = array();
+        $this->follower = array();
+        $this->build_mobiles = array();
+        $this->unbuild_mobiles = array();
+    }
+
+    private function subscrib_me($member_id)
+    {
+        $ret = algorithm_helper::bsearch($member_id,$this->subscriber);
+        if($ret == false) {
+            array_push($this->subscriber,$member_id);
+            sort($this->subscriber);
+            $this->dirty = true;
+        }
+
+        $ret = algorithm_helper::bsearch($member_id,$this->new_subscriber);
+        if($ret == false) {
+            array_push($this->new_subscriber,$member_id);
+            sort($this->new_subscriber);
+            $this->dirty = true;
+        }
+    }
+
+    private function doSubscribe($mobile, $name)
+    {
+        $someone = new mem_relation('', $mobile, $name);
+        $someone->subscrib_me($this->member_id);
+        $someone->save();
+        return $someone->member_id;
+    }
+
+    public function subscribe($mobile,$name)
+    {
+        $someone_id = $this->doSubscribe($mobile, $name);
+        if($someone_id > 0) {
+            $this->add_fllower($someone_id);
+            $this->remove_unbuild($mobile);
+            $this->add_build($mobile);
+        } else {
+            $this->add_unbuild($mobile);
+        }
+        $this->save();
+    }
+
+    public function pass_subscrib()
+    {
+
+    }
+    private function subscribed($mobile)
+    {
+        if(algorithm_helper::bsearch($mobile,$this->build_mobiles) != false) {
+            return true;
+        }
+        if(algorithm_helper::bsearch($mobile,$this->unbuild_mobiles) != false) {
+            return true;
+        }
+        return false;
+    }
+
+    public function add_fllower($someone_id)
+    {
+        if(algorithm_helper::bsearch($someone_id,$this->follower) != false) {
+            array_push($this->follower,$someone_id);
+            sort($this->follower);
+            $this->dirty = true;
+        }
+    }
+
+    private function add_unbuild($mobile)
+    {
+        $ret = algorithm_helper::bsearch($mobile,$this->unbuild_mobiles);
+        if($ret == false) {
+            array_push($this->unbuild_mobiles,$mobile);
+            sort($this->unbuild_mobiles);
+            $this->dirty = true;
+        }
+    }
+
+    private function remove_unbuild($mobile)
+    {
+        $ret = algorithm_helper::bsearch($mobile,$this->unbuild_mobiles);
+        if($ret != false) {
+            unset($this->unbuild_mobiles[$ret]);
+            $this->dirty = true;
+        }
+    }
+
+    private function add_build($mobile)
+    {
+        $ret = algorithm_helper::bsearch($mobile,$this->build_mobiles);
+        if($ret == false) {
+            array_push($this->build_mobiles,$mobile);
+            sort($this->build_mobiles);
+            $this->dirty = true;
+        }
+    }
+
+    public function handle_contacts($contacts)
+    {
+        foreach ($contacts as $mobile => $name)
+        {
+            if($this->subscribed($mobile)) {
+                continue;
+            }
+
+            $someone_id = $this->doSubscribe($mobile,$name);
+            if($someone_id > 0) {
+                $this->add_fllower($someone_id);
+                $this->remove_unbuild($mobile);
+                $this->add_build($mobile);
+            } else {
+                $this->add_unbuild($mobile);
+            }
+        }
+        $this->save();
+    }
+
+    private function save()
+    {
+        if($this->dirty) {
+            $data = array();
+            $data['member_mobile'] = $this->member_mobile;
+            $data['member_id'] = $this->member_id;
+            $data['contact_name'] = $this->contact_name;
+
+            $data['subscriber'] = serialize($this->subscriber);
+            $data['new_subscriber '] = serialize($this->new_subscriber);
+            $data['follower'] = serialize($this->follower);
+            $data['build_mobiles'] = serialize($this->build_mobiles);
+            $data['unbuild_mobiles'] = serialize($this->unbuild_mobiles);
+            $this->mod_relation->replace($data);
+        }
+    }
+
+    private function init($info)
+    {
+        $this->member_id = $info['member_id'];
+        $this->member_mobile = $info['member_mobile'];
+        $this->contact_name = $info['contact_name'];
+        $subscriber = $info['subscriber'];
+        $new_subscriber = $info['new_subscriber'];
+        $follower = $info['follower'];
+        $build_mobiles = $info['build_mobiles'];
+        $unbuild_mobiles = $info['unbuild_mobiles'];
+
+        if(is_null($subscriber) || empty($subscriber)) {
+            $this->subscriber = array();
+        } else {
+            $this->subscriber = unserialize($subscriber);
+        }
+
+        if(is_null($new_subscriber) || empty($new_subscriber)) {
+            $this->new_subscriber = array();
+        } else {
+            $this->new_subscriber = unserialize($new_subscriber);
+        }
+
+        if(is_null($follower) || empty($follower)) {
+            $this->follower = array();
+        } else {
+            $this->follower = unserialize($follower);
+        }
+
+        if(is_null($build_mobiles) || empty($build_mobiles)) {
+            $this->build_mobiles = array();
+        } else {
+            $this->build_mobiles = unserialize($build_mobiles);
+        }
+
+        if(is_null($unbuild_mobiles) || empty($unbuild_mobiles)) {
+            $this->unbuild_mobiles = array();
+        } else {
+            $this->unbuild_mobiles = unserialize($unbuild_mobiles);
+        }
+
+        sort($this->subscriber);
+        sort($this->new_subscriber);
+        sort($this->follower);
+        sort($this->build_mobiles);
+        sort($this->unbuild_mobiles);
+    }
+}

+ 10 - 2
helper/relation_helper.php

@@ -6,13 +6,21 @@
  * Date: 16/6/23
  * Time: 下午4:26
  */
+
+require_once (BASE_ROOT_PATH . '/helper/relation/mem_relation.php');
+require_once (BASE_ROOT_PATH . '/helper/algorithm_helper.php');
+
 class relation_helper
 {
+    //
     static public function onRegister($member_id) {
 
     }
 
-    static public function onUpContacts($member_id) {
-        QueueClient::push('generateRelations',array('member_id'=>$member_id));
+    static public function onUpContacts($member_id,$contacts) { //我准备关注的人
+        //QueueClient::push('generateRelations',array('member_id'=>$member_id));
+        //$relation = new relation\mem_relation($member_id,$_SESSION['member_mobile'],'江海苗');
+        $relation = new relation\mem_relation($member_id,'13911129867','江海苗');
+        $relation->handle_contacts($contacts);
     }
 }

+ 1 - 1
helper/user_helper.php

@@ -14,7 +14,7 @@ class user_helper
 
     static public function mobile_valid($mobile)
     {
-        if(isset($mobile) || empty($mobile) || strlen($mobile) < self::mobile_len) {
+        if(!isset($mobile) || empty($mobile) || strlen($mobile) < self::mobile_len) {
             return false;
         }
 

+ 1 - 6
mobile/control/member_info.php

@@ -117,13 +117,8 @@ class member_infoControl extends mbMemberControl
 
             $ar_contact[$mobile] = $name;
         }
-
-        $ret = Model('mobile_contacts')->add($_SESSION['member_id'], $ar_contact);
-        if ($ret == false) {
-            return self::outerr(errcode::ErrDB);
-        }
         
-        relation_helper::onUpContacts($_SESSION['member_id']);
+        relation_helper::onUpContacts($_SESSION['member_id'],$ar_contact);
         return self::outsuccess(NULL);
     }
 

+ 25 - 0
test/algorithm_helperTest.php

@@ -0,0 +1,25 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/6/24
+ * Time: 上午1:20
+ */
+
+define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
+
+require_once(BASE_ROOT_PATH . '/helper/algorithm_helper.php');
+
+class algorithm_helperTest extends PHPUnit_Framework_TestCase
+{
+    public function testSearch()
+    {
+        $mobiles = array('13911129897','13951129867','13916129867','13918129867','13911179867','13911109867');
+        sort($mobiles);
+        $ret = algorithm_helper::bsearch('13911109867',$mobiles);
+        
+        $ret = $mobiles[$ret];
+        $x = $mobiles[0];
+    }
+}

+ 33 - 0
test/relation_helperTest.php

@@ -0,0 +1,33 @@
+<?php
+
+/**
+ * Created by PhpStorm.
+ * User: stanley-king
+ * Date: 16/6/23
+ * Time: 下午10:21
+ */
+
+define('BASE_ROOT_PATH',str_replace('/test','',dirname(__FILE__)));
+
+require_once(BASE_ROOT_PATH . '/fooder.php');
+require_once(BASE_ROOT_PATH . '/helper/relation_helper.php');
+
+class relation_helperTest extends PHPUnit_Framework_TestCase
+{
+    public static function setUpBeforeClass()
+    {
+        Base::run_util();
+    }
+
+    public function testUpContacts()
+    {
+        //36490
+        $contacts = array();
+        $contacts['13911129868'] = 'a';
+        $contacts['13911129869'] = 'b';
+        $contacts['13911129870'] = 'c';
+        $contacts['13911129871'] = 'd';
+
+        relation_helper::onUpContacts(36490,$contacts);
+    }
+}