/// <summary> /// 写模板 /// </summary> /// <param name="hInfo"></param> /// <param name="uInfo"></param> /// <param name="username"></param> /// <param name="hName"></param> /// <returns></returns> public static bool WriteFile(htmlInfo hInfo, cmUserInfo uInfo, string username, string hName) { //文件输出目录 string path = HttpContext.Current.Server.MapPath("~/test/" + username + "/"); // 读取模板文件 string temp = HttpContext.Current.Server.MapPath("~/templates/DetailModel.html");//模版文件 //string str = SiteTemplate();//读取模版页面html代码 string str = ""; using (StreamReader sr = new StreamReader(temp, Encoding.GetEncoding("gb2312"))) { str = sr.ReadToEnd(); sr.Close(); } string htmlfilename = hName;//静态文件名 // 替换内容 str = str.Replace("companyName_Str", uInfo.companyName); if (hInfo.title.Length > 6) { str = str.Replace("keywords_Str", hInfo.title + "," + hInfo.title.Substring(0, 2) + "," + hInfo.title.Substring(2, 2) + "," + hInfo.title.Substring(4, 2)); } else { str = str.Replace("keywords_Str", hInfo.title); } str = str.Replace("description_Str", hInfo.articlecontent.Substring(0, 80)); str = str.Replace("host_Str", host); str = str.Replace("catid_Str", hInfo.columnId); str = str.Replace("Id_Str", hInfo.Id.ToString()); str = str.Replace("title_Str", hInfo.title); str = str.Replace("addTime_Str", hInfo.addTime); str = str.Replace("pinpai_Str", hInfo.pinpai); str = str.Replace("price_Str", hInfo.price); str = str.Replace("qiding_Str", hInfo.smallCount); str = str.Replace("gonghuo_Str", hInfo.sumCount); str = str.Replace("xinghao_Str", hInfo.xinghao); str = str.Replace("city_Str", hInfo.city); str = str.Replace("unit_Str", hInfo.unit); str = str.Replace("titleImg_Str", hInfo.titleImg); str = str.Replace("content_Str", hInfo.articlecontent); str = str.Replace("username_Str", username); str = str.Replace("site_Str", hInfo.realmNameId); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } // 写文件 using (StreamWriter sw = new StreamWriter(path + htmlfilename, true)) { sw.Write(str); sw.Flush(); sw.Close(); } return(true); }
/// <summary> /// 获取目录主页内容 /// </summary> /// <param name="sqlstr"></param> /// <returns></returns> public List <htmlInfo> GetMainHtmlList() { List <htmlInfo> hList = new List <htmlInfo>(); // DataTable dt = SqlHelper.ExecuteDataSet(@"select title,titleImg,titleURL,addTime,columnId,columnName from //( select //RANK()OVER(PARTITION BY columnInfo.Id ORDER BY htmlInfo.addTime DESC) AS //RANK2, title,titleImg,titleURL,addTime,htmlInfo.columnId,columnName from //htmlInfo left join columnInfo On htmlInfo.columnId = columnInfo.Id) T //where RANK2<=10 ").Tables[0]; DataTable dt = SqlHelperCatalog.ExecuteDataSet(@"select title,titleURL,titleImg,addTime,columnId from ( select RANK()OVER(PARTITION BY columnId ORDER BY addTime DESC) AS RANK2, title,titleImg,titleURL,addTime,columnId from htmlPara ) T where RANK2<=10").Tables[0]; if (dt.Rows.Count < 1) { return(null); } foreach (DataRow row in dt.Rows) { htmlInfo hInfo = new htmlInfo(); hInfo.title = (string)row["title"]; hInfo.titleImg = (string)row["titleImg"]; hInfo.titleURL = (string)row["titleURL"]; hInfo.columnId = (string)row["columnId"];//栏目Id hInfo.addTime = row["addTime"].ToString(); //hInfo.realmNameId = (string)row["realmNameId"];//目录名 hList.Add(hInfo); } return(hList); }
public string Post(string strJson) { //需要做一个时间,每隔多长时间才允许访问一次 string keyValue = NetHelper.GetMD5("liu" + "100dh888"); string username = ""; string url = ""; try { JObject jo = (JObject)JsonConvert.DeserializeObject(strJson); string key = jo["key"].ToString(); if (key != keyValue) { return(json.WriteJson(0, "参数错误", new { })); } htmlInfo hInfo = new htmlInfo(); username = jo["username"].ToString(); hInfo.userId = bll.GetUserId(username);//用户名 hInfo.title = jo["title"].ToString(); string cid = jo["catid"].ToString(); if (string.IsNullOrEmpty(cid)) { return(json.WriteJson(0, "行业或栏目不能为空", new { })); } //命名规则:ip/目录/用户名/show_行业id+(五位数id) string showName = "show_" + cid + (bll.GetMaxId() + 1).ToString() + ".html"; url = host + "/" + username + "/" + showName; hInfo.titleURL = url; hInfo.articlecontent = HttpUtility.UrlDecode(jo["content"].ToString(), Encoding.UTF8); //内容,UrlDecode解码 hInfo.columnId = cid; //行业id,行业新闻id=23 hInfo.pinpai = jo["pinpai"].ToString(); hInfo.xinghao = jo["xinghao"].ToString(); hInfo.price = jo["price"].ToString(); hInfo.smallCount = jo["qiding"].ToString(); hInfo.sumCount = jo["gonghuo"].ToString(); hInfo.unit = jo["unit"].ToString(); hInfo.city = jo["city"].ToString(); hInfo.titleImg = jo["thumb"].ToString(); hInfo.addTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); hInfo.realmNameId = "1"; //发到哪个站 bll.AddHtml(hInfo); //存入数据库 //公司/会员信息 cmUserInfo uInfo = bll.GetUser(string.Format("where username='******'", username)); WriteFile(hInfo, uInfo, username, showName);//写模板 } catch (Exception ex) { return(json.WriteJson(0, ex.ToString(), new { })); } return(json.WriteJson(1, "发布成功", new { url, username })); }
/// <summary> /// 将html内容参数存入数据库 /// </summary> /// <param name="info"></param> public void AddHtml(htmlInfo info) { int a = SqlHelper.ExecuteNonQuery(@"INSERT INTO [AutouSend].[dbo].[htmlInfo] ([title] ,[titleURL] ,[articlecontent] ,[columnId] ,[pinpai] ,[xinghao] ,[price] ,[smallCount] ,[sumCount] ,[unit] ,[city] ,[titleImg] ,[addTime] ,[realmNameId] ,[userId]) VALUES (@title ,@titleURL ,@articlecontent ,@columnId ,@pinpai ,@xinghao ,@price ,@smallCount ,@sumCount ,@unit ,@city ,@titleImg ,@addTime ,@realmNameId ,@userId)", new SqlParameter("@title", SqlHelper.ToDBNull(info.title)), new SqlParameter("@titleURL", SqlHelper.ToDBNull(info.titleURL)), new SqlParameter("@articlecontent", SqlHelper.ToDBNull(info.articlecontent)), new SqlParameter("@columnId", SqlHelper.ToDBNull(info.columnId)), new SqlParameter("@pinpai", SqlHelper.ToDBNull(info.pinpai)), new SqlParameter("@xinghao", SqlHelper.ToDBNull(info.xinghao)), new SqlParameter("@price", SqlHelper.ToDBNull(info.price)), new SqlParameter("@smallCount", SqlHelper.ToDBNull(info.smallCount)), new SqlParameter("@sumCount", SqlHelper.ToDBNull(info.sumCount)), new SqlParameter("@unit", SqlHelper.ToDBNull(info.unit)), new SqlParameter("@city", SqlHelper.ToDBNull(info.city)), new SqlParameter("@titleImg", SqlHelper.ToDBNull(info.titleImg)), new SqlParameter("@addTime", SqlHelper.ToDBNull(info.addTime)), new SqlParameter("@realmNameId", SqlHelper.ToDBNull(info.realmNameId)), new SqlParameter("@userId", SqlHelper.ToDBNull(info.userId))); }