using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Runtime.Serialization.Json; using System.Text; using System.Threading.Tasks; using System.Web; using CZFW.Core; using CZFW.Framework.Interface; using CZKJ.GBRS2.Interface; using CZKJ.GBRS2.Model; using CZKJ.GBRS2.ViewModel; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Newtonsoft.Json; using Newtonsoft.Json.Linq; namespace CZKJ.GBRS2.WebMVC.Controllers { public class UserController : Controller { ICustomerQuery _logic; ISysConfig _sysConfigLogic; IUserInfoCustomer _clogic; public UserController(ICustomerQuery logic, ISysConfig sysConfigLogic, IUserInfoCustomer infoCustomer) { _logic = logic; _sysConfigLogic = sysConfigLogic; _clogic = infoCustomer; } /// /// 登录 /// /// /// public IActionResult Index(string code = null) { string session = HttpContext.Session.GetString("GBRS2018"); //个人登录跳转链接 var loginurl = ConfigHelper.GetValue("LoginCallback:Url"); if (session != null) { var userinfo = JsonConvert.DeserializeObject(session); var userIndexUrl = $"{loginurl}?personalId={userinfo.User_id}"; return Redirect(userIndexUrl); //return View(JsonConvert.DeserializeObject(session)); } //获取当前请求地址 var url = HttpUtility.UrlEncode($"{ConfigHelper.GetValue("Login:Scheme")}://{Request.Host}{Request.Path}").Replace("%3a", "%3A").Replace("%2f", "%2F"); //判断code是否为空 if (string.IsNullOrEmpty(code)) { //拼接获取Code地址 // var redirectUrl = $"{ConfigHelper.GetValue("Login:Url")}/uai/user/passport/login?client_id={ConfigHelper.GetValue("Login:ClientId")}&redirect_uri={url}&response_type=code"; var redirectUrl = $"{ConfigHelper.GetValue("Login:Url")}/uai/user/passport/login?client_id={ConfigHelper.GetValue("Login:ClientId")}&redirect_uri={url}&response_type=code"; //跳转获取Code return Redirect(redirectUrl); } // CustomerInfoViewModel customerInfoViewModel = new CustomerInfoViewModel(); //拼接获取accessToken地址 var getAccessToken = $"{ConfigHelper.GetValue("Login:Url")}/uai/api/oauth20/accesstoken/code?code={code}&client_id={ConfigHelper.GetValue("Login:ClientId")}&client_secret={ConfigHelper.GetValue("Login:PassWord")}&redirect_uri={url}&grant_type=authorization_code"; //通过POST调用地址获取实体 var accessTokenInfo = HttpHelper.HttpPost(getAccessToken); //反序列化 DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(AccessTokenModel)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(accessTokenInfo)); AccessTokenModel accessTokenModel = (AccessTokenModel)dataContractJsonSerializer.ReadObject(ms); //拼接通过accessToken获取用户信息地址 var getUserInfoUrl = $"{ConfigHelper.GetValue("Login:Url")}/uai/api/oauth20/getuserinfo?access_token={accessTokenModel.access_token}"; //通过Get请求获取用户信息 var userInfo = HttpHelper.HttpGet(getUserInfoUrl); //把accessToken 写入session HttpContext.Session.SetString("token", accessTokenModel.access_token == null ? "" : accessTokenModel.access_token); //反序列化用户信息 var user = JsonConvert.DeserializeObject(userInfo); switch (user.Type) { case "1": user.Type = "客户"; break; case "2": user.Type = "国宝内勤"; break; case "3": user.Type = "国宝外勤"; break; case "4": user.Type = "企业用户"; break; case "5": user.Type = "合作伙伴内勤"; break; case "6": user.Type = "合作伙伴外勤"; break; } switch (user.Customer_info.Gender) { case "1": user.Customer_info.Gender = "男"; break; case "2": user.Customer_info.Gender = "女"; break; } switch (user.Customer_info.Id_type) { case "0": user.Customer_info.Id_type = "居民身份证"; break; case "1": user.Customer_info.Id_type = "护照"; break; case "2": user.Customer_info.Id_type = "军人证"; break; case "3": user.Customer_info.Id_type = "居民户口薄"; break; case "4": user.Customer_info.Id_type = "通行证"; break; case "5": user.Customer_info.Id_type = "出生证明"; break; case "8": user.Customer_info.Id_type = "其他"; break; } var Id = user.Customer_info.Id_no; if (!string.IsNullOrEmpty(Id)) { user.Customer_info.Id_no = Id.Substring(0, 6) + "********" + Id.Substring(14, 4); } var phone = user.Mobile; if (!string.IsNullOrEmpty(phone)) { user.Mobile = phone.Substring(0, 3) + "****" + phone.Substring(7, 4); } HttpContext.Session.SetString("GBRS2018", JsonConvert.SerializeObject(user)); var queryUser = _clogic.GetCuser(user.User_id); //if (queryUser == null) //{ // _clogic.AddModel(user); //} //else //{ // queryUser.LastLoginTime = DateTime.Now; // //_clogic.ExcuteSql($"UPDATE gbrs2_customerinfo SET LastLoginTime='{DateTime.Now.ToString()}' WHERE User_id={user.User_id}"); // _clogic.EditEntity(queryUser); //} //if (string.IsNullOrWhiteSpace(user.Core_customer_code)) //{ // return View("Notice",user); //} //:http://uat01.guobaojinrong.com/wef/pc/api/personaluser/callback?personalId=xxxx&openId=xxx var personalRedirectUrl = $"{loginurl}?personalId={user.User_id}"; return Redirect(personalRedirectUrl); //return View(user); } //回调官网 public IActionResult CallBackHome(string personalId) { string session = HttpContext.Session.GetString("GBRS2018"); if (session != null) { var user = JsonConvert.DeserializeObject(session); if (user.User_id != personalId) { //从session获取用户信息 string sessiontoken = HttpContext.Session.GetString("token"); //拼接登出链接 var logOutUrl = $"{ConfigHelper.GetValue("Login:Url")}/uai/api/oauth20/logout?access_token={sessiontoken}"; var res = HttpHelper.HttpGet(logOutUrl); HttpContext.Session.Remove("GBRS2018"); } } return Redirect("/"); } /// /// 登出 /// /// public IActionResult LogOut(string personalId) { //从session获取用户信息 string session = HttpContext.Session.GetString("token"); //拼接登出链接 var logOutUrl = $"{ConfigHelper.GetValue("Login:Url")}/uai/api/oauth20/logout?access_token={session}"; var res = HttpHelper.HttpGet(logOutUrl); HttpContext.Session.Remove("GBRS2018"); //通过Get请求调用接口 return RedirectToAction("Index", "Home"); } /// /// 注册 /// /// /// public IActionResult Register(string code = null, string beforeUrl = null) { //if (!string.IsNullOrEmpty(beforeUrl)) //{ // HttpContext.Session.SetString("before", beforeUrl); //} //获取当前请求地址 var url = HttpUtility.UrlEncode($"{ConfigHelper.GetValue("Login:Scheme")}://{Request.Host}{Request.Path}").Replace("%3a", "%3A").Replace("%2f", "%2F"); //判断code是否为空 if (string.IsNullOrEmpty(code)) { //拼接获取Code地址 // var redirectUrl = $"{ConfigHelper.GetValue("Login:Url")}/uai/user/passport/register?client_id=201803236916747755&redirect_uri={url}&response_type=code"; var redirectUrl = $"{ConfigHelper.GetValue("Login:Url")}/uai/user/passport/register?client_id={ConfigHelper.GetValue("Login:ClientId")}&redirect_uri={url}&response_type=code"; //跳转获取Code return Redirect(redirectUrl); } //拼接获取accessToken地址 var getAccessToken = $"{ConfigHelper.GetValue("Login:Url")}/uai/api/oauth20/accesstoken/code?code={code}&grant_type=authorization_code&client_id={ConfigHelper.GetValue("Login:ClientId")}&client_secret={ConfigHelper.GetValue("Login:PassWord")}&redirect_uri={url}"; //通过POST调用地址获取实体 var accessTokenInfo = HttpHelper.HttpPost(getAccessToken); //反序列化 DataContractJsonSerializer dataContractJsonSerializer = new DataContractJsonSerializer(typeof(AccessTokenModel)); MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(accessTokenInfo)); AccessTokenModel accessTokenModel = (AccessTokenModel)dataContractJsonSerializer.ReadObject(ms); //拼接通过accessToken获取用户信息地址 var getUserInfoUrl = $"{ConfigHelper.GetValue("Login:Url")}/uai/api/oauth20/getuserinfo?access_token={accessTokenModel.access_token}"; //通过Get请求获取用户信息 var userInfo = HttpHelper.HttpGet(getUserInfoUrl); //把accessToken 写入session //string session = HttpContext.Session.GetString("before"); //if (!string.IsNullOrEmpty(beforeUrl)) //{ // return Redirect(beforeUrl); //} //else //{ // return RedirectToAction("Index", "Home"); //} HttpContext.Session.SetString("token", accessTokenModel.access_token == null ? "" : accessTokenModel.access_token); //反序列化用户信息 var user = JsonConvert.DeserializeObject(userInfo); switch (user.Type) { case "1": user.Type = "客户"; break; case "2": user.Type = "国宝内勤"; break; case "3": user.Type = "国宝外勤"; break; case "4": user.Type = "企业用户"; break; case "5": user.Type = "合作伙伴内勤"; break; case "6": user.Type = "合作伙伴外勤"; break; } switch (user.Customer_info.Gender) { case "1": user.Customer_info.Gender = "男"; break; case "2": user.Customer_info.Gender = "女"; break; } switch (user.Customer_info.Id_type) { case "0": user.Customer_info.Id_type = "居民身份证"; break; case "1": user.Customer_info.Id_type = "护照"; break; case "2": user.Customer_info.Id_type = "军人证"; break; case "3": user.Customer_info.Id_type = "居民户口薄"; break; case "4": user.Customer_info.Id_type = "通行证"; break; case "5": user.Customer_info.Id_type = "出生证明"; break; case "8": user.Customer_info.Id_type = "其他"; break; } var Id = user.Customer_info.Id_no; if (!string.IsNullOrEmpty(Id)) { user.Customer_info.Id_no = Id.Substring(0, 6) + "********" + Id.Substring(14, 4); } var phone = user.Mobile; if (!string.IsNullOrEmpty(phone)) { user.Mobile = phone.Substring(0, 3) + "****" + phone.Substring(7, 4); } HttpContext.Session.SetString("GBRS2018", JsonConvert.SerializeObject(user)); var queryUser = _clogic.GetCuser(user.User_id); if (queryUser == null) { _clogic.AddModel(user); } else { //_clogic.ExcuteSql($"UPDATE gbrs2_customerinfo SET LastLoginTime='{DateTime.Now.ToString()}' WHERE User_id={user.User_id}"); _clogic.EditEntity(queryUser); } var loginurl = ConfigHelper.GetValue("LoginCallback:Url"); var personalRedirectUrl = $"{loginurl}?personalId={user.User_id}"; return Redirect(personalRedirectUrl); //return RedirectToAction("Index", "User"); } /// /// 根据保单号查询保单详情 /// /// /// public IActionResult PolicyDetail(string contNo) { if (string.IsNullOrEmpty(contNo)) { return Content("该保单信息不存在!"); } ViewBag.PolicyPhone = _sysConfigLogic.GetEntity(x => x.Key == "PolicyPhone").Value; var result = _logic.Transformation(contNo).Result; string session = HttpContext.Session.GetString("GBRS2018"); if (string.IsNullOrEmpty(session)) { return RedirectToAction("Index", "User"); } result.UserId = JsonConvert.DeserializeObject(session).Core_customer_code; if (!string.IsNullOrEmpty(result.InsurancePolicyModel.CValiDate)) { var backDate = Convert.ToDateTime(result.InsurancePolicyModel.CValiDate); var backDate2 = backDate.AddDays(15); if (DateTime.Now < backDate2) { result.InsurancePolicyModel.InHesitationFlag = "犹豫期内"; } else { result.InsurancePolicyModel.InHesitationFlag = "犹豫期外"; } } if (result.InsurancePolicyModel == null) { return Content("该保单信息不存在!"); } return View(result); } /// /// 获取当前用户名下所有表单 /// /// /// public IActionResult MyPolicy(string userId, int pageIndex = 1, int pageSize = 10) { ReportListViewModel viewModel = new ReportListViewModel(); string session = HttpContext.Session.GetString("GBRS2018"); if (string.IsNullOrEmpty(session)) { return Redirect("/User/Index"); } var user1 = JsonConvert.DeserializeObject(session); var ContNos = GetConNos(user1.Core_customer_code, "2"); if (ContNos.Count > 0) { var list = _logic.GetReportNoList(ContNos).Result; viewModel.Count = list.Count; viewModel.ReportNoList = list.Skip((pageIndex - 1) * pageSize).Take(pageSize).ToList(); } var user = JsonConvert.DeserializeObject(session); viewModel.Head_Url = user.Head_url; viewModel.NickName = user.Nickname; ViewData["pageCount"] = viewModel.Count; ViewData["pageIndex"] = pageIndex; viewModel.User = user; return View(viewModel); } private List GetConNos(string customerCode, string customerType) { JToken jobJect = null; var jsonString = _logic.GetListByUserId(customerCode, customerType); if (!string.IsNullOrEmpty(jsonString.Result)) { jobJect = JObject.Parse(jsonString.Result)["TransData"]["ContentData"]["ContNos"]; } List ContNos = new List(); if (jobJect != null) { ContNos = JsonConvert.DeserializeObject>(jobJect.ToString()); } return ContNos; } /// /// 保单下载 /// /// public async Task DownLoadPolicy(string contNo) { var huaWeiToken = await _logic.GetHuaWeiToken(contNo); if (string.IsNullOrEmpty(huaWeiToken)) { return Content("系统错误"); } HuaWeiDownLoadModel downLoadModel = JsonConvert.DeserializeObject(huaWeiToken); if (downLoadModel.Code == 500) { return Content("该保单无法下载"); } HttpWebRequest request; request = WebRequest.Create(downLoadModel.Data.v4TemporarySignatureResponse.signedUrl) as HttpWebRequest; request.Method = "GET"; request.Accept = "*/*"; request.Timeout = 15000; request.AllowAutoRedirect = false; var response = request.GetResponse(); return File(response.GetResponseStream(), "application/pdf", $"{downLoadModel.Data.FileName}"); } public IActionResult Notice() { return View(); } } }