diff --git a/CoreCms.Net.Web.WebApi/Controllers/CommonController.cs b/CoreCms.Net.Web.WebApi/Controllers/CommonController.cs index b9a0412..2e8d085 100644 --- a/CoreCms.Net.Web.WebApi/Controllers/CommonController.cs +++ b/CoreCms.Net.Web.WebApi/Controllers/CommonController.cs @@ -259,7 +259,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers return jm; } } - + filesStorageOptions.Path = "users"; string url = string.Empty; if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.LocalStorage.ToString()) { diff --git a/CoreCms.Net.Web.WebApi/Controllers/UserController.cs b/CoreCms.Net.Web.WebApi/Controllers/UserController.cs index d7bddb1..6d02a3f 100644 --- a/CoreCms.Net.Web.WebApi/Controllers/UserController.cs +++ b/CoreCms.Net.Web.WebApi/Controllers/UserController.cs @@ -45,6 +45,9 @@ using NLog; using SKIT.FlurlHttpClient.Wechat.Api; using SKIT.FlurlHttpClient.Wechat.Api.Models; using SqlSugar; +using DotLiquid.Util; +using System.IO; +using System.Text.RegularExpressions; namespace CoreCms.Net.Web.WebApi.Controllers { @@ -85,7 +88,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers private readonly ICoreCmsStoreServices _storeServices; private readonly ICoreCmsCouponServices _couponServices; private readonly ICoreCmsOrderServices _orderServices; - + private readonly IToolsServices _toolsServices; private readonly IWeChatApiHttpClientFactory _weChatApiHttpClientFactory; private readonly WeChatOptions _weChatOptions; @@ -120,7 +123,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers , ICoreCmsSettingServices settingServices , ICoreCmsServicesServices servicesServices , IOptions weChatOptions - , ICoreCmsUserServicesOrderServices userServicesOrderServices, ICoreCmsUserServicesTicketServices userServicesTicketServices, ICoreCmsStoreServices storeServices, ICoreCmsCouponServices couponServices, ICoreCmsOrderServices orderServices, IWeChatApiHttpClientFactory weChatApiHttpClientFactory) + , ICoreCmsUserServicesOrderServices userServicesOrderServices, ICoreCmsUserServicesTicketServices userServicesTicketServices, ICoreCmsStoreServices storeServices, ICoreCmsCouponServices couponServices, ICoreCmsOrderServices orderServices, IWeChatApiHttpClientFactory weChatApiHttpClientFactory, IToolsServices toolsServices) { _user = user; _userWeChatInfoServices = userWeChatInfoServices; @@ -153,7 +156,7 @@ namespace CoreCms.Net.Web.WebApi.Controllers _orderServices = orderServices; _weChatApiHttpClientFactory = weChatApiHttpClientFactory; _weChatOptions = weChatOptions.Value; - + _toolsServices = toolsServices; } /// /// wx.login登陆成功之后发送的请求 @@ -882,16 +885,16 @@ namespace CoreCms.Net.Web.WebApi.Controllers jm.code = 14007; return jm; } - //获取用户等级 - var userGrade = await _userGradeServices.QueryByClauseAsync(p => p.id == user.grade); - //获取优惠券 - var userCouponCount = await _couponServices.GetMyCouponCount(user.id); - //订单数量 - var orderCount = await _orderServices.OrderCount(0, user.id); - //足迹 - var footPrintCount = await _goodsBrowsingServices.GetCountAsync(p => p.userId == user.id); - //收藏 - var collectionCount = await _goodsCollectionServices.GetCountAsync(p => p.userId == user.id); + ////获取用户等级 + //var userGrade = await _userGradeServices.QueryByClauseAsync(p => p.id == user.grade); + ////获取优惠券 + //var userCouponCount = await _couponServices.GetMyCouponCount(user.id); + ////订单数量 + //var orderCount = await _orderServices.OrderCount(0, user.id); + ////足迹 + //var footPrintCount = await _goodsBrowsingServices.GetCountAsync(p => p.userId == user.id); + ////收藏 + //var collectionCount = await _goodsCollectionServices.GetCountAsync(p => p.userId == user.id); jm.data = new @@ -900,22 +903,18 @@ namespace CoreCms.Net.Web.WebApi.Controllers user.userName, user.mobile, user.sex, - user.birthday, + birthday = user.birthday != null ? user.birthday?.ToString("yyyy-MM-dd") : "", user.avatarImage, user.nickName, user.balance, user.point, user.grade, + age = 30, user.createTime, user.updataTime, user.status, user.parentId, - user.passWord, - gradeName = userGrade != null ? userGrade.title : "", - userCouponCount, - orderCount, - footPrintCount, - collectionCount + }; return jm; } @@ -1768,6 +1767,127 @@ namespace CoreCms.Net.Web.WebApi.Controllers } + /// + /// 编辑用户信息 + /// + /// + /// + [HttpPost] + public async Task EditUserInfo([FromBody] EditLoginUserInfo entity) + { + var jm = new WebApiCallBack(); + + if (entity.birthday == null) + { + jm.msg = GlobalErrorCodeVars.Code11027; + return jm; + } + + if (string.IsNullOrEmpty(entity.nickName)) + { + jm.msg = GlobalErrorCodeVars.Code11028; + return jm; + } + + if (entity.sex <= 0) + { + jm.msg = GlobalErrorCodeVars.Code11029; + return jm; + } + if (string.IsNullOrEmpty(entity.avatar)) + { + jm.msg = GlobalErrorCodeVars.Code10003; + return jm; + } + if (entity.avatar.IndexOf("http") == -1) + { + //entity.avatar # data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/4 + //IFormFile file + var filesStorageOptions = await _settingServices.GetFilesStorageOptions(); + + //初始化上传参数 + var maxSize = 1024 * 1024 * filesStorageOptions.MaxSize; //上传大小5M + + Random random = new Random(); + string fileName = _user.ID + "_" + random.Next(1000, 9999) + ".png"; + string fileExt = Path.GetExtension(fileName).ToLowerInvariant(); + + + //检查文件扩展名 + if (string.IsNullOrEmpty(fileExt) || + Array.IndexOf(filesStorageOptions.FileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1) + { + jm.msg = "上传文件扩展名是不允许的扩展名,请上传后缀名为:" + filesStorageOptions.FileTypes; + return jm; + } + var file = Base64ToFormFile(entity.avatar, fileName); + //// 使用StreamReader来读取文件内容 + //using (var reader = new StreamReader(file.OpenReadStream(), Encoding.UTF8)) + //{ + // var content = await reader.ReadToEndAsync(); // 注意:这可能会消耗大量内存对于大文件,所以需要限制上传大小 + // // 检查内容是否合法 + // if (CommonHelper.CheckData(content)) + // { + // jm.msg = "请勿提交非法数据。"; + // return jm; + // } + //} + filesStorageOptions.Path = "users"; + + string url = string.Empty; + if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.LocalStorage.ToString()) + { + url = await _toolsServices.UpLoadFileForLocalStorage(filesStorageOptions, fileExt, file, (int)GlobalEnumVars.FilesStorageLocation.API); + } + else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.AliYunOSS.ToString()) + { + url = await _toolsServices.UpLoadFileForAliYunOSS(filesStorageOptions, fileExt, file); + } + else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QCloudOSS.ToString()) + { + url = await _toolsServices.UpLoadFileForQCloudOSS(filesStorageOptions, fileExt, file); + } + else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QiNiuKoDo.ToString()) + { + url = await _toolsServices.UpLoadFileForQiNiuKoDo(filesStorageOptions, fileExt, file); + } + entity.avatar = url; + } + + + var up = await _userServices.UpdateAsync(p => new CoreCmsUser() + { + avatarImage = entity.avatar, + birthday = entity.birthday, + nickName = entity.nickName, + sex = entity.sex + }, + p => p.id == _user.ID); + + jm.status = up; + jm.msg = jm.status ? "资料保存成功" : "资料保存失败"; + + return jm; + } + + private IFormFile Base64ToFormFile(string base64, string fileName) + { + // 匹配并去除 data:image/png;base64, 前缀 + var match = Regex.Match(base64, @"data:image/(?.+?);base64,(?.+)"); + if (!match.Success) + throw new ArgumentException("base64格式不正确"); + + var base64Data = match.Groups["data"].Value; + var bytes = Convert.FromBase64String(base64Data); + var stream = new MemoryStream(bytes); + + // 构造 IFormFile + return new FormFile(stream, 0, bytes.Length, "file", fileName) + { + Headers = new HeaderDictionary(), + ContentType = $"image/{match.Groups["type"].Value}" + }; + } #endregion #region 修改用户密码 diff --git a/CoreCms.Net.Web.WebApi/Doc.xml b/CoreCms.Net.Web.WebApi/Doc.xml index 807222c..1962cf7 100644 --- a/CoreCms.Net.Web.WebApi/Doc.xml +++ b/CoreCms.Net.Web.WebApi/Doc.xml @@ -850,18 +850,32 @@ 用户操作事件 - + 构造函数 - + wx.login登陆成功之后发送的请求 + + + 微信小程序授权拉取手机号码并登录 + + + + + + + 微信小程序匿名转正式登录 + + + + wx.login登陆成功之后发送的请求 @@ -1148,6 +1162,13 @@ + + + 编辑用户信息 + + + + 修改用户密码