e9d15629bb025a4f3c6e66b4ef247864491c1583.svn-base 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. package util
  2. import (
  3. "bytes"
  4. "encoding/xml"
  5. "fmt"
  6. "github.com/pkg/errors"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. )
  11. //eas webservice
  12. var (
  13. LoginStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:log="http://login.webservice.bos.kingdee.com">
  14. <soapenv:Header/>
  15. <soapenv:Body>
  16. <log:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  17. <userName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
  18. <password xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">password?</password>
  19. <slnName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
  20. <dcName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
  21. <language xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
  22. <dbType xsi:type="xsd:int">dbType?</dbType>
  23. </log:login>
  24. </soapenv:Body>
  25. </soapenv:Envelope>
  26. `
  27. LogoutStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:log="http://login.webservice.bos.kingdee.com">
  28. <soapenv:Header/>
  29. <soapenv:Body>
  30. <log:logout soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  31. <userName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
  32. <slnName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
  33. <dcName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
  34. <language xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
  35. </log:logout>
  36. </soapenv:Body>
  37. </soapenv:Envelope>
  38. `
  39. InsertPartsStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.synfacade.modernfarming.eas.kingdee.com">
  40. <soapenv:Header>
  41. <ns1:SessionId
  42. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  43. </ns1:SessionId>
  44. </soapenv:Header>
  45. <soapenv:Body>
  46. <web:synMaterial soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  47. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</request>
  48. </web:synMaterial>
  49. </soapenv:Body>
  50. </soapenv:Envelope>
  51. `
  52. QureyPartsStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.synfacade.modernfarming.eas.kingdee.com">
  53. <soapenv:Header>
  54. <ns1:SessionId
  55. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  56. </ns1:SessionId>
  57. </soapenv:Header>
  58. <soapenv:Body>
  59. <web:queryMaterialData soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  60. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{"number":"strJson?"}</request>
  61. </web:queryMaterialData>
  62. </soapenv:Body>
  63. </soapenv:Envelope>`
  64. InsertPartsuselaidStr = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="http://app.ws.scm.eas.kingdee.com">
  65. <soapenv:Header>
  66. <ns1:SessionId
  67. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  68. </ns1:SessionId>
  69. </soapenv:Header>
  70. <soapenv:Body>
  71. <app:importBill soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  72. <billType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">billType?</billType>
  73. <billDataJSON xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</billDataJSON>
  74. <billStatus xsi:type="xsd:int">2</billStatus>
  75. </app:importBill>
  76. </soapenv:Body>
  77. </soapenv:Envelope>
  78. `
  79. QureyBill = `<soapenv:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:app="http://app.ws.scm.eas.kingdee.com">
  80. <soapenv:Header>
  81. <ns1:SessionId
  82. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  83. </ns1:SessionId>
  84. </soapenv:Header>
  85. <soapenv:Body>
  86. <app:queryScmBillData soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  87. <billType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">billType?</billType>
  88. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{"number":"strJson?"}</request>
  89. </app:queryScmBillData>
  90. </soapenv:Body>
  91. </soapenv:Envelope>`
  92. )
  93. var user EASLoginInfo
  94. type EASLoginInfo struct {
  95. UserName string `json:"userName"`
  96. Password string `json:"password"`
  97. SlnName string `json:"slnName"`
  98. DcName string `json:"dcName"`
  99. Language string `json:"language"`
  100. DbType string `json:"dbType"`
  101. Url string `json:"dbType"`
  102. }
  103. type Response struct {
  104. Body rspBody `xml:"Body" json:"body"`
  105. }
  106. type rspBody struct {
  107. Record []rqRecord `xml:"multiRef" json:"multiRef"`
  108. SynMaterialResponse SMReturn `xml:"synMaterialResponse" json:"SynMaterialResponse"`
  109. ImportBillResponse ImportBillResponse `xml:"importBillResponse" json:"importBillResponse"`
  110. QueryMaterialDataResponse QureyParts `xml:"queryMaterialDataResponse" json:"queryMaterialDataResponse"`
  111. QueryScmBillDataResponse QureyBills `xml:"queryScmBillDataResponse" json:"queryScmBillDataResponse"`
  112. }
  113. type ImportBillResponse struct {
  114. ImportBillReturn string `xml:"importBillReturn" json:"importBillReturn"`
  115. }
  116. type QureyParts struct {
  117. QueryMaterialDataReturn string `xml:"queryMaterialDataReturn" json:"queryMaterialDataReturn"`
  118. }
  119. type QureyBills struct {
  120. QueryScmBillDataReturn string `xml:"queryScmBillDataReturn" json:"queryScmBillDataReturn"`
  121. }
  122. type SMReturn struct {
  123. SynMaterialReturn string `xml:"synMaterialReturn" json:"synMaterialReturn"`
  124. }
  125. type rqRecord struct {
  126. SessionId string `xml:"sessionId" json:"sessionId"`
  127. }
  128. func InitWebS(use, pass, url string) {
  129. user.Password = pass
  130. user.UserName = use
  131. user.DbType = "1"
  132. user.DcName = "MASM"
  133. user.Language = "L2"
  134. user.SlnName = "eas"
  135. user.Url = url
  136. }
  137. func eastest() {
  138. sessionid, _ := EASWebServiceLogin()
  139. //EASWebServicInsertParts(sessionid,"{}")
  140. EASWebServicInsertPartsuselaid(sessionid, "{}", "007")
  141. //EASWebServiceLogout()
  142. }
  143. func EASWebServiceLogin() (string, error) {
  144. client := &http.Client{}
  145. resstr := ReplaceStrWebSevice("login", user.UserName, user.Password, user.SlnName, user.DcName, user.Language, user.DbType, "", "", "")
  146. body := bytes.NewBuffer([]byte(resstr))
  147. req, err := http.NewRequest("POST",
  148. user.Url+"/ormrpc/services/EASLogin?wsdl",
  149. body,
  150. )
  151. req.Header.Set("Content-Type", "text/xml")
  152. req.Header.Set("SOAPAction", "application/soap+xml")
  153. if err != nil {
  154. return "", err
  155. }
  156. resp, err := client.Do(req)
  157. if err != nil {
  158. fmt.Println(err)
  159. return "", err
  160. }
  161. defer resp.Body.Close()
  162. result_body, err := ioutil.ReadAll(resp.Body)
  163. if err != nil {
  164. fmt.Println(err)
  165. return "", err
  166. }
  167. v := Response{}
  168. err = xml.Unmarshal([]byte(string(result_body)), &v)
  169. if err != nil {
  170. fmt.Println(err.Error())
  171. }
  172. if len(v.Body.Record) > 0 {
  173. return v.Body.Record[0].SessionId, nil
  174. }
  175. return "", err
  176. }
  177. func EASWebServiceLogout() error {
  178. client := &http.Client{}
  179. resstr := ReplaceStrWebSevice("logout", user.UserName, "", user.SlnName, user.DcName, user.Language, "", "", "", "")
  180. body := bytes.NewBuffer([]byte(resstr))
  181. req, err := http.NewRequest("POST",
  182. user.Url+"/ormrpc/services/EASLogin?wsdl",
  183. body,
  184. )
  185. req.Header.Set("Content-Type", "text/xml")
  186. req.Header.Set("SOAPAction", "application/soap+xml")
  187. if err != nil {
  188. // handle error
  189. }
  190. resp, err := client.Do(req)
  191. if err != nil {
  192. return err
  193. }
  194. defer resp.Body.Close()
  195. _, err = ioutil.ReadAll(resp.Body)
  196. if err != nil {
  197. return err
  198. }
  199. return nil
  200. //fmt.Println(string(result_body))
  201. }
  202. func EASWebServiceQureyParts(SessionId, PartCode string) error {
  203. client := &http.Client{}
  204. resstr := ReplaceStrWebSevice("QureyPartsStr", "", "", "", "", "", "", SessionId, PartCode, "")
  205. body := bytes.NewBuffer([]byte(resstr))
  206. req, err := http.NewRequest("POST",
  207. user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
  208. body,
  209. )
  210. req.Header.Set("Content-Type", "text/xml")
  211. req.Header.Set("SOAPAction", "application/soap+xml")
  212. if err != nil {
  213. // handle error
  214. }
  215. resp, err := client.Do(req)
  216. if err != nil {
  217. return err
  218. }
  219. defer resp.Body.Close()
  220. result_body, err := ioutil.ReadAll(resp.Body)
  221. if err != nil {
  222. return err
  223. }
  224. v := Response{}
  225. err = xml.Unmarshal(result_body, &v)
  226. if err != nil {
  227. fmt.Println(err.Error())
  228. }
  229. resmap := make(map[string]interface{}, 0)
  230. err = json.Unmarshal([]byte(v.Body.QueryMaterialDataResponse.QueryMaterialDataReturn), &resmap)
  231. //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
  232. switch resmap["data"].(type) {
  233. case []interface{}:
  234. if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
  235. return errors.New("单据未查询到")
  236. }
  237. default:
  238. return errors.New("单据未查询到")
  239. }
  240. return nil
  241. }
  242. func EASWebServiceQureyBill(SessionId, BillCode,billType string) error {
  243. client := &http.Client{}
  244. resstr := ReplaceStrWebSevice("QureyBill", "", "", "", "", "", "", SessionId, BillCode, billType)
  245. body := bytes.NewBuffer([]byte(resstr))
  246. req, err := http.NewRequest("POST",
  247. user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
  248. body,
  249. )
  250. req.Header.Set("Content-Type", "text/xml")
  251. req.Header.Set("SOAPAction", "application/soap+xml")
  252. if err != nil {
  253. return err
  254. }
  255. resp, err := client.Do(req)
  256. if err != nil {
  257. return err
  258. }
  259. defer resp.Body.Close()
  260. result_body, err := ioutil.ReadAll(resp.Body)
  261. fmt.Println(string(result_body))
  262. if err != nil {
  263. fmt.Println(err)
  264. return err
  265. }
  266. v := Response{}
  267. err = xml.Unmarshal(result_body, &v)
  268. if err != nil {
  269. fmt.Println(err.Error())
  270. }
  271. resmap := make(map[string]interface{}, 0)
  272. err = json.Unmarshal([]byte(v.Body.QueryScmBillDataResponse.QueryScmBillDataReturn), &resmap)
  273. //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
  274. switch resmap["data"].(type) {
  275. case []interface{}:
  276. if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
  277. return errors.New("单据未查询到")
  278. }
  279. default:
  280. return errors.New("单据未查询到")
  281. }
  282. return nil
  283. }
  284. func EASWebServicInsertParts(SessionId string, parts Parts) error {
  285. client := &http.Client{}
  286. soapencJson, err := json.Marshal(parts)
  287. if err != nil {
  288. return err
  289. }
  290. println(string(soapencJson))
  291. resstr := ReplaceStrWebSevice("InsertParts", "", "", "", "", "", "", SessionId, string(soapencJson), "")
  292. body := bytes.NewBuffer([]byte(resstr))
  293. req, err := http.NewRequest("POST",
  294. user.Url+"/ormrpc/services/WSSynMasterDataFacade?wsdl",
  295. body,
  296. )
  297. req.Header.Set("Content-Type", "text/xml")
  298. req.Header.Set("SOAPAction", "application/soap+xml")
  299. if err != nil {
  300. // handle error
  301. }
  302. resp, err := client.Do(req)
  303. if err != nil {
  304. fmt.Println(err)
  305. return err
  306. }
  307. defer resp.Body.Close()
  308. result_body, err := ioutil.ReadAll(resp.Body)
  309. if err != nil {
  310. fmt.Println(err)
  311. return err
  312. }
  313. v := Response{}
  314. err = xml.Unmarshal(result_body, &v)
  315. if err != nil {
  316. fmt.Println(err.Error())
  317. }
  318. resmap := make(map[string]string, 0)
  319. err = json.Unmarshal([]byte(v.Body.SynMaterialResponse.SynMaterialReturn), &resmap)
  320. if resmap["success"] == "0" {
  321. fmt.Println("resmap", resmap["message"])
  322. return errors.New(resmap["message"])
  323. }
  324. fmt.Println("resmap success", resmap["message"], resmap["success"])
  325. return err
  326. }
  327. func EASWebServicInsertPartsuselaid(SessionId, strJson, billType string) error { //billType采购入库单:006 销售出库单:007 其他入库单:008 其他出库单:009 领料出库单:100 生产入库单:101
  328. client := &http.Client{}
  329. resstr := ReplaceStrWebSevice("InsertPartsuselaidStr", "", "", "", "", "", "", SessionId, strJson, billType)
  330. body := bytes.NewBuffer([]byte(resstr))
  331. req, err := http.NewRequest("POST",
  332. user.Url+"/ormrpc/services/WSSCMWebServiceFacade?wsdl",
  333. body,
  334. )
  335. req.Header.Set("Content-Type", "text/xml")
  336. req.Header.Set("SOAPAction", "application/soap+xml")
  337. if err != nil {
  338. // handle error
  339. }
  340. resp, err := client.Do(req)
  341. if err != nil {
  342. fmt.Println(err)
  343. return err
  344. }
  345. defer resp.Body.Close()
  346. result_body, err := ioutil.ReadAll(resp.Body)
  347. if err != nil {
  348. fmt.Println(err)
  349. return err
  350. }
  351. v := Response{}
  352. err = xml.Unmarshal(result_body, &v)
  353. if err != nil {
  354. fmt.Println(err.Error())
  355. }
  356. resmap := make(map[string]interface{}, 0)
  357. if v.Body.ImportBillResponse.ImportBillReturn != "" {
  358. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
  359. fmt.Println(v.Body.ImportBillResponse.ImportBillReturn)
  360. //if len(resmap["billErrors"].([]interface{})) > 0 || resmap["status"] != "1" {
  361. //if resmap["status"] != "1" {
  362. // return errors.New(v.Body.ImportBillResponse.ImportBillReturn)
  363. //}
  364. }
  365. return nil
  366. }
  367. func ReplaceStrWebSevice(typeIn, userName, password, slnName, dcName, language, dbType, SessionId, strJson, billType string) string {
  368. res := ""
  369. switch typeIn {
  370. case "login":
  371. res = LoginStr
  372. res = strings.ReplaceAll(res, "userName?", userName)
  373. res = strings.ReplaceAll(res, "password?", password)
  374. res = strings.ReplaceAll(res, "slnName?", slnName)
  375. res = strings.ReplaceAll(res, "dcName?", dcName)
  376. res = strings.ReplaceAll(res, "language?", language)
  377. res = strings.ReplaceAll(res, "dbType?", dbType)
  378. case "logout":
  379. res = LogoutStr
  380. res = strings.ReplaceAll(res, "userName?", userName)
  381. res = strings.ReplaceAll(res, "slnName?", slnName)
  382. res = strings.ReplaceAll(res, "dcName?", dcName)
  383. res = strings.ReplaceAll(res, "language?", language)
  384. case "InsertParts":
  385. res = InsertPartsStr
  386. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  387. res = strings.ReplaceAll(res, "strJson?", strJson)
  388. case "QureyPartsStr":
  389. res = QureyPartsStr
  390. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  391. res = strings.ReplaceAll(res, "strJson?", strJson)
  392. case "InsertPartsuselaidStr":
  393. res = InsertPartsuselaidStr
  394. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  395. res = strings.ReplaceAll(res, "strJson?", strJson)
  396. res = strings.ReplaceAll(res, "billType?", billType)
  397. case "QureyBill":
  398. res = QureyBill
  399. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  400. res = strings.ReplaceAll(res, "strJson?", strJson)
  401. res = strings.ReplaceAll(res, "billType?", billType)
  402. }
  403. return res
  404. }