PageRenderTime 54ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/wojilu.Controller/Layouts/TopNavController.cs

https://bitbucket.org/kingshine/wojilu
C# | 361 lines | 240 code | 108 blank | 13 comment | 25 complexity | 4fa88510b5ef4771dda831217d81ff72 MD5 | raw file
Possible License(s): MIT
  1. /*
  2. * Copyright (c) 2010, www.wojilu.com. All rights reserved.
  3. */
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using wojilu.Web.Mvc;
  8. using wojilu.Web.Controller.Users.Admin;
  9. using wojilu.Serialization;
  10. using wojilu.Members.Sites.Domain;
  11. using wojilu.Members.Sites.Service;
  12. using wojilu.Members.Users.Service;
  13. using wojilu.Members.Users.Domain;
  14. using wojilu.Common.Onlines;
  15. using wojilu.Common.MemberApp.Interface;
  16. using wojilu.Common.Msg.Interface;
  17. using wojilu.Common.Msg.Service;
  18. using wojilu.Common.Menus.Interface;
  19. using wojilu.Common.Money.Domain;
  20. using wojilu.Common.Money.Interface;
  21. using wojilu.Common.Money.Service;
  22. using wojilu.Common;
  23. using wojilu.Common.Microblogs.Domain;
  24. using wojilu.Common.Msg.Domain;
  25. using wojilu.Common.Feeds.Domain;
  26. using wojilu.Config;
  27. using wojilu.Web.Controller.Security;
  28. namespace wojilu.Web.Controller.Layouts {
  29. public partial class TopNavController : ControllerBase {
  30. public IMemberAppService userAppService { get; set; }
  31. public IMessageService msgService { get; set; }
  32. private IMenuService menuService { get; set; }
  33. public ICurrencyService currencyService { get; set; }
  34. public TopNavController() {
  35. userAppService = new UserAppService();
  36. msgService = new MessageService();
  37. menuService = new SiteMenuService();
  38. currencyService = new CurrencyService();
  39. }
  40. public void Index() {
  41. if (config.Instance.Site.TopNavDisplay == TopNavDisplay.Hide ||
  42. (config.Instance.Site.RegisterType == RegisterType.Close && config.Instance.Site.TopNavDisplay == TopNavDisplay.NoRegHide)
  43. ) {
  44. utils.setCurrentView( new Template() );
  45. return;
  46. }
  47. set( "site.Name", config.Instance.Site.SiteName );
  48. set( "site.Url", SystemInfo.SiteRoot );
  49. set( "LoginAction", Link.T2( Site.Instance, new MainController().CheckLogin ) );
  50. set( "RegLink", Link.T2( Site.Instance, new RegisterController().Register ) );
  51. set( "resetPwdLink", Link.T2( Site.Instance, new wojilu.Web.Controller.Common.ResetPwdController().StepOne ) );
  52. String emailCredit = getEmailConfirmCredit( 18 );
  53. String uploadCredit = getEmailConfirmCredit( 17 );
  54. set( "emailCredit", emailCredit );
  55. set( "uploadCredit", uploadCredit );
  56. //IBlock cblock = getBlock( "Captcha" );
  57. //if (config.Instance.Site.LoginNeedImgValidation) {
  58. // cblock.Set( "ValidationCode", Html.Captcha );
  59. // cblock.Next();
  60. //}
  61. set( "ValidationCode", Html.Captcha );
  62. //ajax
  63. //set( "navUrl", Link.T2( Site.Instance, Nav ) );
  64. set( "navUrl", t2( Nav ) );
  65. }
  66. private string getEmailConfirmCredit( int actionId ) {
  67. KeyIncomeRule rule = currencyService.GetKeyIncomeRulesByAction( actionId ); // 获取当前操作action收入规则。这里获取的是中心货币,你也可以使用 GetRulesByAction(actionId) 获取其他所有货币的收入规则
  68. return string.Format( "可奖励{0}{1}", rule.Income, rule.CurrencyUnit );
  69. }
  70. //------------------------------------------------------------------------------------------------------
  71. public void Nav() {
  72. // TODO 如果是在访问用户空间,则判断:是否好友、是否关注
  73. echoJson( getLoginJsonString() );
  74. }
  75. private String getLoginJsonString() {
  76. User user = ctx.viewer.obj as User;
  77. Boolean isViewerOwnerSame = (ctx.owner.Id == ctx.viewer.Id && ctx.owner.obj.GetType() == ctx.viewer.obj.GetType());
  78. Boolean isAlertActivation = config.Instance.Site.EnableEmail && config.Instance.Site.AlertActivation;
  79. Dictionary<String, object> dic = new Dictionary<string, object>();
  80. Dictionary<String, object> viewer = new Dictionary<string, object>();
  81. viewer.Add( "Id", user.Id );
  82. viewer.Add( "IsLogin", ctx.viewer.IsLogin );
  83. viewer.Add( "IsAdministrator", ctx.viewer.IsAdministrator() );
  84. viewer.Add( "IsInAdminGroup", SiteRole.IsInAdminGroup( ctx.viewer.obj.RoleId ) );
  85. viewer.Add( "HasPic", user.HasUploadPic() );
  86. viewer.Add( "EmailConfirm", user.IsEmailConfirmed == 1 );
  87. viewer.Add( "IsAlertActivation", isAlertActivation );
  88. viewer.Add( "IsAlertUserPic", config.Instance.Site.AlertUserPic );
  89. Dictionary<String, object> objViewer = new Dictionary<string, object>();
  90. objViewer.Add( "Id", user.Id );
  91. objViewer.Add( "Name", user.Name );
  92. objViewer.Add( "FriendlyUrl", user.Url );
  93. objViewer.Add( "Url", Link.ToMember( user ) );
  94. objViewer.Add( "PicMedium", user.PicMedium );
  95. viewer.Add( "obj", objViewer );
  96. dic.Add( "viewer", viewer );
  97. dic.Add( "viewerOwnerSame", isViewerOwnerSame );
  98. Dictionary<String, object> owner = new Dictionary<string, object>();
  99. owner.Add( "IsSite", ctx.owner.obj.GetType() == typeof( Site ) );
  100. owner.Add( "Id", ctx.owner.Id );
  101. dic.Add( "owner", owner );
  102. dic.Add( "navInfo", loginNavInfo() );
  103. dic.Add( "online", getOnlineDic() );
  104. return JsonString.Convert( dic );
  105. }
  106. public Dictionary<String, Object> loginNavInfo() {
  107. Dictionary<String, Object> dic = new Dictionary<String, Object>();
  108. dic.Add( "topNavDisplay", config.Instance.Site.TopNavDisplay );
  109. dic.Add( "siteName", config.Instance.Site.SiteName );
  110. User user = (User)ctx.viewer.obj;
  111. dic.Add( "viewerName", user.Name );
  112. dic.Add( "viewerPicSmall", user.PicSmall );
  113. dic.Add( "viewerFeeds", Link.T2( user, new FeedController().My, -1 ) );
  114. dic.Add( "viewerSpace", Link.ToMember( user ) );
  115. dic.Add( "viewerMicroblogHome", Link.T2( user, new Microblogs.My.MicroblogController().Home ) );
  116. Boolean isUserHomeClose = Component.IsClose( typeof( UserHome ) );
  117. Boolean isMicroblogClose = Component.IsClose( typeof( MicroblogApp ) );
  118. Boolean isFriendClose = Component.IsClose( typeof( FriendApp ) );
  119. Boolean isSkinClose = Component.IsClose( typeof( SkinApp ) );
  120. Boolean isMessageClose = Component.IsClose( typeof( MessageApp ) );
  121. Boolean isFeedClose = Component.IsClose( typeof( FeedApp ) );
  122. Boolean isUserInviteClose = Component.IsClose( typeof( UserInviteApp ) );
  123. Boolean isUserAppAdminClose = Component.IsClose( typeof( UserAppAdmin ) );
  124. Boolean isUserMenuAdminClose = Component.IsClose( typeof( UserMenuAdmin ) );
  125. Boolean isUserLinksClose = Component.IsClose( typeof( UserLinks ) );
  126. Boolean isUserPrivacyClose = Component.IsClose( typeof( UserPrivacy ) );
  127. dic.Add( "isEnableUserSpace", Component.IsEnableUserSpace() );
  128. dic.Add( "isEnableGroup", Component.IsEnableGroup() );
  129. dic.Add( "isUserHomeClose", isUserHomeClose );
  130. dic.Add( "isMicroblogClose", isMicroblogClose );
  131. dic.Add( "isFriendClose", isFriendClose );
  132. dic.Add( "isSkinClose", isSkinClose );
  133. dic.Add( "isMessageClose", isMessageClose );
  134. dic.Add( "isFeedClose", isFeedClose );
  135. dic.Add( "isUserInviteClose", isUserInviteClose );
  136. dic.Add( "isUserAppAdminClose", isUserAppAdminClose );
  137. dic.Add( "isUserMenuAdminClose", isUserMenuAdminClose );
  138. dic.Add( "isUserLinksClose", isUserLinksClose );
  139. dic.Add( "isUserPrivacyClose", isUserPrivacyClose );
  140. dic.Add( "viewerTemplateUrl", Link.T2( user, new Users.Admin.SkinController().My ) );
  141. dic.Add( "viewerMsg", Link.T2( user, new MsgController().All ) );
  142. dic.Add( "viewerNewMsgCount", this.getMsgCount() );
  143. dic.Add( "viewerNewNotificationCount", this.getNewNotificationCount() );
  144. dic.Add( "viewerNewMicroblogAtCount", this.getNewMicroblogAtCount() );
  145. dic.Add( "viewerSiteNotification", getSiteNotification() );
  146. dic.Add( "viewerProfileUrl", Link.T2( user, new UserProfileController().Profile ) );
  147. dic.Add( "viewerContactLink", Link.T2( user, new UserProfileController().Contact ) );
  148. dic.Add( "viewerInterestUrl", Link.T2( user, new UserProfileController().Interest ) );
  149. dic.Add( "viewerTagUrl", Link.T2( user, new UserProfileController().Tag ) );
  150. dic.Add( "viewerFaceUrl", Link.T2( user, new UserProfileController().Face ) );
  151. dic.Add( "viewerPwdUrl", Link.T2( user, new UserProfileController().Pwd ) );
  152. dic.Add( "viewerInviteLink", Link.T2( user, new Users.Admin.InviteController().Index ) );
  153. dic.Add( "uploadAvatarLink", Link.T2( user, new Users.Admin.UserProfileController().Face ) );
  154. dic.Add( "confirmEmailLink", Link.T2( user, new UserProfileController().Contact ) );
  155. dic.Add( "viewerFriends", Link.T2( user, new Users.Admin.Friends.FriendController().List, 0 ) );
  156. dic.Add( "viewerCurrency", Link.T2( user, new Users.Admin.CreditController().My ) );
  157. dic.Add( "viewerSettings", Link.T2( user, new Users.Admin.UserProfileController().Privacy ) );
  158. dic.Add( "logoutLink", Link.T2( Site.Instance, new MainController().Logout ) );
  159. String siteAdminCmd = getAdminCmd();
  160. dic.Add( "siteAdminCmd", siteAdminCmd );
  161. dic.Add( "siteOnlineCount", OnlineStats.Instance.Count );
  162. dic.Add( "siteOnlineUrl", Link.T2( Site.Instance, new Users.MainController().OnlineUser ) );
  163. dic.Add( "shareLink", Link.T2( user, new Users.Admin.ShareController().Index, -1 ) );
  164. dic.Add( "myGroupsLink", Link.T2( user, new MyGroupController().My ) );
  165. dic.Add( "appAdminUrl", Link.T2( user, new AppController().Index ) );
  166. dic.Add( "menuAdminUrl", Link.T2( user, new MenuController().Index ) );
  167. dic.Add( "myUrlList", Link.T2( user, new MyLinkController().Index ) );
  168. IList userAppList = userAppService.GetByMember( user.Id );
  169. dic.Add( "userAppList", getAppList( userAppList ) );
  170. return dic;
  171. }
  172. private Dictionary<String, object> getOnlineDic() {
  173. OnlineStats o = OnlineStats.Instance;
  174. Dictionary<String, object> dic = new Dictionary<string, object>();
  175. dic.Add( "count", o.Count );
  176. dic.Add( "member", o.MemberCount );
  177. dic.Add( "guest", o.GuestCount );
  178. dic.Add( "max", o.MaxCount );
  179. dic.Add( "maxTime", o.MaxTime.ToShortDateString() );
  180. return dic;
  181. }
  182. private List<String> getAppList( IList userAppList ) {
  183. List<String> list = new List<String>();
  184. foreach (IMemberApp app in userAppList) {
  185. if (app.AppInfo.IsInstanceClose( ctx.viewer.obj.GetType() )) continue;
  186. list.Add( getNameAndUrl( app ) );
  187. }
  188. return list;
  189. }
  190. private string getSiteNotification() {
  191. if (ctx.viewer.obj.Id != SiteRole.Administrator.Id) return "";
  192. int newCount = new NotificationService().GetUnReadCount( Site.Instance.Id, typeof( Site ).FullName );
  193. if (newCount <= 0) return "";
  194. User user = (User)ctx.viewer.obj;
  195. String lnk = Link.T2( user, new Users.Admin.SiteNfController().List );
  196. return string.Format( "<a href=\"{0}\">通知(<span id=\"siteNotificationText\">{1}</span>)</a>", lnk, newCount );
  197. }
  198. //-------------------------------------------------------------------------------------------------------------
  199. public void Header() {
  200. set( "site.Name", Site.Instance.Name );
  201. set( "site.Logo", config.Instance.Site.GetLogoHtml() );
  202. set( "adBanner", AdItem.GetAdById( AdCategory.Banner ) );
  203. set( "adNavBottom", AdItem.GetAdById( AdCategory.NavBottom ) );
  204. List<IMenu> list = menuService.GetList( Site.Instance );
  205. IMenu currentRootMenu = bindSubMenus( list );
  206. bindNavList( list, currentRootMenu );
  207. List<Dictionary<string, string>> langs = wojilu.lang.GetSupportedLang();
  208. bindList( "langs", "lang", langs, bindLang );
  209. }
  210. private void bindLang( IBlock block, String lbl, object obj ) {
  211. Dictionary<string, string> map = (Dictionary<string, string>)obj;
  212. block.Set( "lang.SetLink", t2( new LangController().Switch ) + "?lang=" + map["Value"] );
  213. String img = "<img src=\"" + sys.Path.Img + "oks.gif\" {0} />";
  214. String currentStyle = map["Value"].Equals( wojilu.lang.getLangString() ) ? string.Format( img, "" ) : string.Format( img, "class=\"noCurrentLang\"" );
  215. block.Set( "lang.CurrentStyle", currentStyle );
  216. }
  217. private void bindNavList( List<IMenu> menus, IMenu currentRootMenu ) {
  218. List<IMenu> list = MenuHelper.getRootMenus( menus );
  219. IBlock block = getBlock( "navLink" );
  220. for (int i = 0; i < list.Count; i++) {
  221. String itemId = (i == list.Count - 1 ? "menuItemLast" : "menuItem" + i);
  222. block.Set( "menu.ItemId", itemId );
  223. IMenu menu = list[i];
  224. String currentClass = "";
  225. if (currentRootMenu != null && menu.Id == currentRootMenu.Id) currentClass = " class=\"currentRootMenu\" ";
  226. block.Set( "menu.CurrentClass", currentClass );
  227. IBlock subNavBlock = block.GetBlock( "subNav" );
  228. IBlock rootBlock = block.GetBlock( "rootNav" );
  229. List<IMenu> subMenus = MenuHelper.getSubMenus( menus, menu );
  230. if (subMenus.Count == 0) {
  231. MenuHelper.bindMenuSingle( rootBlock, menu, ctx );
  232. }
  233. else {
  234. IBlock subBlock = subNavBlock.GetBlock( "subMenu" );
  235. MenuHelper.bindSubMenus( subBlock, subMenus, ctx );
  236. MenuHelper.bindMenuSingle( subNavBlock, menu, ctx );
  237. }
  238. block.Next();
  239. }
  240. }
  241. private IMenu bindSubMenus( List<IMenu> list ) {
  242. IMenu currentRootMenu = MenuHelper.getCurrentRootMenu( list, ctx );
  243. List<IMenu> subMenus = MenuHelper.getSubMenus( list, currentRootMenu );
  244. IBlock subMenusPanel = getBlock( "subMenusPanel" );
  245. if (subMenus.Count > 0) {
  246. IBlock block = subMenusPanel.GetBlock( "subMenus" );
  247. MenuHelper.bindSubMenus( block, subMenus, ctx );
  248. subMenusPanel.Next();
  249. }
  250. return currentRootMenu;
  251. }
  252. }
  253. }