public async Task <IActionResult> Start(UserUrlModel model) { using (var client = new HttpClient()) { // Assuming the API is in the same web application. string baseUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}"; //Check if the user is already available in db client.BaseAddress = new Uri(baseUrl); var responseMessage = client.GetAsync("/api/Users"); responseMessage.Wait(); User user = null; var result = responseMessage.Result; if (result.IsSuccessStatusCode) { var stream = await result.Content.ReadAsStreamAsync(); using (JsonReader jsonReader = new JsonTextReader(new System.IO.StreamReader(stream))) { var serializer = new JsonSerializer(); var users = serializer.Deserialize <IEnumerable <User> >(jsonReader); user = users.Where(t => t.Email == model.Email).FirstOrDefault(); } } if (user == null) { user = new User() { UserId = Guid.NewGuid().ToString(), Email = model.Email }; var postTask = client.PostAsJsonAsync <User>("/api/Users", user); postTask.Wait(); if (!postTask.Result.IsSuccessStatusCode) { return(View("Error")); } /*else * { * var stream = await result.Content.ReadAsStreamAsync(); * using (JsonReader jsonReader = new JsonTextReader(new System.IO.StreamReader(stream))) * { * var serializer = new JsonSerializer(); * user = serializer.Deserialize<User>(jsonReader); * } * }*/ } UserUrl userUrl = new UserUrl { UrlId = Guid.NewGuid().ToString(), UserId = user.UserId, ActualUrl = model.ActualUrl, Description = model.Description }; userUrl.ShortUrl = $"{this.Request.Scheme}://{this.Request.Host}{this.Request.PathBase}/Home/DynamicUrl?id=" + userUrl.UrlId; var usersUrlTask = client.PostAsJsonAsync <UserUrl>("/api/UserUrls", userUrl); usersUrlTask.Wait(); if (usersUrlTask.Result.IsSuccessStatusCode) { return(View("Added", new UserUrlModel { UrlId = userUrl.UrlId, Email = user.Email, ShortUrl = userUrl.ShortUrl, ActualUrl = userUrl.ActualUrl, Description = userUrl.Description })); } } return(View()); }
public HttpResponseMessage CheckLoginJson(string username, string password) { try { RoleBLL roleBLL = new RoleBLL(); DepartmentBLL departmentBLL = new DepartmentBLL(); OrganizeBLL organizeBLL = new OrganizeBLL(); UserBLL userBLL = new UserBLL(); byte[] sor = Encoding.UTF8.GetBytes(password); MD5 md5 = MD5.Create(); byte[] result = md5.ComputeHash(sor); StringBuilder strbul = new StringBuilder(40); for (int i = 0; i < result.Length; i++) { strbul.Append(result[i].ToString("x2"));//加密结果"x2"结果为32位,"x3"结果为48位,"x4"结果为64位 } password = strbul.ToString(); UserEntity userEntity = new UserBLL().CheckLogin(username, password); if (userEntity == null) { return(new HttpResponseMessage { Content = new StringContent("查询失败", Encoding.GetEncoding("UTF-8"), "application/json") }); } UserUrlModel userurlmodel = new UserUrlModel(); EntityToEntity(userurlmodel, userEntity); OrganizeEntity orgent = organizeBLL.GetEntity(userEntity.OrganizeId); if (string.IsNullOrEmpty(orgent.ParentId) || orgent.ParentId == "0") { userurlmodel.IsTopOrg = true; } //var orgnizedata = organizeBLL.GetList().ToList(); //List<OrganizeEntity> lstOrg = new List<OrganizeEntity>(); Operator operators = new Operator(); //foreach (OrganizeEntity org in orgnizedata) //{ // if (org.ManagerId == userurlmodel.UserId) // { // // operators.LstOrganizeId.Add(org.OrganizeId); //登录用户作为负责人的公司 // lstOrg=(Reorg(orgnizedata, org.OrganizeId));//登录用户作为负责人的公司及下属 // } //} //operators.LstOrganizeId = new List<string>(); //foreach (OrganizeEntity org in lstOrg) //{ // operators.LstOrganizeId.Add(org.OrganizeId); //} //var departmentdata = departmentBLL.GetList().ToList(); //List<DepartmentEntity> lstDep = new List<DepartmentEntity>(); //foreach (DepartmentEntity dep in departmentdata) //{ // if (dep.DepartmentId == userurlmodel.DepartmentId) // { // userurlmodel.DepartmentName = dep.FullName; // } // if (dep.ManagerId == userurlmodel.UserId) // { // // operators.LstOrganizeId.Add(dep.DepartmentId);//登录用户作为负责人的部门 // lstDep=(Redep(departmentdata, dep.DepartmentId));//登录用户作为负责人的部门及下属 // } //} //operators.LstDepartmentId = new List<string>(); //foreach (DepartmentEntity dep in lstDep) //{ // operators.LstDepartmentId.Add(dep.DepartmentId); //} //var userdata = userBLL.GetList().ToList(); //List<UserEntity> lstUser = new List<UserEntity>(); //foreach (UserEntity usr in userdata) //{ // if (usr.ManagerId == userurlmodel.UserId) // { // //operators.LstStaffId.Add(usr.UserId);//登录用户上级主管的用户 // lstUser=(Reusr(userdata, userurlmodel.UserId));//登录用户作为负责人的部门及下属 // } //} //operators.LstStaffId = new List<string>(); //foreach (UserEntity usr in lstUser) //{ // operators.LstStaffId.Add(usr.UserId); //} ////var depuser1 = new List<string>(); ////var depuser2 = new List<UserEntity>(); ////var depuser3 = new List<UserEntity>(); //if (operators.LstOrganizeId.Count > 0) //{//获取公司及下属公司人员 // var depuser1 = userdata.FindAll(a => operators.LstOrganizeId.Contains(a.OrganizeId.ToString())) ; // foreach(var ent in depuser1) // { // operators.LstStaffId.Add(ent.UserId); // } //} //if (operators.LstDepartmentId.Count > 0) //{//获取部门及下属部门人员 // var depuser2 = userdata.FindAll(a => operators.LstDepartmentId.Contains(a.DepartmentId.ToString())) ; // foreach (var ent in depuser2) // { // operators.LstStaffId.Add(ent.UserId); // } //} //if (operators.LstStaffId.Count > 0) //{////获取人员及下属人员 // var depuser3 = userdata.FindAll(a => operators.LstStaffId.Contains(a.UserId.ToString())) ; // foreach (var ent in depuser3) // { // operators.LstStaffId.Add(ent.UserId); // } //} operators.UserId = userEntity.UserId; operators.UserName = userEntity.RealName; operators.Token = DESEncrypt.Encrypt(Guid.NewGuid().ToString()); operators.LogTime = DateTime.Now; OperatorProvider.Provider.AddCurrent(operators); var roledata = roleBLL.GetList(); foreach (RoleEntity dep in roledata) { if (dep.RoleId == userEntity.RoleId) { userurlmodel.RoleName = dep.FullName; userurlmodel.RoleCode = dep.EnCode; } } CommonMethod.cache.Remove(userEntity.UserId + "organize"); CommonMethod.cache.Remove(userEntity.UserId + "user"); CommonMethod.GetUserList(userEntity.UserId); CommonMethod.GetOrgnizeList(userEntity.UserId); JavaScriptSerializer serializer = new JavaScriptSerializer(); string str = serializer.Serialize(userurlmodel); HttpResponseMessage ut = new HttpResponseMessage { Content = new StringContent(str, Encoding.GetEncoding("UTF-8"), "application/json") }; return(ut); } catch (Exception ex) { return(new HttpResponseMessage { Content = new StringContent(ex.Message, Encoding.GetEncoding("UTF-8"), "application/json") }); } }