using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.WebUtilities; using Microsoft.Net.Http.Headers; using Nirvana.Common; using Nirvana.Common.ApiBase; using Nirvana.Common.File; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Threading.Tasks; using YBDevice.Entity; using YBDevice.NApi.Application.UserInfo; namespace YBDevice.NApi.Controllers { /// /// 用户管理接口 /// public class UserController : BaseController { private static readonly FormOptions _defaultFormOptions = new FormOptions(); private readonly IWebHostEnvironment _hostingEnvironment; private readonly IUserService _userService; public UserController(IWebHostEnvironment hostingEnvironment,IUserService userService) { _hostingEnvironment = hostingEnvironment; _userService = userService; } /// /// 获取用户使用过的设备类型列表 /// /// [HttpGet] public async Task GetDevTypeListAsync(string appid) { return await _userService.GetDevTypeListAsync(appid); } /// /// 添加设备类型 /// /// /// [HttpGet] public async Task AddDevTypeAsync(int devtype) { return await _userService.AddDevTypeAsync(devtype); } /// /// 获取用户基本信息 /// /// 家庭成员ID /// 设备类型 /// [HttpGet] public async Task GetUserInfoAsync(int familyid=0,int devtype=0) { return await _userService.GetUserInfoAsync(familyid,devtype); } /// /// 获取用户测量数据 /// /// /// [HttpPost] public async Task GetMeasureInfoAsync([FromBody] UserMeasureInfoModel data) { return await _userService.GetMeasureInfoAsync(data); } /// /// 获取八电极测量数据 /// /// /// [HttpPost] public async Task GetBodyMeasureInfoAsync([FromBody] UserBodyMeasureInfoModel data) { return await _userService.GetBodyMeasureInfoAsync(data); } /// /// 获取八电极趋势信息 /// /// /// [HttpPost] public async Task GetBodyTrendListAsync([FromBody] BodyQueryTrandDto data) { return await _userService.GetBodyTrendListAsync(data); } /// /// 获取趋势信息 /// /// /// [HttpPost] public async Task GetTrendListAsync([FromBody] BodyQueryTrandDto data) { return await _userService.GetTrendListAsync(data); } /// /// 解密手机号 /// /// /// [HttpPost] [ApiAuthorizeFilter(ignore:true)] public async Task DecryptPhoneAsync([FromBody] WxOpenDecryptSubmitModel model) { return await _userService.DecryptPhoneAsync(model); } /// /// 解密微信运动 /// /// /// [HttpPost] public async Task DecryptWxRunAsync([FromBody] WxOpenDecryptSubmitModel model) { var wxrun = await _userService.DecodeWxRunBySessionIdAsync(model.sessionId, model.encryptedData, model.iv); if(wxrun == null) { return new ResultInfo(ResultState.FAIL, "步数获取失败"); } else { return new ResultInfo(ResultState.SUCCESS, "success", wxrun); } } /// /// 文件上传 /// /// /// [HttpPost] public async Task OnPostUploadAsync(string root = "user") { if (!MultipartRequestHelper.IsMultipartContentType(Request.ContentType)) { return new ResultInfo(ResultState.FAIL,"只支持multipart/form-data类型"); } var boundary = MultipartRequestHelper.GetBoundary(MediaTypeHeaderValue.Parse(Request.ContentType), _defaultFormOptions.MultipartBoundaryLengthLimit); var reader = new MultipartReader(boundary, HttpContext.Request.Body); var section = await reader.ReadNextSectionAsync(); List filelist = new List(); while (section != null) { var hasContentDispositionHeader = ContentDispositionHeaderValue.TryParse( section.ContentDisposition, out var contentDisposition); if (hasContentDispositionHeader) { if (!MultipartRequestHelper.HasFileContentDisposition(contentDisposition)) { } else { string[] _permittedExtensions = { ".gif", ".jpg", ".jpeg", ".png", ".bmp" }; long _fileSizeLimit = 1048576 * 5; string rootname = $"/images/{root}/{DateTime.Now.ToString("yyyyMMdd")}"; string savefolder = _hostingEnvironment.WebRootPath; var name = Path.GetRandomFileName() + ".jpg"; var streamedFileContent = await FileHelpers.ProcessStreamedFile( section, contentDisposition, ModelState, _permittedExtensions, _fileSizeLimit); if (!ModelState.IsValid) { string msg = "上传失败"; if (ModelState.Values.Count() > 0) { msg = ModelState.Values.FirstOrDefault().Errors.FirstOrDefault().ErrorMessage; } return new ResultInfo(ResultState.FAIL, msg); } var thumbnailPath = Path.Combine(savefolder + "/" + rootname, name); if (!Directory.Exists(Path.GetDirectoryName(thumbnailPath))) { Directory.CreateDirectory(Path.GetDirectoryName(thumbnailPath)); } else { //检查文件是否存在 if (System.IO.File.Exists(thumbnailPath)) { return new ResultInfo(ResultState.FAIL, "此文件已经存在"); } } var filepath = Path.Combine("wwwroot" + rootname + "/", name); var virualpath = Path.Combine(rootname + "/", name); var filename = contentDisposition.FileName.Value; using (var targetStream = System.IO.File.Create(filepath)) { await targetStream.WriteAsync(streamedFileContent); filelist.Add(virualpath); } } } section = await reader.ReadNextSectionAsync(); } return new ResultInfo(ResultState.SUCCESS, "success", filelist); } /// /// 设备激活 /// /// 设备机器码 /// 设备序列号,贴纸上,支持二维码和条形码 /// [HttpGet] public async Task ActiveAsync(string sn, string code) { return await _userService.ActiveAsync(sn, code); } /// /// 退出登录 /// /// /// [HttpGet] public async Task OutLogin(string sessionId) { return await _userService.OutLogin(sessionId); } /// /// 修改初始体重 /// /// /// [HttpPost] public async Task SubmitFirstWeight(UserFirstWeightC2SDto data) { return await _userService.SubmitFirstWeight(data); } } }