NeximApiUtil.cs
7.4 KB
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
using Model;
using Newtonsoft.Json;
using RestSharp;
using System;
using System.Collections.Generic;
using System.Configuration;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.ListView;
using Http = Model.Http;
namespace BLL
{
public class NeximApiUtil
{
public bool IsEnalbe()
{
//判断 Nexim_AuthUrl 是否为空
if (string.IsNullOrEmpty(Config.Nexim_AuthUrl))
{
return false;
}
return true;
}
private string OK = "ok";
private string ERROE = "error";
/**
* 获取登录的token
* @return
*/
private string getAccessToken(out string errMsg)
{
errMsg = "";
Dictionary<string, object> paramMap = new Dictionary<string, object>();
paramMap.Add("userName", Config.Nexim_UserName.Val);
paramMap.Add("password", Config.Nexim_Password.Val);
//string paramStr = JsonConvert.ToString(paramMap);
//LogNet.log.Info("获取Fuji的token参数为:" + paramStr);
string apiUrl = Config.Nexim_AuthUrl;
string accessToken = "";
string result = "";
try
{
string body=JsonConvert.SerializeObject (paramMap);
result = Model.Http.PostWithHeader(apiUrl, body, new Dictionary<string, string>());
LogNet.log.Info("获取Fuji的token结果为:" + result);
Dictionary<string, object> resultObj = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
if (resultObj != null && resultObj.ContainsKey("accessToken"))
{
accessToken = resultObj["accessToken"].ToString();
}
else
{
errMsg = accessToken;
LogNet.log.Info("获取Fuji的token失败,原因:" + errMsg);
}
}
catch (Exception e)
{
errMsg = e.ToString();
LogNet.log.Info("获取Fuji的token异常:", e);
accessToken = "";
}
string status = "";
if (String.IsNullOrEmpty(accessToken))
{
errMsg = "accessToken is null";
status = ERROE;
}
return accessToken;
}
private string getReelInfo(Dictionary<string, string> reelData, string key)
{
string dataKey = key;
Dictionary<string, string> paramKeyM = GetParamMap();
if (paramKeyM.ContainsKey(key))
{
dataKey = paramKeyM[key];
}
if (reelData.ContainsKey(dataKey))
{
return reelData[dataKey];
}
return "";
}
private string RI = "RI";
private string PN = "PN";
private string QTY = "QTY";
private string SP = "SP";
private string BATCH = "BATCH";
private string PRODATE = "PRODATE";
private Dictionary<string, string> _paramKeyMap = null;
private Dictionary<string, string> GetParamMap()
{
if (_paramKeyMap == null)
{
_paramKeyMap = new Dictionary<string, string>();
string[] configMap = Config.Nexim_ParamKeyMap; ;
foreach (string key in configMap)
{
string[] arrys = key.Trim().Split('=');
if (arrys.Length == 2 && arrys[0] != "" && arrys[1] != "")
{
_paramKeyMap.Add(arrys[0], arrys[1]);
}
}
}
return _paramKeyMap;
}
public bool registerNewDid(Dictionary<string, string> reelData, out string msg)
{
msg = "";
if (!IsEnalbe())
{
return true;
}
Dictionary<string, string> headerMap = new Dictionary<string, string>();
string token = getAccessToken(out string errmsg);
if (String.IsNullOrEmpty(token))
{
//获取token为空
msg = "getAccessToken fail:" + errmsg;
LogNet.log.Info("getAccessToken fail");
return false;
}
headerMap.Add("fujiAccessToken", token);
Dictionary<string, Object> paramsMap = new Dictionary<string, object>();
paramsMap.Add("did", getReelInfo(reelData, RI));
paramsMap.Add("partBarcode", getReelInfo(reelData, PN));
paramsMap.Add("quantity", getReelInfo(reelData, QTY));
paramsMap.Add("packageType", "paper"); //默认是paper,
paramsMap.Add("partsoutWarning", getReelInfo(reelData, QTY));
paramsMap.Add("splicingWarning", getReelInfo(reelData, QTY));
paramsMap.Add("partNumber", getReelInfo(reelData, PN));
paramsMap.Add("vendorName", getReelInfo(reelData, SP));
paramsMap.Add("lotName", getReelInfo(reelData, BATCH));
paramsMap.Add("dateCode", getReelInfo(reelData, PRODATE));
paramsMap.Add("location", "");
paramsMap.Add("memo", "");
paramsMap.Add("note1", "");
paramsMap.Add("note2", "");
paramsMap.Add("note3", "");
paramsMap.Add("note4", "");
paramsMap.Add("useSplicing", true);
paramsMap.Add("useTrayPackage", true);
paramsMap.Add("trayStackCount", 1);
paramsMap.Add("trayPickupPositionX", 1);
paramsMap.Add("trayPickupPositionY", 1);
paramsMap.Add("traySizeX", 0);
paramsMap.Add("traySizeY", 0);
List<Dictionary<string, object>> paramList=new List<Dictionary<string, object>>();
paramList.Add(paramsMap);
//string paramStr = JSON.toJSONstring(params);
//LogNet.log.Info("注册Fuji的did参数为:" + paramStr);
string result = "";
string apiUrl = Config.Nexim_RegisterDidInfoUrl;
try
{
string body=JsonConvert.SerializeObject (paramList);
result = Http.PostWithHeader(apiUrl, body, headerMap);
LogNet.log.Info("注册Fuji的did结果为:" + result);
Dictionary<string, object> resultObj = JsonConvert.DeserializeObject<Dictionary<string, object>>(result);
if (resultObj != null && resultObj.ContainsKey("addedCount"))
{
int addedCount = Convert.ToInt32(resultObj["addedCount"]);
if (addedCount > 0)
{
return true;
}
}
if (resultObj != null && resultObj.ContainsKey("details"))
{
string details = resultObj["details"].ToString();
msg ="Register new did failed: "+ details;
}
else
{
msg = "Register new did failed, result is null or not contains details ";
}
}
catch (Exception e)
{
//改为英文提示
msg = "Register new did failed, exception message:" + e.ToString();
LogNet.log.Info("注册Fuji的did失败:", e);
}
return false;
}
}
}