新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
php怎么實現(xiàn)關注功能
本文操作環(huán)境:windows7系統(tǒng)、php7.1版、DELL G3電腦

你所需要的網(wǎng)站建設服務,我們均能行業(yè)靠前的水平為你提供.標準是產(chǎn)品質(zhì)量的保證,主要從事網(wǎng)站設計制作、成都網(wǎng)站建設、企業(yè)網(wǎng)站建設、成都手機網(wǎng)站制作、網(wǎng)頁設計、成都品牌網(wǎng)站建設、網(wǎng)頁制作、做網(wǎng)站、建網(wǎng)站。創(chuàng)新互聯(lián)公司擁有實力堅強的技術研發(fā)團隊及素養(yǎng)的視覺設計專才。
php怎么實現(xiàn)關注功能?
php + redis 實現(xiàn)關注功能:
應用場景
具體實現(xiàn)
input('type', 'follow'); // 1-關注-follow 2-取消關注-remove
$userId = $request->input('user_id', 0); // 我的用戶ID
$otherId = $request->input('other_id', 0); // 我關注的用戶ID
if ($userId == $otherId) {
return $this->response->apiResponse();
}
$this->testFollowService->follow($type, $userId, $otherId);
return $this->response->apiResponse();
}
/**
* 我的關注/粉絲
* @param Request $request
* @return mixed
*/
public function myFollowAndFans(Request $request)
{
$type = $request->input('type', 'follow'); // 1-關注-follow 2-粉絲-fans
$userId = $request->input('user_id', 0); // 我的用戶ID
$page = $request->input('page', 1); // 頁碼
$limit = $request->input('limit', 10); // 每頁顯示條數(shù)
$res = $this->testFollowService->myFollowAndFans($userId, $type, $page, $limit);
return $this->response->apiResponse($res);
}
}
?>
testFollowRedis->zAddFollow($userId, $otherId);
$this->testFollowRedis->zAddFans($otherId, $userId);
}
// 取消關注
if ($type === 'remove') {
// 先處理 mysql
// TODO mysql 操作
// 然后處理 redis
$this->testFollowRedis->zRemFollow($userId, $otherId);
$this->testFollowRedis->zRemFans($otherId, $userId);
}
}
/**
* 我的關注/粉絲
* @param int $userId 當前登錄用戶的ID
* @param string $type 要獲取的數(shù)據(jù)
* @param int $page 頁碼
* @param int $limit 限制條數(shù)
* @return array
*/
public function myFollowAndFans(int $userId, $type = 'follow', $page = 1, $limit = 10)
{
$start = $limit * ($page - 1);
$end = $start + $limit - 1;
$res = [];
if ($type === 'follow') {
$res = $this->testFollowRedis->zRangeFollow($userId, $start, $end);
}
if ($type === 'fans') {
$res = $this->testFollowRedis->zRangeFans($userId, $start, $end);
}
return $res;
}
}
?>
redis->zAdd(sprintf($this->prefix . $this->followKey, $userId), time(), $otherId);
}
/**
* 取消關注
* @param $userId
* @param $otherId
*/
public function zRemFollow($userId, $otherId)
{
$this->redis->zRem(sprintf($this->prefix . $this->followKey, $userId), $otherId);
}
/**
* 我的關注 | 正序
* @param int $userId
* @param int $start
* @param int $end
* @return array
*/
public function zRangeFollow(int $userId, int $start = 0, int $end = 9)
{
return $this->redis->zRange(sprintf($this->prefix . $this->followKey, $userId), $start, $end);
}
/**
* 我的關注 | 倒序
* @param int $userId
* @param int $start
* @param int $end
* @return array
*/
public function zRevRangeFollow(int $userId, int $start = 0, int $end = 9)
{
return $this->redis->zRevRange(sprintf($this->prefix . $this->followKey, $userId), $start, $end);
}
/**
* 增加粉絲
* @param $userId
* @param $otherId
*/
public function zAddFans($userId, $otherId)
{
$this->redis->zAdd(sprintf($this->prefix . $this->fansKey, $userId), time(), $otherId);
}
/**
* 移除粉絲
* @param $userId
* @param $otherId
*/
public function zRemFans($userId, $otherId)
{
$this->redis->zRem(sprintf($this->prefix . $this->fansKey, $userId), $otherId);
}
/**
* 我的粉絲 | 正序
* @param int $userId
* @param int $start
* @param int $end
* @return array
*/
public function zRangeFans(int $userId, int $start = 0, int $end = 9)
{
return $this->redis->zRange(sprintf($this->prefix . $this->fansKey, $userId), $start, $end);
}
/**
* 我的粉絲 | 倒序
* @param int $userId
* @param int $start
* @param int $end
* @return array
*/
public function zRevRangeFans(int $userId, int $start = 0, int $end = 9)
{
return $this->redis->zRevRange(sprintf($this->prefix . $this->fansKey, $userId), $start, $end);
}
} 文章題目:php怎么實現(xiàn)關注功能
文章位置:http://m.fisionsoft.com.cn/article/cdosdhi.html


咨詢
建站咨詢
