2
0
visuddhinanda 4 жил өмнө
parent
commit
bff84579d6

+ 39 - 0
app/api/like.php

@@ -0,0 +1,39 @@
+<?php
+require_once "../db/like.php";
+require_once "../redis/function.php";
+require_once "../public/function.php";
+
+$model = new Like(redis_connect());
+
+switch ($_REQUEST["_method"]) {
+	case 'index':
+		# get
+		$model->index();
+		break;
+	case 'list':
+		# post
+		$model->list();
+		break;
+	case 'create':
+		# post
+		$model->create();
+		break;
+	case 'show':
+		# get
+		$model->show();
+		break;	
+	case 'update':
+		# post
+		$model->update();
+		break;	
+	case 'delete':
+		# get
+		$model->delete();
+		break;	
+	default:
+		# code...
+		break;
+}
+
+
+?>

+ 4 - 1
app/article/article.js

@@ -67,7 +67,10 @@ function collect_load(id) {
 							$("#article_subtitle").html(result.subtitle);
 						}
 						$("#article_author").html(result.username.nickname + "@" + result.username.username);
-						$("#like_div").html("<like restype='collection' resid='"+id+"'></like>");
+						let htmlLike = "";
+						htmlLike += "<like liketype='like' restype='collection' resid='"+id+"'></like>";
+						htmlLike += "<like liketype='favorite' restype='collection' resid='"+id+"'></like>";
+						$("#like_div").html(htmlLike);
 						$("#summary").html(result.summary);
 						$("#contents").html("<div id='content_text'></div><h3>目录</h3><div id='content_toc'></div>");
 

+ 100 - 0
app/db/like.php

@@ -0,0 +1,100 @@
+<?php
+require_once "../path.php";
+require_once "../db/table.php";
+/*
+CREATE TABLE likes (
+    id            INTEGER      PRIMARY KEY AUTOINCREMENT,
+    like_type     VARCHAR (16) NOT NULL,
+    resource_type VARCHAR (32) NOT NULL,
+    resource_id   CHAR (36)    NOT NULL,
+    user_id       INTEGER      NOT NULL,
+    created_at    TIMESTAMP DEFAULT CURRENT_TIMESTAMP     NOT NULL //只做初始化,更新时不自动更新
+);
+*/
+class Like extends Table
+{
+    function __construct($redis=false) {
+		parent::__construct(_FILE_DB_LIKE_, "likes", "", "",$redis);
+    }
+
+	public function  index(){
+		$where["like_type"] = "like";
+		$where["resource_type"] = $_GET["type"];
+		$where["resource_id"] = explode($_GET["id"],",");
+		echo json_encode($this->_index(["resource_id","user_id"],$where), JSON_UNESCAPED_UNICODE);
+	}
+	
+	public function  list(){
+		if(!isset($_COOKIE["userid"])){
+			$userId = $_COOKIE["userid"];
+		}
+
+		$json = file_get_contents('php://input');
+		$data = json_decode($json,true);
+		foreach ($data as $key => $value) {
+			# code...
+			$data[$key]['like']=$this->medoo->count($this->table,[
+											'like_type'=>$value['like_type'],
+											'resource_type'=>$value['resource_type'],
+											'resource_id'=>$value['resource_id'],
+											  ]);
+		}
+		if(isset($_COOKIE["userid"])){
+			$userId = $_COOKIE["userid"];
+			foreach ($data as $key => $value) {
+				# code...
+				$data[$key]['me']=$this->medoo->count($this->table,[
+												'like_type'=>$value['like_type'],
+												'resource_type'=>$value['resource_type'],
+												'resource_id'=>$value['resource_id'],
+												'user_id'=>$userId,
+												]);
+			}
+		}
+
+		$this->result["ok"]=true;
+		$this->result["message"]="";
+		$this->result["data"]=$data;
+		echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
+
+	}
+
+
+	public function  create(){
+		if(!isset($_COOKIE["userid"])){
+			return;
+		}
+		$json = file_get_contents('php://input');
+		$data = json_decode($json,true);
+		$data["user_id"] = $_COOKIE["userid"];
+		$isExist = $this->medoo->has("likes",$data);
+		if(!$isExist){
+			echo json_encode($this->_create($data,["like_type","resource_type","resource_id","user_id"]), JSON_UNESCAPED_UNICODE);
+		}
+		else{
+			$this->result["ok"]=false;
+			$this->result["message"]="is exist";
+			echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
+		}
+	}
+	
+	public function  delete(){
+		if(!isset($_COOKIE["userid"])){
+			return;
+		}
+		$where["like_type"] = $_GET["like_type"];
+		$where["resource_type"] = $_GET["resource_type"];
+		$where["resource_id"] = $_GET["resource_id"];
+		$where["user_id"] = $_COOKIE["userid"];
+		$row = $this->_delete($where);
+		if($row["data"]>0){
+			$this->result["data"] = $where;
+		}else{
+			$this->result["ok"]=false;
+			$this->result["message"]="no delete";			
+		}
+		echo json_encode($this->result, JSON_UNESCAPED_UNICODE);
+	}
+}
+
+?>

