锐腾搅拌上料功能
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.8 KiB

5 months ago
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using ICSSoft.Frame.Data.Entity;
  6. using ICSSoft.Base.Config.AppConfig;
  7. using ICSSoft.Base.Config.DBHelper;
  8. using System.Data;
  9. using System.Net;
  10. using System.IO;
  11. using Newtonsoft.Json;
  12. namespace ICSSoft.Frame.Data.DAL
  13. {
  14. public class ICSKodDbApiDAL
  15. {
  16. public static DataTable GetFileDirectory(string conn, string WorkPoint, string id, string name, string parentName)
  17. {
  18. //获取api地址
  19. string sql = "SELECT DBIpAddress,DBName FROM dbo.Sys_DataBase WHERE DBSourceName='KodDbApi' AND WorkCode='" + WorkPoint + "'";
  20. DataTable dtApiAddress = DBHelper.ExecuteDataset(conn, CommandType.Text, sql).Tables[0];
  21. if (dtApiAddress.Rows.Count == 0)
  22. {
  23. throw new Exception("获取可道云自建api地址失败,KodDbApi未维护!");
  24. }
  25. string host = dtApiAddress.Rows[0][0].ToString().Replace("http:", "").Trim(new char[] { ',', '/', '\\' });
  26. string siteName = dtApiAddress.Rows[0][1].ToString().Trim(new char[] { ',', '/', '\\' });
  27. string url = "http://" + host + "/" + siteName + "/api/FileDirectory";
  28. //StringBuilder inputData = new StringBuilder();
  29. //inputData.Append("{");
  30. //inputData.AppendFormat("\"baseSourceid\":\"{0}\",", id);
  31. //inputData.AppendFormat("\"name\":\"{0}\",", name);
  32. //inputData.AppendFormat("\"parentName\":\"{0}\"", parentName);
  33. //inputData.Append("}");
  34. Input4FileDirectory inputData = new Input4FileDirectory();
  35. inputData.baseSourceid = id;
  36. inputData.name = name;
  37. inputData.parentName = parentName;
  38. string @in = JsonConvert.SerializeObject(inputData);
  39. string resultData = HttpPost(url, @in);
  40. Result result = JsonConvert.DeserializeObject<Result>(resultData);
  41. if (result.data == null)
  42. {
  43. throw new Exception("未找到此物料对应的资料" + name);
  44. }
  45. DataTable dt = JsonConvert.DeserializeObject<DataTable>(result.data);
  46. return dt;
  47. }
  48. /// <summary>
  49. /// 接口调用方法
  50. /// </summary>
  51. /// <param name="url"></param>
  52. /// <param name="body"></param>
  53. /// <returns></returns>
  54. private static string HttpPost(string url, string body)
  55. {
  56. try
  57. {
  58. Encoding encoding = Encoding.UTF8;
  59. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  60. request.Method = "POST";
  61. request.Accept = "application/json, text/javascript, */*"; //"text/html, application/xhtml+xml, */*";
  62. request.ContentType = "application/json; charset=utf-8";
  63. byte[] buffer = encoding.GetBytes(body);
  64. request.ContentLength = buffer.Length;
  65. request.GetRequestStream().Write(buffer, 0, buffer.Length);
  66. HttpWebResponse response = (HttpWebResponse)request.GetResponse();
  67. using (StreamReader reader = new StreamReader(response.GetResponseStream(), encoding))
  68. {
  69. return reader.ReadToEnd();
  70. }
  71. }
  72. catch (WebException ex)
  73. {
  74. if (ex.Response != null)
  75. {
  76. var res = (HttpWebResponse)ex.Response;
  77. StringBuilder sb = new StringBuilder();
  78. using (StreamReader sr = new StreamReader(res.GetResponseStream(), Encoding.UTF8))
  79. {
  80. sb.Append(sr.ReadToEnd());
  81. }
  82. throw new Exception(sb.ToString());
  83. }
  84. throw new Exception(ex.Message);
  85. }
  86. }
  87. }
  88. }