BasePage.cs 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  1. using SiteCore.Redis;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Reflection;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Web;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using Utils;
  11. namespace SiteCore
  12. {
  13. public class BasePage : Page
  14. {
  15. public string _t = "";
  16. public bool _checkRight = true;
  17. public int _pageSize = 30;
  18. public bool _addVisible = false;
  19. public int _selfAddVisible = 0;
  20. public bool _refreshVisible = true;
  21. public bool _choiceVisible = false;
  22. public bool _selfCheckPermission = false;
  23. public DateTime _filterDate;
  24. public string _searchFilter = "";
  25. public string _searchDate = "";
  26. public string _filterItem = "";
  27. public bool _filterDateBtn = true; //本日本周本月的按钮显示
  28. public string _b_sDate = "";
  29. public string _b_eDate = "";
  30. public bool _b_HavingTime = false;
  31. public bool _hasExport = false;
  32. public string gcTag = "";
  33. public string _repTitle = "";
  34. public SearchDateEnum _searchDateEnum = SearchDateEnum.Empty;
  35. public bool _noneDefaultTime = false;
  36. public bool onlyEndTime = false;
  37. public bool _keyFilterVisible = false;
  38. public bool _isCheckLogin = true;
  39. public static readonly string _btnStr = "<a class=\"mini-button\" iconCls=\"icon-{0}\" plain=\"true\" href=\"javascript:{0}_0\" >{1}</a>";
  40. public List<string> _permissions = new List<string>();//权限
  41. //public string _action=string.Empty;
  42. public static readonly string SiteVer = webConfig.SiteVer;
  43. public static readonly string SiteName = webConfig.SiteName;
  44. public static readonly string SiteDomain = webConfig.SiteDomain;
  45. public static readonly string CookieName = webConfig.CookieName;
  46. public static readonly string SiteUrl = webConfig.SiteUrl;
  47. public string dis_stime = "00:00:00";///webConfig.DisStartTime;
  48. public string dis_etime = "23:59:59";// webConfig.DisEndTime;
  49. #region 账户相关
  50. public string Round(double d, int n = 2)
  51. {
  52. return d.ToString("F" + n);
  53. }
  54. public string Round(decimal d, int n = 2)
  55. {
  56. return d.ToString("F" + n);
  57. }
  58. public decimal RoundDecimal(decimal d, int n = 2)
  59. {
  60. return Convert.ToDecimal(Round(d, n));
  61. }
  62. public double RoundDouble(double d, int n = 2)
  63. {
  64. return Convert.ToDouble(Round(d, n));
  65. }
  66. private WebUser _currentUser = null;
  67. //获取用户数据
  68. public WebUser CurrentUser
  69. {
  70. get
  71. {
  72. if (_currentUser == null)
  73. {
  74. _currentUser = WebUser.GetUser();
  75. }
  76. return _currentUser;
  77. }
  78. }
  79. public int UserID
  80. {
  81. get
  82. {
  83. if (CurrentUser == null) return 0;
  84. return Convert.ToInt32(CurrentUser.UserID);
  85. }
  86. }
  87. public string PKey
  88. {
  89. get
  90. {
  91. if (CurrentUser == null) return "";
  92. if (CurrentUser.User.State == 5) return "admin";
  93. return CurrentUser.UserPost.PostID.ToString();
  94. }
  95. }
  96. protected override void OnPreLoad(EventArgs e)
  97. {
  98. if (isLoginOut())
  99. {
  100. string sPath = CommonHelper.GetPrePath();
  101. Response.Write("<script type=\"text/javascript\">alert('会话超时过期,请重新登录。');window.top.location='" + sPath + "Login.aspx" + "';</script>");
  102. Response.End();
  103. return;
  104. }
  105. //_t = Session["_t"].ToString();
  106. base.OnPreLoad(e);
  107. }
  108. public bool isLoginOut()
  109. {
  110. //人员是否已登录
  111. //if (Session["userId"] == null || Session["userId"].ToString() == "")
  112. // return true;
  113. //传过来的Ticket是否有效
  114. HttpCookie lcCookie = HttpContext.Current.Request.Cookies[webConfig.CookieName];
  115. if (lcCookie == null)
  116. {
  117. return true;
  118. }
  119. string name = lcCookie.Values["User"];
  120. string ticket = lcCookie.Values["Ticket"];
  121. if (name == null || ticket == null || name == "" || ticket == "")
  122. {
  123. return true;
  124. }
  125. name = HttpUtility.UrlDecode(name);
  126. string dl_ticket = erpRedis.RedisHelper.StringGet("dl_" + name);
  127. //if (String.IsNullOrEmpty(dl_ticket))
  128. //{
  129. // erpRedis.RedisHelper.StringSet("dl_" + name, ticket);
  130. // return false;
  131. //}
  132. if (ticket != dl_ticket)
  133. {
  134. return true;
  135. }
  136. return false;
  137. }
  138. /// <summary>
  139. /// 检测是否登录
  140. /// </summary>
  141. public bool CheckRights()
  142. {
  143. //if (Session["xyUserID"] == null)
  144. //{
  145. // Session["xyUserID"] = "1";
  146. // Session["xyUserName"] = "ljq";
  147. //}
  148. if (!_isCheckLogin) return true;
  149. if (CurrentUser == null)
  150. {
  151. string sPath = CommonHelper.GetPrePath();
  152. Response.Write("<script type=\"text/javascript\">window.top.location='" + sPath + "Login.aspx" + "';</script>");
  153. Response.End();
  154. return false;
  155. }
  156. return true;
  157. }
  158. public bool CheckRightSession()
  159. {
  160. if (Session["cur_user"] == null)
  161. {
  162. string sPath = CommonHelper.GetPrePath();
  163. Response.Write("<script type=\"text/javascript\">window.top.location='" + sPath + "Login.aspx" + "';</script>");
  164. Response.End();
  165. return false;
  166. }
  167. return true;
  168. }
  169. //权限标签
  170. public string PmTag = "";
  171. public string GetPersonName(object pName)
  172. {
  173. if (pName.ToString() == "") return "";
  174. return pName.ToString();
  175. }
  176. public StringBuilder _script = new StringBuilder();
  177. public void AppendScript(String str)
  178. {
  179. _script.Append(str);
  180. }
  181. public object GetReflectionObject(string className)
  182. {
  183. if (string.IsNullOrEmpty(className)) return null;
  184. string tmpName = "BizCom." + className;
  185. return Assembly.Load("BizCom").CreateInstance(tmpName);
  186. }
  187. #endregion
  188. //显示错误页面
  189. public void ShowErrorPage()
  190. {
  191. Response.Redirect(CommonHelper.GetPrePath() + "ErrorPage.htm", true);
  192. }
  193. public void ShowErrorHtml(string url)
  194. {
  195. Response.Write("你访问的页面错误啦!<a href='" + url + "'>点击这里返回</a>");
  196. Response.End();
  197. }
  198. public void ShowErrorHtml(string txt, string url)
  199. {
  200. Response.Write(txt + "!<a href='" + url + "'>点击这里返回</a>");
  201. Response.End();
  202. }
  203. public void ShowError(string txt)
  204. {
  205. Response.Write(txt);
  206. Response.End();
  207. }
  208. public int GetDecodeInt(string param)
  209. {
  210. string value = GetString(param);
  211. if (value.Length < 1) return 0;
  212. return CommonHelper.DecodeInt(value);
  213. }
  214. public string GetDecodeString(string param)
  215. {
  216. string value = GetString(param);
  217. if (value.Length < 1) return "";
  218. return CommonHelper.DecodeString(value);
  219. }
  220. public void SelItemSelected(DropDownList ddl, object value)
  221. {
  222. ddl.ClearSelection();
  223. System.Web.UI.WebControls.ListItem item = ddl.Items.FindByValue(value.ToString());
  224. if (item != null) item.Selected = true;
  225. }
  226. public void SelRadioItemSelected(RadioButtonList rbl, object value)
  227. {
  228. rbl.ClearSelection();
  229. System.Web.UI.WebControls.ListItem item = rbl.Items.FindByValue(value.ToString());
  230. if (item != null) item.Selected = true;
  231. }
  232. public string GDis_STime
  233. {
  234. get
  235. {
  236. string dm = DateTime.Now.ToString("yyyyMM");
  237. if (Convert.ToInt32(dm) >= 202009) return dis_stime;
  238. return "00:00:00";
  239. }
  240. }
  241. public string GDis_ETime
  242. {
  243. get
  244. {
  245. string dm = DateTime.Now.ToString("yyyyMM");
  246. if (Convert.ToInt32(dm) >= 202009) return dis_etime;
  247. return "23:59:59";
  248. }
  249. }
  250. public string getTodTime(DateTime sTime)
  251. {
  252. return getTodTime(sTime.ToString("yyyy-MM-dd"));
  253. }
  254. public string getTodTime(string stime)
  255. {
  256. DisTime dTime = redis.RedisHelper.HashGet<DisTime>("distime", stime);
  257. if (dTime != null && dTime.stime != "") return dTime.stime;
  258. return stime + " " + GDis_STime;
  259. //string st = redis.RedisHelper.StringGet("dis_" + stime);
  260. //if (string.IsNullOrEmpty(st)) return stime + " " + dis_stime;
  261. //return st;
  262. }
  263. public string getTormTime(DateTime eTime)
  264. {
  265. return getTormTime(eTime.ToString("yyyy-MM-dd"));
  266. }
  267. public string getTormTime(string etime)
  268. {
  269. DisTime dTime = redis.RedisHelper.HashGet<DisTime>("distime", etime);
  270. //string et = redis.RedisHelper.StringGet("die_" + etime);
  271. if (dTime == null || dTime.etime == "")
  272. {
  273. if (dis_etime.IndexOf("-") != -1)
  274. {
  275. string detime = dis_etime.Replace("-", "");
  276. DateTime _ctime = Convert.ToDateTime(etime);
  277. return _ctime.AddDays(1).ToString("yyyy-MM-dd") + " " + detime;
  278. }
  279. return etime + " " + GDis_ETime;
  280. }
  281. return dTime.etime;
  282. }
  283. public void getTTime(string stime, string etime, out string tstime, out string tetime)
  284. {
  285. DisTime sdTime = null;
  286. DisTime edTime = null;
  287. if (stime == etime)
  288. {
  289. sdTime = redis.RedisHelper.HashGet<DisTime>("distime", etime);
  290. edTime = sdTime;
  291. }
  292. else
  293. {
  294. sdTime = redis.RedisHelper.HashGet<DisTime>("distime", stime);
  295. edTime = redis.RedisHelper.HashGet<DisTime>("distime", etime);
  296. }
  297. if (sdTime == null)
  298. {
  299. tstime = stime + " " + GDis_STime;
  300. }
  301. else
  302. {
  303. if (sdTime.stime != "") tstime = sdTime.stime;
  304. else tstime = stime + " " + GDis_STime;
  305. }
  306. if (edTime == null)
  307. {
  308. if (dis_etime.IndexOf("-") != -1)
  309. {
  310. string detime = dis_etime.Replace("-", "");
  311. DateTime _ctime = Convert.ToDateTime(etime);
  312. tetime = _ctime.AddDays(1).ToString("yyyy-MM-dd") + " " + detime;
  313. }
  314. else
  315. {
  316. tetime = etime + " " + GDis_ETime;
  317. }
  318. }
  319. else
  320. {
  321. if (string.IsNullOrEmpty(edTime.etime))
  322. {
  323. if (dis_etime.IndexOf("-") != -1)
  324. {
  325. string detime = dis_etime.Replace("-", "");
  326. DateTime _ctime = Convert.ToDateTime(etime);
  327. tetime = _ctime.AddDays(1).ToString("yyyy-MM-dd") + " " + detime;
  328. }
  329. else
  330. {
  331. tetime = etime + " " + GDis_ETime;
  332. }
  333. }
  334. else tetime = edTime.etime;
  335. }
  336. }
  337. public string getDefDateWhere(string sdate, string edate)
  338. {
  339. string _stime = ""; //getTodTime(edate);
  340. string _etime = "";// getTormTime(edate);
  341. if (sdate == "" && edate != "")
  342. {
  343. getTTime(edate, edate, out _stime, out _etime);
  344. return "(datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0)";
  345. }
  346. if (sdate != "" && edate != "")
  347. {
  348. getTTime(sdate, edate, out _stime, out _etime);
  349. return "datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0)";
  350. }
  351. return "";
  352. }
  353. public string getDispatchDateWhere(string sdate, string edate)
  354. {
  355. string _stime = ""; //getTodTime(edate);
  356. string _etime = "";// getTormTime(edate);
  357. if (sdate == "" && edate != "")
  358. {
  359. getTTime(edate, edate, out _stime, out _etime);
  360. return "(datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0)";
  361. }
  362. if (sdate != "" && edate != "")
  363. {
  364. getTTime(sdate, edate, out _stime, out _etime);
  365. return "(datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0)";
  366. }
  367. return "";
  368. }
  369. public string getDispatchDateWhere(string field, string sdate, string edate)
  370. {
  371. string _stime = ""; //getTodTime(edate);
  372. string _etime = "";// getTormTime(edate);
  373. if (sdate == "" && edate != "")
  374. {
  375. getTTime(edate, edate, out _stime, out _etime);
  376. return "(datediff(d,'" + _stime + "'," + field + ")>=0 and datediff(s,'" + _stime + "'," + field + ")>=0 and datediff(s,'" + _etime + "'," + field + ")<=0)";
  377. }
  378. if (sdate != "" && edate != "")
  379. {
  380. getTTime(sdate, edate, out _stime, out _etime);
  381. return "(datediff(d,'" + _stime + "'," + field + ")>=0 and datediff(s,'" + _stime + "'," + field + ")>=0 and datediff(s,'" + _etime + "'," + field + ")<=0)";
  382. }
  383. return "";
  384. }
  385. public string getDispatchDateWhere(DateTime etime)
  386. {
  387. if (Convert.ToInt32(etime.ToString("HHmmss")) < 60000)
  388. {
  389. etime = etime.AddDays(-1);
  390. }
  391. string _stime = getTodTime(etime.ToString("yyyy-MM-dd"));
  392. string _etime = getTormTime(etime.ToString("yyyy-MM-dd"));
  393. return " datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0";
  394. }
  395. public string getDispatchDateNoTimeWhere(DateTime etime)
  396. {
  397. string _stime = getTodTime(etime);
  398. string _etime = getTormTime(etime);
  399. return " datediff(d,'" + _stime + "',createtime)>=0 and datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0";
  400. }
  401. public string getProductionTaskWhere(string stime, string etime)
  402. {
  403. return "productiontaskid in (select distinct productiontaskid from ce_erpdispatchtask where " + getSaleDispatchDateWhere(stime, etime) + " and ispatch=0 )";
  404. }
  405. public string getSaleDispatchDateWhere(string stime, string etime)
  406. {
  407. return getDispatchDateWhere(stime, etime);
  408. }
  409. //public string getSaleDispatchDateNoTimeWhere(DateTime etime)
  410. //{
  411. // string _stime = etime.ToString("yyyy-MM-dd") + " 00:00:00";
  412. // string _etime = etime.ToString("yyyy-MM-dd") + " 23:59:59";
  413. // return " datediff(s,'" + _stime + "',createtime)>=0 and datediff(s,'" + _etime + "',createtime)<=0";
  414. //}
  415. //private void setDisTimeRedis2(string sdate,string edate, out string _stime, out string _etime)
  416. //{
  417. // _stime = "";
  418. // _etime = "";
  419. // string dkey = Convert.ToDateTime(sdate).ToString("yyyy-MM-dd");
  420. // string dStr = redis.RedisHelper.StringGet("dis_" + dkey);
  421. // if (!String.IsNullOrEmpty(dStr))
  422. // {
  423. // string[] dArr = dStr.Split('|');
  424. // _stime = dArr[0];
  425. // }
  426. // else
  427. // {
  428. // _stime = getTodTime(sdate);
  429. // }
  430. // dkey = Convert.ToDateTime(edate).ToString("yyyy-MM-dd");
  431. // dStr = redis.RedisHelper.StringGet("dis_" + dkey);
  432. // if (!string.IsNullOrEmpty(dStr))
  433. // {
  434. // string[] dArr = dStr.Split('|');
  435. // _etime = dArr[1];
  436. // }
  437. // if (_etime == "") _etime = getTodTime(edate);
  438. //}
  439. //private void setDisTimeRedis(string date,out string _stime,out string _etime)
  440. //{
  441. // string dkey = Convert.ToDateTime(date).ToString("yyyy-MM-dd");
  442. // string dStr = redis.RedisHelper.StringGet("dis_" + dkey);
  443. // if (!String.IsNullOrEmpty(dStr))
  444. // {
  445. // string[] dArr = dStr.Split('|');
  446. // _stime = dArr[0];
  447. // _etime = dArr[1];
  448. // }
  449. // else
  450. // {
  451. // _stime = getTodTime(date);
  452. // _etime = getTormTime(date);
  453. // }
  454. //}
  455. #region 格式化显示方式
  456. /// <summary>
  457. /// 返回是否过期
  458. /// </summary>
  459. /// <param name="endTime"></param>
  460. /// <returns></returns>
  461. public static bool CheckIsExpire(object endTime)
  462. {
  463. return DateTime.Now.Subtract(Convert.ToDateTime(endTime)).Days > 0;
  464. }
  465. /// <summary>
  466. /// 布尔值显示文字
  467. /// </summary>
  468. /// <param name="result"></param>
  469. /// <param name="tStr"></param>
  470. /// <param name="fStr"></param>
  471. /// <returns></returns>
  472. public string GetBoolenString(object result, string tStr, string fStr)
  473. {
  474. if (result.Equals(DBNull.Value)) return "";
  475. return Convert.ToBoolean(result) ? tStr : fStr;
  476. }
  477. /// <summary>
  478. /// 图片显示
  479. /// </summary>
  480. /// <param name="imgUrl"></param>
  481. /// <returns></returns>
  482. public string GetImgString(object imgUrl)
  483. {
  484. return GetImgString(imgUrl, "");
  485. }
  486. public string GetImgString(object imgUrl, string noPic)
  487. {
  488. string str = "<img alt='' class=\"g_img\" align=\"absmiddle\" src=\"{0}\" />";
  489. string img = noPic == "" ? "images/noImg.gif" : "images/" + noPic;
  490. if (imgUrl.ToString() != "") img = imgUrl.ToString().Replace("images/", "images/i2/");
  491. return string.Format(str, "../../" + img);
  492. }
  493. public string GetMerchantImgString(object imgUrl, string noPic)
  494. {
  495. string str = "<img alt='' class=\"g_img\" align=\"absmiddle\" src=\"{0}\" />";
  496. string img = noPic == "" ? "images/noImg.gif" : "images/" + noPic;
  497. if (imgUrl.ToString() != "") img = "Documents/" + imgUrl;
  498. return string.Format(str, "../../" + img);
  499. }
  500. public string GetSiteImgString(object imgUrl, object title)
  501. {
  502. string str = "<img alt=\"{1}\" title\"{1}\" class=\"g_img\" align=\"absmiddle\" src=\"{0}\" />";
  503. string img = "";
  504. if (imgUrl.ToString() != "") img = "Documents/" + imgUrl;
  505. return string.Format(str, "../../" + img, title);
  506. }
  507. public string GetCurrentImgString(object imgUrl, string noPic)
  508. {
  509. string img = noPic == "" ? "images/noImg.gif" : "images/" + noPic;
  510. if (imgUrl.ToString() != "") img = "Documents/" + imgUrl;
  511. return img;
  512. }
  513. /// <summary>
  514. /// 格式化日期
  515. /// </summary>
  516. /// <param name="date"></param>
  517. /// <returns></returns>
  518. public string GetFormatDate(object date)
  519. {
  520. return GetFormatDate(date, "无");
  521. }
  522. public string GetFormatDate(object date, string def)
  523. {
  524. if (date == null) return def;
  525. return date.ToString() == "" ? def : Convert.ToDateTime(date).ToString("yyyy-MM-dd");
  526. }
  527. /// <summary>
  528. /// 过期日期
  529. /// </summary>
  530. /// <param name="sDate"></param>
  531. /// <param name="eDate"></param>
  532. /// <returns></returns>
  533. public string GetFormatExpireDate(object sDate, object eDate)
  534. {
  535. return GetFormatExpireDate(sDate, eDate, "无");
  536. }
  537. public string GetFormatExpireDate(object sDate, object eDate, string defaultTxt)
  538. {
  539. if (sDate.ToString() == "" && eDate.ToString() == "")
  540. {
  541. return defaultTxt;
  542. }
  543. return "<span style=\"color:green\">" + GetFormatDate(sDate) + "<span> 至 <span style=\"color:blue\">" + GetFormatDate(eDate) + "</span>";
  544. }
  545. /// <summary>
  546. /// 格式日期带时间
  547. /// </summary>
  548. /// <param name="date"></param>
  549. /// <returns></returns>
  550. public string GetFormatDateTime(object date)
  551. {
  552. if (date == null || date.Equals(DBNull.Value)) return "";
  553. return date.ToString() == "" ? "" : Convert.ToDateTime(date).ToString("yyyy-MM-dd HH:mm");
  554. }
  555. public string GetCurrentDateTime(object date)
  556. {
  557. if (date.Equals(DBNull.Value) || date.ToString() == "") return "";
  558. DateTime dt1 = DateTime.Now;
  559. DateTime dt2 = Convert.ToDateTime(date);
  560. TimeSpan span = dt1.Subtract(dt2);
  561. if (span.TotalDays > 5)
  562. {
  563. return dt2.ToString("yyyy-MM-dd HH:mm");
  564. }/*
  565. else if (span.TotalDays > 30)
  566. {
  567. return "1个月前";
  568. }
  569. else if (span.TotalDays > 14)
  570. {
  571. return "2周前";
  572. }
  573. else if (span.TotalDays > 7)
  574. {
  575. return "1周前";
  576. }*/
  577. else if (span.TotalDays > 1)
  578. {
  579. return string.Format("{0}天前", (int)Math.Floor(span.TotalDays));
  580. }
  581. else if (span.TotalHours > 1)
  582. {
  583. return string.Format("{0}小时前", (int)Math.Floor(span.TotalHours));
  584. }
  585. else if (span.TotalMinutes > 1)
  586. {
  587. return string.Format("{0}分钟前", (int)Math.Floor(span.TotalMinutes));
  588. }
  589. else if (span.TotalSeconds >= 1)
  590. {
  591. return string.Format("{0}秒前", (int)Math.Floor(span.TotalSeconds));
  592. }
  593. else
  594. {
  595. return "刚刚";
  596. }
  597. }
  598. /// <summary>
  599. /// 格式化时间
  600. /// </summary>
  601. /// <param name="sTime"></param>
  602. /// <param name="eTime"></param>
  603. /// <returns></returns>
  604. public string GetFormatTime(object sTime, object eTime)
  605. {
  606. string tmp = sTime.ToString();
  607. string firstTime = "", endTime = "";
  608. if (tmp == "" || tmp == "-1") return "全天";
  609. if (tmp == "0") firstTime = "00:00";
  610. else if (tmp == "30") firstTime = "00:30";
  611. else firstTime = tmp.Substring(0, tmp.Length - 2).PadLeft(2, '0') + ":" + tmp.Substring(tmp.Length - 2);
  612. tmp = eTime.ToString();
  613. if (tmp == "0") endTime = "00:00";
  614. else if (tmp == "30") endTime = "00:30";
  615. else endTime = tmp.Substring(0, tmp.Length - 2).PadLeft(2, '0') + ":" + tmp.Substring(tmp.Length - 2);
  616. return firstTime + " ~ " + endTime;
  617. }
  618. public void SetDateWhere(string field, string start, string end, IList<string> where)
  619. {
  620. if (start != "" || end != "")
  621. {
  622. if (start != "" && end != "")
  623. where.Add(string.Format(" (DATEDIFF(d,'{0}',{2})>=0 and DATEDIFF(d,'{1}',{2})<=0 )", start, end, field));
  624. else if (start != "")
  625. where.Add(string.Format(" {1}>='{0}'", start, field));
  626. else
  627. where.Add(string.Format(" dateDiff(d,{1},'{0}')=0 ", end, field));
  628. }
  629. }
  630. public void SetDateWhereB(string field, string start, string end, IList<string> where)
  631. {
  632. if (start != "" || end != "")
  633. {
  634. if (start != "" && end != "")
  635. {
  636. if (start == end) where.Add(string.Format(" dateDiff(d,{1},'{0}')=0 ", end, field));
  637. else where.Add(string.Format(" (DATEDIFF(s,'{0}',{2})>=0 and DATEDIFF(s,'{1}',{2})<=0 )", start, end, field));//where.Add(string.Format(" ({2} Between '{0}' and '{1}' )", start, end, field));
  638. }
  639. else if (start != "")
  640. where.Add(string.Format(" {1}>='{0}'", start, field));
  641. else
  642. where.Add(string.Format(" dateDiff(d,{1},'{0}')=0 ", end, field));
  643. }
  644. }
  645. public string GetDateWhere(string field, string start, string end)
  646. {
  647. if (start != "" || end != "")
  648. {
  649. if (start != "" && end != "")
  650. return string.Format(" (DATEDIFF(d,'{0}',{2})>=0 and DATEDIFF(d,'{1}',{2})<=0 )", start, end, field);
  651. else if (start != "")
  652. return string.Format(" {1}>='{0}'", start, field);
  653. else
  654. return string.Format(" dateDiff(d,{1},'{0}')=0 ", end, field);
  655. }
  656. return "";
  657. }
  658. /// <summary>
  659. /// 获取精度数据
  660. /// </summary>
  661. /// <param name="value"></param>
  662. /// <returns></returns>
  663. public string GetDoubleString(object value)
  664. {
  665. if (string.IsNullOrEmpty(value.ToString())) return "";
  666. string db = Convert.ToDouble(value).ToString("F2");
  667. return db.Replace(".00", "");
  668. }
  669. public string GetDoubleString(object value, int num)
  670. {
  671. if (string.IsNullOrEmpty(value.ToString())) return "0";
  672. return Convert.ToDouble(value).ToString("N" + num);
  673. }
  674. public string GetDoubleString(object value, string txt)
  675. {
  676. if (string.IsNullOrEmpty(value.ToString())) return txt;
  677. if (Convert.ToDouble(value) < 1) return txt;
  678. else return GetDoubleString(value);
  679. }
  680. //获取还剩几天
  681. public string GetDayByDate(object date)
  682. {
  683. DateTime curDt = DateTime.Now;
  684. if (date.ToString() == "") return "己过期";
  685. DateTime exDt = Convert.ToDateTime(date);
  686. TimeSpan tdays = exDt - curDt;
  687. if (tdays.Days < 0) return "己过期";
  688. else if (tdays.Days == 0) return "今天";
  689. return tdays.Days + "天";
  690. }
  691. /// <summary>
  692. /// 获取带有空值的字符串
  693. /// </summary>
  694. /// <param name="result">data</param>
  695. /// <param name="nullStr">为空时显示的内容</param>
  696. /// <returns></returns>
  697. public string GetWithEmptyString(object result, string nullStr)
  698. {
  699. if (result == null) return nullStr;
  700. return result.ToString().Length > 0 ? result.ToString() : nullStr;
  701. }
  702. /// <summary>
  703. /// 获取两个数字相差的数量
  704. /// </summary>
  705. /// <param name="aNum"></param>
  706. /// <param name="num"></param>
  707. /// <returns></returns>
  708. public object GetMinuteNum(object aNum, object num)
  709. {
  710. if (aNum.ToString() == "" || num.ToString() == "") return 0;
  711. return Convert.ToInt32(aNum) - Convert.ToInt32(num);
  712. }
  713. public double GetDoubleMinuteNum(object aNum, object num)
  714. {
  715. return Convert.ToDouble(aNum) - Convert.ToDouble(num);
  716. }
  717. public string GetDoubleMinuteString(object aNum, object num)
  718. {
  719. return GetDoubleString(Convert.ToDouble(aNum) - Convert.ToDouble(num));
  720. }
  721. #endregion
  722. #region 页面方法
  723. //设置过期
  724. public void SetPageExpires()
  725. {
  726. Response.Buffer = true;
  727. Response.ExpiresAbsolute = DateTime.Now.AddSeconds(-1);
  728. Response.Expires = 0;
  729. Response.AddHeader("pragma", "no-cache");
  730. Response.AddHeader("cache-control", "private");
  731. Response.CacheControl = "no-cache";
  732. }
  733. #endregion
  734. #region URL参数
  735. /// <summary>
  736. /// 获取当前Url带完整参数
  737. /// </summary>
  738. /// <param name="url"></param>
  739. /// <param name="parm"></param>
  740. /// <param name="nshow">不加的参数</param>
  741. /// <returns></returns>
  742. public string GetCurrentUrl(string url, string parm, string nshow)
  743. {
  744. string query = HttpContext.Current.Request.Url.Query;
  745. StringBuilder qStr = new StringBuilder();
  746. qStr.Append(url + "?");
  747. if (query != "")
  748. {
  749. query = query.Substring(1);
  750. string key = parm.Split('=')[0];
  751. string[] qArr = query.Split('&');
  752. for (int i = 0; i < qArr.Length; i++)
  753. {
  754. if (qArr[i].Split('=')[0] != key && qArr[i].Split('=')[0] != nshow)
  755. qStr.Append(qArr[i] + "&");
  756. }
  757. }
  758. qStr.Append(parm);
  759. return qStr.ToString();
  760. }
  761. public string GetCurrentUrl(string parm, string nshow)
  762. {
  763. return GetCurrentUrl("", parm, nshow);
  764. }
  765. /// <summary>
  766. /// 获取当前Url带完整参数
  767. /// </summary>
  768. /// <param name="parm"></param>
  769. /// <returns></returns>
  770. public string GetCurrentUrl(string parm)
  771. {
  772. return GetCurrentUrl("", parm, "");
  773. }
  774. /// <summary>
  775. /// 检测URL参数是否存在
  776. /// </summary>
  777. /// <param name="parms"></param>
  778. /// <returns></returns>
  779. public bool UrlParmsCheck(string parms)
  780. {
  781. return CommonHelper.UrlParmsCheck(parms);
  782. }
  783. public bool UrlPostParmsCheck(string parms)
  784. {
  785. return CommonHelper.UrlPostParmsCheck(parms);
  786. }
  787. public int GetInt(string parm)
  788. {
  789. return CommonHelper.GetInt(parm);
  790. }
  791. public int GetPostInt(string param)
  792. {
  793. return CommonHelper.GetPostInt(param);
  794. }
  795. public double GetDouble(string param)
  796. {
  797. string v = GetString(param);
  798. if (v == "") return 0;
  799. return Convert.ToDouble(v);
  800. }
  801. public double GetPostDouble(string param)
  802. {
  803. string v = GetPostString(param);
  804. if (v == "") return 0;
  805. return Convert.ToDouble(v);
  806. }
  807. public static String cleanHtml(String htmlText)
  808. {
  809. if (htmlText == null)
  810. return null;
  811. htmlText = htmlText.Trim();
  812. if (htmlText.Length == 0)
  813. return htmlText;
  814. string[] regex = { "expression\\((.*?)\\)", "eval\\((.*?)\\)", "<script[^>]*?>[\\s\\S]*?</script>", "<style[^>]*?>[\\s\\S]*?</style>",
  815. "<!--.*?-->", "<script[^>]*?>", "<!--", "javascript:", "vbscript:","onload(.*?)=" };
  816. for (int i = 0; i < regex.Length; i++)
  817. {
  818. htmlText = Regex.Replace(htmlText, regex[i], "", RegexOptions.IgnoreCase);
  819. }
  820. htmlText = htmlText.Replace("--", "- -").Replace("'", "'").Replace("/*", "/ *");//.Replace("(", "(").Replace(")", ")");
  821. return htmlText;
  822. }
  823. public string GetString(string param)
  824. {
  825. string text = CommonHelper.GetString(param);
  826. text = cleanHtml(text);
  827. return text;
  828. }
  829. public string GetPostString(string param)
  830. {
  831. string text = CommonHelper.GetPostString(param).Trim();
  832. text = cleanHtml(text);
  833. return text;
  834. }
  835. public string GetDeepString(params string[] parms)
  836. {
  837. string text = CommonHelper.GetDeepString(parms);
  838. text = cleanHtml(text);
  839. return text;
  840. }
  841. public string GetHttpDecodeString(string parm)
  842. {
  843. string text = HttpUtility.UrlDecode(GetString(parm));
  844. text = cleanHtml(text);
  845. return text;
  846. }
  847. public string GetHttpDecodeString(string parm, Encoding coding)
  848. {
  849. string text = HttpUtility.UrlDecode(GetString(parm), coding);
  850. text = cleanHtml(text);
  851. return text;
  852. }
  853. public int GetDeepIndex(params string[] parms)
  854. {
  855. return CommonHelper.GetDeepIndex(parms);
  856. }
  857. #endregion
  858. #region 运行JS脚本显示
  859. public void ShowScript(string script)
  860. {
  861. Page.ClientScript.RegisterStartupScript(GetType(), "alt", "<script>" + script + "</script>");
  862. }
  863. /// <summary>
  864. /// 显示消息
  865. /// </summary>
  866. /// <param name="msg">信息内容</param>
  867. public void ShowMsg(string msg)
  868. {
  869. ShowMsg("提示", msg);
  870. }
  871. /// <summary>
  872. /// 显示消息
  873. /// </summary>
  874. /// <param name="title">提示</param>
  875. /// <param name="msg">信息内容</param>
  876. public void ShowMsg(string title, string msg)
  877. {
  878. ShowMsg(title, msg, "");
  879. }
  880. /// <summary>
  881. /// 显示消息
  882. /// </summary>
  883. /// <param name="title">提示</param>
  884. /// <param name="msg">信息内容</param>
  885. /// <param name="msg">方法</param>
  886. public void ShowMsg(string title, string msg, string func)
  887. {
  888. Page.ClientScript.RegisterStartupScript(GetType(), title, "<script>alert('" + msg + "');" + func + "</script>");
  889. }
  890. /// <summary>
  891. /// 弹出显示结果
  892. /// </summary>
  893. /// <param name="msg"></param>
  894. public void ShowResult(string msg)
  895. {
  896. ShowResult(msg, "");
  897. }
  898. /// <summary>
  899. /// 弹出显示结果
  900. /// </summary>
  901. /// <param name="msg"></param>
  902. /// <param name="func"></param>
  903. public void ShowResult(string msg, string func)
  904. {
  905. func = func.Replace("'", "\"");
  906. Page.ClientScript.RegisterStartupScript(GetType(), "result", "<script>$(document).ready( function(){ resultShow('" + msg + "','" + func + "');});</script>");
  907. }
  908. #endregion
  909. }
  910. public enum SearchDateEnum
  911. {
  912. Empty = 9,
  913. Default = 0,
  914. DefaultTime = 5,
  915. OneDay = 1,
  916. OneMonth = 2,
  917. OneYear = 3,
  918. TwoMonth = 4
  919. }
  920. }