yz_server/Server/Controllers/FunctionSetting/PubController.cs

488 lines
16 KiB
C#
Raw Normal View History

2022-04-16 07:48:12 +00:00
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
{
/// <summary>
/// 全局设置
/// </summary>
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<PublicConfig>(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<ConfigData>().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<Robot>().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相关
/// <summary>
/// 查询机器人通知接口
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult GetRobotNotify()
{
var PageIndex = GetInt("PageIndex");
var PageSize = GetInt("PageSize");
var tNumber = 0;
var Keyword = GetString("Keyword");
var exp = Expressionable.Create<RobotNotify>();
if (!string.IsNullOrEmpty(Keyword))
{
exp.And(f => f.Name.Contains(Keyword));
}
var DataList = Db.Queryable<RobotNotify>().Where(exp.ToExpression())
.ToPageList(PageIndex, PageSize, ref tNumber);
var res = new PageResult<RobotNotify>(DataList, tNumber, PageSize, PageIndex);
return PutData(res);
}
/// <summary>
/// 编辑通知接口
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult UpdRobotNotify()
{
var Id = GetInt("Id", true);
var notify = Db.Queryable<RobotNotify>()
.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;
}
/// <summary>
/// 删除通知接口
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult DeleteNotify()
{
var Id = GetInt("Id", true);
var res = Db.Deleteable<RobotNotify>()
.Where(f => f.Id == Id).ExecuteCommand();
if (res > 0)
return PutSuccess;
return PutData("删除失败,此通知接口不存在");
}
/// <summary>
/// 新增通知接口
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult AddNotify()
{
var Name = GetString("Name", true);
//var NoticeApiType = GetEnum<NoticeApiType>("NoticeApiType", true);//通知接口类型 : 企业钉钉机器人API =1, 企业微信机器人API = 2, 飞书机器人API = 3
var ApiLocation = GetString("ApiLocation", true);
var Appsecret = GetString("Appsecret");
var notify = Db.Queryable<RobotNotify>().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;
}
/// <summary>
/// 测试通知接口
/// </summary>
/// <returns></returns>
[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
/// <summary>
/// 获取商户信息信息
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult GetShops()
{
var PageIndex = GetInt("PageIndex");
var PageSize = GetInt("PageSize");
var tNumber = 0;
var Keyword = GetString("Keyword");
var exp = Expressionable.Create<Shop>();
if (!string.IsNullOrEmpty(Keyword))
{
exp.And(f => f.Remark.Contains(Keyword));
}
var DataList = Db.Queryable<Shop>().Where(exp.ToExpression())
.ToPageList(PageIndex, PageSize, ref tNumber);
var res = new PageResult<Shop>(DataList, tNumber, PageSize, PageIndex);
return PutData(res);
}
/// <summary>
/// 新增商户
/// </summary>
/// <returns></returns>
[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>("ShopType", true);
bool isExist = false;
if (shopType == ShopType.)
{
PartnerId = GetString("PartnerId", true);
isExist = Db.Queryable<Shop>().Any(x => x.PartnerId == PartnerId);
}
else
{
isExist = Db.Queryable<Shop>().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;
}
/// <summary>
/// 编辑商户
/// </summary>
/// <returns></returns>
[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>("ShopType", true);
var shop = Db.Queryable<Shop>().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;
}
/// <summary>
/// 删除商户
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult DelShop()
{
var Id = GetInt("Id", true);
var shop = Db.Queryable<Shop>().First(x => x.Id == Id);
if (shop == null)
{
return PutData("目标商户ID不存在");
}
Db.Deleteable(shop).ExecuteCommand();
return PutSuccess;
}
#endregion
/// <summary>
/// 新增全局变量
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult AddGlobalVariable()
{
var name = GetString("Name", true);
var value = GetString("Value", true);
var item = this.Db.Queryable<GlobalVariable>().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;
}
/// <summary>
/// 编辑全局变量
/// </summary>
/// <returns></returns>
[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<GlobalVariable>().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;
}
/// <summary>
/// 分页获取全局变量
/// </summary>
/// <returns></returns>
[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<GlobalVariable>();
if (!string.IsNullOrEmpty(Keyword))
{
exp.And(f => f.Name.Contains(Keyword));
}
var DataList = Db.Queryable<GlobalVariable>().Where(exp.ToExpression()).ToPageList(PageIndex, PageSize, ref tNumber);
var res = new PageResult<GlobalVariable>(DataList, tNumber, PageSize, PageIndex);
return PutData(res);
}
/// <summary>
/// 删除全局变量
/// </summary>
/// <returns></returns>
[HttpPost, ErrorFilter]
public WebResult RemoveGlobalVariable()
{
var name = GetString("Name", true);
var item = this.Db.Queryable<GlobalVariable>().Where(w => w.Name == name).First();
if (item != null)
{
this.Db.Deleteable(item).ExecuteCommand();
}
return PutSuccess;
}
/// <summary>
/// 设置网站信息
/// </summary>
/// <returns></returns>
[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;
}
}
}