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.

353 lines
14 KiB

2 years ago
  1. using EasyBL;
  2. using Entity.Sugar;
  3. using Microsoft.Office365.OutlookServices;
  4. using Newtonsoft.Json;
  5. using Newtonsoft.Json.Linq;
  6. using SqlSugar;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Net.Http;
  10. using System.Net.Http.Headers;
  11. using System.Text;
  12. using System.Threading.Tasks;
  13. using WebApp.Outlook.Models;
  14. namespace WebApp.Outlook
  15. {
  16. public class ServiceHelper
  17. {
  18. public static string RedirectUri
  19. {
  20. get
  21. {
  22. return Common.GetAppSettings("ida:RedirectUri");
  23. }
  24. }
  25. public static string AppId
  26. {
  27. get
  28. {
  29. return Common.GetAppSettings("ida:AppId");
  30. }
  31. }
  32. public static string AppSecret
  33. {
  34. get
  35. {
  36. return Common.GetAppSettings("ida:AppPassword");
  37. }
  38. }
  39. public static string Scopes
  40. {
  41. get
  42. {
  43. return Common.GetAppSettings("ida:AppScopes");
  44. //return Common.GetAppSettings("ida:AppScopes").Replace(" ", ",https://outlook.office.com/").Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
  45. }
  46. }
  47. public static string Scopes_Outlook
  48. {
  49. get
  50. {
  51. return Common.GetAppSettings("ida:AppScopes").Replace(" ", ",https://outlook.office.com/").Replace(",", " ");
  52. }
  53. }
  54. public static async Task<HttpResponseMessage> SendRequestAsync(HttpMethod method, String endPoint, string accessToken, dynamic content = null)
  55. {
  56. HttpResponseMessage response = null;
  57. using (var client = new HttpClient())
  58. {
  59. using (var request = new HttpRequestMessage(method, endPoint))
  60. {
  61. request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  62. request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
  63. if (content != null)
  64. {
  65. string c;
  66. c = content is string ? content : JsonConvert.SerializeObject(content);
  67. request.Content = new StringContent(c, Encoding.UTF8, "application/json");
  68. }
  69. response = await client.SendAsync(request);
  70. }
  71. }
  72. return response;
  73. }
  74. /// <summary>
  75. /// Helper function to prepare the ResultsItem list from request response.
  76. /// </summary>
  77. /// <param name="response">Request response</param>
  78. /// <param name="idPropertyName">Property name of the item Id</param>
  79. /// <param name="displayPropertyName">Property name of the item display name</param>
  80. /// <param name="resourcePropId">todo: describe resourcePropId parameter on GetResultsItemAsync</param>
  81. /// <returns></returns>
  82. public static async Task<List<ResultsItem>> GetResultsItemAsync(
  83. HttpResponseMessage response, string idPropertyName, string displayPropertyName, string resourcePropId)
  84. {
  85. var items = new List<ResultsItem>();
  86. var json = JObject.Parse(await response.Content.ReadAsStringAsync());
  87. foreach (JProperty content in json.Children<JProperty>())
  88. {
  89. if (content.Name.Equals("value"))
  90. {
  91. var res = content.Value.AsJEnumerable().GetEnumerator();
  92. res.MoveNext();
  93. while (res.Current != null)
  94. {
  95. var display = "";
  96. var id = "";
  97. foreach (JProperty prop in res.Current.Children<JProperty>())
  98. {
  99. if (prop.Name.Equals(idPropertyName))
  100. {
  101. id = prop.Value.ToString();
  102. }
  103. if (prop.Name.Equals(displayPropertyName))
  104. {
  105. display = prop.Value.ToString();
  106. }
  107. }
  108. items.Add(new ResultsItem
  109. {
  110. Display = display,
  111. Id = id,
  112. Properties = new Dictionary<string, object>
  113. {
  114. { resourcePropId, id }
  115. }
  116. });
  117. res.MoveNext();
  118. }
  119. }
  120. }
  121. return items;
  122. }
  123. public static Microsoft.Graph.Event BuildEvents(SqlSugarClient db, OTB_SYS_Calendar evnt, bool requested = false)
  124. {
  125. var saAttendee = new List<Microsoft.Graph.Attendee>();
  126. if (evnt.OpenMent == "G")
  127. {
  128. var saGroupMembers = evnt.GroupMembers.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
  129. foreach (string member in saGroupMembers)
  130. {
  131. var oMembers = db.Queryable<OTB_SYS_Members>().Single(x => x.MemberID == member && x.OrgID == evnt.OrgID);
  132. if (oMembers == null)
  133. {//外部人員
  134. var oOuterUsers = db.Queryable<OTB_SYS_OuterUsers>().Single(x => x.Guid == member);
  135. if (oOuterUsers != null)
  136. {
  137. saAttendee.Add(new Microsoft.Graph.Attendee
  138. {
  139. EmailAddress = new Microsoft.Graph.EmailAddress
  140. {
  141. Address = oOuterUsers.Email,
  142. Name = oOuterUsers.UserName
  143. },
  144. Type = Microsoft.Graph.AttendeeType.Resource
  145. });
  146. }
  147. }
  148. else
  149. {//內部人員
  150. saAttendee.Add(new Microsoft.Graph.Attendee
  151. {
  152. EmailAddress = new Microsoft.Graph.EmailAddress
  153. {
  154. Address = oMembers.OutlookAccount,
  155. Name = oMembers.MemberName
  156. },
  157. Type = Microsoft.Graph.AttendeeType.Resource
  158. });
  159. }
  160. }
  161. }
  162. else if (evnt.OpenMent == "C")
  163. {
  164. var saMembers = db.Queryable<OTB_SYS_Members>().Where(x => x.Effective == "Y" && x.OrgID == evnt.OrgID).ToList();
  165. foreach (var _user in saMembers)
  166. {//公司所有人
  167. saAttendee.Add(new Microsoft.Graph.Attendee
  168. {
  169. EmailAddress = new Microsoft.Graph.EmailAddress
  170. {
  171. Address = _user.OutlookAccount,
  172. Name = _user.MemberName
  173. },
  174. Type = Microsoft.Graph.AttendeeType.Resource
  175. });
  176. }
  177. }
  178. var _event = new Microsoft.Graph.Event
  179. {
  180. Subject = evnt.Title,
  181. Importance = evnt.Importment == "H" ? Microsoft.Graph.Importance.High : Microsoft.Graph.Importance.Normal,
  182. IsAllDay = evnt.AllDay,
  183. Body = new Microsoft.Graph.ItemBody
  184. {
  185. ContentType = Microsoft.Graph.BodyType.Html,
  186. Content = evnt.Description
  187. },
  188. BodyPreview = evnt.Description,
  189. Start = new Microsoft.Graph.DateTimeTimeZone
  190. {
  191. DateTime = evnt.StartDate.ToString("yyyy-MM-dd HH:mm:ss"),
  192. TimeZone = "Asia/Shanghai"
  193. },
  194. End = new Microsoft.Graph.DateTimeTimeZone
  195. {
  196. DateTime = evnt.EndDate.ToString("yyyy-MM-dd HH:mm:ss"),
  197. TimeZone = "Asia/Shanghai"
  198. },
  199. Attendees = saAttendee,//邀請的所有人員
  200. ResponseRequested = false//如果發送方希望在接受或拒絕事件時響應,則設置為true
  201. };
  202. return _event;
  203. }
  204. public static Event BuildOutlookEvents(SqlSugarClient db, OTB_SYS_Calendar evnt, bool leave_privilege = false)
  205. {
  206. var saAttendee = new List<Attendee>();
  207. if (evnt.OpenMent == "G")
  208. {
  209. var saGroupMembers = evnt.GroupMembers.Split(new string[] { ",", "," }, StringSplitOptions.RemoveEmptyEntries);
  210. foreach (string member in saGroupMembers)
  211. {
  212. var oMembers = db.Queryable<OTB_SYS_Members>().Single(x => x.MemberID == member && x.OrgID == evnt.OrgID);
  213. if (oMembers == null)
  214. {//外部人員
  215. var oOuterUsers = db.Queryable<OTB_SYS_OuterUsers>().Single(x => x.Guid == member);
  216. if (oOuterUsers != null)
  217. {
  218. saAttendee.Add(new Attendee
  219. {
  220. EmailAddress = new EmailAddress
  221. {
  222. Address = oOuterUsers.Email,
  223. Name = oOuterUsers.UserName
  224. },
  225. Type = AttendeeType.Resource
  226. });
  227. }
  228. }
  229. else
  230. {//內部人員
  231. saAttendee.Add(new Attendee
  232. {
  233. EmailAddress = new EmailAddress
  234. {
  235. Address = oMembers.OutlookAccount,
  236. Name = oMembers.MemberName
  237. },
  238. Type = AttendeeType.Resource
  239. });
  240. }
  241. }
  242. }
  243. else if (evnt.OpenMent == "C")
  244. {
  245. var saMembers = db.Queryable<OTB_SYS_Members>().Where(x => x.Effective == "Y" && x.OrgID == evnt.OrgID).ToList();
  246. if (evnt.Memo == "leave")
  247. {//如果是請假的話,(依據系統設定抓去能看到所有資訊的人來分類同步)
  248. var oSystemSetting = db.Queryable<OTB_SYS_SystemSetting>().Single(x => x.Effective == "Y" && x.OrgID == evnt.OrgID && x.SettingItem == "Leave_Privilege");
  249. if (leave_privilege)
  250. {//特權
  251. if (oSystemSetting != null)
  252. {
  253. var saMembersPrivileges = saMembers.FindAll(x => oSystemSetting.SettingValue.IndexOf(x.MemberID) > -1);
  254. foreach (var privilege in saMembersPrivileges)
  255. {
  256. saAttendee.Add(new Attendee
  257. {
  258. EmailAddress = new EmailAddress
  259. {
  260. Address = privilege.OutlookAccount,
  261. Name = privilege.MemberName
  262. },
  263. Type = AttendeeType.Resource
  264. });
  265. }
  266. }
  267. }
  268. else
  269. {//非特權
  270. var saMembersUnPrivileges = saMembers;
  271. if (oSystemSetting != null)
  272. {
  273. var saMembersPrivileges = saMembers.FindAll(x => oSystemSetting.SettingValue.IndexOf(x.MemberID) == -1);
  274. }
  275. foreach (var unprivilege in saMembersUnPrivileges)
  276. {
  277. saAttendee.Add(new Attendee
  278. {
  279. EmailAddress = new EmailAddress
  280. {
  281. Address = unprivilege.OutlookAccount,
  282. Name = unprivilege.MemberName
  283. },
  284. Type = AttendeeType.Resource
  285. });
  286. }
  287. }
  288. }
  289. else
  290. {//其他
  291. foreach (var _user in saMembers)
  292. {//公司所有人
  293. saAttendee.Add(new Attendee
  294. {
  295. EmailAddress = new EmailAddress
  296. {
  297. Address = _user.OutlookAccount,
  298. Name = _user.MemberName
  299. },
  300. Type = AttendeeType.Resource
  301. });
  302. }
  303. }
  304. }
  305. var _event = new Event
  306. {
  307. Subject = evnt.Title,
  308. Importance = evnt.Importment == "H" ? Importance.High : Importance.Normal,
  309. IsAllDay = evnt.AllDay,
  310. Body = new ItemBody
  311. {
  312. ContentType = BodyType.HTML,
  313. Content = evnt.Description
  314. },
  315. BodyPreview = evnt.Description,
  316. Start = new DateTimeTimeZone
  317. {
  318. DateTime = evnt.StartDate.ToString("yyyy-MM-dd HH:mm:ss"),
  319. TimeZone = "Asia/Shanghai"
  320. },
  321. End = new DateTimeTimeZone
  322. {
  323. DateTime = evnt.EndDate.ToString("yyyy-MM-dd HH:mm:ss"),
  324. TimeZone = "Asia/Shanghai"
  325. },
  326. Attendees = saAttendee,
  327. ResponseRequested = false
  328. };
  329. return _event;
  330. }
  331. }
  332. }