using Kingdee.BOS.WebApi.Client; using Newtonsoft.Json; using Newtonsoft.Json.Linq; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Diagnostics; using System.Linq; using System.ServiceProcess; using System.Text; using System.Threading.Tasks; namespace WindowsServiceTest { public partial class Service1 : ServiceBase { public Service1() { InitializeComponent(); } protected override void OnStart(string[] args) { System.Timers.Timer timer = new System.Timers.Timer(); timer.Elapsed += new System.Timers.ElapsedEventHandler(TimedEvent); timer.Interval = 1000 * 30; ;//每10秒执行一次 timer.Enabled = true; } protected override void OnStop() { } private string FieldKeys_MATERIAL = "FNUMBER,FMATERIALID,FMaterialId.FName,FITEMMODEL,FUNITID"; private string FieldKeys_BOM = "FNUMBER,FMATERIALID,FMaterialId.FName,FITEMMODEL,FUNITID"; private K3CloudApiClient apiClient = new K3CloudApiClient("https://rychip.ik3cloud.com/k3cloud/");//serverUrl需要以“/”结尾 private bool LoginByAppSecret() { string appId = "214972_7c3q2cuP4npZ2a1EQ/TtSZ9G4L2W7smL"; string appSecret = "e36cf74480c24f23b33eddb76760b434"; //dbId:账套ID //userName:第三方系统登录授权的用户 //appId:第三方系统登录授权的应用ID //appSecret:第三方系统登录授权的应用密钥 //lcid:账套语系,默认2052,(中文为2052,中文繁体为3076,英文为1033) string loginResult = apiClient.LoginByAppSecret("20180807124126", "administrator", appId, appSecret, 2052); JObject loginResultObj = JObject.Parse(loginResult); JToken loginResultType; loginResultObj.TryGetValue("LoginResultType", out loginResultType); // 登陆成功,开始保存数据 //loginResultType.Value(): // 0 密码错误 // 1 登录成功 // -1 登录失败 if (loginResultType != null && loginResultType.Value() == 1) { return true; } else { return false; } } private DataTable ToDataTable(List> jsonList, string fieldKeys) { DataTable dt = new DataTable(); string[] sArray = fieldKeys.Split(new string[] { "," }, StringSplitOptions.None); foreach (var item in sArray) { dt.Columns.Add(item); } for (int i = 0; i < jsonList.Count; i++) { DataRow dr = dt.NewRow(); for (int j = 0; j < jsonList[i].Count; j++) { dr[j] = jsonList[i][j] == null ? "" : jsonList[i][j].ToString(); } dt.Rows.Add(dr); } return dt; } private DataTable GetSource(string formId, string fieldKeys) { var dataObj = new { FormId = formId,//物料清单唯一标识"FormId"="BD_MATERIAL" ; BOM唯一标识FormId = "ENG_BOM" FieldKeys = fieldKeys,//需查询的字段key集合,字符串类型,格式:"key1,key2,..."(必录) 注(查询单据体内码,需加单据体Key和下划线,如:FEntryKey_FEntryId) FilterString = "FDocumentStatus='C'",//获取已经审核BOM OrderString = "",//排序 TopRowCount = 0, StartRow = 0, Limit = 0 }; var data = JsonConvert.SerializeObject(dataObj); var result = apiClient.ExecuteBillQuery(data); DataTable dataTable = ToDataTable(result, fieldKeys); return dataTable; } private void TimedEvent(object sender, System.Timers.ElapsedEventArgs e) { if (LoginByAppSecret()) { DataTable dataTable_MATERIAL = GetSource("BD_MATERIAL", FieldKeys_MATERIAL); DataTable dataTable_BOM = GetSource("ENG_BOM", FieldKeys_BOM); Console.WriteLine("已获取到数据"); Console.Read(); } } } }