防止提交重复订单的方法
背景交代: 使用ajax来提交创建订单,需求是不能创建重复的订单
-
使用js限制提交的频率
// 设置内容为可用,但是不能继续操作
$.fn.setBtnTxtEnabled = function (func) {
if (!this) {
throw new Error("element 不能为空");
}this.text(this.data('text'));
}// 设置按钮不可用
$.fn.setBtnEnabled = function (func) {
if (!this) {
throw new Error("element 不能为空");
}this.text(this.data('text'));
this.data('cando', true); //设置该属性判断不能提交
func && func(); //执行额外操作
}// 判断button是否可以操作
$.fn.isBtnEnabled = function (func) {
if (!this) {
throw new Error("element 不能为空");
}if (this.data('cando') == undefined) { //第一次未设置返回true
return true;
}
return this.data('cando'); //之后返回设置的值
} -
在服务器端限制提交频率
public static class ApiLimit
{
///
///
///
///
/// 默认5秒
public static void Limit(string sessionname, int time = 5)
{
// 这个session不需要清除,3秒之后,已经跳转到其他其他页面,然后用户3s之后,这个session的检验就已经失效了
if (HttpContext.Current.Session[sessionname] != null && (HttpContext.Current.Session[sessionname].SafeToLong() > DateTime.Now.Ticks))
{
throw new Exception("提交太频繁,请稍后重试");
}
else
{
HttpContext.Current.Session[sessionname] = DateTime.Now.AddSeconds(time).Ticks;
;
}
}
} -
在提交order的时候做判断
3.1. 判断orderid是否存在,存在则不添加
3.2. 获取用户最后一条订单记录,可以按业务需求限制两条记录的时间间隔必须大于多少,否则忽略
基本上这样就可以保证在并发时,防止重复订单的出现,但是效率应该是不高的

浙公网安备 33010602011771号