UserController.cs 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Linq;
  5. using System.Net;
  6. using System.Runtime.Serialization.Json;
  7. using System.Text;
  8. using System.Threading.Tasks;
  9. using System.Web;
  10. using CZFW.Core;
  11. using CZFW.Framework.Interface;
  12. using CZKJ.GBRS2.Interface;
  13. using CZKJ.GBRS2.Model;
  14. using CZKJ.GBRS2.ViewModel;
  15. using Microsoft.AspNetCore.Http;
  16. using Microsoft.AspNetCore.Mvc;
  17. using Newtonsoft.Json;
  18. using Newtonsoft.Json.Linq;
  19. namespace CZKJ.GBRS2.WebMVC.Controllers
  20. {
  21. public class UserController : Controller
  22. {
  23. ICustomerQuery _logic;
  24. ISysConfig _sysConfigLogic;
  25. IUserInfoCustomer _clogic;
  26. public UserController(ICustomerQuery logic, ISysConfig sysConfigLogic, IUserInfoCustomer infoCustomer)
  27. {
  28. _logic = logic;
  29. _sysConfigLogic = sysConfigLogic;
  30. _clogic = infoCustomer;
  31. }
  32. /// <summary>
  33. /// 登录
  34. /// </summary>
  35. /// <param name="code"></param>
  36. /// <returns></returns>
  37. public IActionResult Index(string code = null)
  38. {
  39. string session = HttpContext.Session.GetString("GBRS2018");
  40. //个人登录跳转链接
  41. var loginurl = ConfigHelper.GetValue<string>("LoginCallback:Url");
  42. if (session != null)
  43. {
  44. var userinfo = JsonConvert.DeserializeObject<UserInfoModel>(session);
  45. var userIndexUrl = $"{loginurl}?personalId={userinfo.User_id}";
  46. return Redirect(userIndexUrl);
  47. //return View(JsonConvert.DeserializeObject<UserInfoModel>(session));
  48. }
  49. //获取当前请求地址
  50. var url = HttpUtility.UrlEncode($"{ConfigHelper.GetValue<string>("Login:Scheme")}://{Request.Host}{Request.Path}").Replace("%3a", "%3A").Replace("%2f", "%2F");
  51. //判断code是否为空
  52. if (string.IsNullOrEmpty(code))
  53. {
  54. //拼接获取Code地址
  55. // var redirectUrl = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/user/passport/login?client_id={ConfigHelper.GetValue<string>("Login:ClientId")}&redirect_uri={url}&response_type=code";
  56. var redirectUrl = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/user/passport/login?client_id={ConfigHelper.GetValue<string>("Login:ClientId")}&redirect_uri={url}&response_type=code";
  57. //跳转获取Code
  58. return Redirect(redirectUrl);
  59. }
  60. // CustomerInfoViewModel customerInfoViewModel = new CustomerInfoViewModel();
  61. //拼接获取accessToken地址
  62. var getAccessToken = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/api/oauth20/accesstoken/code?code={code}&client_id={ConfigHelper.GetValue<string>("Login:ClientId")}&client_secret={ConfigHelper.GetValue<string>("Login:PassWord")}&redirect_uri={url}&grant_type=authorization_code";
  63. //通过POST调用地址获取实体
  64. var accessTokenInfo = HttpHelper.HttpPost(getAccessToken);
  65. //反序列化
  66. DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(AccessTokenModel));
  67. MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(accessTokenInfo));
  68. AccessTokenModel accessTokenModel = (AccessTokenModel)dataContractJsonSerializer.ReadObject(ms);
  69. //拼接通过accessToken获取用户信息地址
  70. var getUserInfoUrl = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/api/oauth20/getuserinfo?access_token={accessTokenModel.access_token}";
  71. //通过Get请求获取用户信息
  72. var userInfo = HttpHelper.HttpGet(getUserInfoUrl);
  73. //把accessToken 写入session
  74. HttpContext.Session.SetString("token", accessTokenModel.access_token == null ? "" : accessTokenModel.access_token);
  75. //反序列化用户信息
  76. var user = JsonConvert.DeserializeObject<UserInfoModel>(userInfo);
  77. switch (user.Type)
  78. {
  79. case "1":
  80. user.Type = "客户";
  81. break;
  82. case "2":
  83. user.Type = "国宝内勤";
  84. break;
  85. case "3":
  86. user.Type = "国宝外勤";
  87. break;
  88. case "4":
  89. user.Type = "企业用户";
  90. break;
  91. case "5":
  92. user.Type = "合作伙伴内勤";
  93. break;
  94. case "6":
  95. user.Type = "合作伙伴外勤";
  96. break;
  97. }
  98. switch (user.Customer_info.Gender)
  99. {
  100. case "1":
  101. user.Customer_info.Gender = "男";
  102. break;
  103. case "2":
  104. user.Customer_info.Gender = "女";
  105. break;
  106. }
  107. switch (user.Customer_info.Id_type)
  108. {
  109. case "0":
  110. user.Customer_info.Id_type = "居民身份证";
  111. break;
  112. case "1":
  113. user.Customer_info.Id_type = "护照";
  114. break;
  115. case "2":
  116. user.Customer_info.Id_type = "军人证";
  117. break;
  118. case "3":
  119. user.Customer_info.Id_type = "居民户口薄";
  120. break;
  121. case "4":
  122. user.Customer_info.Id_type = "通行证";
  123. break;
  124. case "5":
  125. user.Customer_info.Id_type = "出生证明";
  126. break;
  127. case "8":
  128. user.Customer_info.Id_type = "其他";
  129. break;
  130. }
  131. var Id = user.Customer_info.Id_no;
  132. if (!string.IsNullOrEmpty(Id))
  133. {
  134. user.Customer_info.Id_no = Id.Substring(0, 6) + "********" + Id.Substring(14, 4);
  135. }
  136. var phone = user.Mobile;
  137. if (!string.IsNullOrEmpty(phone))
  138. {
  139. user.Mobile = phone.Substring(0, 3) + "****" + phone.Substring(7, 4);
  140. }
  141. HttpContext.Session.SetString("GBRS2018", JsonConvert.SerializeObject(user));
  142. var queryUser = _clogic.GetCuser(user.User_id);
  143. //if (queryUser == null)
  144. //{
  145. // _clogic.AddModel(user);
  146. //}
  147. //else
  148. //{
  149. // queryUser.LastLoginTime = DateTime.Now;
  150. // //_clogic.ExcuteSql($"UPDATE gbrs2_customerinfo SET LastLoginTime='{DateTime.Now.ToString()}' WHERE User_id={user.User_id}");
  151. // _clogic.EditEntity(queryUser);
  152. //}
  153. //if (string.IsNullOrWhiteSpace(user.Core_customer_code))
  154. //{
  155. // return View("Notice",user);
  156. //}
  157. //:http://uat01.guobaojinrong.com/wef/pc/api/personaluser/callback?personalId=xxxx&openId=xxx
  158. var personalRedirectUrl = $"{loginurl}?personalId={user.User_id}";
  159. return Redirect(personalRedirectUrl);
  160. //return View(user);
  161. }
  162. //回调官网
  163. public IActionResult CallBackHome(string personalId)
  164. {
  165. string session = HttpContext.Session.GetString("GBRS2018");
  166. if (session != null)
  167. {
  168. var user = JsonConvert.DeserializeObject<UserInfoModel>(session);
  169. if (user.User_id != personalId)
  170. {
  171. //从session获取用户信息
  172. string sessiontoken = HttpContext.Session.GetString("token");
  173. //拼接登出链接
  174. var logOutUrl = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/api/oauth20/logout?access_token={sessiontoken}";
  175. var res = HttpHelper.HttpGet(logOutUrl);
  176. HttpContext.Session.Remove("GBRS2018");
  177. }
  178. }
  179. return Redirect("/");
  180. }
  181. /// <summary>
  182. /// 登出
  183. /// </summary>
  184. /// <returns></returns>
  185. public IActionResult LogOut(string personalId)
  186. {
  187. //从session获取用户信息
  188. string session = HttpContext.Session.GetString("token");
  189. //拼接登出链接
  190. var logOutUrl = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/api/oauth20/logout?access_token={session}";
  191. var res = HttpHelper.HttpGet(logOutUrl);
  192. HttpContext.Session.Remove("GBRS2018");
  193. //通过Get请求调用接口
  194. return RedirectToAction("Index", "Home");
  195. }
  196. /// <summary>
  197. /// 注册
  198. /// </summary>
  199. /// <param name="code"></param>
  200. /// <returns></returns>
  201. public IActionResult Register(string code = null, string beforeUrl = null)
  202. {
  203. //if (!string.IsNullOrEmpty(beforeUrl))
  204. //{
  205. // HttpContext.Session.SetString("before", beforeUrl);
  206. //}
  207. //获取当前请求地址
  208. var url = HttpUtility.UrlEncode($"{ConfigHelper.GetValue<string>("Login:Scheme")}://{Request.Host}{Request.Path}").Replace("%3a", "%3A").Replace("%2f", "%2F");
  209. //判断code是否为空
  210. if (string.IsNullOrEmpty(code))
  211. {
  212. //拼接获取Code地址
  213. // var redirectUrl = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/user/passport/register?client_id=201803236916747755&redirect_uri={url}&response_type=code";
  214. var redirectUrl = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/user/passport/register?client_id={ConfigHelper.GetValue<string>("Login:ClientId")}&redirect_uri={url}&response_type=code";
  215. //跳转获取Code
  216. return Redirect(redirectUrl);
  217. }
  218. //拼接获取accessToken地址
  219. var getAccessToken = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/api/oauth20/accesstoken/code?code={code}&grant_type=authorization_code&client_id={ConfigHelper.GetValue<string>("Login:ClientId")}&client_secret={ConfigHelper.GetValue<string>("Login:PassWord")}&redirect_uri={url}";
  220. //通过POST调用地址获取实体
  221. var accessTokenInfo = HttpHelper.HttpPost(getAccessToken);
  222. //反序列化
  223. DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(AccessTokenModel));
  224. MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(accessTokenInfo));
  225. AccessTokenModel accessTokenModel = (AccessTokenModel)dataContractJsonSerializer.ReadObject(ms);
  226. //拼接通过accessToken获取用户信息地址
  227. var getUserInfoUrl = $"{ConfigHelper.GetValue<string>("Login:Url")}/uai/api/oauth20/getuserinfo?access_token={accessTokenModel.access_token}";
  228. //通过Get请求获取用户信息
  229. var userInfo = HttpHelper.HttpGet(getUserInfoUrl);
  230. //把accessToken 写入session
  231. //string session = HttpContext.Session.GetString("before");
  232. //if (!string.IsNullOrEmpty(beforeUrl))
  233. //{
  234. // return Redirect(beforeUrl);
  235. //}
  236. //else
  237. //{
  238. // return RedirectToAction("Index", "Home");
  239. //}
  240. HttpContext.Session.SetString("token", accessTokenModel.access_token == null ? "" : accessTokenModel.access_token);
  241. //反序列化用户信息
  242. var user = JsonConvert.DeserializeObject<UserInfoModel>(userInfo);
  243. switch (user.Type)
  244. {
  245. case "1":
  246. user.Type = "客户";
  247. break;
  248. case "2":
  249. user.Type = "国宝内勤";
  250. break;
  251. case "3":
  252. user.Type = "国宝外勤";
  253. break;
  254. case "4":
  255. user.Type = "企业用户";
  256. break;
  257. case "5":
  258. user.Type = "合作伙伴内勤";
  259. break;
  260. case "6":
  261. user.Type = "合作伙伴外勤";
  262. break;
  263. }
  264. switch (user.Customer_info.Gender)
  265. {
  266. case "1":
  267. user.Customer_info.Gender = "男";
  268. break;
  269. case "2":
  270. user.Customer_info.Gender = "女";
  271. break;
  272. }
  273. switch (user.Customer_info.Id_type)
  274. {
  275. case "0":
  276. user.Customer_info.Id_type = "居民身份证";
  277. break;
  278. case "1":
  279. user.Customer_info.Id_type = "护照";
  280. break;
  281. case "2":
  282. user.Customer_info.Id_type = "军人证";
  283. break;
  284. case "3":
  285. user.Customer_info.Id_type = "居民户口薄";
  286. break;
  287. case "4":
  288. user.Customer_info.Id_type = "通行证";
  289. break;
  290. case "5":
  291. user.Customer_info.Id_type = "出生证明";
  292. break;
  293. case "8":
  294. user.Customer_info.Id_type = "其他";
  295. break;
  296. }
  297. var Id = user.Customer_info.Id_no;
  298. if (!string.IsNullOrEmpty(Id))
  299. {
  300. user.Customer_info.Id_no = Id.Substring(0, 6) + "********" + Id.Substring(14, 4);
  301. }
  302. var phone = user.Mobile;
  303. if (!string.IsNullOrEmpty(phone))
  304. {
  305. user.Mobile = phone.Substring(0, 3) + "****" + phone.Substring(7, 4);
  306. }
  307. HttpContext.Session.SetString("GBRS2018", JsonConvert.SerializeObject(user));
  308. var queryUser = _clogic.GetCuser(user.User_id);
  309. if (queryUser == null)
  310. {
  311. _clogic.AddModel(user);
  312. }
  313. else
  314. {
  315. //_clogic.ExcuteSql($"UPDATE gbrs2_customerinfo SET LastLoginTime='{DateTime.Now.ToString()}' WHERE User_id={user.User_id}");
  316. _clogic.EditEntity(queryUser);
  317. }
  318. var loginurl = ConfigHelper.GetValue<string>("LoginCallback:Url");
  319. var personalRedirectUrl = $"{loginurl}?personalId={user.User_id}";
  320. return Redirect(personalRedirectUrl);
  321. //return RedirectToAction("Index", "User");
  322. }
  323. /// <summary>
  324. /// 根据保单号查询保单详情
  325. /// </summary>
  326. /// <param name="contNo"></param>
  327. /// <returns></returns>
  328. public IActionResult PolicyDetail(string contNo)
  329. {
  330. if (string.IsNullOrEmpty(contNo))
  331. {
  332. return Content("该保单信息不存在!");
  333. }
  334. ViewBag.PolicyPhone = _sysConfigLogic.GetEntity(x => x.Key == "PolicyPhone").Value;
  335. var result = _logic.Transformation(contNo).Result;
  336. string session = HttpContext.Session.GetString("GBRS2018");
  337. if (string.IsNullOrEmpty(session))
  338. {
  339. return RedirectToAction("Index", "User");
  340. }
  341. result.UserId = JsonConvert.DeserializeObject<UserInfoModel>(session).Core_customer_code;
  342. if (!string.IsNullOrEmpty(result.InsurancePolicyModel.CValiDate))
  343. {
  344. var backDate = Convert.ToDateTime(result.InsurancePolicyModel.CValiDate);
  345. var backDate2 = backDate.AddDays(15);
  346. if (DateTime.Now < backDate2)
  347. {
  348. result.InsurancePolicyModel.InHesitationFlag = "犹豫期内";
  349. }
  350. else
  351. {
  352. result.InsurancePolicyModel.InHesitationFlag = "犹豫期外";
  353. }
  354. }
  355. if (result.InsurancePolicyModel == null)
  356. {
  357. return Content("该保单信息不存在!");
  358. }
  359. return View(result);
  360. }
  361. /// <summary>
  362. /// 获取当前用户名下所有表单
  363. /// </summary>
  364. /// <param name="userId"></param>
  365. /// <returns></returns>
  366. public IActionResult MyPolicy(string userId, int pageIndex = 1, int pageSize = 10)
  367. {
  368. ReportListViewModel viewModel = new ReportListViewModel();
  369. string session = HttpContext.Session.GetString("GBRS2018");
  370. if (string.IsNullOrEmpty(session))
  371. {
  372. return Redirect("/User/Index");
  373. }
  374. var user1 = JsonConvert.DeserializeObject<UserInfoModel>(session);
  375. var ContNos = GetConNos(user1.Core_customer_code, "2");
  376. if (ContNos.Count > 0)
  377. {
  378. var list = _logic.GetReportNoList(ContNos).Result;
  379. viewModel.Count = list.Count;
  380. viewModel.ReportNoList = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList();
  381. }
  382. var user = JsonConvert.DeserializeObject<UserInfoModel>(session);
  383. viewModel.Head_Url = user.Head_url;
  384. viewModel.NickName = user.Nickname;
  385. ViewData["pageCount"] = viewModel.Count;
  386. ViewData["pageIndex"] = pageIndex;
  387. viewModel.User = user;
  388. return View(viewModel);
  389. }
  390. private List<string> GetConNos(string customerCode, string customerType)
  391. {
  392. JToken jobJect = null;
  393. var jsonString = _logic.GetListByUserId(customerCode, customerType);
  394. if (!string.IsNullOrEmpty(jsonString.Result))
  395. {
  396. jobJect = JObject.Parse(jsonString.Result)["TransData"]["ContentData"]["ContNos"];
  397. }
  398. List<string> ContNos = new List<string>();
  399. if (jobJect != null)
  400. {
  401. ContNos = JsonConvert.DeserializeObject<List<string>>(jobJect.ToString());
  402. }
  403. return ContNos;
  404. }
  405. /// <summary>
  406. /// 保单下载
  407. /// </summary>
  408. /// <returns></returns>
  409. public async Task<IActionResult> DownLoadPolicy(string contNo)
  410. {
  411. var huaWeiToken = await _logic.GetHuaWeiToken(contNo);
  412. if (string.IsNullOrEmpty(huaWeiToken))
  413. {
  414. return Content("系统错误");
  415. }
  416. HuaWeiDownLoadModel downLoadModel = JsonConvert.DeserializeObject<HuaWeiDownLoadModel>(huaWeiToken);
  417. if (downLoadModel.Code == 500)
  418. {
  419. return Content("该保单无法下载");
  420. }
  421. HttpWebRequest request;
  422. request = WebRequest.Create(downLoadModel.Data.v4TemporarySignatureResponse.signedUrl) as HttpWebRequest;
  423. request.Method = "GET";
  424. request.Accept = "*/*";
  425. request.Timeout = 15000;
  426. request.AllowAutoRedirect = false;
  427. var response = request.GetResponse();
  428. return File(response.GetResponseStream(), "application/pdf", $"{downLoadModel.Data.FileName}");
  429. }
  430. public IActionResult Notice()
  431. {
  432. return View();
  433. }
  434. }
  435. }