kyy
2025-10-23 caab9910ce800cf879b3b99a73399dcf320c8e58
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Web;
 
namespace FileList
{
    public partial class Form1 : Form
    {
        public Form1(string[] data)
        {
            InitializeComponent();
            _ = CreateDynamicLinks(data); // Òì²½³õʼ»¯
        }
 
        private async Task CreateDynamicLinks(string[] data)
        {
            // ²ÎÊýУÑé
            if (data == null || data.Length < 4)
            {
                MessageBox.Show("²ÎÊý´íÎó£ºÐèÒªÖÁÉÙ4¸ö²ÎÊý" + data[0] + data[1] + data[2] + data[3]);
                Close();
                return;
            }
 
            // ÉèÖô°¿Ú±êÌâ
            var type = data[2];
            this.Text = type == "1" ? "ͼֽ¹æ¸ñÊé" : "½øÁϼìÑéÎļþ";
 
            try
            {
                // »ñÈ¡ÎļþÁÐ±í£¨POSTÇëÇó£©
                var items = await GetDrawingItems(data[3]);
 
                if (items == null || items.Count == 0)
                {
                    MessageBox.Show("δÕÒµ½Ïà¹ØÎļþ");
                    return;
                }
 
                // ¶¯Ì¬´´½¨Á´½Ó
                CreateLinkLabels(items);
            }
            catch (Exception ex)
            {
                MessageBox.Show($"³õʼ»¯Ê§°Ü£º{ex.Message}");
                Close();
            }
        }
 
        private void CreateLinkLabels(List<DrawingItem> items)
        {
            const int startTop = 20;
            var currentTop = startTop;
 
            foreach (var item in items)
            {
                if (string.IsNullOrEmpty(item.FileId)) // ²¹È«À¨ºÅ
                {
                    continue;
                }
 
                var linkLabel = new LinkLabel
                {
                    Text = item.FileName ?? "δÃüÃûÎļþ",
                    Location = new Point(20, currentTop),
                    AutoSize = true,
                    Tag = new { FileId = item.FileId, FileName = item.FileName }, // ´æ´¢ÔªÊý¾Ý
                    Margin = new Padding(0, 0, 0, 15)
                };
 
                linkLabel.LinkClicked += OnLinkClicked;
                this.Controls.Add(linkLabel);
                currentTop += linkLabel.Height + 10;
            }
 
            this.Height = currentTop + 50;
        }
 
        // Ð޸ĵã»÷ʼþ´¦ÀíÂß¼­
        private async void OnLinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var linkLabel = (LinkLabel)sender;
            dynamic tagData = linkLabel.Tag;
            string fileId = tagData.FileId;
            string fileName = tagData.FileName;
 
            try
            {
                // ÏÂÔØÎļþ²¢»ñÈ¡±¾µØÂ·¾¶
                var localFilePath = await OpenDrawingFile(fileId, fileName);
 
                if (!string.IsNullOrEmpty(localFilePath) && File.Exists(localFilePath))
                {
                    // Ê¹ÓÃϵͳĬÈϳÌÐò´ò¿ª£¨»òÖ¸¶¨ WPS Â·¾¶£©
                    Process.Start(new ProcessStartInfo
                    {
                        FileName = localFilePath,
                        UseShellExecute = true // ÒÀÀµÏµÍ³Îļþ¹ØÁª
                    });
 
                    // ¿ÉÑ¡£ºÔÚ³ÌÐòÍ˳öʱÇåÀíÁÙʱÎļþ
                    Application.ApplicationExit += (s, args) =>
                    {
                        if (File.Exists(localFilePath))
                        {
                            File.Delete(localFilePath);
                        }
                    };
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show($"´ò¿ªÎļþʧ°Ü£º{ex.Message}");
            }
        }
 
        #region API ½»»¥Âß¼­
        private static readonly HttpClient _httpClient = new HttpClient();
 
