Coreshop/CoreCms.Net.Web.WebApi/Controllers/CommonController.cs
2025-09-05 13:20:26 +08:00

296 lines
12 KiB
C#

/***********************************************************************
* 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
{
/// <summary>
/// 通用调用接口数据
/// </summary>
[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;
private readonly ISQReservationsServices _SQReservationsServices;
private readonly ISQRoomsServices _SQRoomsServices;
private readonly ISysDictionaryDataServices _sysDictionaryDataServices;
private readonly ISysDictionaryServices _sysDictionaryServices;
private readonly ISQReservationParticipantsServices _SQReservationParticipantsServices;
/// <summary>
/// 构造函数
/// </summary>
public CommonController(ICoreCmsSettingServices settingServices
, ICoreCmsAreaServices areaServices
, IWebHostEnvironment webHostEnvironment, ICoreCmsServiceDescriptionServices serviceDescriptionServices, ICoreCmsSettingServices coreCmsSettingServices,
IToolsServices toolsServices,
ISQReservationsServices SQReservationsServices,
ISQRoomsServices SQRoomsServices,
ISysDictionaryDataServices sysDictionaryDataServices,
ISysDictionaryServices sysDictionaryServices,
ISQReservationParticipantsServices SQReservationParticipantsServices
)
{
_webHostEnvironment = webHostEnvironment;
_serviceDescriptionServices = serviceDescriptionServices;
_coreCmsSettingServices = coreCmsSettingServices;
_toolsServices = toolsServices;
_settingServices = settingServices;
_areaServices = areaServices;
_SQReservationsServices = SQReservationsServices;
_SQRoomsServices = SQRoomsServices;
_sysDictionaryDataServices = sysDictionaryDataServices;
_sysDictionaryServices = sysDictionaryServices;
_SQReservationParticipantsServices = SQReservationParticipantsServices;
}
//公共接口====================================================================================================
#region =============================================================
/// <summary>
/// 接口测试反馈
/// </summary>
/// <returns></returns>
[HttpGet]
public WebApiDto h()
{
var jm = new WebApiDto { Code = 0, Msg = "", Data = DateTime.Now };
return jm;
}
#endregion
#region V2.0===============================================================
/// <summary>
/// 返回配置数据文件V2.0
/// </summary>
/// <returns></returns>
[HttpGet]
public async Task<WebApiDto> 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 defaultImage = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopDefaultImage); //分享图片
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 dic = await _sysDictionaryServices.QueryListByClauseAsync(it => !it.deleted, it => it.sortNumber
, OrderByType.Asc);
var dicData = await _sysDictionaryDataServices.QueryListByClauseAsync(it => !it.deleted);
var options = new List<object>();
foreach (var item in dic)
{
var d = dicData.Where(it => it.dictId == item.id).Select(it => new { name = it.dictDataName, it.id }).ToList();
options.Add(new
{
name = item.dictName,
item.id,
children = d
});
}
var data = new
{
shopLogo,
shareTitle,
defaultImage,
shareDesc,
shareImage,
aboutArticleId,
userAgreementId,
privacyPolicyId,
playingMethodOptions = options
};
jm.Data = data;
return jm;
}
#endregion
#region =============================================================================
/// <summary>
/// 获取层级分配后的区域信息
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<WebApiCallBack> GetAreas()
{
var jm = new WebApiCallBack();
var areas = await _areaServices.GetCaChe();
jm.status = true;
jm.data = AreaHelper.GetList(areas);
return jm;
}
#endregion
#region
/// <summary>
/// 获取商城关键词说明列表
/// </summary>
/// <returns></returns>
[HttpPost]
public async Task<WebApiCallBack> 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 ====================================================
/// <summary>
/// 上传附件通用接口
/// </summary>
/// <returns></returns>
[Authorize]
[HttpPost]
public async Task<WebApiCallBack> 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
}
}