目录
产品价值
应用场景
具体实现
首页 后端开发 PHP问题 php怎么实现关注功能

php怎么实现关注功能

Nov 19, 2021 am 09:34 AM
php

php实现关注功能的方法:1、创建控制层实现代码“namespace App\Controller\Test...”;2、设计服务层实现代码“namespace App\Service\Ptg...”;3、设置好仓储层代码即可。

php怎么实现关注功能

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

php怎么实现关注功能?

php + redis 实现关注功能:

产品价值

1: 关注功能
2: 功能分析之“关注”功能
3: 平平无奇的「关注」功能,背后有4点重大价值

应用场景

在做PC或者APP端时,掺杂点社交概念就有关注和粉丝功能;
数据量小的话数据库还能支持,如果数据量很庞大还是用缓存比较好。

具体实现

1 控制层实现

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

<?php

 

namespace App\Controller\Test;

 

use App\Controller\AbstractController;

use App\Service\Ptg\TestFollowService;

use Hyperf\Di\Annotation\Inject;

use Hyperf\HttpServer\Annotation\Controller;

use Hyperf\HttpServer\Annotation\Middleware;

use Hyperf\HttpServer\Annotation\RequestMapping;

 

/**

 * 测试 - 关注

 * Class TestFollowController

 * @package App\Controller

 * @Controller(prefix="test")

 */

class TestFollowController extends AbstractController

{

    /**

     * 服务层 - 关注

     * @Inject()

     * @var TestFollowService

     */

    protected $testFollowService;

 

    /**

     * 关注/取消关注

     * @param Request $request

     * @return mixed

     */

    public function follow(Request $request)

    {

        $type = $request->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);

    }

}

?>

登录后复制

2 服务层实现

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

<?php

 

namespace App\Service\Ptg;

 

use App\Repository\Redis\TestFollowRedis;

use App\Service\AbstractService;

use Hyperf\Di\Annotation\Inject;

 

class TestFollowService extends AbstractService

{

    /**

     * 仓储层 - 关注

     * @Inject()

     * @var TestFollowRedis

     */

    protected $testFollowRedis;

 

    /**

     * 关注/取消关注

     * @param string $type

     * @param int $userId

     * @param int $otherId

     * @return mixed

     */

    public function follow($type = 'follow', int $userId, int $otherId)

    {

        // 关注

        if ($type === 'follow') {

            // 先处理 mysql

            // TODO mysql 操作

            // 然后处理 redis

            $this->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;

    }

}

?>

登录后复制

仓储层实现【推荐:PHP视频教程

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

<?php

 

namespace App\Repository\Redis;

 

class TestFollowRedis extends AbstractRedis

{

    /**

     * 关注key

     * @var string

     */

    private $followKey = '%u:follow';

 

    /**

     * 粉丝key

     * @var string

     */

    private $fansKey = '%u:fans';

 

    /**

     * 前缀

     */

    public function initPrefix()

    {

        return 'follow:';

    }

 

    /**

     * 增加关注

     * @param $userId

     * @param $otherId

     */

    public function zAddFollow($userId, $otherId)

    {

        $this->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怎么实现关注功能的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门文章

两个点博物馆:邦格荒地地点指南
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章

两个点博物馆:邦格荒地地点指南
4 周前 By 尊渡假赌尊渡假赌尊渡假赌
仓库:如何复兴队友
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒险:如何获得巨型种子
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热门文章标签

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南 Dec 24, 2024 pm 04:42 PM

适用于 Ubuntu 和 Debian 的 PHP 8.4 安装和升级指南

CakePHP 日期和时间 CakePHP 日期和时间 Sep 10, 2024 pm 05:27 PM

CakePHP 日期和时间

CakePHP 文件上传 CakePHP 文件上传 Sep 10, 2024 pm 05:27 PM

CakePHP 文件上传

CakePHP 路由 CakePHP 路由 Sep 10, 2024 pm 05:25 PM

CakePHP 路由

讨论 CakePHP 讨论 CakePHP Sep 10, 2024 pm 05:28 PM

讨论 CakePHP

CakePHP 快速指南 CakePHP 快速指南 Sep 10, 2024 pm 05:27 PM

CakePHP 快速指南

CakePHP 项目配置 CakePHP 项目配置 Sep 10, 2024 pm 05:25 PM

CakePHP 项目配置

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 如何设置 Visual Studio Code (VS Code) 进行 PHP 开发 Dec 20, 2024 am 11:31 AM

如何设置 Visual Studio Code (VS Code) 进行 PHP 开发

See all articles