ke_junjie
2025-06-04 101c57ec4c28bc3c36e49c50a926e9e7c0dd0247
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
using iWareSda_QQJF.WEBAPI.JSONEntity;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.IO;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
 
namespace iWareSda_QQJF.WEBAPI
{
    public class ReadErpSendToWMS
    {
        /// <summary>  
        /// 调用api返回json  
        /// </summary>  
        /// <param name="url">api地址</param>  
        /// <param name="jsonstr">接收参数</param>  
        /// <param name="type">类型</param>  
        /// <returns></returns>  
        public static string HttpApi(string url, string jsonstr, string type)
        {
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//webrequest请求api地址  
 
            //公有设置头文件
            SetHeaderValue(request.Headers, "Content-Type", "application/json");
            SetHeaderValue(request.Headers, "apiType", "client");
            SetHeaderValue(request.Headers, "appKey", "PT20180001");
            SetHeaderValue(request.Headers, "appSecret", "EC83335F-F695-41D5-8304-B5BFC351E5C0");
            TimeSpan ts = DateTime.Now - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string time = Convert.ToInt64(ts.TotalMilliseconds).ToString();
            //string time = DateTime.Now.Ticks.ToString();
            SetHeaderValue(request.Headers, "timestamp", time);
            Random rd = new Random();
            long n = (long)Math.Floor((new Random()).NextDouble() * 100000000000D);
            string num = n.ToString().PadLeft(11, '0');
            SetHeaderValue(request.Headers, "nonce", num);
            SetHeaderValue(request.Headers, "signature", GetSignature(time, num, jsonstr));
 
            request.Accept = "text/html,application/xhtml+xml,*/*";
            //request.Accept = "*/*";
            request.ContentType = "application/json";
            request.Method = type.ToUpper().ToString();//get或者post  
            byte[] buffer = encoding.GetBytes(jsonstr);
            request.ContentLength = buffer.Length;
            request.GetRequestStream().Write(buffer, 0, buffer.Length);
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    return reader.ReadToEnd();
                }
            }
            catch (WebException ex)
            {
                HttpWebResponse resp = (HttpWebResponse)ex.Response;
 
                Stream responseStream = resp.GetResponseStream();
 
                StreamReader sr = new StreamReader(responseStream, Encoding.UTF8);
 
                string sReturn = sr.ReadToEnd().Trim();
 
                resp.Close();
 
                sr.Close();
 
                Console.WriteLine(sReturn);
            }
            return "";
        }
 
        /// <summary>
        /// 添加物料
        /// </summary>
        /// <param name="item"></param>
        public static GetJsonOfItem AdditemMethod(AddItem item)
        {
            string url = "http://10.103.11.236:7001/api/basicInfo/base/productInfo/add";
            string sendJson = JsonConvert.SerializeObject(item);
            string getInfo = HttpApi(url, sendJson, "post");
            GetJsonOfItem getmsg = JsonConvert.DeserializeObject<GetJsonOfItem>(getInfo);
            return getmsg;
        }
 
        /// <summary>
        /// 增加任务
        /// </summary>
        public static GetJsonOfItem AddTaskMethod(ERPTask task)
        {
            string url = "http://10.103.11.236:7001/api/outbound/orderPlan/add";
            string sendJson = JsonConvert.SerializeObject(task);
 
            string getInfo = HttpApi(url, sendJson, "post");
            GetJsonOfItem getmsg = JsonConvert.DeserializeObject<GetJsonOfItem>(getInfo);
            //if (getmsg.result != false)
            //{
            //    int a = 0;
            //}
            return getmsg;
 
        }
 
        /// <summary>
        /// 修改任务
        /// </summary>
        public static GetJsonOfItem UpdateTaskMethod(ERPTask task)
        {
            string url = "http://10.103.11.236:7001/api/outbound/orderPlan/update";
            string sendJson = JsonConvert.SerializeObject(task);
            string getInfo = HttpApi(url, sendJson, "post");
            GetJsonOfItem getmsg = JsonConvert.DeserializeObject<GetJsonOfItem>(getInfo);
            return getmsg;
        }
 
        /// <summary>
        /// 删除任务
        /// </summary>
        public static GetJsonOfItem DeleteTaskMethod(DeleteTask task)
        {
            string url = "http://10.103.11.236:7001/api/outbound/orderPlan/delete";
            string sendJson = JsonConvert.SerializeObject(task);    
            string getInfo = HttpApi(url, sendJson, "post");
            GetJsonOfItem getmsg = JsonConvert.DeserializeObject<GetJsonOfItem>(getInfo);
            return getmsg;
        }
        
        /// <summary>
        /// 加密
        /// </summary>
        /// <param name="timeStamp"></param>
        /// <param name="nonce"></param>
        /// <param name="paramjson"></param>
        /// <param name="data"></param>
        /// <returns></returns>
        public static string GetSignature(string timeStamp, string nonce,string data)
        {
 
            var hash = System.Security.Cryptography.MD5.Create();
            //拼接签名数据
            string appkey = "PT20180001";
            string appscret = "EC83335F-F695-41D5-8304-B5BFC351E5C0";
 
            //timeStamp = "1615911903352";
            //nonce = "25883581687";
            //data="{\"createDate\":\"2021/3/16 16:32:10\"}";
 
            data = data.Replace(".0,", ",");
            data = data.Replace(".0}", "}");
            var signStr = timeStamp + nonce + appkey + appscret + data;
            //将字符串中字符按升序排序
            var sortStr = string.Concat(signStr.OrderBy(c => c));
            var bytes = Encoding.UTF8.GetBytes(sortStr);
            //使用MD5加密
            var md5Val = hash.ComputeHash(bytes);
            //把二进制转化为大写的十六进制
            StringBuilder result = new StringBuilder();
            foreach (var c in md5Val)
            {
                result.Append(c.ToString("X2"));
            }
            
 
            return result.ToString().ToUpper();
        }
 
        /// <summary>
        /// 设置头属性公有方法
        /// </summary>
        /// <param name="header"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        public static void SetHeaderValue(WebHeaderCollection header, string name, string value)
        {
            var property = typeof(WebHeaderCollection).GetProperty("InnerCollection", BindingFlags.Instance | BindingFlags.NonPublic);
            if (property != null)
            {
                var collection = property.GetValue(header, null) as NameValueCollection;
                collection[name] = value;
            }
        }
    }
}