+ 15 - 0
app/widget/like.css

@@ -0,0 +1,15 @@
+.like_mine {
+    background-color: var(--btn-bg-color);
+}
+.like_inner {
+    border-radius: 5px;
+    margin: 5px;
+    cursor: pointer;
+    min-width: 40px;
+    border: 1px solid var(--border-line-color);		
+	padding: 2px 8px;
+}
+
+like{
+    display: inline-block;
+}

+ 159 - 0
app/widget/like.js

@@ -0,0 +1,159 @@
+var arrElement = new Array();
+function Like (){
+	$("like").each(function(){
+		if($(this).attr("init")!="true"){
+			arrElement.push({
+							like_type:$(this).attr("liketype"),
+							resource_type:$(this).attr("restype"),
+							resource_id:$(this).attr("resid"),
+							like:0,
+							me:0,
+							init:false
+						});
+		}
+	});
+	$("like").on("click",function(){
+			let liketype = $(this).attr("liketype");
+			let rettype = $(this).attr("restype");
+			let resid = $(this).attr("resid");		
+		let e = arrElement.find(function(item){
+
+			if(liketype===item.like_type && rettype===item.resource_type && resid===item.resource_id){
+				return true;
+			}
+			else{
+				return false;
+			}
+		});
+		if(e.me==0 ){
+			add(e.like_type,e.resource_type,e.resource_id);				
+		}else{
+			remove(e.like_type,e.resource_type,e.resource_id);	
+		}
+	})
+	LikeRefreshAll();
+}
+function add(liketype,restype,resid) {
+	$.ajaxSetup({contentType: "application/json; charset=utf-8"});
+	$.post(
+		"../api/like.php?_method=create",
+		JSON.stringify({
+			like_type:liketype,
+			resource_type:restype,
+			resource_id:resid
+		})
+		
+	).done(function (data) {
+		console.log(data);
+		let result = JSON.parse(data);
+		if(result.ok==true){
+			for (let it of arrElement) {
+				if(result["data"].resource_type===it.resource_type &&
+				result["data"].resource_id===it.resource_id &&
+				result["data"].like_type===it.like_type){
+					it.like++;
+					it.me=1;
+				}
+			}
+			Render();
+		}
+		
+	});
+}
+function remove(liketype,restype,resid) {
+	$.getJSON(
+		"../api/like.php",
+		{
+			_method:"delete",
+			like_type:liketype,
+			resource_type:restype,
+			resource_id:resid
+		}
+	).done(function (data) {
+		console.log("delete",data);
+		if(data.ok){
+			LikeRefresh(data.data);
+		}
+	}).fail(function(jqXHR, textStatus, errorThrown){
+		switch (textStatus) {
+			case "timeout":
+				break;
+			case "error":
+				switch (jqXHR.status) {
+					case 404:
+						break;
+					case 500:
+						break;				
+					default:
+						break;
+				}
+				break;
+			case "abort":
+				break;
+			case "parsererror":
+				console.log("delete-parsererror",jqXHR.responseText);
+				break;
+			default:
+				break;
+		}
+		
+	});
+}
+function LikeRefresh(data){
+	$.ajaxSetup({contentType: "application/json; charset=utf-8"});
+	$.post(
+		"../api/like.php?_method=list",
+		JSON.stringify([data])
+	).done(function (data) {
+		console.log(data);
+		let result = JSON.parse(data);
+		for (let it of arrElement) {
+			if(result["data"][0].resource_type===it.resource_type &&
+			result["data"][0].resource_id===it.resource_id &&
+			result["data"][0].like_type===it.like_type){
+				it.like=result["data"][0].like;
+				it.me=result["data"][0].me;
+			}
+		}
+		Render();
+	});
+}
+
+function LikeRefreshAll(){
+	$.ajaxSetup({contentType: "application/json; charset=utf-8"});
+	$.post(
+		"../api/like.php?_method=list",
+		JSON.stringify(arrElement)
+	).done(function (data) {
+		console.log(data);
+		arrElement = JSON.parse(data).data;
+		Render();
+	});
+}
+
+function Render(){
+	for (const it of arrElement) {
+		let html=" ";
+		let meClass="";
+		let likeIcon="";
+		switch (it.like_type) {
+			case "like":
+				likeIcon = "👍";
+				break;
+			case "favorite":
+				likeIcon = "⭐";
+				break;
+			case "watch":
+				likeIcon = "👁️";
+				break;
+			default:
+				break;
+		}
+		if(it.me>0){
+			meClass = " like_mine";
+		}
+		html +="<div class='like_inner "+meClass+"'>"+likeIcon+it.like+"</div>";
+
+		$("like[liketype='"+it.like_type+"'][restype='"+it.resource_type+"'][resid='"+it.resource_id+"']").html(html);
+	}
+}