private void BuildView(string viewPath, XDocument doc) { ConvertCshtml _cs = new ConvertCshtml(_db); int addressId = _data.Id; int languageId = _languageId; var review = _cs.ReviewModel(addressId, languageId); review.JsSrc = @"/Areas/{0}/Views/_js/{1}-{2}.js".ToFormat(_data.AreaName, _data.ControllerName.ToLower(), _data.ActionName.ToLower()); review.CssHref = @"/Areas/{0}/Views/_css/{1}-{2}.css".ToFormat(_data.AreaName, _data.ControllerName.ToLower(), _data.ActionName.ToLower()); ContainConfigToCsproj(@"Areas\{0}\Views\_css\{1}-{2}.css".ToFormat(_data.AreaName, _data.ControllerName.ToLower(), _data.ActionName.ToLower()), _solutionDir, _projectName, doc); ContainConfigToCsproj(@"Areas\{0}\Views\_js\{1}-{2}.js".ToFormat(_data.AreaName, _data.ControllerName.ToLower(), _data.ActionName.ToLower()), _solutionDir, _projectName, doc); string jsPath = FileSugar.GetMapPath(review.JsSrc); string cssPath = FileSugar.GetMapPath(review.CssHref); if (FileSugar.IsExistFile(jsPath).IsFalse()) { FileSugar.CreateFile(jsPath); FileSugar.WriteFile(jsPath, "///<reference path=\"/_theme/tool/_reference.js\" />", "utf-8"); } if (FileSugar.IsExistFile(cssPath).IsFalse()) { FileSugar.CreateFile(cssPath); } var viewCode = GetTemplateCodeByName("view.cshtml", review); var languageSuffix = string.Empty; if (languageId != 1) { languageSuffix = $"_{GetLanguageList.Single(it => it.Id == languageId).Suffix}"; } FileSugar.WriteFile(viewPath, viewCode); ContainConfigToCsproj(@"Areas\{0}\Views\{1}\{2}{3}.cshtml".ToFormat(_data.AreaName, _data.ControllerName, _data.ActionName, languageSuffix), _solutionDir, _projectName, doc); }
public static void Filter(HttpContext context) { var localPath = context.Request.Url.LocalPath.ToLower(); var isViewsStaticFile = Regex.IsMatch(localPath, @"^/views/.*\.(js|css|jpg|png|gif|doc|docx|xls|xlsx|pdf|txt)$"); var isAreaViewsStaticFile = Regex.IsMatch(localPath, @"^/areas/\w+/views/.*\.(js|css|jpg|png|gif|doc|docx|xls|xlsx|pdf|txt)$"); if (isViewsStaticFile || isAreaViewsStaticFile) { string filePath = FileSugar.GetMapPath(localPath); var isExistFile = FileSugar.IsExistFile(filePath); if (isExistFile) { using (FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read)) { long fileSize = fileStream.Length; byte[] fileBuffer = new byte[fileSize]; fileStream.Read(fileBuffer, 0, (int)fileSize); //如果不写fileStream.Close()语句,用户在下载过程中选择取消,将不能再次下载 fileStream.Close(); var fileExtension = FileSugar.GetExtension(filePath); context.Response.ContentType = fileExtension.Switch().Case(".css", "text/css").Case(".js", "text/js").Default("application/octet-stream").Break(); context.Response.AppendHeader("Content-Disposition", "attachment;filename=" + FileSugar.GetFileName(filePath)); context.Response.AddHeader("Content-Length", fileSize.ToString()); context.Response.BinaryWrite(fileBuffer); context.Response.Flush(); context.Response.End(); } } } }
/// <summary> /// 发送邮件通知 /// </summary> /// <param name="isLogin"></param> /// <param name="_userInfo"></param> /// <param name="tid"></param> /// <param name="p"></param> /// <param name="db"></param> internal void SendMail(UserInfo currentUser, int tid, BBS_Posts p, SqlSugarClient db) { //未配置邮箱不发送 if (PubGet.GetEmailUserName.IsNullOrEmpty()) { return; } var topic = db.Queryable <BBS_Topics>().Single(it => it.Tid == tid); var isOneUser = currentUser.Id == topic.Posterid; var html = FileSugar.FileToString(FileSugar.GetMapPath("~/Template/mail/Replies.html")).Replace('\r', ' ').Replace('\n', ' '); var oldHtml = html; //发贴和回贴不是同一个人 if (isOneUser.IsFalse()) { var toUser = db.Queryable <UserInfo>().Single(it => it.Id == topic.Posterid); if (toUser.Email.IsEamil()) { string toUserName = toUser.NickName; string fromUserName = currentUser.NickName; string toMail = toUser.Email; MailSmtp ms = new MailSmtp(PubGet.GetEmailSmtp, PubGet.GetEmailUserName, PubGet.GetEmailPassword); string url = RequestInfo.HttpDomain + "/Ask/{0}/{1}#btnSubmit".ToFormat(topic.Fid, topic.Tid); html = html.ToFormat(toUserName, fromUserName, topic.Title, DateTime.Now, url); var title = PubMethod.RemoveAllSpace(fromUserName + "回复了:" + StringSugar.ToCutString(topic.Title, 10, "...")); ms.Send(PubGet.GetEmailUserName, PubConst.SiteMailUserName, toMail, title, html); System.Threading.Thread.Sleep(100); } } html = oldHtml; //处理@ if (p.Message.IsValuable() && p.Message.Contains("@")) { var adUserIds = db.Queryable <BBS_Posts>().Where(it => it.Tid == tid && it.Parentid > 0).Select(it => it.Posterid).ToList(); var adUsers = db.Queryable <UserInfo>().In(adUserIds).ToList(); var matchUsers = Regex.Matches(p.Message, @"\<span style\=""color:#4f99cf""\>@(.+?)\<\/span\>"); if (matchUsers != null && matchUsers.Count > 0) { var userNames = matchUsers.Cast <Match>().Select(it => it.Groups[1].Value).ToList(); adUsers = adUsers.Where(it => userNames.Contains(it.NickName)).ToList(); foreach (var item in adUsers) { if (item.Email.IsValuable() && item.Id != currentUser.Id) { string toUserName = item.NickName; string fromUserName = currentUser.NickName; string toMail = item.Email; MailSmtp ms = new MailSmtp(PubGet.GetEmailSmtp, PubGet.GetEmailUserName, PubGet.GetEmailPassword); string url = RequestInfo.HttpDomain + "/Ask/{0}/{1}#btnSubmit".ToFormat(topic.Fid, topic.Tid); html = html.ToFormat(toUserName, fromUserName, p.Message, DateTime.Now, url); var title = PubMethod.RemoveAllSpace(fromUserName + "在【" + topic.Title.TryToString().Trim() + "】@了你"); ms.Send(PubGet.GetEmailUserName, PubConst.SiteMailUserName, toMail, title, html); System.Threading.Thread.Sleep(100); } } } } }
public JsonResult ActivateMailSend(string key, string userId, string mail) //命名反的误导黑客 { Check.Exception(mail.IsEamil().IsFalse(), "参数不合法"); var userIdInt = EncryptSugar.GetInstance().Decrypto(key).ObjToInt(); var date = EncryptSugar.GetInstance().Decrypto(userId).ObjToDate(); var model = new ResultModel <string>(); if (base.IsLogin == false) { model.ResultInfo = "登录超时请刷新页面重新登录"; return(Json(model, JsonRequestBehavior.AllowGet)); } var cm = CacheManager <DateTime> .GetInstance(); string mailTimeKey = PubConst.SessionMailTime + key.ToLower(); if (cm.ContainsKey(mailTimeKey)) { var mins = (DateTime.Now - cm[mailTimeKey]).TotalSeconds; if (mins < 60) { model.ResultInfo = "您刚才已经发送成功,如果还没有收到邮件,请等待{0}秒后重新发送。".ToFormat(Convert.ToUInt32(60 - mins)); return(Json(model, JsonRequestBehavior.AllowGet)); } } _service.Command <HomeOutsourcing>((db, o) => { var isAnyUser = db.Queryable <UserInfo>().Any(it => userIdInt == it.Id); var isOkDate = ((DateTime.Now - date).TotalDays <= 3); var isAnyMail = db.Queryable <UserInfo>().Any(it => mail.ToLower() == it.Email); if (isAnyUser && isOkDate && isAnyMail.IsFalse()) { var html = FileSugar.FileToString(FileSugar.GetMapPath("~/Template/mail/Validate.html")).Replace('\r', ' ').Replace('\n', ' '); string userName = _userInfo.NickName; string aHtml = "<a href=\"{0}\">{1}</a>".ToFormat(RequestInfo.HttpDomain + "" + Url.Action("ActivateMailSubmitSuccess", "UserCenter", new { key = key, userId = userId, mail }), "请点击这儿完成激活"); string dateString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"); MailSmtp ms = new MailSmtp(PubGet.GetEmailSmtp, PubGet.GetEmailUserName, PubGet.GetEmailPassword); html = html.ToFormat(userName, aHtml, dateString); ms.Send(PubGet.GetEmailUserName, PubConst.SiteMailUserName, mail, userName + "邮箱激活通知", html); model.ResultInfo = "发送成功,请打开邮箱完成激活!"; string uniqueKey = PubGet.GetUserKey; base.AddUpdateMailCache(uniqueKey); model.IsSuccess = true; Check.Exception(ms.Result.IsValuable(), "邮件激活失败!" + ms.Result); cm.Add(mailTimeKey, DateTime.Now, cm.Minutes); } else if (isAnyMail) { model.ResultInfo = "发送失败,该邮箱已经被激活,或已经存在。"; } else { model.ResultInfo = "发送失败"; } }); return(Json(model, JsonRequestBehavior.AllowGet)); }
/// <summary> /// 将错误信息写入日志 /// </summary> /// <param name="ex"></param> private static void WriteExMessage(Exception ex) { var logPath = FileSugar.MergeUrl( FileSugar.GetMapPath("~/"), "log", DateTime.Now.ToString("yyyy-MM-dd.txt") ); FileSugar.AppendText(logPath, "***********{0}{1}***********".ToFormat("开始:", DateTime.Now)); FileSugar.AppendText(logPath, ex.Message); FileSugar.AppendText(logPath, "***********{0}***********\r\n".ToFormat("结束")); }
public JsonResult UploadAvatar() { ResultModel <string> model = new ResultModel <string>(); if (base.IsLogin.IsFalse()) { model.ResultInfo = "您还没有登录!"; return(Json(model, JsonRequestBehavior.AllowGet)); } try { _service.Command <UserCenterOutsourcing>((db, o) => { var imgObj = new UploadImage(); var file = Request.Files[0]; if (file.ContentLength / 1024.0 / 1024.0 > 1) { model.ResultInfo = "上传图片不能超过一M!"; } else if (file.ContentType.IsIn("image/png", "image/gif", "image/jpg", "image/jpeg").IsFalse()) { model.ResultInfo = "上传失败,您的图片不是PNG GIF JPG 格式!"; } else { model.IsSuccess = true; string webPath = "/_theme/img/avatar" + _userInfo.Id + ".jpg"; string savePath = FileSugar.GetMapPath("~" + webPath); string smallPath = FileSugar.GetMapPath("~/_theme/img/_avatar" + _userInfo.Id + ".jpg"); file.SaveAs(savePath); model.ResultInfo = Url.Content("~/" + webPath) + "?r=" + Guid.NewGuid(); imgObj.CreateSmallPhoto(savePath, 100, 100, smallPath);//生成缩略图 FileSugar.DeleteFile(savePath); FileSugar.Move(smallPath, savePath); db.Update <UserInfo>(new { avatar = webPath + "?r=" + RandomSugar.GetRandomNum(5) }, it => it.Id == _userInfo.Id); RestCurrentUserCache(); } }); return(Json(model)); } catch (Exception ex) { PubMethod.WirteExp(ex); model.ResultInfo = "上传失败!"; } return(Json(model, JsonRequestBehavior.AllowGet)); }
internal void SaveAvatar(SqlSugarClient db, UserInfo user) { //将远程图片下载到本地 if (user.Avatar.IsValuable() && user.Avatar.Contains("http://")) { string path = "/_theme/img/avatar{0}.jpg".ToFormat(user.Id); string savePath = FileSugar.GetMapPath("~" + path); WebClient my = new WebClient(); byte[] mybyte; mybyte = my.DownloadData(user.Avatar.Replace(@"/30", @"/100")); using (MemoryStream ms = new MemoryStream(mybyte)) { System.Drawing.Image img; img = System.Drawing.Image.FromStream(ms); img.Save(savePath, ImageFormat.Jpeg);//保存 } user.Avatar = path; db.Update <UserInfo>(new { Avatar = user.Avatar }, it => it.Id == user.Id); } }
/// <summary> /// 写入日志文件 /// </summary> /// <param name="ex"></param> public static void WirteExp(Exception ex) { try { var logPath = FileSugar.MergeUrl( FileSugar.GetMapPath("~/"), "log", DateTime.Now.ToString("yyyy-MM-dd") + ".txt" ); if (FileSugar.IsExistFile(logPath).IsFalse()) { FileSugar.CreateFile(logPath); } FileSugar.AppendText(logPath, "***********{0}{1}***********".ToFormat("开始:", DateTime.Now)); FileSugar.AppendText(logPath, ex.Message); FileSugar.AppendText(logPath, "***********{0}***********\r\n".ToFormat("结束")); } catch { } }
/// <summary> /// 站内信 /// </summary> /// <param name="currentUser"></param> /// <param name="tid"></param> /// <param name="p"></param> /// <param name="db"></param> internal void SendPMS(UserInfo currentUser, int tid, BBS_Posts p, SqlSugarClient db) { var topic = db.Queryable <BBS_Topics>().Single(it => it.Tid == tid); var isOneUser = currentUser.Id == topic.Posterid; var html = FileSugar.FileToString(FileSugar.GetMapPath("~/Template/mail/PMS.html")).Replace('\r', ' ').Replace('\n', ' '); var oldHtml = html; //发贴和回贴不是同一个人 if (isOneUser.IsFalse()) { var toUser = db.Queryable <UserInfo>().Single(it => it.Id == topic.Posterid); string toUserName = toUser.NickName; string fromUserName = currentUser.NickName; string url = RequestInfo.HttpDomain + "/Ask/{0}/{1}#btnSubmit".ToFormat(topic.Fid, topic.Tid); html = html.ToFormat(toUserName, fromUserName, topic.Title, url); var title = PubMethod.RemoveAllSpace(fromUserName + "回复了:" + StringSugar.ToCutString(topic.Title, 10, "...")); db.Insert <BBS_PMS>(new BBS_PMS() { Message = html, Subject = title, Msgfrom = fromUserName, Msgfromid = currentUser.Id, Msgto = toUserName, Msgtoid = toUser.Id, New = 1, Postdatetime = DateTime.Now }); } html = oldHtml; //处理@ if (p.Message.IsValuable() && p.Message.Contains("@")) { var adUserIds = db.Queryable <BBS_Posts>().Where(it => it.Tid == tid && it.Parentid > 0).Select(it => it.Posterid).ToList(); var adUsers = db.Queryable <UserInfo>().In(adUserIds).ToList(); var matchUsers = Regex.Matches(p.Message, @"\<span style\=""color:#4f99cf""\>@(.+?)\<\/span\>"); if (matchUsers != null && matchUsers.Count > 0) { var userNames = matchUsers.Cast <Match>().Select(it => it.Groups[1].Value).ToList(); adUsers = adUsers.Where(it => userNames.Contains(it.NickName)).ToList(); foreach (var item in adUsers) { if (item.Email.IsValuable() && item.Id != currentUser.Id) { string toUserName = item.NickName; string fromUserName = currentUser.NickName; string url = RequestInfo.HttpDomain + "/Ask/{0}/{1}#btnSubmit".ToFormat(topic.Fid, topic.Tid); html = html.ToFormat(toUserName, fromUserName, p.Message, url); var title = PubMethod.RemoveAllSpace(fromUserName + "在【" + topic.Title.TryToString().Trim() + "】@了你"); db.Insert <BBS_PMS>(new BBS_PMS() { Message = html, Subject = title, Msgfrom = fromUserName, Msgfromid = currentUser.Id, Msgto = toUserName, Msgtoid = item.Id, New = 1, Postdatetime = DateTime.Now }); } } } } }