Files
IOT/Service/APIService.cs
2025-12-19 10:28:46 +08:00

389 lines
15 KiB
C#

using Common.DTOS;
using NetTaste;
using RestSharp;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Service
{
public class APIService
{
private static string Token = "Bearer u-cVaGe.hv160U5.zqxeyRKx11ifIR04iPjoayYQC00EXF";
#region
/// <summary>
/// 初始化多维表格
/// </summary>
/// <param name="FormName">表格名称</param>
public static FeishuResponse<CreateBitableAppData> InitForm(string FormName)
{
try
{
var client = new RestClient("https://open.feishu.cn/open-apis/bitable/v1/apps");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Token);
request.AddParameter("application/json", ParameterType.RequestBody);
request.AddJsonBody(new
{
folder_token = "",
name = FormName
});
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<CreateBitableAppData>>(response.Content);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 创建表
/// </summary>
/// <param name="FormName">多维表格ID</param>
/// <param name="FormName">表名称</param>
public static FeishuResponse<CreateTableData> AddTable(string FormID, string TableName)
{
try
{
var client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Token);
request.AddJsonBody(new
{
table = new
{
default_view_name = "默认的表格视图",
name = TableName,
fields = new object[]
{
new
{
field_name = "索引字段",
type = 1
},
new
{
field_name = "单选",
type = 3,
ui_type = "SingleSelect",
property = new
{
options = new[]
{
new { color = 0, name = "Enabled" },
new { color = 1, name = "Disabled" },
new { color = 2, name = "Draft" }
}
}
}
}
}
});
request.AddParameter("application/json", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<CreateTableData>>(response.Content);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 删除表
/// </summary>
/// <param name="FormID">多维表格ID</param>
/// <param name="TabelID">表ID</param>
public static FeishuResponse<object> DeleteTable(string FormID, string TabelID)
{
var client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables/{TabelID}");
client.Timeout = -1;
var request = new RestRequest(Method.DELETE);
request.AddHeader("Authorization", Token);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<object>>(response.Content);
}
/// <summary>
/// 获得表列表
/// </summary>
/// <param name="FormName">多维表格ID</param>
/// <param name="FormName">分页数</param>
/// <param name="FormName">表ID</param>
public static FeishuResponse<QueryTableListData> GetTableList(string FormID, int PageSize, string TabelID = "")
{
RestClient client;
if (TabelID == "")
{
client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables?page_size={PageSize}");
}
else
{
client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables?page_size={PageSize}&page_token={TabelID}");
}
client.Timeout = -1;
var request = new RestRequest(Method.GET);
request.AddHeader("Authorization", Token);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<QueryTableListData>>(response.Content);
}
#endregion
#region
/// <summary>
/// 添加一行记录
/// </summary>
/// <param name="FormID">多维表格ID</param>
/// <param name="TabelID">表ID</param>
public static FeishuResponse<CreateRecordData> AddRecord(string FormID, string TableID)
{
try
{
var client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables/{TableID}/records");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Token);
request.AddJsonBody(new
{
fields = new
{
= "333",
= "拜访潜在客户"
}
});
request.AddParameter("application/json", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<CreateRecordData>>(response.Content);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 更新一行记录
/// </summary>
/// <param name="FormID">多维表格ID</param>
/// <param name="TabelID">表ID</param>
/// <param name="TabelID">记录ID</param>
public static FeishuResponse<CreateRecordData> UpdateRecord(string FormID, string TableID, string RecordID)
{
try
{
var client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables/{TableID}/records/{RecordID}");
client.Timeout = -1;
var request = new RestRequest(Method.PUT);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Token);
request.AddJsonBody(new
{
fields = new
{
= "99",
= "mike"
}
});
request.AddParameter("application/json", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<CreateRecordData>>(response.Content);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 删除记录
/// </summary>
/// <param name="FormID">多维表格ID</param>
/// <param name="TabelID">表ID</param>
/// <param name="TabelID">记录ID</param>
public static FeishuResponse<object> DeleteRecord(string FormID, string TableID, string RecordID)
{
try
{
var client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables/{TableID}/records/{RecordID}");
client.Timeout = -1;
var request = new RestRequest(Method.DELETE);
request.AddHeader("Authorization", Token);
var body = "";
request.AddParameter("", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<object>>(response.Content);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 获取记录
/// </summary>
/// <param name="FormID">多维表格ID</param>
/// <param name="TabelID">表ID</param>
/// <param name="TabelID">分页数量</param>
public static FeishuResponse<QueryRecordListData> GetRecord(string FormID, string TableID, int PageSize)
{
try
{
var client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables/{TableID}/records/search?page_size={PageSize}");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", "Bearer u-dSpI1fRVp2jWi4Nyz0lIkal5i0n504UVMUaaYNy021um");
var body = "{}";
request.AddParameter("application/json", body, ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<QueryRecordListData>>(response.Content);
}
catch (Exception ex)
{
throw;
}
}
#endregion
#region
/// <summary>
/// 添加多行记录
/// </summary>
/// <param name="FormID">多维表格ID</param>
/// <param name="TabelID">表ID</param>
public static FeishuResponse<CreateRecordsRequest> AddMutipulRecords(string FormID, string TableID)
{
try
{
var client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables/{TableID}/records/batch_create");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Token);
request.AddJsonBody(new
{
records = new[]
{
new
{
fields = new
{
= "333",
= "拜访潜在客户1"
}
},
new
{
fields = new
{
= "333",
= "拜访潜在客户2"
}
}
}
});
request.AddParameter("application/json", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<CreateRecordsRequest>>(response.Content);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 更新多行记录
/// </summary>
/// <param name="FormID">多维表格ID</param>
/// <param name="TabelID">表ID</param>
/// <param name="FieldIDs">记录ID列表</param>
public static FeishuResponse<CreateRecordsRequest> UpdateRecords(string FormID, string TableID, string[] FieldIDs)
{
try
{
var client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables/{TableID}/records/batch_update");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Token);
request.AddJsonBody(new
{
records = new[]
{
new
{
record_id=FieldIDs[0],
fields = new
{
= "333",
= "改拜访潜在客户1"
}
},
new
{
record_id=FieldIDs[1],
fields = new
{
= "333",
= "改拜访潜在客户2"
}
}
}
});
request.AddParameter("application/json", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<CreateRecordsRequest>>(response.Content);
}
catch (Exception ex)
{
throw;
}
}
/// <summary>
/// 删除多行记录
/// </summary>
/// <param name="FormID">多维表格ID</param>
/// <param name="TabelID">表ID</param>
/// <param name="FieldIDs">记录ID列表</param>
public static FeishuResponse<object> DeleteRecords(string FormID, string TableID, string[] FieldIDs)
{
try
{
var client = new RestClient($"https://open.feishu.cn/open-apis/bitable/v1/apps/{FormID}/tables/{TableID}/records/batch_delete");
client.Timeout = -1;
var request = new RestRequest(Method.POST);
request.AddHeader("Content-Type", "application/json");
request.AddHeader("Authorization", Token);
request.AddJsonBody(new
{
records = FieldIDs
});
request.AddParameter("application/json", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
return Newtonsoft.Json.JsonConvert.DeserializeObject<FeishuResponse<object>>(response.Content);
}
catch (Exception ex)
{
throw;
}
}
#endregion
}
}