/*********************************************************************** * Project: CoreCms * ProjectName: 核心内容管理系统 * Web: https://www.corecms.net * Author: 大灰灰 * Email: jianweie@163.com * CreateTime: 2021/1/31 21:45:10 * Description: 暂无 ***********************************************************************/ using System; using System.Collections.Generic; using System.Globalization; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using Aliyun.OSS; using Aliyun.OSS.Util; using CoreCms.Net.Configuration; using CoreCms.Net.IServices; using CoreCms.Net.Model.Entities; using CoreCms.Net.Model.FromBody; using CoreCms.Net.Model.ViewModels.Options; using CoreCms.Net.Model.ViewModels.UI; using CoreCms.Net.Utility.Extensions; using CoreCms.Net.Utility.Helper; using COSXML; using COSXML.Auth; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Mvc; using Microsoft.Extensions.Options; using SqlSugar; namespace CoreCms.Net.Web.WebApi.Controllers { /// /// 通用调用接口数据 /// [Route("api/[controller]/[action]")] [ApiController] public class CommonController : ControllerBase { private readonly IWebHostEnvironment _webHostEnvironment; private readonly ICoreCmsSettingServices _settingServices; private readonly ICoreCmsAreaServices _areaServices; private readonly ICoreCmsServiceDescriptionServices _serviceDescriptionServices; private readonly ICoreCmsSettingServices _coreCmsSettingServices; private readonly IToolsServices _toolsServices; /// /// 构造函数 /// public CommonController(ICoreCmsSettingServices settingServices , ICoreCmsAreaServices areaServices , IWebHostEnvironment webHostEnvironment, ICoreCmsServiceDescriptionServices serviceDescriptionServices, ICoreCmsSettingServices coreCmsSettingServices, IToolsServices toolsServices) { _webHostEnvironment = webHostEnvironment; _serviceDescriptionServices = serviceDescriptionServices; _coreCmsSettingServices = coreCmsSettingServices; _toolsServices = toolsServices; _settingServices = settingServices; _areaServices = areaServices; } //公共接口==================================================================================================== #region 接口测试反馈============================================================= /// /// 接口测试反馈 /// /// [HttpPost] public WebApiCallBack InterFaceTest() { var jm = new WebApiCallBack { status = true, msg = "接口访问正常", data = DateTime.Now }; return jm; } #endregion #region 返回配置数据文件V2.0=============================================================== /// /// 返回配置数据文件V2.0 /// /// [HttpGet] public async Task GetConfig() { var jm = new WebApiDto(); var allConfigs = await _settingServices.GetConfigDictionaries(); var shopLogo = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopLogo); //店铺logo var shareTitle = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShareTitle); //分享标题 var shareDesc = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShareDesc); //分享描述 var shareImage = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShareImage); //分享图片 var aboutArticleId = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.AboutArticleId).ObjectToInt(2); //关于我们文章 //var entId = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.EntId); //客服ID var userAgreementId = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.UserAgreementId).ObjectToInt(3); //用户协议 var privacyPolicyId = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.PrivacyPolicyId).ObjectToInt(4); //隐私政策 var data = new { shopLogo, shareTitle, shareDesc, shareImage, aboutArticleId, userAgreementId, privacyPolicyId, }; jm.Data = data; return jm; } #endregion #region 获取区域配置============================================================================= /// /// 获取层级分配后的区域信息 /// /// [HttpPost] public async Task GetAreas() { var jm = new WebApiCallBack(); var areas = await _areaServices.GetCaChe(); jm.status = true; jm.data = AreaHelper.GetList(areas); return jm; } #endregion #region 获取商城关键词说明列表 /// /// 获取商城关键词说明列表 /// /// [HttpPost] public async Task GetServiceDescription() { var jm = new WebApiCallBack(); var caCheList = await _serviceDescriptionServices.GetCaChe(); jm.status = true; jm.data = new { commonQuestion = caCheList.Where(p => p.type == (int)GlobalEnumVars.ShopServiceNoteType.CommonQuestion && p.isShow == true).OrderBy(p => p.sortId).ToList(), service = caCheList.Where(p => p.type == (int)GlobalEnumVars.ShopServiceNoteType.Service && p.isShow == true).OrderBy(p => p.sortId).ToList(), delivery = caCheList.Where(p => p.type == (int)GlobalEnumVars.ShopServiceNoteType.Delivery && p.isShow == true).OrderBy(p => p.sortId).ToList() }; return jm; } #endregion //验证接口==================================================================================================== #region 上传附件通用接口==================================================== /// /// 上传附件通用接口 /// /// [Authorize] [HttpPost] public async Task UploadImages() { var jm = new WebApiCallBack(); var filesStorageOptions = await _coreCmsSettingServices.GetFilesStorageOptions(); //初始化上传参数 var maxSize = 1024 * 1024 * filesStorageOptions.MaxSize; //上传大小5M var file = Request.Form.Files["file"]; if (file == null) { jm.msg = "请选择文件"; return jm; } string fileName = file.FileName; string fileExt = Path.GetExtension(fileName).ToLowerInvariant(); //检查大小 if (file.Length > maxSize) { jm.msg = "上传文件大小超过限制,最大允许上传" + filesStorageOptions.MaxSize + "M"; return jm; } //检查文件扩展名 if (string.IsNullOrEmpty(fileExt) || Array.IndexOf(filesStorageOptions.FileTypes.Split(','), fileExt.Substring(1).ToLower()) == -1) { jm.msg = "上传文件扩展名是不允许的扩展名,请上传后缀名为:" + filesStorageOptions.FileTypes; return jm; } // 使用StreamReader来读取文件内容 using (var reader = new StreamReader(file.OpenReadStream(), Encoding.UTF8)) { var content = await reader.ReadToEndAsync(); // 注意:这可能会消耗大量内存对于大文件,所以需要限制上传大小 // 检查内容是否合法 if (CommonHelper.CheckData(content)) { jm.msg = "请勿提交非法数据。"; return jm; } } 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); } var bl = !string.IsNullOrEmpty(url); jm.status = bl; jm.code = bl ? 0 : 1; jm.msg = bl ? "上传成功!" : "上传失败"; jm.data = new { fileUrl = url, src = url, imageId = url }; return jm; } #endregion } }