using MAX.Core.Commons; using MAX.Core.Cryptography; using MAX.Core.DependencyInjection; using MAX.Core.Helpers; using Newtonsoft.Json; using Store.Client.Apis; using Store.Framework.Apis; using Store.Framework.Inerterfaces; using Store.Framework.Models; using Store.Framework.Services; using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Text; using System.Threading.Tasks; namespace Store.Client.Services { /// /// 用户操作 /// public class UserService : ITransientDependency { private readonly UserApi _userApi; private readonly ICurrentUser _currentUser; public UserService(UserApi userApi, ICurrentUser currentUser) { _userApi = userApi; _currentUser = currentUser; } /// /// 用户登录 /// /// public Result Login(UserInfo user) { Result ret = new Result(); user.Password = MD5Helper.Md5_Lower32(user.Password); HttpResult result = _userApi.Login(user.UserName, user.Password); if (result.StatusCode == HttpStatusCode.Unauthorized) { ret.Msg = "登录失败\r\n链接异常请重试"; return ret; } if (string.IsNullOrEmpty(result.Html)) { ret.Msg = "链接服务器失败"; return ret; } ApiResponse apiResponse = null; try { apiResponse = JsonConvert.DeserializeObject>(result.Html); } catch (JsonException) { } if (apiResponse == null) { ret.Msg = result.Html; return ret; } ret.Code = apiResponse.Code; if (apiResponse.Code != 200 || apiResponse.Data == null) { ret.Msg = string.IsNullOrEmpty(apiResponse.Msg) ? result.Html : apiResponse.Msg; return ret; } FillUserInfo(user, apiResponse.Data); ret.Success = true; ret.Msg = apiResponse.Msg; ret.Data = result; (_currentUser as CurrentUserService).SetUser(user); //_mediator.Publish(new UserNotification(user)); return ret; } /// /// 将接口返回的登录用户信息回填到调用方对象。 /// private static void FillUserInfo(UserInfo target, UserInfo source) { target.UserId = source.UserId; target.UserName = source.UserName; target.Password = source.Password; target.GroupId = source.GroupId; target.UserKey = source.UserKey; target.CompanyId = source.CompanyId; target.CompanyName = source.CompanyName; target.NickName = source.NickName; target.ParentUserName = source.ParentUserName; target.SaLevel = source.SaLevel; target.UserAuthority = source.UserAuthority; target.ParentAuthority = source.ParentAuthority; target.ForbiddenPage = source.ForbiddenPage; target.Mobile = source.Mobile; target.UserVersion = source.UserVersion; target.UserVersionString = source.UserVersionString; target.AuthorizedAmount = source.AuthorizedAmount; target.Points = source.Points; target.ShopPointsPerMonth = source.ShopPointsPerMonth; target.Balance = source.Balance; target.RechargeAmountTotal = source.RechargeAmountTotal; target.PostagePerOrder = source.PostagePerOrder; target.RefundPostagePerOrder = source.RefundPostagePerOrder; target.GiftOemAmountPerOrder = source.GiftOemAmountPerOrder; target.OemId = source.OemId; target.OemName = source.OemName; target.IsOemOwner = source.IsOemOwner; target.EndDate = source.EndDate; target.QqGroupNum = source.QqGroupNum; target.QqGroupUrl = source.QqGroupUrl; target.WechatUrl = source.WechatUrl; target.Pinduoduo = source.Pinduoduo; target.AlimamaDomain = source.AlimamaDomain; target.AlimamaPid = source.AlimamaPid; target.LockUserName = source.LockUserName; target.WeakPassword = source.WeakPassword; target.MessageInfo = source.MessageInfo; target.AgentAuth = source.AgentAuth; target.IsImport = source.IsImport; target.CardId = source.CardId; target.IsSa = source.IsSa; target.AddTime = source.AddTime; target.MdyTime = source.MdyTime; target.Notes = source.Notes; } } }