        // ½Ó¿Ú1£º»ñÈ¡ÎļþÁÐ±í£¨POST£©
        public async Task<List<DrawingItem>> GetDrawingItems(string u9No)
        {
            try
            {
                const string baseUrl = "http://192.168.1.22:10054/api/PLM/RetrieveDrawings";
                var itemNo = u9No;
 
                // ¹¹Ôì´ø²éѯ²ÎÊýµÄ URL
                var urlWithParams = $"{baseUrl}?ItemNo={itemNo}";
 
                // ·¢ËÍ POST ÇëÇó£¨ÇëÇóÌåΪ¿Õ£©
                var response = await _httpClient.PostAsync(urlWithParams, null);
                response.EnsureSuccessStatusCode();
 
                // ½âÎöÏìÓ¦
                var responseContent = await response.Content.ReadAsStringAsync();
                var result = JsonConvert.DeserializeObject<ApiResponse>(responseContent);
 
                return result?.Status == 0 ? result.Data : null;
            }
            catch (Exception ex)
            {
                MessageBox.Show($"ÇëÇóÎļþÁбíʧ°Ü£º{ex.Message}");
                return null;
            }
        }
 
        // ½Ó¿Ú2£º´ò¿ªÎļþ£¨POST£©²¢ÏÂÔØÎļþÁ÷
        public async Task<string> OpenDrawingFile(string fileId, string fileName)
        {
            try
            {
                const string baseUrl = "http://192.168.1.22:10054/api/PLM/OpenDrawings";
 
                // ¶Ô²ÎÊý½øÐРURL ±àÂë
                var encodedFileId = Uri.EscapeDataString(fileId);
                var encodedFileName = Uri.EscapeDataString(fileName);
 
                // ¹¹Ôì´ø²éѯ²ÎÊýµÄ URL
                var urlWithParams = $"{baseUrl}?fileId={encodedFileId}&fName={encodedFileName}";
 
                // ·¢ËÍ POST ÇëÇó
                var response = await _httpClient.PostAsync(urlWithParams, null);
                response.EnsureSuccessStatusCode();
 
                // Éú³É°²È«ÎļþÃû£¨Ìæ»»·Ç·¨×Ö·û£©
                var safeFileName = string.Join("_", fileName.Split(Path.GetInvalidFileNameChars()));
                var tempFilePath = Path.Combine(Path.GetTempPath(), safeFileName);
 
                // È·±£ÎļþÀ©Õ¹ÃûÕýÈ·£¨¸ù¾Ýʵ¼ÊÇé¿öµ÷Õû£©
                if (!Path.HasExtension(tempFilePath))
                {
                    tempFilePath += ".pdf"; // ¼ÙÉèĬÈÏÊÇ PDF Îļþ
                }
 
                // Ð´ÈëÎļþÁ÷
                using (var fileStream = File.Create(tempFilePath))
                {
                    var contentStream = await response.Content.ReadAsStreamAsync();
                    await contentStream.CopyToAsync(fileStream);
                }
 
                return tempFilePath; // ·µ»Ø±¾µØÎļþ·¾¶
            }
            catch (Exception ex)
            {
                MessageBox.Show($"ÎļþÏÂÔØÊ§°Ü£º{ex.Message}");
                return null;
            }
        }
 
        #region Êý¾ÝÄ£ÐÍ
        public class ApiResponse
        {
            [JsonProperty("status")]
            public int Status { get; set; }
 
            [JsonProperty("message")]
            public string Message { get; set; }
 
            [JsonProperty("data")]
            public List<DrawingItem> Data { get; set; }
        }
 
        public class DrawingItem
        {
            [JsonProperty("fFileId")]
            public string FileId { get; set; }
 
            [JsonProperty("fName")]
            public string FileName { get; set; }
        }
 
        public class OpenDrawingResponse
        {
            [JsonProperty("url")]
            public string FileUrl { get; set; }
        }
        #endregion
        #endregion
    }
}