C#:万能表单+ajax实现网站文章阅读次数限制,若达到限制次数,则需付费购买文章(单篇文章)

mac2024-05-09  35

网站后台创建万能表单,字段如图 html页面判断用户是否登录状态,参数传递ajax异步,成功或失败后的业务处理。

//判断用户是否登陆 <script type="text/javascript"> <%csharp%> DTcms.Model.users muc=GetUserInfo(); if(muc==null) { <%/csharp%> $('#centent').html('<div></div>'); layer.confirm('请先登陆,再继续阅读文章!', { btn: ['确定'] //按钮 }, function(){ location.href = '/login.aspx'; }); <%csharp%> }else { <%/csharp%> //参数传递 ajax异步 var userid=<%=muc.id%>; var wzid=<%=model.id%>; var title="<%=model.title%>"; $.ajax({ url: '/dev/ClickLoad.ashx', type: 'POST', dataType: 'json', data: { action: 'GetContent', userid:userid , wzid: wzid, title:title, }, //成功或失败后的业务处理 success: function(d) { if(d.code==0){ $('#centent').html(d.content); }else{ $('#centent').html('<div></div>'); layer.msg(d.msg, function(){ $('#centent').html('<div class="goumai"><a href="/zhifu.aspx?actionss={chanid}&wzid={model.id}">点击购买</a></div>'); }); } }, error: function(e) {} }) <%csharp%> } <%/csharp%> </script>

C# ashx处理业务逻辑

<%@ WebHandler Language="C#" Class="DTcms.Web.dev.ClickLoad" %> using System; using System.Collections.Generic; using System.Web; using System.IO; using System.Text; using System.Data; using System.Data.Sql; using System.Data.SqlClient; using DTcms.Common; using DTcms.DBUtility; using NetWing.Common.Data.SQLServer; using NetWing.Common.Request; using NetWing.Common; namespace DTcms.Web.dev { /// <summary> /// dev 的摘要说明 /// </summary> public class ClickLoad : IHttpHandler { public void ProcessRequest(HttpContext context) { context.Response.ContentType = "text/plain"; string action = context.Request["action"]; if (string.IsNullOrEmpty(action)) { context.Response.Write("{\"code\":1000, \"msg\":\"请求失败,参数有误!\"}"); return; } switch (action) { case "GetContent": //获得要购买的文章 GetContent(context); break; } } #region 购买文章 public void GetContent(HttpContext context) { string userid = context.Request["userid"]; string wzid = context.Request["wzid"]; string title = context.Request["title"]; if (string.IsNullOrEmpty(userid) || string.IsNullOrEmpty(wzid)) { context.Response.Write("{\"code\":1000, \"msg\":\"请求失败,参数有误!\"}"); return; } string dtA = (string)SqlEasy.ExecuteScalar("select content from dt_article where id=" + wzid); DataRow drw = SqlEasy.ExecuteDataRow("select * from nw_customform_user_ffwz where userid=" + userid + " and wzid=" + wzid); string cishusql = "select * FROM dt_article_attribute_value where article_id="+wzid+"";//查询后台填写的文章可免费查看次数 DataRow tongji = SqlEasy.ExecuteDataRow(cishusql); int lick = int.Parse(tongji["cishu"].ToString()); if (drw == null) { int code = SqlEasy.ExecuteNonQuery("INSERT INTO nw_customform_user_ffwz (userid,wzid,statu,add_time,number,title) VALUES (" + userid + "," + wzid + ",0,'" + DateTime.Now + "',1,'" + title + "')"); if (code > 0) { context.Response.Write("{\"code\":0, \"msg\":\"请求成功!\",\"content\":\"" + dtA + "\"}"); } else { context.Response.Write("{\"code\":200, \"msg\":\"网络繁忙,请重新刷新网页!\",\"content\":\"" + dtA + "\"}"); } } else { if (int.Parse(drw["statu"].ToString()) == 0 && int.Parse(drw["number"].ToString()) < lick) { int number = int.Parse(drw["number"].ToString()) + 1; int code = SqlEasy.ExecuteNonQuery("update nw_customform_user_ffwz set number=" + number + " where id=" + drw["id"].ToString()); if (code > 0) { context.Response.Write("{\"code\":0, \"msg\":\"请求成功!\",\"content\":\"" + dtA + "\"}"); } else { context.Response.Write("{\"code\":200, \"msg\":\"网络繁忙,请重新刷新网页!\",\"content\":\"" + dtA + "\"}"); } } else if (int.Parse(drw["statu"].ToString()) == 1) { int number = int.Parse(drw["number"].ToString()) + 1; int code = SqlEasy.ExecuteNonQuery("update nw_customform_user_ffwz set number=" + number + " where id=" + drw["id"].ToString()); if (code > 0) { context.Response.Write("{\"code\":0, \"msg\":\"请求成功!\",\"content\":\"" + dtA + "\"}"); } else { context.Response.Write("{\"code\":200, \"msg\":\"网络繁忙,请重新刷新网页!\",\"content\":\"" + dtA + "\"}"); } } else { context.Response.Write("{\"code\":200, \"msg\":\"购买后才能查看!\"}"); } } } #endregion public bool IsReusable { get { return false; } } } }
最新回复(0)