C #: achieve universal form + ajax sites limit the number of articles to read, if the limit is reached, you need to pay for the article (single article)

Site background create a universal form, as shown in the field
Here Insert Picture Description
to determine whether the user is logged in html page, parameter passing ajax asynchronous service processing after success or failure.

//判断用户是否登陆
<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 business logic

<%@ 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;
            }
        }
    }
}
Released six original articles · won praise 3 · Views 165

Guess you like

Origin blog.csdn.net/weixin_43739179/article/details/102838151