好友体系

PW个人中心允许用户与用户进行互动,主要有添加好友,添加关注。可以通过邀请好友注册功能来扩大你的社区影响力,通过邀请链接注册的用户即自动加为好友。并可对好友进送生日祝福等。

好友的程序与目录如下:

Friend.php是好友的入口文件,负责分发请求。如果是自己登陆访问则请求到u/require/friend/my.php文件,如果是浏览其他用户信息则请求到u/require/friend/viewphp文件:

而模板在u/thems/default/  friend_开头的几个文件。

数据库:

       朋友类型与关系记录表主要为 pw_friendtypepw_friends,而好友数量粉丝数,及关注,记录在pw_memberdata表中,对用的字段为f_num,fans,follows, newfans是新加的粉丝数.

 

代码分析

.我的关注

 

if ($type == 'attention') {

//加载好友service lib/friend/attention.class.php

    $attentionSerivce = L::loadClass('Attention', 'friend'); /* @var $attentionSerivce PW_Attention */

    $updateSQL = array();

    $count = $winddb['follows'];

    //当关注的人数小于1000或多于10000000时从新查找数据库

    if ($count < 1000 ||  $count > 10000000) {

       $num = $attentionSerivce->countFollows($winduid);

       if ($num != $count) {

           $updateSQL['follows'] = $winddb['follows'] = $count = $num;

       }

    }

    $page > ceil($count/$db_perpage) && $page = ceil($count/$db_perpage);

//得到关注列表

    $attentions = $count ? $attentionSerivce->getFollowListInPage($winduid, $page, $db_perpage) : array();

    $attentionedUids = $myAttentionUids = array();

    foreach ($attentions as $attention) {

       $attentionedUids[] = $attention['uid'];

    }

    //得到我同时关注我的列表

    $myAttentionUids = $attentionSerivce->getUidsInFansListByFriendids($winduid, $attentionedUids);

    foreach ($attentions as $key => $attention) {

       $attentions[$key]['attention'] = 1;

       if (!in_array($attention['uid'], $myAttentionUids)) continue;

       $attentions[$key]['attentionEach'] = 1;

    }

    //更新关注人员信息

    if ($updateSQL) {

       $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */

       $userService->update($winduid, array(), $updateSQL);

    }

    $pages = numofpage($count,$page,ceil($count/$db_perpage),"{$thisbase}&");

 

    //所有好友

    $friendsAllUids = getFriendsIdArr($winduid);

 

二.我的粉丝

 

} elseif ($type == 'attentioned') {

//加载lib/friend/attention.class.php

    $attentionSerivce = L::loadClass('Attention', 'friend'); /* @var $attentionSerivce PW_Attention */

    $updateSQL = array();

    $count = $winddb['fans'];

    //当粉丝的人数小于1000或多于10000000时从新查找数据库

    if ($count < 1000 ||  $count > 10000000) {

//得到粉丝数量

       $num = $attentionSerivce->countFans($winduid);

       //如果pw_memberdata记录的粉丝数量不等于粉丝数,则更新数据库

       if ($num != $count) {

           $updateSQL['fans'] = $winddb['fans'] = $count = $num;

       }

    }

    if ($winddb['newfans'] > 0) {

       $updateSQL['newfans'] = $winddb['newfans'] = 0;

    }

    $page > ceil($count/$db_perpage) && $page = ceil($count/$db_perpage);

//得到粉丝列表

    $attentions = $count ? $attentionSerivce->getFansListInPage($winduid, $page, $db_perpage) : array();

    $attentionedUids = $myAttentionUids = array();

    foreach ($attentions as $attention) {

       $attentionedUids[] = $attention['uid'];

    }

    //自己的粉丝中找出相互关注的人

    $myAttentionUids = $attentionSerivce->getUidsInFollowListByFriendids($winduid, $attentionedUids);

    foreach ($attentions as $key => $attention) {

       $attentions[$key]['attention'] = 0;

       if (!in_array($attention['uid'], $myAttentionUids)) continue;

       $attentions[$key]['attentionEach'] = 1;

       $attentions[$key]['attention'] = 1;

    }

    //更新pw_mamberdata表粉丝数

    if ($updateSQL) {

       $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */

       $userService->update($winduid, array(), $updateSQL);

    }

    $pages = numofpage($count,$page,ceil($count/$db_perpage),"{$thisbase}&");

 

    //所有好友

    $friendsAllUids = getFriendsIdArr($winduid);

 

三.我的好友

 

} elseif ($type == 'my') {

 

    $page = (int) S::getGP('page');

    $ftid = (int)S::getGP('ftid');

    $page < 1 && $page = 1;

    $db_perpage = 12;

    $ftype = $ftid == '-1' ? 0 : (is_numeric($ftid) && $ftid > 0 ? $ftid : null);

    $friendType  = array();

    $count = $friendsNums = $defaultTypeFriendNum = 0;

    $friendsService = L::loadClass('Friend', 'friend'); /* @var $friendsService PW_Friend */

    list($friendsNums, $friendType, $defaultTypeFriendNum) = $friendsService->getFriendsTypeAndNum($winduid);

    //如果数据库记录的好友数如果和pw_memberdata表不一样则更新pw_memberdata表(因为可能缓存等原因使pw_memberdata没同步)

    if ($friendsNums != $winddb['f_num']) {

       $userService = L::loadClass('UserService', 'user');

       $userService->update($winduid, array(), array('f_num' => $friendsNums));

    }

    //得到分组好友个数

    $count = (int)$friendsService->countUserFriends($u,$ftype);

    $page > ceil($count/$db_perpage) && $page = ceil($count/$db_perpage);

//分组好友分页

    $friends = $count ? $friendsService->findUserFriendsInPage($u, $page, $db_perpage, $ftype) : array();

 

    $uids = array_keys($friends);

    $attentionSerivce = L::loadClass('attention', 'friend'); /* @var $attentionSerivce PW_Attention */

//得到好友是否关注自己

    $myFansUids = $attentionSerivce->getUidsInFansListByFriendids($winduid, $uids);

    foreach ($friends as $key => $friend) {

       $attentions[$key]['attentionEach'] = 0;

       if ($friend['attention'] && in_array($friend['uid'], $myFansUids)) {

           $friends[$key]['attentionEach'] = 1;

       }

    }

    //得到所有好友id

    $friendsAllUids = getFriendsIdArr($winduid);

    $pages = numofpage($count,$page,ceil($count/$db_perpage),"{$thisbase}ftid=$ftype&");

 

四.好友查找

 

} elseif ($type == 'find') {

 

    S::gp(array('step', 'according'));

    $db_perpage = 12;

    $according = $according ? $according : 'user';

    ${$according.'checked'} = 'selected="selected"';

//所有好友

    $friendsAllUids = getFriendsIdArr($winduid);

 

    if ($step == 2) {

       S::gp(array('f_keyword', 'decode'));

       !isset($f_keyword) && Showmsg('pse_input_keyword');

      

       //关键词转义 require/common.php pwConvert

       $decode && $f_keyword = pwConvert($f_keyword, $db_charset, 'utf8');

 

       if($according && !in_array($according,array('user','uid','email','tags'))){

           showMsg("抱歉,搜索类型不存在");

       }

 

       $f_keyword = strip_tags($f_keyword);

       $count = 0;

       $members = $myAttentionUids = array();

       $searchURL = "u.php?a=friend&type=find";

      

       //根据类型搜索,有用户名,UID,电子邮件,标签,4种方式

       switch($according){

           case "user" :

              $searcherService = L::loadclass('searcher', 'search'); /* @var $searcherService PW_searcher */

              $uids = $memberdata = $attentionData = array();

              //根据关键词搜索用户返回数量及,用户信息 lib/search/searcher.class.php searchUsers函数

              //* 值得注意的是,搜索提供了sphinxmysql搜索,默认如果sphinx未开启的话,会使用mysql搜索

              //具体也就是$searcherService->searchUsers()最终调用的是 lib/search/search/mysql.search.php下面

//searchUsers()函数

              list($count,$users) = $searcherService->searchUsers($f_keyword,$page,$db_perpage);

              $users = $users ? $users : array();

              foreach ($users as $user) {

                  $uids[] = $user['uid'];

              }

              if ($uids) {

                  $userService = L::loadClass('UserService', 'user'); /* @var $userService PW_UserService */

                  //得到用户的基本信息包括pw_memberpw_memberdata

                  foreach ($userService->getUsersWithMemberDataByUserIds($uids) as $rt) {

                     $memberdata[$rt['uid']] = $rt['thisvisit'];

                  }

 

                  $attentionSerivce = L::loadClass('attention', 'friend'); /* @var $attentionSerivce PW_Attention */

//得到自己关注的信息

                  $myAttentionsInfo = $attentionSerivce->getFollowListByFriendids($winduid, $uids);

                  foreach ($myAttentionsInfo as $myAttentions) {

                     $myAttentionUids[] = $myAttentions['friendid'];

                  }

                  foreach($users as $key => $user) {

                     $user['thisvisit'] = $memberdata[$user['uid']];

                     list($user['face']) = showfacedesign($user['icon'], '1', 's');

                     in_array($user['uid'], $myAttentionUids) && $user['attention'] = 1;

                     $members[] = $user;

                  }

              }

              $members && $pages = ($count) ? numofpage($count,$page,ceil($count/$db_perpage),$searchURL."&f_keyword=".urlencode($f_keyword)."&step=2&",null,'',true) : '';

              break;

           //其他类型搜索雷同不再多过多讲解

 

前面已经说了好友的三大功能另外还有邀请朋友,好友生日列表,不在做代码解释。

我这边说说邀请的流程:

1)  在后台开启邀请注册才可以使用邀请功能。

2)  后台设定邀请码可以使用的用户组,及购买的价格,可在:全局>注册设置>邀请注册设置,设置完后生成邀请码

3)  普通用户登录以后,需要根据设置的购买价格购买邀请码,然后就可以看到邀请链接了,通过类似: http://localhost/pw87/register.php?invcode=552391d69c9fa54c的链接注册用户后,其用户即成为你的好友。