本文操作环境:windows7系统、php7.1版、DELL G3电脑

你所需要的网站建设服务,我们均能行业靠前的水平为你提供.标准是产品质量的保证,主要从事网站设计制作、成都网站建设、企业网站建设、成都手机网站制作、网页设计、成都品牌网站建设、网页制作、做网站、建网站。创新互联公司拥有实力坚强的技术研发团队及素养的视觉设计专才。
php怎么实现关注功能?
php + redis 实现关注功能:
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); // 每页显示条数
$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 要获取的数据
* @param int $page 页码
* @param int $limit 限制条数
* @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);
}
}