WebBasePage.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Data;
  4. using System.IO;
  5. using System.Text;
  6. using System.Web;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using Utils;
  10. namespace SiteCore
  11. {
  12. public class WebBasePage:BasePage
  13. {
  14. public int PkID; //int主键值
  15. protected string tmpId = "";
  16. protected bool tmpExpire;
  17. protected string tmpState = "";
  18. protected string fullScreen = "全屏查看";
  19. #region 防重复刷新
  20. private bool _refreshState;
  21. //是否是页面刷新
  22. private bool _isRefresh;
  23. public bool IsRefresh
  24. {
  25. get
  26. {
  27. return _isRefresh;
  28. }
  29. }
  30. protected override void OnInit(EventArgs e)
  31. {
  32. base.OnInit(e);
  33. if (Context.Session["__ISREFRESH"] == null)
  34. Context.Session["__ISREFRESH"] = false;
  35. }
  36. protected override void LoadViewState(object savedState)
  37. {
  38. object[] allStates = (object[])savedState;
  39. base.LoadViewState(allStates[0]);
  40. _refreshState = (bool)allStates[1];
  41. _isRefresh = _refreshState == (bool)Session["__ISREFRESH"];
  42. }
  43. protected override object SaveViewState()
  44. {
  45. Session["__ISREFRESH"] = _refreshState;
  46. object[] allStates = new object[2];
  47. allStates[0] = base.SaveViewState();
  48. allStates[1] = !_refreshState;
  49. return allStates;
  50. }
  51. #endregion
  52. protected bool IsReport { get; set; }
  53. protected bool IsFullScreen { get; set; }
  54. #region 页面私有方法
  55. /// <summary>
  56. /// 检测是否登录
  57. /// </summary>
  58. public bool CheckRights()
  59. {
  60. string sPath=CommonHelper.GetPrePath();
  61. string curUrl = HttpContext.Current.Request.Url.AbsolutePath;
  62. if (CurrentUser == null)
  63. {
  64. Response.Write("<script type=\"text/javascript\">window.top.location='" + sPath + "SiteTransfer.aspx" + "';</script>");
  65. Response.End();
  66. return false;
  67. }
  68. else if (CurrentUser.MerchantID < 1)//是会员登录状态
  69. {
  70. if (curUrl.IndexOf("/MerchantCenter", StringComparison.OrdinalIgnoreCase) != -1)//如果是会员访问商家页面则退出
  71. {
  72. Response.Write("<script type=\"text/javascript\">window.top.location='" + sPath + "SiteTransfer.aspx" + "';</script>");
  73. Response.End();
  74. return false;
  75. }
  76. }
  77. else if (CurrentUser.MerchantID >0)//
  78. {
  79. if (curUrl.IndexOf("/UserCenter", StringComparison.OrdinalIgnoreCase) != -1)//如果是会员访问商家页面则退出
  80. {
  81. Response.Write("<script type=\"text/javascript\">window.top.location='" + sPath + "SiteTransfer.aspx" + "';</script>");
  82. Response.End();
  83. return false;
  84. }
  85. }
  86. else
  87. {
  88. ShowTabPage();
  89. }
  90. return true;
  91. }
  92. /// <summary>
  93. /// 显示通用错误
  94. /// </summary>
  95. public void ShowCustomError()
  96. {
  97. ShowMsg("提示", "系统繁忙或发生错误!");
  98. }
  99. /// <summary>
  100. /// 显示全屏查看
  101. /// </summary>
  102. /// <returns></returns>
  103. public bool ShowFullScreen()
  104. {
  105. if (!UrlParmsCheck("fs"))
  106. {
  107. string str = "<a href=\"javascript:openCurFullWin();\">(<span class=\"fullscreen\"></span>全屏查看)</a>";
  108. string script = string.Format("$(\"" + str.Replace("\"", "") + "\").appendTo($(\".title\"))");
  109. AddScript(script);
  110. return true;
  111. }
  112. else
  113. {
  114. IsFullScreen = true;
  115. return false;
  116. }
  117. }
  118. public void ShowTabPage()
  119. {
  120. if (UrlParmsCheck("it"))//如果是tab页中转的
  121. {
  122. string script = string.Format("$(\".title\").hide()");
  123. AddScript(script);
  124. }
  125. }
  126. #endregion
  127. #region 页面事件
  128. protected override void OnPreLoad(EventArgs e)
  129. {
  130. if (UrlParmsCheck("isReport"))IsReport = true;
  131. if (Page.IsPostBack)
  132. {
  133. if (EnableViewState)
  134. {
  135. if (ViewState["ID"] != null)
  136. PkID = Convert.ToInt32(ViewState["ID"]);
  137. }
  138. }
  139. base.OnPreLoad(e);
  140. }
  141. protected override void OnPreRender(EventArgs e)
  142. {
  143. if(EnableViewState)ViewState["ID"] = PkID;
  144. if (WaitShowScript.Count > 0)
  145. Page.ClientScript.RegisterStartupScript(GetType(), "alt", "<script>" + string.Join(";", WaitShowScript.ToArray()) + "</script>");
  146. base.OnPreRender(e);
  147. }
  148. private readonly List<string> _waitShowScript=new List<string>();
  149. public List<string> WaitShowScript
  150. {
  151. get { return _waitShowScript; }
  152. set { WaitShowScript = value;}
  153. }
  154. /// <summary>
  155. /// 添加运行脚本
  156. /// </summary>
  157. /// <param name="func"></param>
  158. public void AddScript(string func)
  159. {
  160. _waitShowScript.Add(func.TrimEnd(';'));
  161. //Page.ClientScript.RegisterStartupScript(GetType(), "alt", "<script>" + func + "</script>");
  162. }
  163. #endregion
  164. #region 设置查询条件
  165. protected void KeyWhere(string field,string value,IList<string> where)
  166. {
  167. value = value.Trim();
  168. if(value!="")
  169. {
  170. string[] sArr = field.Split(',');
  171. StringBuilder str=new StringBuilder();
  172. for (int i = 0; i < sArr.Length;i++ )
  173. {
  174. str.Append(string.Format(" {1} like '%{0}%' or", value, sArr[i]));
  175. }
  176. where.Add(" ("+str.ToString().TrimEnd("or".ToCharArray())+") ");
  177. }
  178. }
  179. protected void EqualsWhere(string field, string value, IList<string> where)
  180. {
  181. if(value!="")
  182. {
  183. where.Add(string.Format(" {1}={0}",value,field));
  184. }
  185. }
  186. protected void StartEndDateWhere(string s_field,string e_field,string start, string end, IList<string> where)
  187. {
  188. if (start != "" || end != "")
  189. {
  190. if (start != "" && end != "")
  191. where.Add(string.Format(" {2}>='{0}' and {3}<='{1}'", start, end, s_field,e_field));
  192. else if (start != "")
  193. where.Add(string.Format(" {1}>='{0}'", start, s_field));
  194. else
  195. where.Add(string.Format(" {1}<='{0}'", end, e_field));
  196. }
  197. }
  198. protected void BetweenStringWhere(string field,string start,string end,IList<string> where)
  199. {
  200. if (start != "" || end != "")
  201. {
  202. if (start != "" && end != "")
  203. where.Add(string.Format(" {2} BetWeen '{0}' and '{1}'", start, end, field));
  204. else if (start != "")
  205. where.Add(string.Format(" {1}>='{0}'", start, field));
  206. else
  207. where.Add(string.Format(" {1}<='{0}'", end, field));
  208. }
  209. }
  210. protected void BetweenIntWhere(string field, string start, string end, IList<string> where)
  211. {
  212. if (start != "" || end != "")
  213. {
  214. if (start != "" && end != "")
  215. where.Add(string.Format(" {2} BetWeen {0} and {1}", start, end, field));
  216. else if (start != "")
  217. where.Add(string.Format(" {1}>={0}", start, field));
  218. else
  219. where.Add(string.Format(" {1}<={0}", end, field));
  220. }
  221. }
  222. #endregion
  223. #region GridView数据处理
  224. /// <summary>
  225. /// 过期
  226. /// </summary>
  227. /// <param name="endTime"></param>
  228. /// <returns></returns>
  229. protected bool GetIsExpire(object endTime)
  230. {
  231. if (endTime != null && endTime.ToString() != "" && Convert.ToDateTime(endTime).AddDays(1) <= DateTime.Now)
  232. return true;
  233. return false;
  234. }
  235. /// <summary>
  236. /// 发布
  237. /// </summary>
  238. /// <param name="expire"></param>
  239. /// <param name="pubstate"></param>
  240. /// <param name="sId"></param>
  241. /// <param name="pType"></param>
  242. /// <returns></returns>
  243. protected string GetPublishString(bool expire, object pubstate, string sId,string pType)
  244. {
  245. if (expire)
  246. return "<span class=\"tip\">发布</span>";
  247. else
  248. {
  249. if (pubstate.ToString() == "1")
  250. return string.Format("<a href=\"javascript:mcAjaxFunc('{0}','{1}','下架','un');\" title=\"下架后将不会在网站上显示\">下架</a>", pType,sId);
  251. else
  252. return string.Format("<a href=\"javascript:showPublish('{0}','{1}');\" title=\"发布后将在网站显示\">发布</a>", pType, sId);
  253. }
  254. }
  255. /// <summary>
  256. /// 删除
  257. /// </summary>
  258. /// <param name="expire"></param>
  259. /// <param name="sId"></param>
  260. /// <param name="pType"></param>
  261. /// <returns></returns>
  262. protected string GetDelString(bool expire, string sId, string pType)
  263. {
  264. if (expire)//过期允许删除
  265. return "<a href=\"javascript:mcAjaxFunc('"+pType+"','"+sId+"','删除');\" >删除</a>";
  266. else
  267. return "<span class=\"tip\" title=\"未过期不允许删除\">删除</span>";
  268. }
  269. /// <summary>
  270. /// 编辑
  271. /// </summary>
  272. /// <param name="expire"></param>
  273. /// <param name="sId"></param>
  274. /// <returns></returns>
  275. protected string GetEditString(bool expire, string sId)
  276. {
  277. if (expire)
  278. return "<span class=\"tip\" >编辑</span>";
  279. else
  280. return "<a href=\"javascript:rectEdit('" + sId + "')\" title=\"修改\">编辑</a>";
  281. }
  282. protected string GetEditString(bool expire, string sId,string url)
  283. {
  284. if (expire)
  285. return "<span class=\"tip\" >编辑</span>";
  286. else
  287. return "<a href=\"javascript:rectEdit('" + sId + "','"+url+"')\" title=\"修改\">编辑</a>";
  288. }
  289. /// <summary>
  290. /// 转赠好友
  291. /// </summary>
  292. /// <param name="expire"></param>
  293. /// <param name="sId"></param>
  294. /// <param name="num"></param>
  295. /// <param name="usedNum"></param>
  296. /// <returns></returns>
  297. protected string GetUserGiveString(bool expire,string sId,object num,object usedNum)
  298. {
  299. if (expire || Convert.ToInt32(GetMinuteNum(num,usedNum))<1)
  300. return "<span class=\"tip\">转赠好友</span>";
  301. else
  302. return "<a href=\"javascript:giveToFriend('" + sId+ "');\" title=\"转赠好友\">转赠好友</a>";
  303. }
  304. /// <summary>
  305. /// 状态显示
  306. /// </summary>
  307. /// <param name="state"></param>
  308. /// <returns></returns>
  309. public string GetStateString(bool expire,object state)
  310. {
  311. if (state.ToString() == "") return "";
  312. if (expire)
  313. return "<span style='color:red'>己过期</span>";
  314. int si = Convert.ToInt32(state);
  315. switch (si)
  316. {
  317. case 1:return "<span style='color:blue'>己发布</span>";
  318. default:
  319. return "<span>未发布</span>";
  320. }
  321. }
  322. /// <summary>
  323. /// 获取分隔后数组中的某项
  324. /// </summary>
  325. /// <param name="result"></param>
  326. /// <param name="idx"></param>
  327. /// <returns></returns>
  328. public string GetSplitString(object result, int idx)
  329. {
  330. if (result.ToString() == "") return "";
  331. string[] arr = result.ToString().Split(',');
  332. if (arr.Length > idx)
  333. return arr[idx];
  334. return "";
  335. }
  336. #endregion
  337. #region GridView 绑定
  338. public void SetGridBind(string sqltable, GridView gridView, DataStruct dStruct)
  339. {
  340. DataTable dt = WebCache.GetData(sqltable, dStruct);
  341. GridViewBind(gridView, dt, dStruct.EmptyMessage);
  342. }
  343. public void SetReportGridBind(string sqltable, GridView gridView, DataStruct dStruct)
  344. {
  345. DataTable dt;
  346. if (IsReport)
  347. {
  348. string sWhere = SecurityHelper.UrlDecoding(GetString("sw"));
  349. if (sWhere != "")
  350. {
  351. dStruct.MainWhere = sWhere.Split('μ')[0];
  352. dStruct.SecondWhere = sWhere.Split('μ')[1];
  353. }
  354. dt = WebCache.GetFullData(sqltable,dStruct);
  355. }
  356. else
  357. {
  358. if (IsReport)
  359. {
  360. HiddenField hidden = new HiddenField();
  361. hidden.ID = "hReport";
  362. //加密条件放至隐藏控件
  363. hidden.Value = SecurityHelper.UrlEncoding(dStruct.MainWhere + "μ" + dStruct.SecondWhere);
  364. Page.Form.Controls.AddAt(0, hidden);
  365. }
  366. dt = WebCache.GetData(sqltable,dStruct);
  367. }
  368. GridViewBind(gridView, dt, dStruct.EmptyMessage);
  369. if (IsReport)
  370. {
  371. Page.EnableViewState = false;
  372. StringWriter oStringWriter = new StringWriter();
  373. HtmlTextWriter oHtmlTextWriter = new HtmlTextWriter(oStringWriter);
  374. Page.RenderControl(oHtmlTextWriter);
  375. WebHelper.ExportGridView(this.Title, oStringWriter.ToString());
  376. }
  377. }
  378. public void SetSimpleGridBind(string sqltable, GridView gridView, DataStruct dStruct)
  379. {
  380. dStruct.SecondWhere = SecondWhere;
  381. DataTable dt = WebCache.GetData(sqltable, dStruct);
  382. GridViewBind(gridView, dt, dStruct.EmptyMessage);
  383. }
  384. public void GridViewBind(GridView gridView,DataTable dt)
  385. {
  386. GridViewBind(gridView, dt, "当前没有记录");
  387. }
  388. public void GridViewBind(GridView gridView, DataTable dt, string emptyMsg)
  389. {
  390. if (dt!=null && dt.Rows.Count < 1)
  391. {
  392. dt.Rows.Add(dt.NewRow());
  393. gridView.DataSource = dt;
  394. gridView.DataBind();
  395. int columnCount = gridView.Rows[0].Cells.Count;
  396. gridView.Rows[0].Cells.Clear();
  397. gridView.Rows[0].Cells.Add(new TableCell());
  398. gridView.Rows[0].Cells[0].CssClass = "GridEmptyDataRowStyle";
  399. gridView.Rows[0].Cells[0].ColumnSpan = columnCount;
  400. gridView.Rows[0].Cells[0].Text = emptyMsg;
  401. gridView.RowStyle.HorizontalAlign = HorizontalAlign.Center;
  402. }
  403. else
  404. {
  405. gridView.DataSource = dt;
  406. gridView.DataBind();
  407. }
  408. }
  409. #endregion
  410. #region 获取编辑页面URL参数
  411. private string _lcPara;
  412. /// <summary>
  413. /// URL参数 lcPara=xx,1,yy,2
  414. /// </summary>
  415. private string GetLcPara(string key)
  416. {
  417. if (string.IsNullOrEmpty(_lcPara))
  418. {
  419. _lcPara = Request.QueryString[key];
  420. }
  421. return _lcPara;
  422. }
  423. /// <summary>
  424. /// 获取默认主键
  425. /// </summary>
  426. /// <returns></returns>
  427. public string GetPk()
  428. {
  429. return GetPara("LcID");
  430. }
  431. /// <summary>
  432. /// 获取自定义url参数
  433. /// </summary>
  434. /// <param name="key">参数名</param>
  435. /// <returns></returns>
  436. public string GetPara(string key)
  437. {
  438. //LCPara=PkID,1,sName,sk
  439. string lcPara = GetLcPara("LcPara");
  440. if (!string.IsNullOrEmpty(lcPara))
  441. {
  442. lcPara = lcPara.Replace("#", "");
  443. string[] sArr = lcPara.Split(',');
  444. for (int i = 0; i < sArr.Length; i++)
  445. {
  446. if (i % 2 != 0) continue;
  447. if (sArr[i].ToLower().Equals(key.ToLower()))
  448. return sArr[i + 1];
  449. }
  450. }
  451. return "";
  452. }
  453. #endregion
  454. }
  455. }