/*********************************************************************** * 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.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.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; private readonly ICoreCmsAppUpdateLogServices _appUpdateLogServices; /// /// 构造函数 /// public CommonController(ICoreCmsSettingServices settingServices , ICoreCmsAreaServices areaServices , IWebHostEnvironment webHostEnvironment, ICoreCmsServiceDescriptionServices serviceDescriptionServices, ICoreCmsSettingServices coreCmsSettingServices, IToolsServices toolsServices, ICoreCmsAppUpdateLogServices appUpdateLogServices) { _webHostEnvironment = webHostEnvironment; _serviceDescriptionServices = serviceDescriptionServices; _coreCmsSettingServices = coreCmsSettingServices; _toolsServices = toolsServices; _appUpdateLogServices = appUpdateLogServices; _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 /// /// [HttpPost] public async Task GetConfigV2() { var jm = new WebApiCallBack { status = true, msg = "接口访问正常", data = DateTime.Now }; var allConfigs = await _settingServices.GetConfigDictionaries(); var shopLogo = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopLogo); //店铺logo var shopName = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopName); //店铺名称 var shopBeiAn = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopBeiAn); //店铺备案 var shopDesc = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopDesc); //店铺描述 var showStoreBalanceRechargeSwitch = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowStoreBalanceRechargeSwitch).ObjectToInt(2); //显示充值功能 var isAllowWithdrawCash = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.IsAllowWithdrawCash).ObjectToInt(2); //显示充值功能 var showStoresSwitch = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowStoresSwitch).ObjectToInt(2); //显示门店列表 var showVas = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowVas).ObjectToInt(2); //显示增值服务板块 var showServicePackage = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowServicePackage).ObjectToInt(2); //显示服务商品按钮 var showCoupon = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowCoupon).ObjectToInt(2); //显示优惠券按钮 var showPinTuan = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowPinTuan).ObjectToInt(2); //显示拼团按钮 var showSeckill = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowSeckill).ObjectToInt(2); //显示秒杀按钮 var showGroupBuying = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowGroupBuying).ObjectToInt(2); //显示团购按钮 var showSolitaire = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowSolitaire).ObjectToInt(2); //显示接龙按钮 var showCalendar = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowCalendar).ObjectToInt(2); //显示签到按钮 var showLiveBroadCast = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowLiveBroadCast).ObjectToInt(2); //显示小程序直播入口 var showIndexPopupWindow = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowIndexPopupWindow).ObjectToInt(2); //显示首页弹窗 var indexPopupWindowImageUrl = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.IndexPopupWindowImageUrl); //首页弹窗图片地址 var indexPopupWindowHrefUrl = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.IndexPopupWindowHrefUrl); //显示首页弹窗 var showCustomForm = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowCustomForm).ObjectToInt(2); //显示自定义表单按钮 var imageMax = 5; //前端上传图片最多几张 var storeSwitch = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.StoreSwitch).ObjectToInt(); //开启门店自提状态 var cateStyle = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.CateStyle).ObjectToInt(); //分类样式 var cateType = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.CateType).ObjectToInt(); //H5分类类型 var toCashMoneyLow = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.TocashMoneyLow); //最低提现 var toCashMoneyRate = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.TocashMoneyRate); //服务费 var pointSwitch = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.PointSwitch).ObjectToInt(); //是否开启积分功能 var pointExchangeModel = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.PointExchangeModel).ObjectToInt(); //积分模式 var pointDiscountedProportion = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.PointDiscountedProportion).ObjectToInt(); //积分折现比例 var pointShowName = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.PointShowName); //积分昵称 var pointShowExchangePrice = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.PointShowExchangePrice).ObjectToInt(); //积分昵称 var pointGetModel = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.PointGetModel).ObjectToInt(); //积分昵称 var pointShowPoint = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.PointShowPoint).ObjectToInt(); //积分昵称 var statistics = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.StatisticsCode); //获取统计代码 var recommendKeysStr = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.RecommendKeys); var recommendKeys = !string.IsNullOrEmpty(recommendKeysStr) ? recommendKeysStr.Split("|") : new string[] { }; //搜索推荐关键字 var invoiceSwitch = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.InvoiceSwitch).ObjectToInt(); //发票功能开关 var goodsStocksWarn = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.GoodsStocksWarn).ObjectToInt(); //库存报警数量 var shopDefaultImage = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopDefaultImage); //获取默认图片 var shopMobile = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShopMobile); //店铺联系电话 var openDistribution = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.OpenDistribution).ObjectToInt(); //是否开启分销 var distributionNotes = string.Empty; var distributionAgreement = string.Empty; var distributionStore = 2; if (openDistribution == 1) { distributionNotes = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DistributionNotes); //用户须知 distributionAgreement = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DistributionAgreement); //分销协议 distributionStore = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.DistributionStore).ObjectToInt(2); //是否开启店铺 } var showInviter = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ShowInviterInfo).ObjectToInt(); //是否显示邀请人信息 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 reshipName = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ReshipName); //退货联系人 var reshipMobile = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ReshipMobile); //退货联系方式 var reshipAreaId = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ReshipAreaId); //退货区域 var reshipAddress = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ReshipAddress); //退货联系方式 var reshipCoordinate = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.ReshipCoordinate); //退货坐标 var orderCancelTime = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.OrderCancelTime).ObjectToInt(60); //订单取消时间 //代理 var isOpenAgent = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.IsOpenAgent).ObjectToInt(); //是否开启代理模块 var isShowAgentPortal = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.IsShowAgentPortal).ObjectToInt(); //是否显示代理模块入口 var agentNotes = string.Empty; var agentAgreement = string.Empty; var agentStore = 2; if (isOpenAgent == 1 && isShowAgentPortal == 1) { agentNotes = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.AgentNotes); //用户须知 agentAgreement = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.AgentAgreement); //分销协议 agentStore = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.AgentStore).ObjectToInt(2); //是否开启代理店铺 } var qqMapKey = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.QqMapKey); //腾讯地图key var checkInIsOpen = CommonHelper.GetConfigDictionary(allConfigs, SystemSettingConstVars.CheckInIsOpen).ObjectToInt(2); //订单取消时间 var model = new { shopLogo, shopName, shopBeiAn, shopDesc, imageMax, storeSwitch, showStoresSwitch, showStoreBalanceRechargeSwitch, isAllowWithdrawCash, cateStyle, cateType, toCashMoneyLow, toCashMoneyRate, pointSwitch, pointExchangeModel, pointShowName, pointShowExchangePrice, pointDiscountedProportion, pointGetModel, pointShowPoint, statistics, recommendKeys, invoiceSwitch, goodsStocksWarn, shopDefaultImage, shopMobile, openDistribution, distributionNotes, distributionAgreement, distributionStore, showInviter, shareTitle, shareDesc, shareImage, aboutArticleId, entId, userAgreementId, privacyPolicyId, reshipName, reshipMobile, reshipAreaId, reshipAddress, reshipCoordinate, orderCancelTime, isOpenAgent, isShowAgentPortal, agentNotes, agentAgreement, agentStore, qqMapKey, checkInIsOpen, showVas, showServicePackage, showCoupon, showPinTuan, showSeckill, showGroupBuying, showSolitaire, showCalendar, showIndexPopupWindow, indexPopupWindowImageUrl, indexPopupWindowHrefUrl, showCustomForm, showLiveBroadCast }; jm.data = model; 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 获取最后更新版本号 /// /// 获取商城关键词说明列表 /// /// [HttpPost] public async Task GetAppVersions() { var jm = new WebApiCallBack(); var model = await _appUpdateLogServices.QueryByClauseAsync(p => p.isUpdate == true, p => p.id, OrderByType.Desc); jm.status = true; jm.data = model; 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; } 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 #region 裁剪Base64上传==================================================== /// /// 裁剪Base64上传 /// /// [Authorize] [HttpPost] public async Task UploadFilesFByBase64([FromBody] FMBase64Post entity) { var jm = new AdminUiCallBack(); var filesStorageOptions = await _coreCmsSettingServices.GetFilesStorageOptions(); if (string.IsNullOrEmpty(entity.base64)) { jm.msg = "请上传合法内容"; return jm; } //检查上传大小 if (!CommonHelper.CheckBase64Size(entity.base64, filesStorageOptions.MaxSize)) { jm.msg = "上传文件大小超过限制,最大允许上传" + filesStorageOptions.MaxSize + "M"; return jm; } entity.base64 = entity.base64.Replace("data:image/png;base64,", "").Replace("data:image/jgp;base64,", "").Replace("data:image/jpg;base64,", "").Replace("data:image/jpeg;base64,", "");//将base64头部信息替换 byte[] bytes = Convert.FromBase64String(entity.base64); MemoryStream memStream = new MemoryStream(bytes); string url = string.Empty; if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.LocalStorage.ToString()) { url = _toolsServices.UpLoadBase64ForLocalStorage(filesStorageOptions, memStream, (int)GlobalEnumVars.FilesStorageLocation.API); } else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.AliYunOSS.ToString()) { //上传到阿里云 url = await _toolsServices.UpLoadBase64ForAliYunOSS(filesStorageOptions, memStream); } else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QCloudOSS.ToString()) { //上传到腾讯云OSS url = _toolsServices.UpLoadBase64ForQCloudOSS(filesStorageOptions, bytes); } else if (filesStorageOptions.StorageType == GlobalEnumVars.FilesStorageOptionsType.QiNiuKoDo.ToString()) { //上传到七牛云kodo url = _toolsServices.UpLoadBase64ForQiNiuKoDo(filesStorageOptions, bytes); } var bl = !string.IsNullOrEmpty(url); jm.code = bl ? 0 : 1; jm.msg = bl ? "上传成功!" : "上传失败"; jm.data = new { fileUrl = url, src = url }; return jm; } #endregion } }