您好,登錄后才能下訂單哦!
php怎么實(shí)現(xiàn)關(guān)注功能,很多新手對(duì)此不是很清楚,為了幫助大家解決這個(gè)難題,下面小編將為大家詳細(xì)講解,有這方面需求的人可以來學(xué)習(xí)下,希望你能有所收獲。
php實(shí)現(xiàn)關(guān)注功能的方法:1、創(chuàng)建控制層實(shí)現(xiàn)代碼“namespace App\Controller\Test...”;2、設(shè)計(jì)服務(wù)層實(shí)現(xiàn)代碼“namespace App\Service\Ptg...”;3、設(shè)置好倉儲(chǔ)層代碼即可。
本文操作環(huán)境:windows7系統(tǒng)、PHP7.1版、DELL G3電腦
php怎么實(shí)現(xiàn)關(guān)注功能?
php + redis 實(shí)現(xiàn)關(guān)注功能:
1: 關(guān)注功能
2: 功能分析之“關(guān)注”功能
3: 平平無奇的「關(guān)注」功能,背后有4點(diǎn)重大價(jià)值
在做PC或者APP端時(shí),摻雜點(diǎn)社交概念就有關(guān)注和粉絲功能;
數(shù)據(jù)量小的話數(shù)據(jù)庫還能支持,如果數(shù)據(jù)量很龐大還是用緩存比較好。
1 控制層實(shí)現(xiàn)
<?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; /** * 測(cè)試 - 關(guān)注 * Class TestFollowController * @package App\Controller * @Controller(prefix="test") */ class TestFollowController extends AbstractController { /** * 服務(wù)層 - 關(guān)注 * @Inject() * @var TestFollowService */ protected $testFollowService; /** * 關(guān)注/取消關(guān)注 * @param Request $request * @return mixed */ public function follow(Request $request) { $type = $request->input('type', 'follow'); // 1-關(guān)注-follow 2-取消關(guān)注-remove $userId = $request->input('user_id', 0); // 我的用戶ID $otherId = $request->input('other_id', 0); // 我關(guān)注的用戶ID if ($userId == $otherId) { return $this->response->apiResponse(); } $this->testFollowService->follow($type, $userId, $otherId); return $this->response->apiResponse(); } /** * 我的關(guān)注/粉絲 * @param Request $request * @return mixed */ public function myFollowAndFans(Request $request) { $type = $request->input('type', 'follow'); // 1-關(guān)注-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); } } ?>
2 服務(wù)層實(shí)現(xiàn)
<?php namespace App\Service\Ptg; use App\Repository\Redis\TestFollowRedis; use App\Service\AbstractService; use Hyperf\Di\Annotation\Inject; class TestFollowService extends AbstractService { /** * 倉儲(chǔ)層 - 關(guān)注 * @Inject() * @var TestFollowRedis */ protected $testFollowRedis; /** * 關(guān)注/取消關(guān)注 * @param string $type * @param int $userId * @param int $otherId * @return mixed */ public function follow($type = 'follow', int $userId, int $otherId) { // 關(guān)注 if ($type === 'follow') { // 先處理 mysql // TODO mysql 操作 // 然后處理 redis $this->testFollowRedis->zAddFollow($userId, $otherId); $this->testFollowRedis->zAddFans($otherId, $userId); } // 取消關(guān)注 if ($type === 'remove') { // 先處理 mysql // TODO mysql 操作 // 然后處理 redis $this->testFollowRedis->zRemFollow($userId, $otherId); $this->testFollowRedis->zRemFans($otherId, $userId); } } /** * 我的關(guān)注/粉絲 * @param int $userId 當(dāng)前登錄用戶的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; } } ?>
倉儲(chǔ)層實(shí)現(xiàn)【推薦:PHP視頻教程】
<?php namespace App\Repository\Redis; class TestFollowRedis extends AbstractRedis { /** * 關(guān)注key * @var string */ private $followKey = '%u:follow'; /** * 粉絲key * @var string */ private $fansKey = '%u:fans'; /** * 前綴 */ public function initPrefix() { return 'follow:'; } /** * 增加關(guān)注 * @param $userId * @param $otherId */ public function zAddFollow($userId, $otherId) { $this->redis->zAdd(sprintf($this->prefix . $this->followKey, $userId), time(), $otherId); } /** * 取消關(guān)注 * @param $userId * @param $otherId */ public function zRemFollow($userId, $otherId) { $this->redis->zRem(sprintf($this->prefix . $this->followKey, $userId), $otherId); } /** * 我的關(guān)注 | 正序 * @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); } /** * 我的關(guān)注 | 倒序 * @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); } }
看完上述內(nèi)容是否對(duì)您有幫助呢?如果還想對(duì)相關(guān)知識(shí)有進(jìn)一步的了解或閱讀更多相關(guān)文章,請(qǐng)關(guān)注億速云行業(yè)資訊頻道,感謝您對(duì)億速云的支持。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。