2025年09月08日/ 浏览 9
在当今电子商务蓬勃发展的时代,拍卖作为一种特殊的商品交易方式,越来越受到企业和个人卖家的青睐。帝国CMS作为国内知名的内容管理系统,虽然本身不直接提供拍卖功能模块,但通过二次开发和系统整合,完全可以实现完善的拍卖系统。
实现拍卖功能需要新增几张核心数据表:
sql
— 拍卖商品主表
CREATE TABLE phome_auction
(
id
int(11) NOT NULL AUTO_INCREMENT,
title
varchar(255) NOT NULL COMMENT ‘拍卖标题’,
classid
smallint(6) NOT NULL DEFAULT ‘0’ COMMENT ‘栏目ID’,
start_time
int(11) NOT NULL COMMENT ‘开始时间’,
end_time
int(11) NOT NULL COMMENT ‘结束时间’,
start_price
decimal(10,2) NOT NULL COMMENT ‘起拍价’,
current_price
decimal(10,2) NOT NULL COMMENT ‘当前价’,
bid_increment
decimal(10,2) NOT NULL COMMENT ‘加价幅度’,
pic_url
varchar(255) DEFAULT NULL COMMENT ‘商品图片’,
content
text COMMENT ‘商品详情’,
status
tinyint(1) NOT NULL DEFAULT ‘0’ COMMENT ‘状态:0未开始1进行中2已结束’,
PRIMARY KEY (id
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
— 拍卖出价记录表
CREATE TABLE phome_auction_bid
(
id
int(11) NOT NULL AUTO_INCREMENT,
auction_id
int(11) NOT NULL COMMENT ‘拍卖ID’,
userid
int(11) NOT NULL COMMENT ‘出价用户ID’,
price
decimal(10,2) NOT NULL COMMENT ‘出价金额’,
bid_time
int(11) NOT NULL COMMENT ‘出价时间’,
PRIMARY KEY (id
)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
在帝国CMS后台,通过”系统”-“模型管理”新建一个”拍卖”模型:
在模板目录下创建拍卖相关模板文件:
auction_list.html
– 拍卖列表页模板auction_show.html
– 拍卖详情页模板auction_bid.html
– 出价表单模板示例代码片段(出价表单部分):
html
在/e/action/
目录下创建拍卖相关处理文件:
bid.php
– 处理出价请求auction_cron.php
– 拍卖状态自动更新脚本(需设置定时任务)出价处理核心代码:
php
<?php
require(‘../class/connect.php’);
require(‘../class/db_sql.php’);
require(‘../class/functions.php’);
$link=db_connect();
$empire=new mysqlquery();
// 验证用户登录
$userid=(int)getcvar(‘mluserid’);
if(!$userid){
printerror(‘请先登录’,”,1);
}
// 获取参数
$auctionid=(int)$POST[‘auctionid’];
$bidprice=floatval($POST[‘bidprice’]);
// 验证拍卖信息
$auction=$empire->fetch1(“SELECT * FROM {$dbtbpre}ecms_auction WHERE id=’$auctionid'”);
if(!$auction){
printerror(‘拍卖不存在’,”,1);
}
// 验证拍卖状态
if($auction[‘status’]!=1){
printerror(‘拍卖已结束或未开始’,”,1);
}
// 验证出价有效性
$nextprice=$auction[‘currentprice’]+$auction[‘bidincrement’];
if($bidprice<$nextprice){
printerror(‘出价必须大于等于’.$nextprice.’元’,”,1);
}
// 记录出价
$time=time();
$sql=$empire->query(“INSERT INTO {$dbtbpre}ecmsauctionbid(auctionid,userid,price,bidtime) VALUES(‘$auctionid’,’$userid’,’$bidprice’,’$time’)”);
// 更新当前价格
$empire->query(“UPDATE {$dbtbpre}ecmsauction SET currentprice=’$bidprice’ WHERE id=’$auctionid'”);
// 返回成功
printerror(‘出价成功’,’/auction/show-‘.$auctionid.’.html’,1);
db_close();
$empire=null;
?>
拍卖系统需要定时检查拍卖状态,可通过以下方式实现:
/e/action/auction_cron.php
文件:php
<?php
require(‘../class/connect.php’);
require(‘../class/db_sql.php’);
$link=db_connect();
$empire=new mysqlquery();
$now=time();
// 更新未开始的拍卖为进行中
$empire->query(“UPDATE {$dbtbpre}ecmsauction SET status=1 WHERE status=0 AND starttime<=$now AND end_time>$now”);
// 更新已结束的拍卖
$empire->query(“UPDATE {$dbtbpre}ecmsauction SET status=2 WHERE status=1 AND endtime<=$now”);
db_close();
$empire=null;
?>
bash
在拍卖详情页添加JavaScript倒计时功能:
javascript
function updateCountdown(endtime) {
var now = new Date().getTime();
var distance = endtime – now;
if (distance < 0) {
clearInterval(countdownTimer);
document.getElementById(“countdown”).innerHTML = “拍卖已结束”;
return;
}
var days = Math.floor(distance / (1000 * 60 * 60 * 24));
var hours = Math.floor((distance % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60));
var minutes = Math.floor((distance % (1000 * 60 * 60)) / (1000 * 60));
var seconds = Math.floor((distance % (1000 * 60)) / 1000);
document.getElementById(“countdown”).innerHTML = days + “天 ” + hours + “小时 ”
+ minutes + “分 ” + seconds + “秒 “;
}
var endDate = new Date(“[!–end_time–]”).getTime();
var countdownTimer = setInterval(function() {
updateCountdown(endDate);
}, 1000);
使用WebSocket或轮询技术实现出价实时通知:
javascript
// 使用AJAX轮询获取最新出价
function checkNewBids() {
$.ajax({
url: ‘/e/action/getnewbids.php’,
data: {auctionid: [!–id–], lastbidid: lastBidId},
success: function(data) {
if(data.newbids.length > 0) {
// 更新页面显示
lastBidId = data.lastbidid;
// 显示通知
}
},
complete: function() {
setTimeout(checkNewBids, 5000); // 5秒后再次检查
}
});
}
var lastBidId = [!–lastbidid–];
$(document).ready(function() {
checkNewBids();
});
通过以上步骤,您可以在帝国CMS上实现一个功能完善的拍卖系统。根据实际需求,可以进一步扩展功能,如多种拍卖模式(英格兰式、荷兰式等)、批量拍卖、组合拍卖等高级功能。