âåè¨
    ç°å¦ä»ï¼ä¸ç®¡æ¯åªç§ç±»åçåºç¨ï¼è¯è®ºåºé½å°ä¸äºãä»å·¥å ·ç±»çå°åªä½ä¿¡æ¯æµç±»çï¼è¯è®ºçè¨é½æ¯æåºæ¬çäºå¨ç¯èãæ¯å¦æé³çè§é¢ä¸ï¼é对è§é¢æ¯ä¸ªç¨æ·é½å¯ä»¥å表èªå·±çè§ç¹ï¼èé对ç¨æ·çè¯è®ºï¼å ¶ä»çç¨æ·åå¯ä»¥å¯¹å ¶è¿è¡è¯è®ºï¼ä¾æ¬¡åå¤ä¸å»ã
    é£ä¹ï¼ä¸ä¸ªè§é¢çè¯è®ºåå¤ç表å¦ä½è®¾è®¡ï¼åè½å¦ä½å®ç°å¢ï¼å¦æ é¢ï¼è¿éæ¯ç¨ä¸å¼ 表å®æï¼ä½æ¯å¨æå®æåè½ååç°ææä¸¤ä¸ªå¼ æ´åé(è¯è®ºè¡¨ååå¤è¡¨)ï¼è¿ä¸ªåé¢å·²ç»æ¹äºï¼æåä¹ä¼è¯´ä¸ä¸ã
ææ
    页é¢ä¸å±ç¤ºæ¯ï¼è§é¢ä¸å页å±ç¤ºç¬¬ä¸çº§çè¯è®ºå表ï¼è¯è®ºä¸çè¯è®ºæ¯è¿è¡æå ï¼ç¹å»âæ¥çå ¨é¨âå页æ¾ç¤ºææå±çº§çè¯è®ºãä¸å¼ 表ç设计ä¸ï¼è¯è®ºä¸çè¯è®ºä¸â¦â¦çè¯è®ºæ¯éè¿å ³èä¸ä¸çº§ä¸»é®çï¼ä¹å°±æ¯éå½çæ¹å¼ãä½æ¯ä¸é¢çæ¯åªè¦æ¯å¯¹è¯è®ºè¿è¡è¯è®ºé½æ¾å¨ç¬¬ä¸çº§çè¯è®ºä¸ï¼èéå½æ¯æ å½¢ç»æãè½ç¶ä¹è½éè¿å¯¹æ å½¢ç»ææ°æ®å¤çæ伸å°äºå£ä¸ï¼ä½æ¯å¨åºä¸è¯è®ºç¹å«å¤çæ åµå°±ä¼ä½éªç¹å«å·®ãæ以é对è¿ä¸ªæå¯¹è¡¨æ ¼å äºä¸ä¸ªé¢å å±å段ï¼è¡¨ç»æå¦ä¸ï¼ç¶ååå次å®æè¯è®ºæ°æ®çå¦ä½æ·»å ï¼æ页é¢æ¹å¼æ¥è¯¢çã
