application\home\controller\Lists.php 文件插入
//點贊模塊開始
public function dianzan()
{
$action = input('post.action');
$aid = input('post.aid/d');
if (empty($aid)) {
return '0';
}
$cookieKey = "dianzan_{$aid}"; // 保持Cookie鍵名一致
// 1. 查詢點贊數(shù)
if ($action == 'chaxun') {
$count = DB::name('archives')->where('aid', $aid)->value('dianzan');
return (string)(is_numeric($count) ? (int)$count : 0);
}
// 2. 判斷是否已點贊
if ($action == 'check_liked') {
return (isset($_COOKIE[$cookieKey]) && $_COOKIE[$cookieKey] == '1') ? '1' : '0';
}
// 3. 取消點贊(新增邏輯)
if ($action == 'cancel') {
// 檢查是否已點贊
if (!isset($_COOKIE[$cookieKey]) || $_COOKIE[$cookieKey] != '1') {
return 'no'; // 未點贊,無法取消
}
// 數(shù)據(jù)庫點贊數(shù)減1
$archivesModel = DB::name('archives');
$article = $archivesModel->where('aid', $aid)->find();
if (!$article) {
return '0';
}
$result = $archivesModel->where('aid', $aid)->setDec('dianzan', 1); // 減1
if ($result) {
setcookie($cookieKey, '0', time() - 3600, '/'); // 刪除Cookie(設(shè)置過期)
$newCount = $archivesModel->where('aid', $aid)->value('dianzan');
return (string)(is_numeric($newCount) ? (int)$newCount : 0);
}
return '0';
}
// 4. 執(zhí)行點贊(原有邏輯)
if (isset($_COOKIE[$cookieKey]) && $_COOKIE[$cookieKey] == '1') {
return 'no'; // 已點贊,防止重復(fù)
}
$archivesModel = DB::name('archives');
$article = $archivesModel->where('aid', $aid)->find();
if (!$article) {
return '0';
}
$result = $archivesModel->where('aid', $aid)->setInc('dianzan', 1); // 加1
if ($result) {
setcookie($cookieKey, '1', strtotime(date('Y-m-d 23:59:59')), '/');
$newCount = $archivesModel->where('aid', $aid)->value('dianzan');
return (string)(is_numeric($newCount) ? (int)$newCount : 1);
}
return '0';
}
//點贊模塊結(jié)束
在上面添加
下面是html數(shù)據(jù)
插入view_article 或者你需要的 其它模板里,添加位置即可,包含了 取消點贊 和 禁止取消點贊 寫的很完整
<div id="dianzan" class="dianzan-btn">
<span class="icon">?</span>
<span class="text">贊</span>
(<span id="dianzan_num">{$eyou.field.dianzan|default=0}</span>)
<span class="loading" style="display:none;">
<svg width="14" height="14" viewBox="0 0 14 14" xmlns="http://www.w3.org/2000/svg">
<circle cx="7" cy="7" r="6" fill="none" stroke="currentColor" stroke-width="2" stroke-dasharray="37.7" stroke-dashoffset="18.85" transform="rotate(0 7 7)">
<animate attributeName="transform" type="rotate" from="0 7 7" to="360 7 7" dur="1s" repeatCount="indefinite"/>
</circle>
</svg>
</span>
</div>
<script src="__STATIC__/js/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
const aid = {$eyou.field.aid};
const apiUrl = "/?m=home&c=lists&a=dianzan";
const $btn = $("#dianzan");
const $text = $btn.find(".text");
const $numEl = $("#dianzan_num");
const $loading = $btn.find(".loading");
let isProcessing = false;
const cookieKey = "dianzan_" + aid;
window.isLiked = getCookie(cookieKey) === "1";
updateBtnState();
// 同步點贊數(shù)
$.post(apiUrl, {action: "chaxun", aid: aid}, function(res) {
const count = isNaN(parseInt(res)) ? 0 : parseInt(res);
$numEl.text(count);
});
// 點擊事件
$btn.click(function() {
if (isProcessing) return;
isProcessing = true;
$btn.addClass("processing");
$loading.show();
// 【取消點贊專用代碼塊:開始】
// 區(qū)分點贊/取消點贊動作
const currentAction = window.isLiked ? "cancel" : "";
// 【取消點贊專用代碼塊:結(jié)束】
$.post(apiUrl, {
aid: aid,
// 【取消點贊專用代碼塊:開始】
action: currentAction // 傳遞取消標(biāo)識
// 【取消點贊專用代碼塊:結(jié)束】
// 如果刪除上面的取消代碼塊,這里改為:action: ""
}, function(result) {
if (result === 'no') {
// 【取消點贊專用代碼塊:開始】
showToast(window.isLiked ? "您未點贊,無法取消" : "您已贊過~", "warning");
// 【取消點贊專用代碼塊:結(jié)束】
// 如果刪除上面的取消代碼塊,這里改為:showToast("您已贊過~", "warning");
} else if (!isNaN(result)) {
const oldNum = parseInt($numEl.text());
animateNumber(oldNum, parseInt(result));
// 【取消點贊專用代碼塊:開始】
// 反轉(zhuǎn)點贊狀態(tài)(允許取消)
window.isLiked = !window.isLiked;
// 【取消點贊專用代碼塊:結(jié)束】
// 如果刪除上面的取消代碼塊,這里改為:window.isLiked = true;
updateBtnState();
// 【取消點贊專用代碼塊:開始】
showToast(window.isLiked ? "點贊成功!" : "取消點贊成功", "success");
// 【取消點贊專用代碼塊:結(jié)束】
// 如果刪除上面的取消代碼塊,這里改為:showToast("點贊成功!", "success");
}
}).fail(function() {
showToast("網(wǎng)絡(luò)異常,請重試", "error");
}).always(function() {
isProcessing = false;
$btn.removeClass("processing");
$loading.hide();
});
});
// 更新按鈕樣式和文字
function updateBtnState() {
if (window.isLiked) {
$btn.addClass("liked");
// 【取消點贊專用代碼塊:開始】
$text.text("取消贊"); // 已點贊時顯示“取消贊”
// 【取消點贊專用代碼塊:結(jié)束】
// 如果刪除上面的取消代碼塊,刪除此行(保持文字為“贊”)
} else {
$btn.removeClass("liked");
$text.text("贊");
}
}
// 數(shù)字動畫
function animateNumber(from, to) {
let current = from;
// 【取消點贊專用代碼塊:開始】
// 支持增長和減少(取消點贊時數(shù)字減少)
const step = (to - from) > 0 ? Math.ceil((to - from)/20) : Math.floor((to - from)/20);
// 【取消點贊專用代碼塊:結(jié)束】
// 如果刪除上面的取消代碼塊,這里改為:const step = Math.ceil((to - from)/20);
const timer = setInterval(() => {
current += step;
// 【取消點贊專用代碼塊:開始】
if ((step > 0 && current >= to) || (step < 0 && current <= to)) {
// 【取消點贊專用代碼塊:結(jié)束】
// 如果刪除上面的取消代碼塊,這里改為:if (current >= to) {
current = to;
clearInterval(timer);
}
$numEl.text(current);
}, 30);
}
// 讀取Cookie
function getCookie(name) {
const value = "; " + document.cookie;
const parts = value.split("; " + name + "=");
if (parts.length === 2) return parts.pop().split(";").shift();
return "";
}
// 提示氣泡
function showToast(text, type) {
const $toast = $("<div class='dianzan-toast " + type + "'>" + text + "</div>");
$("body").append($toast);
setTimeout(() => $toast.addClass("show"), 10);
setTimeout(() => {
$toast.removeClass("show");
setTimeout(() => $toast.remove(), 300);
}, 3000);
}
});
</script>
<style>
/* 樣式不變 */
.dianzan-btn {
display: inline-flex;
align-items: center;
gap: 6px;
padding: 6px 14px;
background: #fff;
border: 1px solid #ddd;
border-radius: 20px;
font-size: 14px;
cursor: pointer;
transition: all 0.3s;
}
.dianzan-btn .icon {
color: #999;
font-size: 16px;
}
.dianzan-btn .text, .dianzan-btn #dianzan_num {
color: #666;
}
.dianzan-btn.liked {
border-color: #ff4d4f;
}
.dianzan-btn.liked .icon {
color: #ff4d4f;
}
.dianzan-btn.liked .text, .dianzan-btn.liked #dianzan_num {
color: #ff4d4f;
}
.dianzan-btn.processing {
opacity: 0.7;
cursor: wait;
}
.dianzan-btn .loading {
margin-left: 4px;
color: #ff4d4f;
}
.dianzan-toast {
position: fixed;
top: 20px;
left: 50%;
transform: translate(-50%, -100px);
padding: 8px 16px;
border-radius: 4px;
color: #fff;
font-size: 14px;
opacity: 0;
transition: all 0.3s;
z-index: 9999;
}
.dianzan-toast.show {
transform: translate(-50%, 0);
opacity: 1;
}
.dianzan-toast.success {
background: #ff4d4f;
}
.dianzan-toast.warning {
background: #ff9800;
}
.dianzan-toast.error {
background: #f44336;
}
</style>下面執(zhí)行數(shù)據(jù)庫
ALTER TABLE ey_archives ADD dianzan varchar(500) NOT NULL DEFAULT 0 COMMENT '點贊數(shù)';
就完成了 下面是視頻,看了下 好像加不了視頻我把視頻單獨上傳服務(wù)器吧 https://110.42.213.41:8888/down/44KPL2StOom3.mp4 求別攻擊
切記清理緩存 不然不顯示點贊
下面分享如何自定義 點贊數(shù) 在application\admin\controller 如 下載 文章 商品等模型 具體看圖

加入以下代碼
可以在這個地方加
// SEO描述
// --點贊數(shù)量
$dianzan = '';
$dianzan = isset($post['dianzan']) ? $post['dianzan'] : 0;
if (intval($dianzan) > 1) {
$dianzan = $post['dianzan'];
}所有模型加的都一樣
然后再 這個地方 application\admin\template
具體看圖
加個傳動軸可以在 點擊量下加 具體看圖

<dl class="row">
<dt class="tit">
<label>點贊量</label>
</dt>
<dd class="opt">
<input type="text" value="{$rand_dianzan}" name="dianzan" id="dianzan" class="input-txt">
<span class="err"></span>
<p class="notic"></p>
</dd>
</dl>注意 add.htm 和edit.htm 都加,這樣 你在后臺輸入數(shù)值 前臺就顯示點贊量多少
,
下面教程結(jié)束 當(dāng)然不推薦這樣搞 有點麻煩 而且后臺要是更新什么 可能會覆蓋 只建議加上面那些 方便