4PX API 对接
由于工作关系,需要和4PX做API对接。4PX是做货运的公司,据说新加坡小包业务不错。
API生产环境接口:
http://api.4px.com/OrderOnlineToolService.dll?wsdl
http://api.4px.com/OrderOnlineService.dll?wsdl
具体每个service做什么功能, 只要在项目里添加服务引用后自然看到。
下面只想说一下过程中遇到的问题。
首先,测试上传订单的时候,API报错:请联系递四方技术人员。这个确实让我摸不着头脑。我是根据4PX提供的java代码稍微改了一点测试的。
后来电话联系对方的技术员, 对方很客气的问我要测试代码,并很快告诉我是由于下面代码造成错误:
1 /** 2 * 形式发票信息 3 */ 4 declareInvoice[] aobjDeclareInvoice = new declareInvoice[2]; 5 /** 6 * 第一个申报信息 7 */ 8 declareInvoice objDeclareInvoice1 = new declareInvoice(); 9 ...... 10 ...... 11 ...... 12 aobjDeclareInvoice[0] = objDeclareInvoice;
由于我的数组声明了长度是2,实际只给第一个赋值了,所以API报错了。
这是我始料不及的。我确实是删掉了第二个元素,并忘记改掉数组声明,但是这个报错还是挺搞笑的。不管怎么样,还是怪我没注意到这个细节。
如果sample代码是我写的, 我不会用数组,而是用List。如果API是我写的, 我不会在API里面按声明长度循环数组而不加判断数组元素是否是null。
第二个遇到的问题是,我们需要自动打印标签。在OrderOnlineToolService里有个接口就叫printLabelService,高兴了一下。测试了一下发现不对了, API报错: 本接口尚未实现。
马上又打电话(话说,他们的技术员服务是相当不错的),他马上告知本接口确实没有实现,何时实现还没有时间表。但是他们提供标签打印页面,可以手动打印,即把网页打印出来。
那么我就要自己根据订单信息和他们网页上的标签格式生成标签页面,然后送到打印机了。
这时遇到的比较麻烦的问题是标签上的条码图片如何生成。因为不知道他们的条码标准,试了一会,发现是128A/B,C不行,EAN也不行。
从园子了找到一个生成条码图片的类,然后转成base64字符串就可以了。
barcode 代码(转自 荒 木 http://www.cnblogs.com/rophie/archive/2012/03/02/2376946.html )
1 public class BarCode128 2 { 3 private DataTable m_Code128 = new DataTable(); 4 private uint m_Height = 40; 5 /// <summary> 6 /// 高度 7 /// </summary> 8 public uint Height { get { return m_Height; } set { m_Height = value; } } 9 private Font m_ValueFont = null; 10 /// <summary> 11 /// 是否显示可见号码 如果为NULL不显示号码 12 /// </summary> 13 public Font ValueFont { get { return m_ValueFont; } set { m_ValueFont = value; } } 14 private byte m_Magnify = 0; 15 /// <summary> 16 /// 放大倍数 17 /// </summary> 18 public byte Magnify { get { return m_Magnify; } set { m_Magnify = value; } } 19 /// <summary> 20 /// 条码类别 21 /// </summary> 22 public enum Encode 23 { 24 Code128A, 25 Code128B, 26 Code128C, 27 EAN128 28 } 29 30 private static BarCode128 instance = null; 31 public static BarCode128 Instance 32 { 33 get 34 { 35 if (instance == null) 36 { 37 instance = new BarCode128(); 38 } 39 return instance; 40 } 41 } 42 43 private BarCode128() 44 { 45 m_Code128.Columns.Add("ID"); 46 m_Code128.Columns.Add("Code128A"); 47 m_Code128.Columns.Add("Code128B"); 48 m_Code128.Columns.Add("Code128C"); 49 m_Code128.Columns.Add("BandCode"); 50 m_Code128.CaseSensitive = true; 51 #region 数据表 52 m_Code128.Rows.Add("0", " ", " ", "00", "212222"); 53 m_Code128.Rows.Add("1", "!", "!", "01", "222122"); 54 m_Code128.Rows.Add("2", "\"", "\"", "02", "222221"); 55 m_Code128.Rows.Add("3", "#", "#", "03", "121223"); 56 m_Code128.Rows.Add("4", "$", "$", "04", "121322"); 57 m_Code128.Rows.Add("5", "%", "%", "05", "131222"); 58 m_Code128.Rows.Add("6", "&", "&", "06", "122213"); 59 m_Code128.Rows.Add("7", "'", "'", "07", "122312"); 60 m_Code128.Rows.Add("8", "(", "(", "08", "132212"); 61 m_Code128.Rows.Add("9", ")", ")", "09", "221213"); 62 m_Code128.Rows.Add("10", "*", "*", "10", "221312"); 63 m_Code128.Rows.Add("11", "+", "+", "11", "231212"); 64 m_Code128.Rows.Add("12", ",", ",", "12", "112232"); 65 m_Code128.Rows.Add("13", "-", "-", "13", "122132"); 66 m_Code128.Rows.Add("14", ".", ".", "14", "122231"); 67 m_Code128.Rows.Add("15", "/", "/", "15", "113222"); 68 m_Code128.Rows.Add("16", "0", "0", "16", "123122"); 69 m_Code128.Rows.Add("17", "1", "1", "17", "123221"); 70 m_Code128.Rows.Add("18", "2", "2", "18", "223211"); 71 m_Code128.Rows.Add("19", "3", "3", "19", "221132"); 72 m_Code128.Rows.Add("20", "4", "4", "20", "221231"); 73 m_Code128.Rows.Add("21", "5", "5", "21", "213212"); 74 m_Code128.Rows.Add("22", "6", "6", "22", "223112"); 75 m_Code128.Rows.Add("23", "7", "7", "23", "312131"); 76 m_Code128.Rows.Add("24", "8", "8", "24", "311222"); 77 m_Code128.Rows.Add("25", "9", "9", "25", "321122"); 78 m_Code128.Rows.Add("26", ":", ":", "26", "321221"); 79 m_Code128.Rows.Add("27", ";", ";", "27", "312212"); 80 m_Code128.Rows.Add("28", "<", "<", "28", "322112"); 81 m_Code128.Rows.Add("29", "=", "=", "29", "322211"); 82 m_Code128.Rows.Add("30", ">", ">", "30", "212123"); 83 m_Code128.Rows.Add("31", "?", "?", "31", "212321"); 84 m_Code128.Rows.Add("32", "@", "@", "32", "232121"); 85 m_Code128.Rows.Add("33", "A", "A", "33", "111323"); 86 m_Code128.Rows.Add("34", "B", "B", "34", "131123"); 87 m_Code128.Rows.Add("35", "C", "C", "35", "131321"); 88 m_Code128.Rows.Add("36", "D", "D", "36", "112313"); 89 m_Code128.Rows.Add("37", "E", "E", "37", "132113"); 90 m_Code128.Rows.Add("38", "F", "F", "38", "132311"); 91 m_Code128.Rows.Add("39", "G", "G", "39", "211313"); 92 m_Code128.Rows.Add("40", "H", "H", "40", "231113"); 93 m_Code128.Rows.Add("41", "I", "I", "41", "231311"); 94 m_Code128.Rows.Add("42", "J", "J", "42", "112133"); 95 m_Code128.Rows.Add("43", "K", "K", "43", "112331"); 96 m_Code128.Rows.Add("44", "L", "L", "44", "132131"); 97 m_Code128.Rows.Add("45", "M", "M", "45", "113123"); 98 m_Code128.Rows.Add("46", "N", "N", "46", "113321"); 99 m_Code128.Rows.Add("47", "O", "O", "47", "133121"); 100 m_Code128.Rows.Add("48", "P", "P", "48", "313121"); 101 m_Code128.Rows.Add("49", "Q", "Q", "49", "211331"); 102 m_Code128.Rows.Add("50", "R", "R", "50", "231131"); 103 m_Code128.Rows.Add("51", "S", "S", "51", "213113"); 104 m_Code128.Rows.Add("52", "T", "T", "52", "213311"); 105 m_Code128.Rows.Add("53", "U", "U", "53", "213131"); 106 m_Code128.Rows.Add("54", "V", "V", "54", "311123"); 107 m_Code128.Rows.Add("55", "W", "W", "55", "311321"); 108 m_Code128.Rows.Add("56", "X", "X", "56", "331121"); 109 m_Code128.Rows.Add("57", "Y", "Y", "57", "312113"); 110 m_Code128.Rows.Add("58", "Z", "Z", "58", "312311"); 111 m_Code128.Rows.Add("59", "[", "[", "59", "332111"); 112 m_Code128.Rows.Add("60", "\\", "\\", "60", "314111"); 113 m_Code128.Rows.Add("61", "]", "]", "61", "221411"); 114 m_Code128.Rows.Add("62", "^", "^", "62", "431111"); 115 m_Code128.Rows.Add("63", "_", "_", "63", "111224"); 116 m_Code128.Rows.Add("64", "NUL", "`", "64", "111422"); 117 m_Code128.Rows.Add("65", "SOH", "a", "65", "121124"); 118 m_Code128.Rows.Add("66", "STX", "b", "66", "121421"); 119 m_Code128.Rows.Add("67", "ETX", "c", "67", "141122"); 120 m_Code128.Rows.Add("68", "EOT", "d", "68", "141221"); 121 m_Code128.Rows.Add("69", "ENQ", "e", "69", "112214"); 122 m_Code128.Rows.Add("70", "ACK", "f", "70", "112412"); 123 m_Code128.Rows.Add("71", "BEL", "g", "71", "122114"); 124 m_Code128.Rows.Add("72", "BS", "h", "72", "122411"); 125 m_Code128.Rows.Add("73", "HT", "i", "73", "142112"); 126 m_Code128.Rows.Add("74", "LF", "j", "74", "142211"); 127 m_Code128.Rows.Add("75", "VT", "k", "75", "241211"); 128 m_Code128.Rows.Add("76", "FF", "I", "76", "221114"); 129 m_Code128.Rows.Add("77", "CR", "m", "77", "413111"); 130 m_Code128.Rows.Add("78", "SO", "n", "78", "241112"); 131 m_Code128.Rows.Add("79", "SI", "o", "79", "134111"); 132 m_Code128.Rows.Add("80", "DLE", "p", "80", "111242"); 133 m_Code128.Rows.Add("81", "DC1", "q", "81", "121142"); 134 m_Code128.Rows.Add("82", "DC2", "r", "82", "121241"); 135 m_Code128.Rows.Add("83", "DC3", "s", "83", "114212"); 136 m_Code128.Rows.Add("84", "DC4", "t", "84", "124112"); 137 m_Code128.Rows.Add("85", "NAK", "u", "85", "124211"); 138 m_Code128.Rows.Add("86", "SYN", "v", "86", "411212"); 139 m_Code128.Rows.Add("87", "ETB", "w", "87", "421112"); 140 m_Code128.Rows.Add("88", "CAN", "x", "88", "421211"); 141 m_Code128.Rows.Add("89", "EM", "y", "89", "212141"); 142 m_Code128.Rows.Add("90", "SUB", "z", "90", "214121"); 143 m_Code128.Rows.Add("91", "ESC", "{", "91", "412121"); 144 m_Code128.Rows.Add("92", "FS", "|", "92", "111143"); 145 m_Code128.Rows.Add("93", "GS", "}", "93", "111341"); 146 m_Code128.Rows.Add("94", "RS", "~", "94", "131141"); 147 m_Code128.Rows.Add("95", "US", "DEL", "95", "114113"); 148 m_Code128.Rows.Add("96", "FNC3", "FNC3", "96", "114311"); 149 m_Code128.Rows.Add("97", "FNC2", "FNC2", "97", "411113"); 150 m_Code128.Rows.Add("98", "SHIFT", "SHIFT", "98", "411311"); 151 m_Code128.Rows.Add("99", "CODEC", "CODEC", "99", "113141"); 152 m_Code128.Rows.Add("100", "CODEB", "FNC4", "CODEB", "114131"); 153 m_Code128.Rows.Add("101", "FNC4", "CODEA", "CODEA", "311141"); 154 m_Code128.Rows.Add("102", "FNC1", "FNC1", "FNC1", "411131"); 155 m_Code128.Rows.Add("103", "StartA", "StartA", "StartA", "211412"); 156 m_Code128.Rows.Add("104", "StartB", "StartB", "StartB", "211214"); 157 m_Code128.Rows.Add("105", "StartC", "StartC", "StartC", "211232"); 158 m_Code128.Rows.Add("106", "Stop", "Stop", "Stop", "2331112"); 159 #endregion 160 } 161 /// <summary> 162 /// 获取128图形 163 /// </summary> 164 /// <param name="p_Text">文字</param> 165 /// <param name="p_Code">编码</param> 166 /// <returns>图形</returns> 167 public Bitmap GetCodeImage(string p_Text, Encode p_Code) 168 { 169 string _ViewText = p_Text; 170 string _Text = ""; 171 IList<int> _TextNumb = new List<int>(); 172 int _Examine = 0; //首位 173 switch (p_Code) 174 { 175 case Encode.Code128C: 176 _Examine = 105; 177 if (!((p_Text.Length & 1) == 0)) throw new Exception("128C长度必须是偶数"); 178 while (p_Text.Length != 0) 179 { 180 int _Temp = 0; 181 try 182 { 183 int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2)); 184 } 185 catch 186 { 187 throw new Exception("128C必须是数字!"); 188 } 189 _Text += GetValue(p_Code, p_Text.Substring(0, 2), ref _Temp); 190 _TextNumb.Add(_Temp); 191 p_Text = p_Text.Remove(0, 2); 192 } 193 break; 194 case Encode.EAN128: 195 _Examine = 105; 196 if (!((p_Text.Length & 1) == 0)) throw new Exception("EAN128长度必须是偶数"); 197 _TextNumb.Add(102); 198 _Text += "411131"; 199 while (p_Text.Length != 0) 200 { 201 int _Temp = 0; 202 try 203 { 204 int _CodeNumb128 = Int32.Parse(p_Text.Substring(0, 2)); 205 } 206 catch 207 { 208 throw new Exception("128C必须是数字!"); 209 } 210 _Text += GetValue(Encode.Code128C, p_Text.Substring(0, 2), ref _Temp); 211 _TextNumb.Add(_Temp); 212 p_Text = p_Text.Remove(0, 2); 213 } 214 break; 215 default: 216 if (p_Code == Encode.Code128A) 217 { 218 _Examine = 103; 219 } 220 else 221 { 222 _Examine = 104; 223 } 224 225 while (p_Text.Length != 0) 226 { 227 int _Temp = 0; 228 string _ValueCode = GetValue(p_Code, p_Text.Substring(0, 1), ref _Temp); 229 if (_ValueCode.Length == 0) throw new Exception("无效的字符集!" + p_Text.Substring(0, 1).ToString()); 230 _Text += _ValueCode; 231 _TextNumb.Add(_Temp); 232 p_Text = p_Text.Remove(0, 1); 233 } 234 break; 235 } 236 if (_TextNumb.Count == 0) throw new Exception("错误的编码,无数据"); 237 _Text = _Text.Insert(0, GetValue(_Examine)); //获取开始位 238 239 for (int i = 0; i != _TextNumb.Count; i++) 240 { 241 _Examine += _TextNumb[i] * (i + 1); 242 } 243 _Examine = _Examine % 103; //获得严效位 244 _Text += GetValue(_Examine); //获取严效位 245 _Text += "2331112"; //结束位 246 Bitmap _CodeImage = GetImage(_Text); 247 GetViewText(_CodeImage, _ViewText); 248 return _CodeImage; 249 } 250 /// <summary> 251 /// 获取目标对应的数据 252 /// </summary> 253 /// <param name="p_Code">编码</param> 254 /// <param name="p_Value">数值 A b 30</param> 255 /// <param name="p_SetID">返回编号</param> 256 /// <returns>编码</returns> 257 private string GetValue(Encode p_Code, string p_Value, ref int p_SetID) 258 { 259 if (m_Code128 == null) return ""; 260 DataRow[] _Row = m_Code128.Select(p_Code.ToString() + "='" + p_Value + "'"); 261 if (_Row.Length != 1) throw new Exception("错误的编码" + p_Value.ToString()); 262 p_SetID = Int32.Parse(_Row[0]["ID"].ToString()); 263 return _Row[0]["BandCode"].ToString(); 264 } 265 /// <summary> 266 /// 根据编号获得条纹 267 /// </summary> 268 /// <param name="p_CodeId"></param> 269 /// <returns></returns> 270 private string GetValue(int p_CodeId) 271 { 272 DataRow[] _Row = m_Code128.Select("ID='" + p_CodeId.ToString() + "'"); 273 if (_Row.Length != 1) throw new Exception("验效位的编码错误" + p_CodeId.ToString()); 274 return _Row[0]["BandCode"].ToString(); 275 } 276 /// <summary> 277 /// 获得条码图形 278 /// </summary> 279 /// <param name="p_Text">文字</param> 280 /// <returns>图形</returns> 281 private Bitmap GetImage(string p_Text) 282 { 283 char[] _Value = p_Text.ToCharArray(); 284 int _Width = 0; 285 for (int i = 0; i != _Value.Length; i++) 286 { 287 _Width += Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1); 288 } 289 Bitmap _CodeImage = new Bitmap(_Width, (int)m_Height); 290 Graphics _Garphics = Graphics.FromImage(_CodeImage); 291 //Pen _Pen; 292 int _LenEx = 0; 293 for (int i = 0; i != _Value.Length; i++) 294 { 295 int _ValueNumb = Int32.Parse(_Value[i].ToString()) * (m_Magnify + 1); //获取宽和放大系数 296 if (!((i & 1) == 0)) 297 { 298 //_Pen = new Pen(Brushes.White, _ValueNumb); 299 _Garphics.FillRectangle(Brushes.White, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height)); 300 } 301 else 302 { 303 //_Pen = new Pen(Brushes.Black, _ValueNumb); 304 _Garphics.FillRectangle(Brushes.Black, new Rectangle(_LenEx, 0, _ValueNumb, (int)m_Height)); 305 } 306 //_Garphics.(_Pen, new Point(_LenEx, 0), new Point(_LenEx, m_Height)); 307 _LenEx += _ValueNumb; 308 } 309 _Garphics.Dispose(); 310 return _CodeImage; 311 } 312 /// <summary> 313 /// 显示可见条码文字 如果小于40 不显示文字 314 /// </summary> 315 /// <param name="p_Bitmap">图形</param> 316 private void GetViewText(Bitmap p_Bitmap, string p_ViewText) 317 { 318 if (m_ValueFont == null) return; 319 320 Graphics _Graphics = Graphics.FromImage(p_Bitmap); 321 SizeF _DrawSize = _Graphics.MeasureString(p_ViewText, m_ValueFont); 322 if (_DrawSize.Height > p_Bitmap.Height - 10 || _DrawSize.Width > p_Bitmap.Width) 323 { 324 _Graphics.Dispose(); 325 return; 326 } 327 328 int _StarY = p_Bitmap.Height - (int)_DrawSize.Height; 329 _Graphics.FillRectangle(Brushes.White, new Rectangle(0, _StarY, p_Bitmap.Width, (int)_DrawSize.Height)); 330 _Graphics.DrawString(p_ViewText, m_ValueFont, Brushes.Black, 0, _StarY); 331 } 332 333 //12345678 334 //(105 + (1 * 12 + 2 * 34 + 3 * 56 + 4 *78)) % 103 = 47 335 //结果为starc +12 +34 +56 +78 +47 +end 336 }
private byte[] Image2Bytes(Image img) { MemoryStream ms = new MemoryStream(); byte[] imagedata = null; img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); imagedata = ms.GetBuffer(); return imagedata; }
第三个遇到的问题是,他们提供的打印标签的网页带几个checkbox,可以选择打印或者不打印报关单和配货单。
我们必须打印报关单,那就必须在加载完页面之后勾选报关单checkbox,隐藏的报关单部分才能显示出来。
写了一个js,发现勾是勾上了,但是没有触发它的js事件,报关单部分还是隐藏的。
于是我想模拟鼠标事件,结果发现不好定位报关单checkbox的位置,而且我的webbrowser是不可见的。
最后发现了一个最简单的办法,直接invoke click方法就可以了。
浙公网安备 33010602011771号