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 表处理 /// /// 初始化多维表格 /// /// 表格名称 public static FeishuResponse 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>(response.Content); } catch (Exception ex) { throw; } } /// /// 创建表 /// /// 多维表格ID /// 表名称 public static FeishuResponse 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>(response.Content); } catch (Exception ex) { throw; } } /// /// 删除表 /// /// 多维表格ID /// 表ID public static FeishuResponse 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>(response.Content); } /// /// 获得表列表 /// /// 多维表格ID /// 分页数 /// 表ID public static FeishuResponse 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>(response.Content); } #endregion #region 行处理 /// /// 添加一行记录 /// /// 多维表格ID /// 表ID public static FeishuResponse 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>(response.Content); } catch (Exception ex) { throw; } } /// /// 更新一行记录 /// /// 多维表格ID /// 表ID /// 记录ID public static FeishuResponse 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>(response.Content); } catch (Exception ex) { throw; } } /// /// 删除记录 /// /// 多维表格ID /// 表ID /// 记录ID public static FeishuResponse 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>(response.Content); } catch (Exception ex) { throw; } } /// /// 获取记录 /// /// 多维表格ID /// 表ID /// 分页数量 public static FeishuResponse 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>(response.Content); } catch (Exception ex) { throw; } } #endregion #region 多行处理 /// /// 添加多行记录 /// /// 多维表格ID /// 表ID public static FeishuResponse 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>(response.Content); } catch (Exception ex) { throw; } } /// /// 更新多行记录 /// /// 多维表格ID /// 表ID /// 记录ID列表 public static FeishuResponse 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>(response.Content); } catch (Exception ex) { throw; } } /// /// 删除多行记录 /// /// 多维表格ID /// 表ID /// 记录ID列表 public static FeishuResponse 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>(response.Content); } catch (Exception ex) { throw; } } #endregion } }