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. 查询点赞数
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'; // 未点赞,无法取消
}
// 数据库点赞数减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(设置过期)
$newCount = $archivesModel->where('aid', $aid)->value('dianzan');
return (string)(is_numeric($newCount) ? (int)$newCount : 0);
}
return '0';
}
// 4. 执行点赞(原有逻辑)
if (isset($_COOKIE[$cookieKey]) && $_COOKIE[$cookieKey] == '1') {
return 'no'; // 已点赞,防止重复
}
$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';
}
//点赞模块结束
在上面添加
下面是html数据
插入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();
// 同步点赞数
$.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();
// 【取消点赞专用代码块:开始】
// 区分点赞/取消点赞动作
const currentAction = window.isLiked ? "cancel" : "";
// 【取消点赞专用代码块:结束】
$.post(apiUrl, {
aid: aid,
// 【取消点赞专用代码块:开始】
action: currentAction // 传递取消标识
// 【取消点赞专用代码块:结束】
// 如果删除上面的取消代码块,这里改为:action: ""
}, function(result) {
if (result === 'no') {
// 【取消点赞专用代码块:开始】
showToast(window.isLiked ? "您未点赞,无法取消" : "您已赞过~", "warning");
// 【取消点赞专用代码块:结束】
// 如果删除上面的取消代码块,这里改为:showToast("您已赞过~", "warning");
} else if (!isNaN(result)) {
const oldNum = parseInt($numEl.text());
animateNumber(oldNum, parseInt(result));
// 【取消点赞专用代码块:开始】
// 反转点赞状态(允许取消)
window.isLiked = !window.isLiked;
// 【取消点赞专用代码块:结束】
// 如果删除上面的取消代码块,这里改为:window.isLiked = true;
updateBtnState();
// 【取消点赞专用代码块:开始】
showToast(window.isLiked ? "点赞成功!" : "取消点赞成功", "success");
// 【取消点赞专用代码块:结束】
// 如果删除上面的取消代码块,这里改为:showToast("点赞成功!", "success");
}
}).fail(function() {
showToast("网络异常,请重试", "error");
}).always(function() {
isProcessing = false;
$btn.removeClass("processing");
$loading.hide();
});
});
// 更新按钮样式和文字
function updateBtnState() {
if (window.isLiked) {
$btn.addClass("liked");
// 【取消点赞专用代码块:开始】
$text.text("取消赞"); // 已点赞时显示“取消赞”
// 【取消点赞专用代码块:结束】
// 如果删除上面的取消代码块,删除此行(保持文字为“赞”)
} else {
$btn.removeClass("liked");
$text.text("赞");
}
}
// 数字动画
function animateNumber(from, to) {
let current = from;
// 【取消点赞专用代码块:开始】
// 支持增长和减少(取消点赞时数字减少)
const step = (to - from) > 0 ? Math.ceil((to - from)/20) : Math.floor((to - from)/20);
// 【取消点赞专用代码块:结束】
// 如果删除上面的取消代码块,这里改为:const step = Math.ceil((to - from)/20);
const timer = setInterval(() => {
current += step;
// 【取消点赞专用代码块:开始】
if ((step > 0 && current >= to) || (step < 0 && current <= to)) {
// 【取消点赞专用代码块:结束】
// 如果删除上面的取消代码块,这里改为: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>下面执行数据库
ALTER TABLE ey_archives ADD dianzan varchar(500) NOT NULL DEFAULT 0 COMMENT '点赞数';
就完成了 下面是视频,看了下 好像加不了视频我把视频单独上传服务器吧 https://110.42.213.41:8888/down/44KPL2StOom3.mp4 求别攻击
切记清理缓存 不然不显示点赞
下面分享如何自定义 点赞数 在application\admin\controller 如 下载 文章 商品等模型 具体看图

加入以下代码
可以在这个地方加
// SEO描述
// --点赞数量
$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 都加,这样 你在后台输入数值 前台就显示点赞量多少
,
下面教程结束 当然不推荐这样搞 有点麻烦 而且后台要是更新什么 可能会覆盖 只建议加上面那些 方便