6dc003a66e2771d8145fff1f10d9fd3f0af76840.svn-base 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. type EASLoginInfo struct{
  12. UserName string `json:"userName"`
  13. Password string `json:"password"`
  14. SlnName string `json:"slnName"`
  15. DcName string `json:"dcName"`
  16. Language string `json:"language"`
  17. DbType string `json:"dbType"`
  18. Url string `json:"dbType"`
  19. }
  20. var(
  21. 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">
  22. <soapenv:Header/>
  23. <soapenv:Body>
  24. <log:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  25. <userName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
  26. <password xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">password?</password>
  27. <slnName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
  28. <dcName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
  29. <language xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
  30. <dbType xsi:type="xsd:int">dbType?</dbType>
  31. </log:login>
  32. </soapenv:Body>
  33. </soapenv:Envelope>
  34. `
  35. 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">
  36. <soapenv:Header/>
  37. <soapenv:Body>
  38. <log:logout soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  39. <userName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
  40. <slnName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
  41. <dcName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
  42. <language xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
  43. </log:logout>
  44. </soapenv:Body>
  45. </soapenv:Envelope>
  46. `
  47. 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">
  48. <soapenv:Header>
  49. <ns1:SessionId
  50. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  51. </ns1:SessionId>
  52. </soapenv:Header>
  53. <soapenv:Body>
  54. <web:synMaterial soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  55. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</request>
  56. </web:synMaterial>
  57. </soapenv:Body>
  58. </soapenv:Envelope>
  59. `
  60. 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">
  61. <soapenv:Header>
  62. <ns1:SessionId
  63. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  64. </ns1:SessionId>
  65. </soapenv:Header>
  66. <soapenv:Body>
  67. <app:importBill soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  68. <billType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">billType?</billType>
  69. <billDataJSON xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</billDataJSON>
  70. <billStatus xsi:type="xsd:int">1</billStatus>
  71. </app:importBill>
  72. </soapenv:Body>
  73. </soapenv:Envelope>
  74. `
  75. )
  76. var user EASLoginInfo
  77. // 备件
  78. type Parts struct{
  79. MaterialNumber string `json:"materialNumber"`
  80. MaterialName string `json:"materialName"`
  81. MaterialGroup string `json:"materialGroup"`
  82. Model string `json:"model"`
  83. BaseUnit string `json:"baseUnit"`
  84. Status int `json:"status"`
  85. //kaClass int32 `json:"kaClass"`
  86. //planningMode int32 `json:"planningMode"`
  87. }
  88. func eastest(){
  89. sessionid,_ := EASWebServiceLogin()
  90. //EASWebServicInsertParts(sessionid,"{}")
  91. EASWebServicInsertPartsuselaid(sessionid,"{}","007")
  92. //EASWebServiceLogout()
  93. }
  94. func InitWebS(use,pass,url string){
  95. user.Password = pass
  96. user.UserName = use
  97. user.DbType = "1"
  98. user.DcName = "MASM"
  99. user.Language = "L2"
  100. user.SlnName = "eas"
  101. user.Url = url
  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. }
  111. type ImportBillResponse struct {
  112. ImportBillReturn string `xml:"importBillReturn" json:"importBillReturn"`
  113. }
  114. type SMReturn struct {
  115. SynMaterialReturn string `xml:"synMaterialReturn" json:"synMaterialReturn"`
  116. }
  117. type rqRecord struct {
  118. SessionId string `xml:"sessionId" json:"sessionId"`
  119. }
  120. func EASWebServiceLogin()(string,error){
  121. client := &http.Client{}
  122. resstr := ReplaceStrWebSevice("login",user.UserName,user.Password,user.SlnName,user.DcName,user.Language,user.DbType,"","","")
  123. body := bytes.NewBuffer([]byte(resstr))
  124. req, err := http.NewRequest("POST",
  125. user.Url +"/ormrpc/services/EASLogin?wsdl",
  126. body,
  127. )
  128. req.Header.Set("Content-Type", "text/xml")
  129. req.Header.Set("SOAPAction", "application/soap+xml")
  130. if err != nil {
  131. return "",err
  132. }
  133. resp, err := client.Do(req)
  134. if err != nil {
  135. fmt.Println(err)
  136. return "",err
  137. }
  138. defer resp.Body.Close()
  139. result_body, err := ioutil.ReadAll(resp.Body)
  140. if err != nil {
  141. fmt.Println(err)
  142. return "",err
  143. }
  144. v := Response {}
  145. err = xml.Unmarshal([]byte(string(result_body)), &v)
  146. println(string(result_body))
  147. if err != nil {
  148. fmt.Println(err.Error())
  149. }
  150. if len(v.Body.Record)>0{
  151. return v.Body.Record[0].SessionId,nil
  152. }
  153. return "",err
  154. }
  155. func EASWebServiceLogout()error{
  156. client := &http.Client{}
  157. resstr := ReplaceStrWebSevice("logout",user.UserName,"",user.SlnName,user.DcName,user.Language,"","","","")
  158. body := bytes.NewBuffer([]byte(resstr))
  159. req, err := http.NewRequest("POST",
  160. user.Url +"/ormrpc/services/EASLogin?wsdl",
  161. body,
  162. )
  163. req.Header.Set("Content-Type", "text/xml")
  164. req.Header.Set("SOAPAction", "application/soap+xml")
  165. if err != nil {
  166. // handle error
  167. }
  168. resp, err := client.Do(req)
  169. if err != nil {
  170. return err
  171. }
  172. defer resp.Body.Close()
  173. _, err = ioutil.ReadAll(resp.Body)
  174. if err != nil {
  175. return err
  176. }
  177. return nil
  178. //fmt.Println(string(result_body))
  179. }
  180. func EASWebServicInsertParts(SessionId string,parts Parts)error{
  181. client := &http.Client{}
  182. soapencJson,err := json.Marshal(parts)
  183. if err != nil {
  184. return err
  185. }
  186. println(string(soapencJson))
  187. resstr := ReplaceStrWebSevice("InsertParts","","","","","","",SessionId,string(soapencJson),"")
  188. body := bytes.NewBuffer([]byte(resstr))
  189. req, err := http.NewRequest("POST",
  190. user.Url +"/ormrpc/services/WSSynMasterDataFacade?wsdl",
  191. body,
  192. )
  193. req.Header.Set("Content-Type", "text/xml")
  194. req.Header.Set("SOAPAction", "application/soap+xml")
  195. if err != nil {
  196. // handle error
  197. }
  198. resp, err := client.Do(req)
  199. if err != nil {
  200. fmt.Println(err)
  201. return err
  202. }
  203. defer resp.Body.Close()
  204. result_body, err := ioutil.ReadAll(resp.Body)
  205. if err != nil {
  206. fmt.Println(err)
  207. return err
  208. }
  209. v := Response {}
  210. err = xml.Unmarshal([]byte(string(result_body)), &v)
  211. if err != nil {
  212. fmt.Println(err.Error())
  213. }
  214. resmap := make(map[string]string,0)
  215. err = json.Unmarshal([]byte(v.Body.SynMaterialResponse.SynMaterialReturn),&resmap)
  216. if resmap["success"] == "0"{
  217. fmt.Println("resmap",resmap["message"])
  218. return errors.New(resmap["success"])
  219. }
  220. fmt.Println("resmap success",resmap["message"],resmap["success"])
  221. return err
  222. }
  223. func EASWebServicInsertPartsuselaid(SessionId,strJson,billType string){ //billType采购入库单:006 销售出库单:007 其他入库单:008 其他出库单:009 领料出库单:100 生产入库单:101
  224. client := &http.Client{}
  225. resstr := ReplaceStrWebSevice("InsertPartsuselaidStr","","","","","","",SessionId,strJson,billType)
  226. body := bytes.NewBuffer([]byte(resstr))
  227. req, err := http.NewRequest("POST",
  228. user.Url +"/ormrpc/services/WSSCMWebServiceFacade?wsdl",
  229. body,
  230. )
  231. req.Header.Set("Content-Type", "text/xml")
  232. req.Header.Set("SOAPAction", "application/soap+xml")
  233. if err != nil {
  234. // handle error
  235. }
  236. resp, err := client.Do(req)
  237. if err != nil {
  238. fmt.Println(err)
  239. return
  240. }
  241. defer resp.Body.Close()
  242. result_body, err := ioutil.ReadAll(resp.Body)
  243. if err != nil {
  244. fmt.Println(err)
  245. return
  246. }
  247. v := Response {}
  248. err = xml.Unmarshal([]byte(string(result_body)), &v)
  249. if err != nil {
  250. fmt.Println(err.Error())
  251. }
  252. resmap := make(map[string]interface{},0)
  253. if v.Body.ImportBillResponse.ImportBillReturn != "" {
  254. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn),&resmap)
  255. fmt.Println(v.Body.ImportBillResponse.ImportBillReturn)
  256. fmt.Println(len(resmap["billErrors"].([]interface {})))
  257. if resmap["status"] == "2"{
  258. fmt.Println(resmap["msg"])
  259. }
  260. }
  261. }
  262. func ReplaceStrWebSevice(typeIn,userName,password,slnName,dcName,language,dbType,SessionId,strJson,billType string)string{
  263. res := ""
  264. switch typeIn {
  265. case "login":
  266. res = LoginStr
  267. res = strings.ReplaceAll(res,"userName?",userName)
  268. res = strings.ReplaceAll(res,"password?",password)
  269. res = strings.ReplaceAll(res,"slnName?",slnName)
  270. res = strings.ReplaceAll(res,"dcName?",dcName)
  271. res = strings.ReplaceAll(res,"language?",language)
  272. res = strings.ReplaceAll(res,"dbType?",dbType)
  273. case "logout":
  274. res = LogoutStr
  275. res = strings.ReplaceAll(res,"userName?",userName)
  276. res = strings.ReplaceAll(res,"slnName?",slnName)
  277. res = strings.ReplaceAll(res,"dcName?",dcName)
  278. res = strings.ReplaceAll(res,"language?",language)
  279. case "InsertParts":
  280. res = InsertPartsStr
  281. res = strings.ReplaceAll(res,"SessionId?",SessionId)
  282. res = strings.ReplaceAll(res,"strJson?",strJson)
  283. case "InsertPartsuselaidStr":
  284. res = InsertPartsuselaidStr
  285. res = strings.ReplaceAll(res,"SessionId?",SessionId)
  286. res = strings.ReplaceAll(res,"strJson?",strJson)
  287. res = strings.ReplaceAll(res,"billType?",billType)
  288. }
  289. return res
  290. }