You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

345 lines
15 KiB

  1. using Learun.Application.OA;
  2. using Learun.Util;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Configuration;
  6. using System.Linq;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using System.Web.Mvc;
  10. using Learun.Application.Base.AuthorizeModule;
  11. using Learun.Application.Organization;
  12. using Learun.Application.TwoDevelopment.LR_Desktop;
  13. using Learun.Util.Operat;
  14. using Microsoft.AspNet.SignalR.Client;
  15. using Microsoft.Owin.Logging;
  16. using Newtonsoft.Json;
  17. namespace Learun.Application.Web.Areas.LR_OAModule.Controllers
  18. {
  19. /// <summary>
  20. /// 版 本 Learun-ADMS-Ultimate V7.0.0 数字化智慧校园
  21. /// Copyright (c) 2013-2018 北京泉江科技有限公司
  22. /// 创建人:陈彬彬
  23. /// 日 期:2017.04.01
  24. /// 描 述:公告管理
  25. /// </summary>
  26. public class NoticeController : MvcControllerBase
  27. {
  28. private NoticeIBLL noticeIBLL = new NoticeBLL();
  29. private WeChatConfigIBLL weChatConfigIbll = new WeChatConfigBLL();
  30. private WeChatTempletIBLL weChatTempletIbll = new WeChatTempletBLL();
  31. private UserIBLL userIbll = new UserBLL();
  32. private UserRelationIBLL userRelationIBLL = new UserRelationBLL();
  33. #region 视图功能
  34. /// <summary>
  35. /// 管理页面
  36. /// </summary>
  37. /// <returns></returns>
  38. [HttpGet]
  39. public ActionResult Index()
  40. {
  41. return View();
  42. }
  43. /// <summary>
  44. /// 表单页面
  45. /// </summary>
  46. /// <returns></returns>
  47. [HttpGet]
  48. public ActionResult Form()
  49. {
  50. return View();
  51. }
  52. /// <summary>
  53. /// 管理页面【查看通知公告】
  54. /// </summary>
  55. /// <returns></returns>
  56. [HttpGet]
  57. public ActionResult ViewIndex()
  58. {
  59. return View();
  60. }
  61. /// <summary>
  62. /// 表单页面【查看通知公告】
  63. /// </summary>
  64. /// <returns></returns>
  65. [HttpGet]
  66. public ActionResult ViewForm()
  67. {
  68. return View();
  69. }
  70. #endregion
  71. #region 获取数据
  72. /// <summary>
  73. /// 获取分页数据
  74. /// </summary>
  75. /// <param name="pagination">分页参数</param>
  76. /// <param name="keyword">关键词</param>
  77. /// <returns></returns>
  78. public ActionResult GetPageList(string pagination, string keyword)
  79. {
  80. Pagination paginationobj = pagination.ToObject<Pagination>();
  81. var data = noticeIBLL.GetPageList(paginationobj, keyword);
  82. var jsonData = new
  83. {
  84. rows = data,
  85. total = paginationobj.total,
  86. page = paginationobj.page,
  87. records = paginationobj.records,
  88. };
  89. return JsonResult(jsonData);
  90. }
  91. /// <summary>
  92. /// 查看通知公告
  93. /// </summary>
  94. /// <param name="pagination">分页参数</param>
  95. /// <param name="keyword">关键词</param>
  96. /// <returns></returns>
  97. public ActionResult GetListOfSelf(string keyword, string categoryId = null)
  98. {
  99. var userinfo = LoginUserInfo.Get();
  100. Pagination paginationobj = new Pagination();
  101. var newsList = noticeIBLL.GetList(keyword, userinfo.userId, categoryId);
  102. var newsListOfSelf = new List<NewsEntity>();
  103. foreach (var newsitemEntity in newsList)
  104. {
  105. if (!string.IsNullOrEmpty(newsitemEntity.F_SendPostId))
  106. {
  107. if (!string.IsNullOrEmpty(userinfo.postIds))
  108. {
  109. if (userinfo.postIds.Contains(","))
  110. {
  111. foreach (var postid in userinfo.postIds.Split(','))
  112. {
  113. if (newsitemEntity.F_SendPostId.Contains(postid))
  114. {
  115. newsListOfSelf.Add(newsitemEntity);
  116. break;
  117. }
  118. }
  119. }
  120. else
  121. {
  122. if (newsitemEntity.F_SendPostId.Contains(userinfo.postIds))
  123. {
  124. newsListOfSelf.Add(newsitemEntity);
  125. }
  126. }
  127. }
  128. }
  129. else
  130. {
  131. if (!string.IsNullOrEmpty(newsitemEntity.F_SendDeptId))
  132. {
  133. if (newsitemEntity.F_SendDeptId.Contains(userinfo.departmentId))
  134. {
  135. newsListOfSelf.Add(newsitemEntity);
  136. }
  137. }
  138. else
  139. {
  140. newsListOfSelf.Add(newsitemEntity);
  141. }
  142. }
  143. }
  144. return JsonResult(newsListOfSelf);
  145. }
  146. /// <summary>
  147. /// 获取实体数据
  148. /// </summary>
  149. /// <param name="keyValue">主键</param>
  150. /// <returns></returns>
  151. public ActionResult GetEntity(string keyValue)
  152. {
  153. var data = noticeIBLL.GetEntity(keyValue);
  154. data.F_NewsContent = WebHelper.HtmlDecode(data.F_NewsContent);
  155. return JsonResult(data);
  156. }
  157. #endregion
  158. #region 提交数据
  159. /// <summary>
  160. /// 保存表单数据
  161. /// </summary>
  162. /// <param name="keyValue">主键</param>
  163. /// <param name="entity">实体</param>
  164. /// <returns></returns>
  165. [HttpPost, ValidateAntiForgeryToken, AjaxOnly, ValidateInput(false)]
  166. public ActionResult SaveForm(string keyValue, NewsEntity entity)
  167. {
  168. entity.F_NewsContent = WebHelper.HtmlEncode(entity.F_NewsContent);
  169. noticeIBLL.SaveEntity(keyValue, entity);
  170. //推送微信
  171. Task.Run(async () => { await DoWeixinPush(entity); });
  172. if (entity.F_IsSendFX.HasValue && entity.F_IsSendFX == true)
  173. {
  174. Task.Run(async () =>
  175. {
  176. using (var hubConnection = new HubConnection(ConfigurationManager.AppSettings["CommunicationServeraddress"]))
  177. {
  178. var hubProxy = hubConnection.CreateHubProxy("SignalRHub");
  179. await hubConnection.Start();
  180. await hubProxy.Invoke("PushAnnouncement", entity.F_NewsId, entity.F_FullHead, Util.Str.ReplaceHtml(HttpUtility.HtmlDecode(entity.F_NewsContent)).Length < 20 ? Util.Str.ReplaceHtml(HttpUtility.HtmlDecode(entity.F_NewsContent)) : Util.Str.ReplaceHtml(HttpUtility.HtmlDecode(entity.F_NewsContent)).Substring(0, 20), "news", entity.F_SendPostId, entity.F_SendDeptId);
  181. }
  182. });
  183. }
  184. return Success("保存成功!");
  185. }
  186. Task DoWeixinPush(NewsEntity entity)
  187. {
  188. //微信推送
  189. try
  190. {
  191. var allteacherlist = userIbll.GetAllList().Where(m =>
  192. m.F_DeleteMark != 1 && m.F_EnabledMark == 1 && m.F_Description == "教师");
  193. var userralationlist = userRelationIBLL.GetUserIdList("2");
  194. var needpostuserlist = new List<UserEntity>();
  195. if (!string.IsNullOrEmpty(entity.F_SendDeptId))
  196. {
  197. if (!entity.F_SendDeptId.Contains(","))
  198. {
  199. var departteacherlist = allteacherlist.Where(m => entity.F_SendDeptId == m.F_DepartmentId);
  200. needpostuserlist.AddRange(departteacherlist.ToList());
  201. if (!string.IsNullOrEmpty(entity.F_SendPostId))
  202. {
  203. var newpostuserlist = new List<UserEntity>();
  204. foreach (var uuitem in needpostuserlist)
  205. {
  206. var postids = userralationlist?.Count(m => m.F_UserId == uuitem.F_UserId && entity.F_SendPostId.Contains(m.F_ObjectId));
  207. if (postids > 0)
  208. {
  209. newpostuserlist.Add(uuitem);
  210. }
  211. }
  212. needpostuserlist = newpostuserlist;
  213. }
  214. }
  215. else
  216. {
  217. foreach (var senddeptid in entity.F_SendDeptId.Split(','))
  218. {
  219. var departteacherlist = allteacherlist.Where(m => senddeptid == m.F_DepartmentId);
  220. needpostuserlist.AddRange(departteacherlist.ToList());
  221. if (!string.IsNullOrEmpty(entity.F_SendPostId))
  222. {
  223. var newpostuserlist = new List<UserEntity>();
  224. foreach (var uuitem in needpostuserlist)
  225. {
  226. var postids = userralationlist?.Count(m => m.F_UserId == uuitem.F_UserId && entity.F_SendPostId.Contains(m.F_ObjectId));
  227. if (postids > 0)
  228. {
  229. newpostuserlist.Add(uuitem);
  230. }
  231. }
  232. needpostuserlist = newpostuserlist;
  233. }
  234. }
  235. }
  236. }
  237. else
  238. {
  239. if (!string.IsNullOrEmpty(entity.F_SendPostId))
  240. {
  241. //岗位下发
  242. var newpostuserlist = new List<UserEntity>();
  243. foreach (var uuitem in allteacherlist)
  244. {
  245. var postids = userralationlist?.Count(m => m.F_UserId == uuitem.F_UserId && entity.F_SendPostId.Contains(m.F_ObjectId));
  246. if (postids > 0)
  247. {
  248. newpostuserlist.Add(uuitem);
  249. }
  250. }
  251. needpostuserlist = newpostuserlist;
  252. }
  253. else
  254. {
  255. //全员下发
  256. needpostuserlist = allteacherlist.ToList();
  257. }
  258. }
  259. PushWeixin(needpostuserlist, entity.F_FullHead);
  260. return Task.CompletedTask;
  261. }
  262. catch (Exception e)
  263. {
  264. return Task.FromException(e);
  265. }
  266. }
  267. /// <summary>
  268. /// 删除表单数据
  269. /// </summary>
  270. /// <param name="keyValue">主键</param>
  271. /// <returns></returns>
  272. [HttpPost]
  273. [AjaxOnly]
  274. public ActionResult DeleteForm(string keyValue)
  275. {
  276. noticeIBLL.DeleteEntity(keyValue);
  277. return Success("删除成功!");
  278. }
  279. public void PushWeixin(List<UserEntity> needpostuserlist, string title)
  280. {
  281. var WeChatConfigentity = weChatConfigIbll.GetEnableEntity();
  282. string appid = WeChatConfigentity.APPId;
  283. string secret = WeChatConfigentity.secret;
  284. var wechatemplete = weChatTempletIbll.GetWeChatTemplateEntityByCodeConfigId(WeChatConfigentity.ID, "task");
  285. string weixintaskurl = wechatemplete.TUrl;
  286. string weixintasktempid = wechatemplete.TempId;
  287. var responsejson = Util.HttpMethods.HttpGet("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" + appid + "&secret=" + secret);
  288. OperateLogModel operateLogModel = new OperateLogModel();
  289. operateLogModel.title = title;
  290. operateLogModel.type = OperationType.Other;
  291. operateLogModel.url = "NoticeController";
  292. operateLogModel.sourceObjectId = "002";
  293. operateLogModel.sourceContentJson = responsejson;
  294. OperatorHelper.Instance.WriteOperateLog(operateLogModel);
  295. foreach (UserEntity userinfo in needpostuserlist)
  296. {
  297. if (userinfo != null && !string.IsNullOrEmpty(userinfo.OpenIdForWeixin))
  298. {
  299. //执行推送任务
  300. if (!string.IsNullOrEmpty(appid) && !string.IsNullOrEmpty(secret) && !string.IsNullOrEmpty(weixintaskurl) && !string.IsNullOrEmpty(weixintasktempid))
  301. {
  302. if (!string.IsNullOrEmpty(responsejson))
  303. {
  304. var weixintokenobj = JsonConvert.DeserializeObject<dynamic>(responsejson);
  305. if (string.IsNullOrEmpty(weixintokenobj.errcode))
  306. {
  307. string access_token = weixintokenobj.access_token;
  308. string jsondata = "{\"touser\":\"" + userinfo.OpenIdForWeixin + "\"," +
  309. "\"template_id\":\"" + weixintasktempid + "\"," +
  310. "\"url\":\"" + weixintaskurl + "\"," +
  311. "\"data\":{" +
  312. "\"first\": {\"value\":\"您有新的未读通知公告\",\"color\":\"#173177\"}," +
  313. "\"keyword1\":{\"value\":\"未读通知公告\",\"color\":\"#173177\"}," +
  314. "\"keyword2\": {\"value\":\"" + title + "\",\"color\":\"#173177\"}," +
  315. "\"keyword3\": {\"value\":\"待查看\",\"color\":\"#173177\"}," +
  316. "\"keyword4\": {\"value\":\"您有新的未读通知公告【" + title + "】\",\"color\":\"#173177\"}" +
  317. "}" +
  318. "}";
  319. string pushresult = Util.HttpMethods.HttpPost("https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" + access_token, jsondata);
  320. operateLogModel.title = title;
  321. operateLogModel.type = OperationType.Other;
  322. operateLogModel.url = "NoticeController";
  323. operateLogModel.sourceObjectId = "002";
  324. operateLogModel.sourceContentJson = pushresult;
  325. OperatorHelper.Instance.WriteOperateLog(operateLogModel);
  326. }
  327. }
  328. }
  329. }
  330. }
  331. }
  332. #endregion
  333. }
  334. }