using Common.DbExtends; using Common.DbExtends.Extends; using Common.Models; using Common.Models.Enums; using Common.Models.JsonModels; using Common.Models.UnqTables; using Common.Utils; using Newtonsoft.Json; using Server.Utils; using SqlSugar; using System; using System.Collections.Generic; using System.Collections.Specialized; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading; using System.Threading.Tasks; using System.Web.Http; namespace Server.Controllers.FunctionSetting { /// /// 全局设置 /// public class PubController : DefaultController { #region 配置存储相关 [HttpPost, ErrorFilter] public WebResult GetConfig() { var pubConfig = Db.GetPubConfig(); if (pubConfig == null) pubConfig = new PublicConfig(); return PutData(pubConfig); } [HttpPost, ErrorFilter] public WebResult SaveConfig() { //var pubConfig = Db.GetPubConfig(); //if (pubConfig == null) pubConfig = new PublicConfig(); //Util.CopyToObj(this.Param, pubConfig); var ConfigStr = GetString("Cfg", true); var PubConfig = JsonConvert.DeserializeObject(ConfigStr); if (PubConfig == null) return PutData("保存失败,未能识别到您的数据"); if (PubConfig.CashNewPrevious < 0.3 || PubConfig.CashNewNext < 0.3) return PutData("保存失败,提现金额不能低于0.3元"); if (PubConfig.UserStatus == UserStatus.未知 || PubConfig.KeywordStatus == UserStatus.未知 || PubConfig.RefundStatus == UserStatus.未知 || PubConfig.SameSellStatus == UserStatus.未知) return PutData("保存失败,用户状态未设置"); if (!PubConfig.ShDefault && PubConfig.ShId == 0) return PutData("保存失败,您尚未选择支付的商户号"); var config = Db.Queryable().Where(f => f.Type == ConfigType.全局).First(); if (config == null) config = new ConfigData() { Type = ConfigType.全局, Name = "" }; config.JsonContent = ConfigStr; Db.Save(config); var RobotIds = Db.Queryable().Select(f => f.Id).ToList(); if (RobotIds.Count == 0) return PutData("添加失败,暂未发现有任何可用的机器人"); foreach (var RobotId in RobotIds) Client.SendClientMsg(RobotId, DeviceMessageType.全局配置修改, new { }); return PutSuccess; } #endregion #region 通知API相关 /// /// 查询机器人通知接口 /// /// [HttpPost, ErrorFilter] public WebResult GetRobotNotify() { var PageIndex = GetInt("PageIndex"); var PageSize = GetInt("PageSize"); var tNumber = 0; var Keyword = GetString("Keyword"); var exp = Expressionable.Create(); if (!string.IsNullOrEmpty(Keyword)) { exp.And(f => f.Name.Contains(Keyword)); } var DataList = Db.Queryable().Where(exp.ToExpression()) .ToPageList(PageIndex, PageSize, ref tNumber); var res = new PageResult(DataList, tNumber, PageSize, PageIndex); return PutData(res); } /// /// 编辑通知接口 /// /// [HttpPost, ErrorFilter] public WebResult UpdRobotNotify() { var Id = GetInt("Id", true); var notify = Db.Queryable() .Single(f => f.Id == Id); if (notify == null) return PutData("编辑失败,此通知接口不存在"); var Name = GetString("Name", true); if (notify.Name == Name) return PutData("编辑失败,此名称已存在"); notify.Name = Name; var ApiLocation = GetString("ApiLocation", true); notify.ApiLocation = ApiLocation; var Appsecret = GetString("Appsecret"); notify.Appsecret = Appsecret; if (ApiLocation.Contains("weixin")) notify.NoticeApiType = NoticeApiType.企业微信机器人API; else if (ApiLocation.Contains("dingding")) notify.NoticeApiType = NoticeApiType.企业钉钉机器人API; else if (ApiLocation.Contains("feishu")) notify.NoticeApiType = NoticeApiType.飞书机器人API; else return PutData("编辑失败,无法识别链接类型"); var res = Db.Updateable(notify).ExecuteCommand(); if (res > 0) return PutSuccess; else return PutError; } /// /// 删除通知接口 /// /// [HttpPost, ErrorFilter] public WebResult DeleteNotify() { var Id = GetInt("Id", true); var res = Db.Deleteable() .Where(f => f.Id == Id).ExecuteCommand(); if (res > 0) return PutSuccess; return PutData("删除失败,此通知接口不存在"); } /// /// 新增通知接口 /// /// [HttpPost, ErrorFilter] public WebResult AddNotify() { var Name = GetString("Name", true); //var NoticeApiType = GetEnum("NoticeApiType", true);//通知接口类型 : 企业钉钉机器人API =1, 企业微信机器人API = 2, 飞书机器人API = 3 var ApiLocation = GetString("ApiLocation", true); var Appsecret = GetString("Appsecret"); var notify = Db.Queryable().Where(f => f.Name == Name).First(); if (notify != null) return PutData("新增失败,此名称已存在"); notify = new RobotNotify() { Name = Name, Appsecret = Appsecret, ApiLocation = ApiLocation }; if (ApiLocation.Contains("weixin")) notify.NoticeApiType = NoticeApiType.企业微信机器人API; else if (ApiLocation.Contains("dingding")) notify.NoticeApiType = NoticeApiType.企业钉钉机器人API; else if (ApiLocation.Contains("feishu")) notify.NoticeApiType = NoticeApiType.飞书机器人API; else return PutData("编辑失败,无法识别链接类型"); var res = Db.Insertable(notify).ExecuteCommand(); if (res > 0) return PutSuccess; return PutError; } /// /// 测试通知接口 /// /// [HttpPost, ErrorFilter] public WebResult TestNotify() { var Message = GetString("Message", true); var ApiLocation = GetString("ApiLocation", true); var Appsecret = GetString("Appsecret"); var noticeApiType = NoticeApiType.其他; if (ApiLocation.Contains("weixin")) noticeApiType = NoticeApiType.企业微信机器人API; else if (ApiLocation.Contains("dingding")) noticeApiType = NoticeApiType.企业钉钉机器人API; else if (ApiLocation.Contains("feishu")) noticeApiType = NoticeApiType.飞书机器人API; else return PutData("编辑失败,无法识别链接类型"); var result = Util.RobotNotify(Message, ApiLocation, noticeApiType, Appsecret); if (!string.IsNullOrEmpty(result)) return PutData(result); else return PutSuccess; } #endregion #region 商户配置相关 /// /// 获取商户信息信息 /// /// [HttpPost, ErrorFilter] public WebResult GetShops() { var PageIndex = GetInt("PageIndex"); var PageSize = GetInt("PageSize"); var tNumber = 0; var Keyword = GetString("Keyword"); var exp = Expressionable.Create(); if (!string.IsNullOrEmpty(Keyword)) { exp.And(f => f.Remark.Contains(Keyword)); } var DataList = Db.Queryable().Where(exp.ToExpression()) .ToPageList(PageIndex, PageSize, ref tNumber); var res = new PageResult(DataList, tNumber, PageSize, PageIndex); return PutData(res); } /// /// 新增商户 /// /// [HttpPost, ErrorFilter] public WebResult AddShop() { var Remark = GetString("Remark"); var AppId = GetString("AppId", true); var AppSecret = GetString("AppSecret", true); var PartnerId = "";//GetString("PartnerId", true); var PartnerKey = GetString("PartnerKey", true); var FootTxt = GetString("FootTxt"); var CertPath = GetString("CertPath", true); var shopType = GetEnum("ShopType", true); bool isExist = false; if (shopType == ShopType.微信商户) { PartnerId = GetString("PartnerId", true); isExist = Db.Queryable().Any(x => x.PartnerId == PartnerId); } else { isExist = Db.Queryable().Any(x => x.AppId == AppId); } if (isExist) { return PutData("当前商户ID已存在"); } var shop = new Shop() { AppId = AppId, AppSecret = AppSecret, PartnerId = PartnerId, PartnerKey = PartnerKey, FootTxt = FootTxt, CertPaths = CertPath, Remark = Remark, ShopType = shopType }; Db.Insertable(shop).ExecuteCommand(); return PutSuccess; } /// /// 编辑商户 /// /// [HttpPost, ErrorFilter] public WebResult SaveShop() { var Id = GetInt("Id", true); var Remark = GetString("Remark"); var AppId = GetString("AppId", true); var AppSecret = GetString("AppSecret", true); //var PartnerId = GetString("PartnerId", true); var PartnerKey = GetString("PartnerKey", true); var FootTxt = GetString("FootTxt"); var CertPath = GetString("CertPath", true); //var shopType = GetEnum("ShopType", true); var shop = Db.Queryable().First(x => x.Id == Id); if (shop == null) { return PutData("目标商户ID不存在"); } if (shop.ShopType == ShopType.微信商户) { shop.PartnerId = GetString("PartnerId", true); } shop.AppId = AppId; shop.AppSecret = AppSecret; //shop.PartnerId = PartnerId; shop.PartnerKey = PartnerKey; shop.FootTxt = FootTxt; shop.CertPaths = CertPath; shop.Remark = Remark; Db.Storageable(shop).ExecuteCommand(); return PutSuccess; } /// /// 删除商户 /// /// [HttpPost, ErrorFilter] public WebResult DelShop() { var Id = GetInt("Id", true); var shop = Db.Queryable().First(x => x.Id == Id); if (shop == null) { return PutData("目标商户ID不存在"); } Db.Deleteable(shop).ExecuteCommand(); return PutSuccess; } #endregion /// /// 新增全局变量 /// /// [HttpPost, ErrorFilter] public WebResult AddGlobalVariable() { var name = GetString("Name", true); var value = GetString("Value", true); var item = this.Db.Queryable().Where(w => w.Name == name).First(); if (item == null) { item = new GlobalVariable(); item.Name = name; item.Value = value; } else { return PutData("数据已经存在"); } this.Db.Storageable(item).ExecuteCommand(); return PutSuccess; } /// /// 编辑全局变量 /// /// [HttpPost, ErrorFilter] public WebResult EditGlobalVariable() { var id = GetInt("Id", true); var name = GetString("Name", true); var value = GetString("Value", true); var item = this.Db.Queryable().Where(w => w.Id == id).First(); if (item == null) { return PutData("数据不存在"); } else { item.Name = name; item.Value = value; } this.Db.Storageable(item).ExecuteCommand(); return PutSuccess; } /// /// 分页获取全局变量 /// /// [HttpPost, ErrorFilter] public WebResult GetGlobalVariableList() { var Keyword = GetString("Keyword"); var PageIndex = GetInt("PageIndex", true); var PageSize = GetInt("PageSize", true); if (PageSize > 100) PageSize = 100; var tNumber = 0; var exp = Expressionable.Create(); if (!string.IsNullOrEmpty(Keyword)) { exp.And(f => f.Name.Contains(Keyword)); } var DataList = Db.Queryable().Where(exp.ToExpression()).ToPageList(PageIndex, PageSize, ref tNumber); var res = new PageResult(DataList, tNumber, PageSize, PageIndex); return PutData(res); } /// /// 删除全局变量 /// /// [HttpPost, ErrorFilter] public WebResult RemoveGlobalVariable() { var name = GetString("Name", true); var item = this.Db.Queryable().Where(w => w.Name == name).First(); if (item != null) { this.Db.Deleteable(item).ExecuteCommand(); } return PutSuccess; } /// /// 设置网站信息 /// /// [HttpPost, ErrorFilter] public WebResult SetWebInfo() { //如果是网站上登录,但是不是创建者 if (this.Session != null && !this.Session.IsCreator) { return PutData("无权操作"); } var logo = GetString("Logo", true); var webColorType = GetString("WebColorType", true); var webName = GetString("WebName", true); var webNaviType = GetString("WebNaviType", true); var webThemeColor = GetString("WebThemeColor"); var copyright = GetString("Copyright"); var data = new WebInfo() { Logo = logo, WebColorType = webColorType, WebName = webName, WebNaviType = webNaviType, WebThemeColor = webThemeColor, Copyright = copyright }; Db.Save(data); return PutSuccess; } } }