表ç»æ
CREATE TABLE `short_video_comment` ( `id` int(11) NOT NULL AUTO_INCREMENT, `pid` int(11) DEFAULT '0' COMMENT 'ç¶çº§ID', `crumbs` json DEFAULT NULL COMMENT 'é¢å å±', `video_id` int(11) DEFAULT '0', `user_id` int(11) DEFAULT '0', `commented_user_id` int(11) DEFAULT '0' COMMENT '被è¯è®ºè ç¨æ·ID', `is_pubisher` tinyint(1) DEFAULT '0' COMMENT 'æ¯å¦ä½è ', `content` varchar(255) DEFAULT '' COMMENT 'è¯è®º', `state` int(1) DEFAULT '1' COMMENT '1. æ¾ç¤º 0. éè', `like_count` int(11) DEFAULT '0' COMMENT 'ç¹èµæ°', `create_time` int(11) DEFAULT '0' COMMENT 'å建æ¶é´', `delete_time` int(11) DEFAULT '0' COMMENT 'å é¤æ¶é´', `update_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT 'æ´æ°æ¶é´', PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=282 DEFAULT CHARSET=utf8mb4 COMMENT='ç¨æ·è¯è®ºè®°å½è¡¨';
        å¦ä¸å¾ç âcrumbsâ å段æ¯ä¸ä¸ªæ°ç»æ¹å¼çjsonï¼ä¹å°±æ¯æ·»å æ¶ä¼è®°å½å½åè¯è®ºçå ³ç³»é¾ï¼ä»æ顶å±IDå°æè¿çä¸ä¸çº§ãèå®ç°å页æ¾ç¤ºç¬¬äºçº§ä¸çææè¯è®ºï¼åªéè¦æ âcrumbsâ ç´¢å¼ä¸º1çpidä½ä¸ºæ¥è¯¢æ¡ä»¶å°±è½æ¾å°è¯¥idä¸çææè¯è®ºï¼å¦ä¸æ¯æ¥è¯¢äºçº§è¯è®ºID为285çææè¯è®ºå表ã
    è对äºå ³äºcrumbså¦ä½åå ¥åè°åå¤è°ï¼å°±åªéè¦å¨è¯è®ºæ·»å çæ¶åï¼å¯¹ä¸ä¸çº§è¯è®ºIDè¿è¡éå½æ¥è¯¢ææä¸çº§IDæ¾å ¥ “crumbs”ï¼ è¢«åå¤è å°±æ´å¥½åäºï¼æ¥è¯¢ä¸ä¸çº§è¯è®ºçè¯è®ºè ID(user_id)ï¼æ¾å ¥å½åè¯è®ºè®°å½ç “commented_user_id”ï¼å±ç¤ºçæ¶ååªéè¦å ³èä¸ä¸ç¨æ·ä¿¡æ¯è¡¨å°±å¯ä»¥äºï¼æ¥ä¸æ¥å°±æ¯ä¸å¡ä»£ç æ¼ç¤ºäºã
Â
代ç æ¼ç¤º
â²è¯è®ºæ·»å
public static function commentAdd($videoInfo, $userId, $content, $commentInfo = []) { $commentId = 0; $videoId = $videoInfo['id'] ?? 0; $publisherId = $videoInfo['user_id'] ?? 0; try { $crumbs = [0]; if ($commentInfo) { $pid = $commentInfo['id'] ?? 0; $insert['commented_user_id'] = $commentInfo['user_id'] ?? 0; $insert['pid'] = $pid; $crumbs = ShortVideoComment::getCrumbs($pid); } $insert['video_id'] = $videoId; $insert['user_id'] = $userId; $insert['is_pubisher'] = $userId == $publisherId ? 1 : 0; $insert['content'] = $content; $insert['state'] = ShortVideoComment::STATE['SHOW']; $insert['create_time'] = time(); $insert['crumbs'] = json_encode($crumbs); $commentId = ShortVideoComment::insertGetId($insert); if ($commentId && $videoId) { $map[] = ['id', '=', $videoId]; ShortVideoModel::where($map)->setInc('comment_count'); $userId && self::setRating(6, $userId, $videoId); } } catch (\Exception $e) { $preFileName = str::snake(__FUNCTION__); $path = self::getClassName(); write_log("msg:" . $e->getMessage(), $preFileName . "_error", $path); } return $commentId; }
â²è·åé¢å å±
public static function getCrumbs($cateId = 0, &$ids = []) { $idArr = array_merge((array)$cateId, $ids); $info = self::where('id', $cateId)->find(); if ($info['pid'] != 0) { $idArr = self::getCrumbs($info['pid'], $idArr); } else { array_unshift($idArr, 0); } return $idArr; }
â²æ°æ®æ¥è¯¢
public static function getList($map, $page = 1, $size = 30, $pid = 0) { $where[] = ['delete_time', '=', 0]; $where[] = ['state', '=', self::STATE['SHOW']]; $map = array_merge($where, $map); $field = ['id,crumbs,pid,video_id,user_id,commented_user_id,content']; $queryObj = self::field($field) ->with([ 'user' => function ($query) { $query->withField('id, nickname, avatar, mobile'); }, 'commented_user' => function ($query) { $query->withField('id, nickname, avatar, mobile'); } ]) ->where($map); $pid && $queryObj->whereRaw("JSON_EXTRACT(`crumbs` ,'$[1]') = $pid"); $list = $queryObj->page($page, $size) ->order('like_count desc') ->select(); return $list; }
Â
åå¨æå
    以ä¸æ¯åçæ¶åæ ¹æ®ä¸å¡è®¾è®¡çè¡¨æ ¼ï¼åæ¥çè¯è®ºåºå®å ¨ä»¿ç §æé³ï¼æ以ä¹å°±å¯¹è¡¨æ ¼è¿è¡äºæåãåæäºè¯è®ºè¡¨ååå¤è¡¨ï¼åªè¦æ¯å¯¹è¯è®ºè¿è¡è¯è®ºå°±æ¯åå¤ï¼è¿æ ·å¨åé¢æ°æ®åºå¤§çæ¶åï¼æ§è½ä¼æ´å¥½ä¸ç¹ãä½æ¯å¦ä½æ¯åæ设计çå°±æ¯ä¸å¼ 表ï¼èè¿ä»£çæ¶åä¹è¦ææé³è¯è®ºçææï¼å¯ä»¥ä½ä¸ºè§£å³æ¹æ³å°è¯æ»´ã
Â
â