0a746a74d5aafc4768e1cb58d1f034775690ac1c.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. package util
  2. import (
  3. "../logging"
  4. "bytes"
  5. "encoding/xml"
  6. "fmt"
  7. "github.com/pkg/errors"
  8. "io/ioutil"
  9. "net/http"
  10. "strings"
  11. "time"
  12. )
  13. //eas webservice 目前使用字符串拼接的方式模拟xml发送
  14. var (
  15. 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">
  16. <soapenv:Header/>
  17. <soapenv:Body>
  18. <log:login soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  19. <userName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
  20. <password xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">password?</password>
  21. <slnName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
  22. <dcName xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
  23. <language xsi:type="xsd:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
  24. <dbType xsi:type="xsd:int">dbType?</dbType>
  25. </log:login>
  26. </soapenv:Body>
  27. </soapenv:Envelope>
  28. `
  29. 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">
  30. <soapenv:Header/>
  31. <soapenv:Body>
  32. <log:logout soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  33. <userName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">userName?</userName>
  34. <slnName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">slnName?</slnName>
  35. <dcName xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">dcName?</dcName>
  36. <language xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">language?</language>
  37. </log:logout>
  38. </soapenv:Body>
  39. </soapenv:Envelope>
  40. `
  41. 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">
  42. <soapenv:Header>
  43. <ns1:SessionId
  44. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  45. </ns1:SessionId>
  46. </soapenv:Header>
  47. <soapenv:Body>
  48. <web:synMaterial soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  49. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</request>
  50. </web:synMaterial>
  51. </soapenv:Body>
  52. </soapenv:Envelope>
  53. `
  54. 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">
  55. <soapenv:Header>
  56. <ns1:SessionId
  57. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  58. </ns1:SessionId>
  59. </soapenv:Header>
  60. <soapenv:Body>
  61. <web:queryMaterialData soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  62. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{"number":"strJson?"}</request>
  63. </web:queryMaterialData>
  64. </soapenv:Body>
  65. </soapenv:Envelope>`
  66. 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">
  67. <soapenv:Header>
  68. <ns1:SessionId
  69. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  70. </ns1:SessionId>
  71. </soapenv:Header>
  72. <soapenv:Body>
  73. <app:importBill soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  74. <billType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">billType?</billType>
  75. <billDataJSON xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">strJson?</billDataJSON>
  76. <billStatus xsi:type="xsd:int">2</billStatus>
  77. </app:importBill>
  78. </soapenv:Body>
  79. </soapenv:Envelope>
  80. `
  81. 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">
  82. <soapenv:Header>
  83. <ns1:SessionId
  84. xmlns:ns1="http://login.webservice.bos.kingdee.com">SessionId?
  85. </ns1:SessionId>
  86. </soapenv:Header>
  87. <soapenv:Body>
  88. <app:queryScmBillData soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  89. <billType xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">billType?</billType>
  90. <request xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">{"number":"strJson?"}</request>
  91. </app:queryScmBillData>
  92. </soapenv:Body>
  93. </soapenv:Envelope>`
  94. )
  95. var (
  96. singelChan chan struct{}
  97. lasttime time.Time
  98. sessionId string
  99. sessionidChan chan string
  100. )
  101. var user EASLoginInfo
  102. type EASLoginInfo struct {
  103. UserName string `json:"userName"`
  104. Password string `json:"password"`
  105. SlnName string `json:"slnName"`
  106. DcName string `json:"dcName"`
  107. Language string `json:"language"`
  108. DbType string `json:"dbType"`
  109. Url string `json:"dbType"`
  110. }
  111. type Response struct {
  112. Body rspBody `xml:"Body" json:"body"`
  113. }
  114. type rspBody struct {
  115. Record []rqRecord `xml:"multiRef" json:"multiRef"`
  116. SynMaterialResponse SMReturn `xml:"synMaterialResponse" json:"SynMaterialResponse"`
  117. ImportBillResponse ImportBillResponse `xml:"importBillResponse" json:"importBillResponse"`
  118. QueryMaterialDataResponse QureyParts `xml:"queryMaterialDataResponse" json:"queryMaterialDataResponse"`
  119. QueryScmBillDataResponse QureyBills `xml:"queryScmBillDataResponse" json:"queryScmBillDataResponse"`
  120. }
  121. type ImportBillResponse struct {
  122. ImportBillReturn string `xml:"importBillReturn" json:"importBillReturn"`
  123. }
  124. type QureyParts struct {
  125. QueryMaterialDataReturn string `xml:"queryMaterialDataReturn" json:"queryMaterialDataReturn"`
  126. }
  127. type QureyBills struct {
  128. QueryScmBillDataReturn string `xml:"queryScmBillDataReturn" json:"queryScmBillDataReturn"`
  129. }
  130. type SMReturn struct {
  131. SynMaterialReturn string `xml:"synMaterialReturn" json:"synMaterialReturn"`
  132. }
  133. type rqRecord struct {
  134. SessionId string `xml:"sessionId" json:"sessionId"`
  135. }
  136. func InitWebS(use, pass, url string) {
  137. user.Password = pass
  138. user.UserName = use
  139. user.DbType = "1"
  140. user.DcName = "MASM" //正式环境是 XDMY ,备件先手动改为XDMY,后面全部切换正式环境后统一修改
  141. user.Language = "L2"
  142. user.SlnName = "eas"
  143. user.Url = url
  144. singelChan = make(chan struct{} ,100)
  145. sessionidChan = make(chan string ,100)
  146. lasttime = time.Now().Add(-6*time.Minute)
  147. go Runsessionid()
  148. }
  149. // sessionId 获取,本次请求与上次间隔5分钟以上 就重新登录
  150. func Runsessionid(){
  151. for {
  152. func() {
  153. defer func() {
  154. if err := recover();err != nil {
  155. logging.Error("Runsessionid recover err",err)
  156. }
  157. }()
  158. select {
  159. case <- singelChan:
  160. if time.Now().Sub(lasttime) > 5*time.Minute{ // 大于五分钟重新退出再登录
  161. println("重新登录")
  162. //EASWebServiceLogout()
  163. sd, err := EASWebServiceLogin("")
  164. if err != nil {
  165. sessionidChan <- "error: "+err.Error()
  166. }else{
  167. lasttime = time.Now()
  168. sessionId = sd
  169. sessionidChan <- sessionId
  170. }
  171. }else{
  172. sessionidChan <- sessionId
  173. }
  174. }
  175. }()
  176. }
  177. }
  178. //登录
  179. func EASWebServiceLogin(url string ) (string, error) {
  180. client := &http.Client{}
  181. resstr := ReplaceStrWebSevice("login", user.UserName, user.Password, user.SlnName, user.DcName, user.Language, user.DbType, "", "", "")
  182. if url == ""{
  183. url = user.Url
  184. }else{
  185. resstr = strings.ReplaceAll(resstr, "MASM", "XDMY") // dcname正式和测试环境不一致
  186. }
  187. body := bytes.NewBuffer([]byte(resstr))
  188. req, err := http.NewRequest("POST",
  189. url+"/ormrpc/services/EASLogin?wsdl",
  190. body,
  191. )
  192. req.Header.Set("Content-Type", "text/xml")
  193. req.Header.Set("SOAPAction", "application/soap+xml")
  194. if err != nil {
  195. return "", err
  196. }
  197. resp, err := client.Do(req)
  198. if err != nil {
  199. fmt.Println(err)
  200. return "", err
  201. }
  202. defer resp.Body.Close()
  203. result_body, err := ioutil.ReadAll(resp.Body)
  204. if err != nil {
  205. fmt.Println(err)
  206. return "", err
  207. }
  208. v := Response{}
  209. err = xml.Unmarshal([]byte(string(result_body)), &v)
  210. if err != nil {
  211. fmt.Println(err.Error())
  212. }
  213. if len(v.Body.Record) > 0 {
  214. return v.Body.Record[0].SessionId, nil
  215. }
  216. return "", err
  217. }
  218. //注销
  219. func EASWebServiceLogout() error {
  220. client := &http.Client{}
  221. resstr := ReplaceStrWebSevice("logout", user.UserName, "", user.SlnName, user.DcName, user.Language, "", "", "", "")
  222. body := bytes.NewBuffer([]byte(resstr))
  223. req, err := http.NewRequest("POST",
  224. user.Url+"/ormrpc/services/EASLogin?wsdl",
  225. body,
  226. )
  227. req.Header.Set("Content-Type", "text/xml")
  228. req.Header.Set("SOAPAction", "application/soap+xml")
  229. if err != nil {
  230. // handle error
  231. }
  232. resp, err := client.Do(req)
  233. if err != nil {
  234. return err
  235. }
  236. defer resp.Body.Close()
  237. _, err = ioutil.ReadAll(resp.Body)
  238. if err != nil {
  239. return err
  240. }
  241. return nil
  242. //fmt.Println(string(result_body))
  243. }
  244. func EASWebServiceQureyParts(SessionId, PartCode string) error {
  245. client := &http.Client{}
  246. resstr := ReplaceStrWebSevice("QureyPartsStr", "", "", "", "", "", "", SessionId, PartCode, "")
  247. body := bytes.NewBuffer([]byte(resstr))
  248. req, err := http.NewRequest("POST",
  249. user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
  250. body,
  251. )
  252. req.Header.Set("Content-Type", "text/xml")
  253. req.Header.Set("SOAPAction", "application/soap+xml")
  254. if err != nil {
  255. // handle error
  256. }
  257. resp, err := client.Do(req)
  258. if err != nil {
  259. return err
  260. }
  261. defer resp.Body.Close()
  262. result_body, err := ioutil.ReadAll(resp.Body)
  263. if err != nil {
  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.QueryMaterialDataResponse.QueryMaterialDataReturn), &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 EASWebServiceQureyBill(SessionId, BillCode,billType string) error {
  285. client := &http.Client{}
  286. resstr := ReplaceStrWebSevice("QureyBill", "", "", "", "", "", "", SessionId, BillCode, billType)
  287. body := bytes.NewBuffer([]byte(resstr))
  288. req, err := http.NewRequest("POST",
  289. user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
  290. body,
  291. )
  292. req.Header.Set("Content-Type", "text/xml")
  293. req.Header.Set("SOAPAction", "application/soap+xml")
  294. if err != nil {
  295. return err
  296. }
  297. resp, err := client.Do(req)
  298. if err != nil {
  299. return err
  300. }
  301. defer resp.Body.Close()
  302. result_body, err := ioutil.ReadAll(resp.Body)
  303. fmt.Println(string(result_body))
  304. if err != nil {
  305. fmt.Println(err)
  306. return err
  307. }
  308. v := Response{}
  309. err = xml.Unmarshal(result_body, &v)
  310. if err != nil {
  311. fmt.Println(err.Error())
  312. }
  313. resmap := make(map[string]interface{}, 0)
  314. err = json.Unmarshal([]byte(v.Body.QueryScmBillDataResponse.QueryScmBillDataReturn), &resmap)
  315. //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
  316. switch resmap["data"].(type) {
  317. case []interface{}:
  318. if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
  319. return errors.New("单据未查询到")
  320. }
  321. default:
  322. return errors.New("单据未查询到")
  323. }
  324. return nil
  325. }
  326. func EASWebServicInsertParts(SessionId string, parts Parts,url string) error {
  327. client := &http.Client{}
  328. soapencJson, err := json.Marshal(parts)
  329. if err != nil {
  330. return err
  331. }
  332. if url == ""{
  333. url = user.Url
  334. }
  335. println(string(soapencJson))
  336. resstr := ReplaceStrWebSevice("InsertParts", "", "", "", "", "", "", SessionId, string(soapencJson), "")
  337. body := bytes.NewBuffer([]byte(resstr))
  338. req, err := http.NewRequest("POST",
  339. url+"/ormrpc/services/WSSynMasterDataFacade?wsdl",
  340. body,
  341. )
  342. req.Header.Set("Content-Type", "text/xml")
  343. req.Header.Set("SOAPAction", "application/soap+xml")
  344. if err != nil {
  345. // handle error
  346. }
  347. resp, err := client.Do(req)
  348. if err != nil {
  349. fmt.Println(err)
  350. return err
  351. }
  352. defer resp.Body.Close()
  353. result_body, err := ioutil.ReadAll(resp.Body)
  354. if err != nil {
  355. fmt.Println(err)
  356. return err
  357. }
  358. v := Response{}
  359. err = xml.Unmarshal(result_body, &v)
  360. if err != nil {
  361. fmt.Println(err.Error())
  362. }
  363. resmap := make(map[string]string, 0)
  364. err = json.Unmarshal([]byte(v.Body.SynMaterialResponse.SynMaterialReturn), &resmap)
  365. _,ok := resmap["success"]
  366. if !ok {
  367. return errors.New(string(result_body))
  368. }
  369. if resmap["success"] == "0" {
  370. return errors.New(resmap["message"])
  371. }
  372. return nil
  373. }
  374. func EASWebServicInsertPartsuselaid(SessionId, strJson, billType string) error { //billType采购入库单:006 销售出库单:007 其他入库单:008 其他出库单:009 领料出库单:100 生产入库单:101
  375. client := &http.Client{}
  376. resstr := ReplaceStrWebSevice("InsertPartsuselaidStr", "", "", "", "", "", "", SessionId, strJson, billType)
  377. body := bytes.NewBuffer([]byte(resstr))
  378. req, err := http.NewRequest("POST",
  379. user.Url+"/ormrpc/services/WSSCMWebServiceFacade?wsdl",
  380. body,
  381. )
  382. req.Header.Set("Content-Type", "text/xml")
  383. req.Header.Set("SOAPAction", "application/soap+xml")
  384. if err != nil {
  385. // handle error
  386. }
  387. resp, err := client.Do(req)
  388. if err != nil {
  389. fmt.Println(err)
  390. return err
  391. }
  392. defer resp.Body.Close()
  393. result_body, err := ioutil.ReadAll(resp.Body)
  394. if err != nil {
  395. fmt.Println(err)
  396. return err
  397. }
  398. v := Response{}
  399. err = xml.Unmarshal(result_body, &v)
  400. if err != nil {
  401. fmt.Println(err.Error())
  402. }
  403. resmap := make(map[string]interface{}, 0)
  404. if v.Body.ImportBillResponse.ImportBillReturn != "" {
  405. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
  406. fmt.Println(v.Body.ImportBillResponse.ImportBillReturn)
  407. //_,ok := resmap["billErrors"]
  408. //if !ok {
  409. // println(string(result_body))
  410. // return errors.New(string(result_body))
  411. //}
  412. _,ok := resmap["status"]
  413. if !ok {
  414. println(string(result_body))
  415. return errors.New(string(result_body))
  416. }
  417. //if len(resmap["billErrors"].([]interface{})) > 0 || resmap["status"] != "1" {
  418. if resmap["status"] != "0" {
  419. resmap := make(map[string]interface{}, 0)
  420. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
  421. switch resmap["billErrors"].(type) {
  422. case []interface{}:
  423. if len(resmap["billErrors"].([]interface{}))>0{
  424. switch resmap["billErrors"].([]interface{})[0].(type) {
  425. case map[string]interface{} :
  426. str := resmap["billErrors"].([]interface{})[0].(map[string]interface{})["errorMsgs"]
  427. switch str.(type) {
  428. case []interface{}:
  429. if len(str.([]interface{})) >0 {
  430. s := str.([]interface{})[0]
  431. switch s.(type) {
  432. case string :
  433. return errors.New(s.(string))
  434. }
  435. }
  436. }
  437. }
  438. }
  439. }
  440. return errors.New(v.Body.ImportBillResponse.ImportBillReturn)
  441. }
  442. }
  443. return nil
  444. }
  445. func ReplaceStrWebSevice(typeIn, userName, password, slnName, dcName, language, dbType, SessionId, strJson, billType string) string {
  446. res := ""
  447. switch typeIn {
  448. case "login":
  449. res = LoginStr
  450. res = strings.ReplaceAll(res, "userName?", userName)
  451. res = strings.ReplaceAll(res, "password?", password)
  452. res = strings.ReplaceAll(res, "slnName?", slnName)
  453. res = strings.ReplaceAll(res, "dcName?", dcName)
  454. res = strings.ReplaceAll(res, "language?", language)
  455. res = strings.ReplaceAll(res, "dbType?", dbType)
  456. case "logout":
  457. res = LogoutStr
  458. res = strings.ReplaceAll(res, "userName?", userName)
  459. res = strings.ReplaceAll(res, "slnName?", slnName)
  460. res = strings.ReplaceAll(res, "dcName?", dcName)
  461. res = strings.ReplaceAll(res, "language?", language)
  462. case "InsertParts":
  463. res = InsertPartsStr
  464. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  465. res = strings.ReplaceAll(res, "strJson?", strJson)
  466. case "QureyPartsStr":
  467. res = QureyPartsStr
  468. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  469. res = strings.ReplaceAll(res, "strJson?", strJson)
  470. case "InsertPartsuselaidStr":
  471. res = InsertPartsuselaidStr
  472. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  473. res = strings.ReplaceAll(res, "strJson?", strJson)
  474. res = strings.ReplaceAll(res, "billType?", billType)
  475. case "QureyBill":
  476. res = QureyBill
  477. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  478. res = strings.ReplaceAll(res, "strJson?", strJson)
  479. res = strings.ReplaceAll(res, "billType?", billType)
  480. }
  481. return res
  482. }