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.

864 lines
37 KiB

2 years ago
2 years ago
  1. using EasyBL.WebApi.Common;
  2. using EasyBL.WebApi.Message;
  3. using EasyBL.WebApi.Models;
  4. using EasyNet;
  5. using Entity;
  6. using Entity.Sugar;
  7. using Newtonsoft.Json;
  8. using SqlSugar;
  9. using SqlSugar.Base;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Linq;
  13. using System.Net.Http;
  14. using System.Web;
  15. using System.Net;
  16. using System.Web.Http;
  17. namespace EasyBL.WEBAPP
  18. {
  19. public class AuthorizeService : ServiceBase
  20. {
  21. public HttpResponseMessage GetLogin([FromBody]dynamic i_value, HttpRequestMessage i_rRequest)
  22. {
  23. string sRes = null;
  24. try
  25. {
  26. string pm = CmdService.DecodeParm(i_value);
  27. var crm = JsonConvert.DeserializeObject<RequestMessage>(pm);
  28. crm.ClientIP = GetClientIp(i_rRequest);
  29. var auth = new AuthorizeService();
  30. sRes = JsonConvert.SerializeObject(auth.Entry(crm));
  31. }
  32. catch (Exception ex)
  33. {
  34. var exCur = ex;
  35. while (null != exCur.InnerException)
  36. {
  37. exCur = exCur.InnerException;
  38. }
  39. sRes = JsonConvert.SerializeObject(new ErrorResponseMessage(exCur.Message));
  40. }
  41. return new HttpResponseMessage
  42. {
  43. Content = new StringContent(sRes, System.Text.Encoding.UTF8, @"application/json")
  44. };
  45. }
  46. //public static string MyToString<T>(T str)
  47. //{
  48. // throw new NotSupportedException("Can only be used in expressions");
  49. //}
  50. #region 系統登入
  51. /// <summary>
  52. /// 函式名稱:Login
  53. /// 函式說明:系統登入
  54. /// </summary>
  55. /// <param name="i_crm">todo: describe i_crm parameter on Login</param>
  56. /// <returns>
  57. /// 回傳 rm(Object)
  58. ///</returns>
  59. public ResponseMessage Login(RequestMessage i_crm)
  60. {
  61. ResponseMessage rm = null;
  62. string sError = null;
  63. var db = SugarBase.GetIntance();
  64. try
  65. {
  66. do
  67. {
  68. var sOrgID = _fetchString(i_crm, @"OrgID");
  69. var sUserID = _fetchString(i_crm, @"UserID");
  70. var sPassword = _fetchString(i_crm, @"Pwd");
  71. var bOutklook = _fetchBool(i_crm, @"Outklook");
  72. var bRelogin = _fetchBool(i_crm, @"Relogin");
  73. var sIP = i_crm.ClientIP;
  74. if (string.IsNullOrWhiteSpace(sOrgID))
  75. {
  76. sError = @"組織代號不能為空"; //組織代號不能為空
  77. break;
  78. }
  79. if (string.IsNullOrWhiteSpace(sUserID))
  80. {
  81. sError = @"帳號不能為空"; //帳號不能為空
  82. break;
  83. }
  84. if (string.IsNullOrWhiteSpace(sPassword))
  85. {
  86. sError = @"密碼不能為空"; //密碼不能為空
  87. break;
  88. }
  89. var oOrg = db.Queryable<OTB_SYS_Organization>().Single(x => x.OrgID == sOrgID);
  90. if (oOrg == null)
  91. {
  92. sError = @"組織代號不正確"; //組織代號不正確
  93. break;
  94. }
  95. if (oOrg.Effective != @"Y")
  96. {
  97. sError = @"該組織無效"; //該組織無效
  98. break;
  99. }
  100. var sEncryptPwd = SecurityUtil.Encrypt(sPassword);//將輸入之密碼轉換驗證格式
  101. //string sPwd1 = SecurityUtil.Decrypt("wTBo6uXVBlVH8Ms76xiE4w==");
  102. //string sPwd1 = SecurityUtil.Decrypt("3EOyqH52VBUg3pj5Wy0rwQ==");
  103. var oUser = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == sOrgID && (x.MemberID == sUserID || x.Email == sUserID) && x.Password == sEncryptPwd);
  104. if (oUser == null)
  105. {
  106. sError = @"帳號或密碼不正確"; //帳號或密碼不正確
  107. break;
  108. }
  109. if (oUser.Effective != @"Y")
  110. {
  111. sError = @"該帳號無效"; //該帳號無效
  112. break;
  113. }
  114. if (bOutklook && string.IsNullOrWhiteSpace(oUser.OutlookAccount))
  115. {
  116. sError = @"Outlook帳號未設定,請管理員幫您設定Outlook帳號";
  117. break;
  118. }
  119. var sIsCheckNet = Common.GetAppSettings(@"IsCheckNet");
  120. if (sIsCheckNet == @"true")
  121. {
  122. var sUrl = HttpContext.Current.Request.Url.ToString();
  123. if (sUrl.IndexOf(@"localhost") == -1)
  124. {
  125. var sClientIP = sIP;
  126. if (!oUser.NetworkLogin)
  127. {
  128. var CheckList = new List<bool>();
  129. var LocalList = new List<IPAddressRange>()
  130. {
  131. new IPAddressRange(IPAddress.Parse("10.0.0.0"), IPAddress.Parse("10.255.255.255")),
  132. new IPAddressRange(IPAddress.Parse("172.16.0.0"), IPAddress.Parse("172.31.255.255")),
  133. new IPAddressRange(IPAddress.Parse("192.168.0.0"), IPAddress.Parse("192.168.255.255")),
  134. };
  135. foreach (var AddressRange in LocalList)
  136. {
  137. var PassThisRange = AddressRange.IsInRange(IPAddress.Parse(sClientIP));
  138. CheckList.Add(PassThisRange);
  139. }
  140. var Pass = CheckList.Any(c => c);
  141. if (!Pass)
  142. {
  143. sError = @"您的帳號不允許外網登錄"; //您的帳號不允許外網登錄
  144. break;
  145. }
  146. }
  147. }
  148. }
  149. var saOnlineUsers = db.Queryable<OTB_SYS_OnlineUsers>().Where(x => x.OrgID != sOrgID && x.UserID == oUser.MemberID).ToList();
  150. if (saOnlineUsers.Count > 0)
  151. {
  152. if (!bRelogin)
  153. {
  154. string sLocation = string.Empty;
  155. sError = @"Tips:此帳號已於";
  156. switch (saOnlineUsers.First().OrgID)
  157. {
  158. case "TE":
  159. sLocation = "台北奕達";
  160. break;
  161. case "TG":
  162. sLocation = "台北駒驛";
  163. break;
  164. case "SG":
  165. sLocation = "上海駒驛";
  166. break;
  167. case "SE":
  168. sLocation = "簡單平台";
  169. break;
  170. };
  171. //此帳號已於[上海駒驛]登入,請先登出該系統後再重新登入
  172. sError = $"{sError}{sLocation}登入,是否繼續登入當前賬號?";
  173. break;
  174. }
  175. else
  176. {
  177. db.Deleteable<OTB_SYS_OnlineUsers>(saOnlineUsers).ExecuteCommand();
  178. }
  179. }
  180. var ticket = new OTB_SYS_TicketAuth
  181. {
  182. OrgID = oOrg.OrgID,
  183. UserID = oUser.MemberID,
  184. UserName = oUser.MemberName,
  185. Token = SignExtension.CreateToken(),
  186. LoginIp = i_crm.ClientIP,
  187. LoginTime = DateTime.Now
  188. };
  189. var iExpireTime = 240;
  190. var sExpireTime = Common.GetSystemSetting(db, oOrg.OrgID, @"ExpireTime");
  191. if (!string.IsNullOrEmpty(sExpireTime))
  192. {
  193. iExpireTime = int.Parse(sExpireTime);
  194. }
  195. else
  196. {
  197. iExpireTime = int.Parse(Common.GetAppSettings(@"ExpireTime"));
  198. }
  199. ticket.ExpireTime = DateTime.Now.AddMinutes(iExpireTime); //30分钟过期
  200. ticket.IsVerify = @"Y";
  201. var oTicket = db.Queryable<OTB_SYS_TicketAuth>().Single(x => x.OrgID == sOrgID && x.UserID == oUser.MemberID);
  202. if (oTicket != null)
  203. {
  204. db.Updateable(ticket).IgnoreColumns(x => x.OutlookId).Where(x => x.NO == oTicket.NO).ExecuteCommand();
  205. }
  206. else
  207. {
  208. ticket.CreateTime = DateTime.Now;
  209. ticket = db.Insertable(ticket).ExecuteReturnEntity();
  210. }
  211. //記錄log日誌
  212. db.Insertable(new OTB_SYS_LoginLog
  213. {
  214. OrgId = ticket.OrgID,
  215. UserId = ticket.UserID,
  216. UserName = ticket.UserName,
  217. LoginIp = ticket.LoginIp,
  218. LoginTime = ticket.LoginTime
  219. }).ExecuteCommand();
  220. HttpRuntimeCache.Set(ticket.OrgID + ticket.UserID, ticket, iExpireTime * 60, true);
  221. HttpContext.Current.Session.Add(@"orgid", ticket.OrgID);
  222. HttpContext.Current.Session.Add(@"userid", ticket.UserID);
  223. HttpCookie cookie = new HttpCookie("EURO_COOKIE");//初始化並設置Cookie的名稱
  224. DateTime dt = DateTime.Now;
  225. TimeSpan ts = new TimeSpan(0, 0, 1, 0, 0);//過期時間為1分鐘
  226. cookie.Expires = dt.Add(ts);//設置過期時間
  227. cookie.Values.Add("orgid", ticket.OrgID);
  228. cookie.Values.Add("userid", ticket.UserID);
  229. HttpContext.Current.Response.AppendCookie(cookie);
  230. var jo = new SetMap
  231. {
  232. { @"orgid", ticket.OrgID },
  233. { @"userid", ticket.UserID },
  234. { @"loginname", ticket.UserName },
  235. { @"usertype", @"inner" },
  236. { @"mode", oUser.SysShowMode },
  237. { @"token", ticket.Token },
  238. { @"outklook", bOutklook }
  239. };
  240. rm = new SuccessResponseMessage(null, i_crm);
  241. rm.DATA.Add(BLWording.REL, jo);
  242. } while (false);
  243. }
  244. catch (Exception ex)
  245. {
  246. sError = Util.GetLastExceptionMsg(ex);
  247. LogAndSendEmail(sError + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(AuthorizeService), nameof(Login), @"Login(系統登入)", @"", @"", @"");
  248. }
  249. finally
  250. {
  251. if (null != sError)
  252. {
  253. rm = new ErrorResponseMessage(sError, i_crm);
  254. }
  255. }
  256. return rm;
  257. }
  258. #endregion 系統登入
  259. #region 獲取個人信息
  260. /// <summary>
  261. /// 函式名稱:Login
  262. /// 函式說明:系統登入
  263. /// </summary>
  264. /// <param name="i_crm">todo: describe i_crm parameter on GetUserInfo</param>
  265. /// <returns>
  266. /// 回傳 rm(Object)
  267. ///</returns>
  268. public ResponseMessage GetUserInfo(RequestMessage i_crm)
  269. {
  270. ResponseMessage rm = null;
  271. string sError = null;
  272. var db = SugarBase.GetIntance();
  273. try
  274. {
  275. do
  276. {
  277. var userInfo = db.Queryable<OTB_SYS_Members, OTB_SYS_Departments, OTB_SYS_Jobtitle>((a, b, c) => new object[] {
  278. JoinType.Left, a.OrgID==b.OrgID && a.DepartmentID==b.DepartmentID,
  279. JoinType.Left, a.OrgID==c.OrgID && a.JobTitle==c.JobtitleID
  280. })
  281. .Where((a) => a.OrgID == i_crm.ORIGID && a.MemberID == i_crm.USERID)
  282. .Select((a, b, c) =>
  283. new UserInfo
  284. {
  285. MemberID = a.MemberID,
  286. MemberName = a.MemberName,
  287. Email = a.Email,
  288. OutlookAccount = a.OutlookAccount,
  289. DepartmentID = a.DepartmentID,
  290. Effective = a.Effective,
  291. CalColor = a.CalColor,
  292. MemberPic = a.MemberPic,
  293. SysShowMode = a.SysShowMode,
  294. Country = a.Country,
  295. ServiceCode = a.ServiceCode,
  296. Address = a.Address,
  297. DepartmentName = b.DepartmentName,
  298. JobtitleName = c.JobtitleName,
  299. Supervisors = a.ImmediateSupervisor + @","
  300. }).Single();
  301. if (userInfo != null)
  302. {
  303. var saRoles = db.Queryable<OTB_SYS_MembersToRule>()
  304. .Where(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID)
  305. .Select(x => x.RuleID)
  306. .ToList();
  307. userInfo.roles = string.Join(@",", saRoles);
  308. var saDepartments = db.Queryable<OTB_SYS_Departments>()
  309. .Where(x => x.OrgID == i_crm.ORIGID && x.ChiefOfDepartmentID == i_crm.USERID)
  310. .Select(x => x.DepartmentID)
  311. .ToList();
  312. var saUsersDown = db.Queryable<OTB_SYS_Members>()
  313. .Where(x => x.OrgID == i_crm.ORIGID && saDepartments.Contains(x.DepartmentID))
  314. .Select(x => SqlFunc.IsNull(x.MemberID, @""))
  315. .ToList();
  316. userInfo.UsersDown = string.Join(@",", saUsersDown);
  317. var saUsersBranch = db.Queryable<OTB_SYS_Members>()
  318. .Where(x => x.OrgID == i_crm.ORIGID && x.ImmediateSupervisor == i_crm.USERID)
  319. .Select(x => SqlFunc.IsNull(x.MemberID, @""))
  320. .ToList();
  321. userInfo.UsersBranch = string.Join(@",", saUsersBranch);
  322. var oDepartments = db.Queryable<OTB_SYS_Departments>().Single(x => x.OrgID == i_crm.ORIGID && x.DepartmentID == userInfo.DepartmentID);
  323. userInfo.Supervisors += oDepartments.ChiefOfDepartmentID ?? @"";
  324. }
  325. rm = new SuccessResponseMessage(null, i_crm);
  326. rm.DATA.Add(BLWording.REL, userInfo);
  327. } while (false);
  328. }
  329. catch (Exception ex)
  330. {
  331. sError = Util.GetLastExceptionMsg(ex);
  332. LogAndSendEmail(sError + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(AuthorizeService), @"", @"GetUserInfo(獲取個人信息)", @"", @"", @"");
  333. }
  334. finally
  335. {
  336. if (null != sError)
  337. {
  338. rm = new ErrorResponseMessage(sError, i_crm);
  339. }
  340. }
  341. return rm;
  342. }
  343. #endregion 獲取個人信息
  344. #region 獲取程式權限
  345. /// <summary>
  346. /// 函式名稱:UpdataPsw
  347. /// 函式說明:獲取程式權限
  348. /// </summary>
  349. /// <param name="i_crm">todo: describe i_crm parameter on GetAuthorize</param>
  350. /// <returns>
  351. /// 回傳 rm(Object)
  352. ///</returns>
  353. public ResponseMessage GetAuthorize(RequestMessage i_crm)
  354. {
  355. ResponseMessage rm = null;
  356. string sMsg = null;
  357. var db = SugarBase.GetIntance();
  358. try
  359. {
  360. do
  361. {
  362. var sProgramID = _fetchString(i_crm, @"ProgramID");
  363. var sTopModuleID = _fetchString(i_crm, @"TopModuleID");
  364. var saRoles = db.Queryable<OTB_SYS_MembersToRule>()
  365. .Where(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID)
  366. .Select(x => x.RuleID)
  367. .ToList();
  368. var oDepartments = db.Queryable<OTB_SYS_Members>().Single(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID);
  369. var saAuthorize = db.UnionAll(
  370. db.Queryable<OTB_SYS_Authorize>()
  371. .Where(x => x.OrgID == i_crm.ORIGID && x.ProgramID == sProgramID && x.TopModuleID == sTopModuleID && x.AllowRight != @"")
  372. .Where(x => saRoles.Contains(x.RuleID))
  373. .Select(x => new AuthorizeInfo
  374. {
  375. RuleID = x.RuleID,
  376. ProgramID = x.ProgramID,
  377. AllowRight = x.AllowRight,
  378. TopModuleID = x.TopModuleID
  379. }),
  380. db.Queryable<OTB_SYS_AuthorizeForDept>()
  381. .Where(x => x.OrgID == i_crm.ORIGID && x.ProgramID == sProgramID && x.TopModuleID == sTopModuleID && x.AllowRight != @"" && x.DepartmentID == oDepartments.DepartmentID)
  382. .Select(x => new AuthorizeInfo
  383. {
  384. RuleID = x.DepartmentID,
  385. ProgramID = x.ProgramID,
  386. AllowRight = x.AllowRight,
  387. TopModuleID = x.TopModuleID
  388. }),
  389. db.Queryable<OTB_SYS_AuthorizeForMember>()
  390. .Where(x => x.OrgID == i_crm.ORIGID && x.ProgramID == sProgramID && x.TopModuleID == sTopModuleID && x.AllowRight != @"" && x.MemberID == i_crm.USERID)
  391. .Select(x => new AuthorizeInfo
  392. {
  393. RuleID = x.MemberID,
  394. ProgramID = x.ProgramID,
  395. AllowRight = x.AllowRight,
  396. TopModuleID = x.TopModuleID
  397. })
  398. ).ToList();
  399. rm = new SuccessResponseMessage(null, i_crm);
  400. rm.DATA.Add(BLWording.REL, saAuthorize);
  401. } while (false);
  402. }
  403. catch (Exception ex)
  404. {
  405. sMsg = Util.GetLastExceptionMsg(ex);
  406. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(AuthorizeService), @"", @"GetAuthorize(獲取程式權限)", @"", @"", @"");
  407. }
  408. finally
  409. {
  410. if (null != sMsg)
  411. {
  412. rm = new ErrorResponseMessage(sMsg, i_crm);
  413. }
  414. }
  415. return rm;
  416. }
  417. #endregion 獲取程式權限
  418. #region 修改個人密碼
  419. /// <summary>
  420. /// 函式名稱:UpdataPsw
  421. /// 函式說明:修改個人密碼
  422. /// </summary>
  423. /// <param name="i_crm">todo: describe i_crm parameter on UpdataPsw</param>
  424. /// <returns>
  425. /// 回傳 rm(Object)
  426. ///</returns>
  427. public ResponseMessage UpdataPsw(RequestMessage i_crm)
  428. {
  429. ResponseMessage rm = null;
  430. string sMsg = null;
  431. var db = SugarBase.GetIntance();
  432. try
  433. {
  434. do
  435. {
  436. var sUserName = _fetchString(i_crm, @"UserName");
  437. var sOldPsw = _fetchString(i_crm, @"OldPsw");
  438. var sNewPsw = _fetchString(i_crm, @"NewPsw");
  439. var sCalColor = _fetchString(i_crm, @"CalColor");
  440. var sMemberPic = _fetchString(i_crm, @"MemberPic");
  441. var oUser = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == i_crm.ORIGID && it.MemberID == i_crm.USERID);
  442. var dicUpdcols = new Dictionary<string, object>();
  443. var sNewPwd_Encrypt = @"";
  444. if (sOldPsw != @"" && sNewPsw != @"")
  445. {
  446. var sOldPwd = SecurityUtil.Encrypt(sOldPsw);//將輸入之密碼轉換驗證格式
  447. if (oUser.Password != sOldPwd) //舊密碼驗證失敗
  448. {
  449. sMsg = @"1";
  450. break;
  451. }
  452. sNewPwd_Encrypt = SecurityUtil.Encrypt(sNewPsw);//將輸入之密碼轉換驗證格式
  453. dicUpdcols.Add(OTB_SYS_Members.CN_PASSWORD, sNewPwd_Encrypt);
  454. }
  455. dicUpdcols.Add(OTB_SYS_Members.CN_MEMBERNAME, sUserName);
  456. dicUpdcols.Add(OTB_SYS_Members.CN_CALCOLOR, sCalColor);
  457. dicUpdcols.Add(OTB_SYS_Members.CN_MEMBERPIC, sMemberPic);
  458. var iRel = db.Updateable<OTB_SYS_Members>(dicUpdcols)
  459. .Where(x => x.OrgID == i_crm.ORIGID && x.MemberID == i_crm.USERID).ExecuteCommand();
  460. if (iRel <= 0)
  461. {
  462. sMsg = @"2";
  463. break;
  464. }
  465. rm = new SuccessResponseMessage(null, i_crm);
  466. } while (false);
  467. }
  468. catch (Exception ex)
  469. {
  470. sMsg = Util.GetLastExceptionMsg(ex);
  471. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(AuthorizeService), nameof(Login), @"UpdataPsw(修改個人資料)", @"", @"", @"");
  472. }
  473. finally
  474. {
  475. if (null != sMsg)
  476. {
  477. rm = new ErrorResponseMessage(sMsg, i_crm);
  478. }
  479. }
  480. return rm;
  481. }
  482. #endregion 修改個人密碼
  483. #region 驗證會員帳號
  484. /// <summary>
  485. /// 函式名稱:CheckMember
  486. /// 函式說明:驗證會員帳號
  487. /// </summary>
  488. /// <param name="i_crm">todo: describe i_crm parameter on CheckMember</param>
  489. /// <returns>
  490. /// 回傳 rm(Object)
  491. ///</returns>
  492. public ResponseMessage CheckMember(RequestMessage i_crm)
  493. {
  494. ResponseMessage rm = null;
  495. string sMsg = null;
  496. try
  497. {
  498. rm = SugarBase.ExecTran(db =>
  499. {
  500. do
  501. {
  502. var sOrgID = _fetchString(i_crm, @"OrgID");
  503. var sUserID = _fetchString(i_crm, @"UserID");
  504. //載入資訊
  505. var oUser = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == sOrgID && it.MemberID == sUserID);
  506. if (oUser == null) //驗證帳號或密碼是否正確
  507. {
  508. sMsg = @"1";
  509. break;
  510. }
  511. db.Deleteable<OTB_SYS_ForgetPassword>().Where(x => x.OrgID == sOrgID && x.MemberID == sUserID).ExecuteCommand();
  512. var sRomd = SecurityUtil.GetRandomString(6);
  513. var oForgetPasswordAdd = new OTB_SYS_ForgetPassword
  514. {
  515. OrgID = sOrgID,
  516. MemberID = sUserID,
  517. VerificationCode = SecurityUtil.Encrypt(sRomd),//將輸入之密碼轉換驗證格式
  518. ModifyDate = DateTime.Now
  519. };
  520. var oForgetPassword = db.Insertable(oForgetPasswordAdd).ExecuteReturnEntity();
  521. if (oForgetPassword == null) // 刪除或新增驗證碼成功與否
  522. {
  523. sMsg = @"2";
  524. break;//儲存失敗
  525. }
  526. //儲存驗證碼成功準備寄信
  527. var oEmailInfo = db.Queryable<OTB_SYS_Email>().Single(x => x.OrgID == sOrgID && x.EmailID == @"getNewPsw");
  528. if (oEmailInfo == null)
  529. {
  530. sMsg = @"系統找不到對應的郵件模版";
  531. break;//儲存失敗
  532. }
  533. var sEmailBody = @"";
  534. sEmailBody = oEmailInfo.BodyHtml.Replace(@"{{:UserName}}", oUser.MemberName).Replace(@"{{:MemberPwd}}", sRomd);
  535. var oEmail = new Emails();
  536. var saEmailTo = new List<EmailTo>();
  537. //收件人
  538. var oEmailTo = new EmailTo
  539. {
  540. ToUserID = oUser.MemberID,
  541. ToUserName = oUser.MemberName,
  542. ToEmail = oUser.Email,
  543. Type = @"to"
  544. };
  545. saEmailTo.Add(oEmailTo);
  546. oEmail.FromUserName = @"系統自動發送";//取fonfig
  547. oEmail.Title = @"驗證碼";//取fonfig
  548. oEmail.EmailBody = sEmailBody;
  549. oEmail.IsCCSelf = false;
  550. oEmail.Attachments = null;
  551. oEmail.EmailTo = saEmailTo;
  552. var bSend = new MailService(sOrgID).MailFactory(oEmail, out sMsg);
  553. if (sMsg != null)
  554. {
  555. break;
  556. }
  557. oForgetPassword.ModifyDate = DateTime.Now;
  558. db.Updateable(oForgetPassword).UpdateColumns(x => x.ModifyDate).ExecuteCommand();
  559. rm = new SuccessResponseMessage(null, i_crm);
  560. } while (false);
  561. return rm;
  562. });
  563. }
  564. catch (Exception ex)
  565. {
  566. sMsg = Util.GetLastExceptionMsg(ex);
  567. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(AuthorizeService), nameof(Login), @"CheckMember(驗證會員帳號)", @"", @"", @"");
  568. }
  569. finally
  570. {
  571. if (null != sMsg)
  572. {
  573. rm = new ErrorResponseMessage(sMsg, i_crm);
  574. }
  575. }
  576. return rm;
  577. }
  578. #endregion 驗證會員帳號
  579. #region 重設密碼
  580. /// <summary>
  581. /// 函式名稱:Check
  582. /// 函式說明:重設密碼
  583. /// </summary>
  584. /// <param name="i_crm">todo: describe i_crm parameter on ReSetPassword</param>
  585. /// <returns>
  586. /// 回傳 rm(Object)
  587. ///</returns>
  588. public ResponseMessage ReSetPassword(RequestMessage i_crm)
  589. {
  590. ResponseMessage rm = null;
  591. string sMsg = null;
  592. try
  593. {
  594. rm = SugarBase.ExecTran(db =>
  595. {
  596. do
  597. {
  598. var sOrgID = _fetchString(i_crm, @"OrgID");
  599. var sUserID = _fetchString(i_crm, @"UserID");
  600. var sVerificationCode = _fetchString(i_crm, @"VerificationCode");
  601. var sNewPsw = _fetchString(i_crm, @"NewPsw");
  602. var oUser = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == sOrgID && it.MemberID == sUserID);
  603. if (oUser.MemberID == null) //查無此會員資料或MemberID為空
  604. {
  605. sMsg = @"1";
  606. break;
  607. }
  608. var oForgetPassword = db.Queryable<OTB_SYS_ForgetPassword>().Single(x => x.OrgID == sOrgID && x.MemberID == oUser.MemberID);
  609. if (oForgetPassword == null)
  610. {
  611. sMsg = @"4";
  612. break;
  613. }
  614. var sEncryptVerificationCode = SecurityUtil.Encrypt(sVerificationCode);//將輸入之密碼轉換驗證格式
  615. if (oForgetPassword.MemberID != sUserID || oForgetPassword.VerificationCode != sEncryptVerificationCode)
  616. {
  617. sMsg = @"0";
  618. break;
  619. }
  620. var Time = DateTime.Now;
  621. var DataNow = new TimeSpan(Time.Ticks);
  622. var CeateDate = new TimeSpan(oForgetPassword.ModifyDate.Value.Ticks);
  623. var ts = DataNow - CeateDate;
  624. var ts5 = DataNow.Subtract(CeateDate);
  625. var RunTime = int.Parse(ts5.TotalSeconds.ToString().Split('.')[0].ToString());
  626. if (RunTime > 60) //驗證碼超出限制時間刪除該筆資料
  627. {
  628. var iDel = db.Deleteable<OTB_SYS_ForgetPassword>().Where(x => x.OrgID == sOrgID && x.MemberID == sUserID).ExecuteCommand();
  629. if (iDel > 0)
  630. {
  631. sMsg = @"2";
  632. break;
  633. }
  634. }
  635. var sNewPwd = SecurityUtil.Encrypt(sNewPsw);//將輸入之密碼轉換驗證格式
  636. var oMembers = new OTB_SYS_Members
  637. {
  638. Password = sNewPwd
  639. };
  640. var iRel = db.Updateable(oMembers).UpdateColumns(x => new { x.Password }).Where(x => x.OrgID == sOrgID && x.MemberID == sUserID).ExecuteCommand();
  641. if (iRel == 0) //更新資料失敗
  642. {
  643. sMsg = @"3";
  644. break;
  645. }
  646. var iDel2 = db.Deleteable<OTB_SYS_ForgetPassword>().Where(x => x.OrgID == sOrgID && x.MemberID == sUserID).ExecuteCommand();
  647. rm = new SuccessResponseMessage(null, i_crm);
  648. } while (false);
  649. return rm;
  650. });
  651. }
  652. catch (Exception ex)
  653. {
  654. sMsg = Util.GetLastExceptionMsg(ex);
  655. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(AuthorizeService), nameof(Login), @"Check(驗證碼時間檢驗)", @"", @"", @"");
  656. }
  657. finally
  658. {
  659. if (null != sMsg)
  660. {
  661. rm = new ErrorResponseMessage(sMsg, i_crm);
  662. }
  663. }
  664. return rm;
  665. }
  666. #endregion 重設密碼
  667. #region 新增帳號寄送初始密碼給新帳號人員
  668. /// <summary>
  669. /// 函式名稱:SendPswToNewMember
  670. /// 函式說明:新增帳號寄送初始密碼給新帳號人員
  671. /// </summary>
  672. /// <param name="i_crm">todo: describe i_crm parameter on SendPswToNewMember</param>
  673. /// <returns>
  674. /// 回傳 rm(Object)
  675. ///</returns>
  676. public ResponseMessage SendPswToNewMember(RequestMessage i_crm)
  677. {
  678. ResponseMessage rm = null;
  679. string sMsg = null;
  680. var db = SugarBase.GetIntance();
  681. try
  682. {
  683. do
  684. {
  685. var sOrgID = _fetchString(i_crm, @"OrgID");
  686. var sUserID = _fetchString(i_crm, @"UserID");
  687. var sEmailBody = @"";
  688. var oUser = db.Queryable<OTB_SYS_Members>().Single(it => it.OrgID == i_crm.ORIGID && it.MemberID == i_crm.USERID);
  689. var oEmail_O = db.Queryable<OTB_SYS_Email>().Single(x => x.OrgID == sOrgID && x.EmailID == @"Member");
  690. if (oEmail_O == null)
  691. {
  692. sMsg = @"系統找不到對應的郵件模版";
  693. break;
  694. }
  695. sEmailBody = oEmail_O.BodyHtml
  696. .Replace(@"{{:UserName}}", oUser.MemberName)
  697. .Replace(@"{{:MemberPwd}}", SecurityUtil.Decrypt(oUser.Password));
  698. var oEmail = new Emails();
  699. var saEmailTo = new List<EmailTo>();
  700. //收件人
  701. var oEmailTo = new EmailTo
  702. {
  703. ToUserID = oUser.MemberID,
  704. ToUserName = oUser.MemberName,
  705. ToEmail = oUser.Email,
  706. Type = @"to"
  707. };
  708. saEmailTo.Add(oEmailTo);
  709. oEmail.FromUserName = @"系統自動發送";//取fonfig
  710. oEmail.Title = @"初始密碼";//取fonfig
  711. oEmail.EmailBody = sEmailBody;
  712. oEmail.IsCCSelf = false;
  713. oEmail.Attachments = null;
  714. oEmail.EmailTo = saEmailTo;
  715. var bSend = new MailService(i_crm.ORIGID).MailFactory(oEmail, out sMsg);
  716. if (sMsg != null)
  717. {
  718. break;
  719. }
  720. rm = new SuccessResponseMessage(null, i_crm);
  721. rm.DATA.Add(BLWording.REL, bSend);
  722. } while (false);
  723. }
  724. catch (Exception ex)
  725. {
  726. sMsg = Util.GetLastExceptionMsg(ex);
  727. LogAndSendEmail(sMsg + @"Param:" + JsonToString(i_crm), ex, i_crm.ORIGID, i_crm.USERID, nameof(AuthorizeService), nameof(Login), @"SendPswToNewMember(新增帳號寄送初始密碼給新帳號人員)", @"", @"", @"");
  728. }
  729. finally
  730. {
  731. if (null != sMsg)
  732. {
  733. rm = new ErrorResponseMessage(sMsg, i_crm);
  734. }
  735. }
  736. return rm;
  737. }
  738. #endregion 新增帳號寄送初始密碼給新帳號人員
  739. /// <summary>
  740. /// </summary>
  741. /// <param name="i_rRequest"></param>
  742. /// <returns></returns>
  743. public static string GetClientIp(HttpRequestMessage i_rRequest) => i_rRequest.Properties.ContainsKey(@"MS_HttpContext") ? ((HttpContextWrapper)i_rRequest.Properties[@"MS_HttpContext"]).Request.UserHostAddress : HttpContext.Current?.Request.UserHostAddress;
  744. private class UserInfo
  745. {
  746. public UserInfo()
  747. {
  748. roles = @"";
  749. Supervisors = @"";
  750. UsersDown = @"";
  751. UsersBranch = @"";
  752. }
  753. public string MemberID { get; set; }
  754. public string MemberName { get; set; }
  755. public string Email { get; set; }
  756. public string DepartmentID { get; set; }
  757. public string Effective { get; set; }
  758. public string CalColor { get; set; }
  759. public string MemberPic { get; set; }
  760. public string SysShowMode { get; set; }
  761. public string Country { get; set; }
  762. public string ServiceCode { get; set; }
  763. public string Address { get; set; }
  764. public string DepartmentName { get; set; }
  765. public string JobtitleName { get; set; }
  766. public string roles { get; set; }
  767. public string Supervisors { get; set; }
  768. public string UsersDown { get; set; }
  769. public string UsersBranch { get; set; }
  770. public string OutlookAccount { get; set; }
  771. }
  772. private class AuthorizeInfo
  773. {
  774. public string RuleID { get; set; }
  775. public string ProgramID { get; set; }
  776. public string AllowRight { get; set; }
  777. public string TopModuleID { get; set; }
  778. }
  779. }
  780. }