From b43fb164d887bcc5b8a923f6c2ecfb737cb42bc3 Mon Sep 17 00:00:00 2001 From: sunny <2371019647@qq.com> Date: Fri, 24 Oct 2025 10:40:32 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E5=AE=9C=E6=90=AD=E6=B5=8B?= =?UTF-8?q?=E8=AF=95=E4=BB=A3=E7=A0=812?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../MQTT-WinformV1/MqttClientService.cs | 159 +++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-) diff --git a/MQTT-Winform/MQTT-WinformV1/MqttClientService.cs b/MQTT-Winform/MQTT-WinformV1/MqttClientService.cs index 0fb4abb..f87e393 100644 --- a/MQTT-Winform/MQTT-WinformV1/MqttClientService.cs +++ b/MQTT-Winform/MQTT-WinformV1/MqttClientService.cs @@ -15,6 +15,20 @@ using System.Threading.Tasks; using System.Windows.Forms; +//上传宜搭相关包 +using AlibabaCloud.SDK.Dingtalkcrm_1_0.Models; +using AlibabaCloud.SDK.Dingtalkoauth2_1_0.Models; +using AlibabaCloud.SDK.Dingtalkyida_1_0.Models; +using Newtonsoft.Json; +using System.Collections.Generic; +using Tea; + + +using AlibabaCloud.SDK.Dingtalkoauth2_1_0; +using AlibabaCloud.SDK.Dingtalkyida_1_0; +using AlibabaCloud.OpenApiClient.Models; + + namespace MqttClient { public class MqttClientService @@ -24,6 +38,16 @@ namespace MqttClient private readonly string _connectionString = "server=139.224.232.211;port=3308;database=fgassembly;user=root;password=doantech123;"; + //上传宜搭信息 + static string tokenAppKey = "dingcr4tknewvoasaz2a"; + static string tokenAppSecret = "EX-7YQjw14pI1bVNA5UyoQT4JcRNfwLFxKQ5n9rt6Kq8GCfsSdaONzYwHYvvIiXF"; + + static string appType = "APP_GLOIXDZ2INP8IW70XB8Y"; + static string systemToken = "R7E66JC1W1QZ2HVOCJUX99KHDNP92XDRRUPGMDI1"; + static string userId = "011117285547650088"; + static string formUuid = "FORM-EA85012D92D444BE92BB1017FF4ACAF8ISRW"; + + public event Action MessageReceived; // 通知UI层 public MqttClientService() @@ -271,7 +295,140 @@ namespace MqttClient - //上传宜搭代码 + //上传宜搭相关代码 + + /// + /// 创建 OAuth2 客户端(获取 token 用) + /// + /// + public static AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client CreateClient() + { + AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config(); + config.Protocol = "https"; + config.RegionId = "central"; + return new AlibabaCloud.SDK.Dingtalkoauth2_1_0.Client(config); + } + + /// + /// 获取钉钉 AccessToken + /// + /// AccessToken 字符串 + public static string GetDingDingToken() + { + string strToken = string.Empty; + GetAccessTokenResponse token = null; + + var client = CreateClient(); + var getAccessTokenRequest = new GetAccessTokenRequest + { + AppKey = tokenAppKey, + AppSecret = tokenAppSecret, + }; + + try + { + token = client.GetAccessToken(getAccessTokenRequest); + } + catch (TeaException err) + { + Console.WriteLine($"获取 token 异常:{err.Code} --- {err.Message}"); + } + catch (Exception ex) + { + Console.WriteLine($"获取 token 异常:{ex.Message}"); + } + + return token != null ? token.Body.AccessToken : string.Empty; + } + + /// + /// 创建宜搭客户端 + /// + /// + public static AlibabaCloud.SDK.Dingtalkyida_1_0.Client CreateYidaClient() + { + Config config = new Config(); + config.Protocol = "https"; + config.RegionId = "central"; + return new AlibabaCloud.SDK.Dingtalkyida_1_0.Client(config); + } + + + /// + /// 上传测试数据到宜搭 + /// + /// + /// + public static bool UploadTestDataToYida(string accessToken) + { + var client = CreateYidaClient(); + + // 先写死 24 个字段数据,对应宜搭唯一标识 + var dataDict = new Dictionary + { + { "textField_pbadehs", "测试值_supplier_code" }, + { "textField_1xi9sfn", "测试值_supplier_name" }, + { "textField_59qd85u", "测试值_vehicle_model" }, + { "textField_bvm0r0n", "测试值_part_number" }, + { "textField_36ov0ev", "测试值_part_name" }, + { "textField_2az30vq", "测试值_configuration" }, + { "textField_rqco1qe", "测试值_working_station" }, + { "textField_u0til0r", "测试值_parameter_name" }, + { "textField_grwfv60", "测试值_parameter_value" }, + { "textField_8jzbj2n", "测试值_tolerance_lower" }, + { "textField_m4zc2at", "测试值_tolerance_upper" }, + { "textField_7van6hm", "测试值_is_qualification" }, + { "dateField_h0zk520", DateTime.Now.ToString("yyyy-MM-dd") }, + { "textField_wyvrvdh", "测试值_leader_part" }, + { "textField_7uday88", "测试值_value" }, + { "textField_sodkifq", "测试值_lowlimit" }, + { "textField_flmgqq8", "测试值_uplimit" }, + { "textField_x18839h", "测试值_intime" }, + { "textField_nvdzppr", "测试值_out_of_tolerance_cause" }, + { "textField_dea190j", "测试值_reproduction_measure" }, + { "textField_ftm1wp0", "测试值_reformation_picture" }, + { "textField_ukkzy00", "测试值_leader" }, + { "textField_w5b9nts", "测试值_abnormality_type" }, + { "textField_buwiwu2", "测试值_leader_out_protection" } + + }; + + // 转成 JSON + string jsonData = JsonConvert.SerializeObject(dataDict); + + // 构建请求 + var headers = new SaveFormDataHeaders { XAcsDingtalkAccessToken = accessToken }; + var request = new SaveFormDataRequest + { + AppType = appType, + SystemToken = systemToken, + UserId = userId, + Language = "zh_CN", + FormUuid = formUuid, + FormDataJson = jsonData + }; + + try + { + var response = client.SaveFormDataWithOptions(request, headers, new AlibabaCloud.TeaUtil.Models.RuntimeOptions()); + if (response != null) + { + Console.WriteLine("✅ 上传成功,实例ID:" + response.Body.Result); + return true; + } + } + catch (TeaException err) + { + Console.WriteLine($"上传异常:{err.Code} --- {err.Message}"); + } + catch (Exception ex) + { + Console.WriteLine($"上传异常:{ex.Message}"); + } + + return false; + } +