d3995014c764a2a923fdb37d67d37b633d836150.svn-base 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504
  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"
  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. func Runsessionid(){
  150. for {
  151. func() {
  152. defer func() {
  153. if err := recover();err != nil {
  154. logging.Error("Runsessionid recover err",err)
  155. }
  156. }()
  157. select {
  158. case <- singelChan:
  159. if time.Now().Sub(lasttime) > 5*time.Minute{ // 大于五分钟重新退出再登录
  160. println("重新登录")
  161. //EASWebServiceLogout()
  162. sd, err := EASWebServiceLogin()
  163. if err != nil {
  164. sessionidChan <- "error: "+err.Error()
  165. }else{
  166. lasttime = time.Now()
  167. sessionid = sd
  168. sessionidChan <- sessionid
  169. }
  170. }else{
  171. sessionidChan <- sessionid
  172. }
  173. }
  174. }()
  175. }
  176. }
  177. func eastest() {
  178. sessionid, _ := EASWebServiceLogin()
  179. //EASWebServicInsertParts(sessionid,"{}")
  180. EASWebServicInsertPartsuselaid(sessionid, "{}", "007")
  181. //EASWebServiceLogout()
  182. }
  183. func EASWebServiceLogin() (string, error) {
  184. client := &http.Client{}
  185. resstr := ReplaceStrWebSevice("login", user.UserName, user.Password, user.SlnName, user.DcName, user.Language, user.DbType, "", "", "")
  186. body := bytes.NewBuffer([]byte(resstr))
  187. req, err := http.NewRequest("POST",
  188. user.Url+"/ormrpc/services/EASLogin?wsdl",
  189. body,
  190. )
  191. req.Header.Set("Content-Type", "text/xml")
  192. req.Header.Set("SOAPAction", "application/soap+xml")
  193. if err != nil {
  194. return "", err
  195. }
  196. resp, err := client.Do(req)
  197. if err != nil {
  198. fmt.Println(err)
  199. return "", err
  200. }
  201. defer resp.Body.Close()
  202. result_body, err := ioutil.ReadAll(resp.Body)
  203. if err != nil {
  204. fmt.Println(err)
  205. return "", err
  206. }
  207. v := Response{}
  208. err = xml.Unmarshal([]byte(string(result_body)), &v)
  209. if err != nil {
  210. fmt.Println(err.Error())
  211. }
  212. if len(v.Body.Record) > 0 {
  213. return v.Body.Record[0].SessionId, nil
  214. }
  215. return "", err
  216. }
  217. func EASWebServiceLogout() error {
  218. client := &http.Client{}
  219. resstr := ReplaceStrWebSevice("logout", user.UserName, "", user.SlnName, user.DcName, user.Language, "", "", "", "")
  220. body := bytes.NewBuffer([]byte(resstr))
  221. req, err := http.NewRequest("POST",
  222. user.Url+"/ormrpc/services/EASLogin?wsdl",
  223. body,
  224. )
  225. req.Header.Set("Content-Type", "text/xml")
  226. req.Header.Set("SOAPAction", "application/soap+xml")
  227. if err != nil {
  228. // handle error
  229. }
  230. resp, err := client.Do(req)
  231. if err != nil {
  232. return err
  233. }
  234. defer resp.Body.Close()
  235. _, err = ioutil.ReadAll(resp.Body)
  236. if err != nil {
  237. return err
  238. }
  239. return nil
  240. //fmt.Println(string(result_body))
  241. }
  242. func EASWebServiceQureyParts(SessionId, PartCode string) error {
  243. client := &http.Client{}
  244. resstr := ReplaceStrWebSevice("QureyPartsStr", "", "", "", "", "", "", SessionId, PartCode, "")
  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. // handle error
  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. if err != nil {
  262. return err
  263. }
  264. v := Response{}
  265. err = xml.Unmarshal(result_body, &v)
  266. if err != nil {
  267. fmt.Println(err.Error())
  268. }
  269. resmap := make(map[string]interface{}, 0)
  270. err = json.Unmarshal([]byte(v.Body.QueryMaterialDataResponse.QueryMaterialDataReturn), &resmap)
  271. //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
  272. switch resmap["data"].(type) {
  273. case []interface{}:
  274. if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
  275. return errors.New("单据未查询到")
  276. }
  277. default:
  278. return errors.New("单据未查询到")
  279. }
  280. return nil
  281. }
  282. func EASWebServiceQureyBill(SessionId, BillCode,billType string) error {
  283. client := &http.Client{}
  284. resstr := ReplaceStrWebSevice("QureyBill", "", "", "", "", "", "", SessionId, BillCode, billType)
  285. body := bytes.NewBuffer([]byte(resstr))
  286. req, err := http.NewRequest("POST",
  287. user.Url+"/ormrpc/services/WSReadEASDataFacade?wsdl",
  288. body,
  289. )
  290. req.Header.Set("Content-Type", "text/xml")
  291. req.Header.Set("SOAPAction", "application/soap+xml")
  292. if err != nil {
  293. return err
  294. }
  295. resp, err := client.Do(req)
  296. if err != nil {
  297. return err
  298. }
  299. defer resp.Body.Close()
  300. result_body, err := ioutil.ReadAll(resp.Body)
  301. fmt.Println(string(result_body))
  302. if err != nil {
  303. fmt.Println(err)
  304. return err
  305. }
  306. v := Response{}
  307. err = xml.Unmarshal(result_body, &v)
  308. if err != nil {
  309. fmt.Println(err.Error())
  310. }
  311. resmap := make(map[string]interface{}, 0)
  312. err = json.Unmarshal([]byte(v.Body.QueryScmBillDataResponse.QueryScmBillDataReturn), &resmap)
  313. //fmt.Println("resmap===============", resmap,len(resmap["data"].([]interface{})))
  314. switch resmap["data"].(type) {
  315. case []interface{}:
  316. if resmap["success"] != "1" || len(resmap["data"].([]interface{})) == 0 {
  317. return errors.New("单据未查询到")
  318. }
  319. default:
  320. return errors.New("单据未查询到")
  321. }
  322. return nil
  323. }
  324. func EASWebServicInsertParts(SessionId string, parts Parts) error {
  325. client := &http.Client{}
  326. soapencJson, err := json.Marshal(parts)
  327. if err != nil {
  328. return err
  329. }
  330. println(string(soapencJson))
  331. resstr := ReplaceStrWebSevice("InsertParts", "", "", "", "", "", "", SessionId, string(soapencJson), "")
  332. body := bytes.NewBuffer([]byte(resstr))
  333. req, err := http.NewRequest("POST",
  334. user.Url+"/ormrpc/services/WSSynMasterDataFacade?wsdl",
  335. body,
  336. )
  337. req.Header.Set("Content-Type", "text/xml")
  338. req.Header.Set("SOAPAction", "application/soap+xml")
  339. if err != nil {
  340. // handle error
  341. }
  342. resp, err := client.Do(req)
  343. if err != nil {
  344. fmt.Println(err)
  345. return err
  346. }
  347. defer resp.Body.Close()
  348. result_body, err := ioutil.ReadAll(resp.Body)
  349. if err != nil {
  350. fmt.Println(err)
  351. return err
  352. }
  353. v := Response{}
  354. err = xml.Unmarshal(result_body, &v)
  355. if err != nil {
  356. fmt.Println(err.Error())
  357. }
  358. resmap := make(map[string]string, 0)
  359. err = json.Unmarshal([]byte(v.Body.SynMaterialResponse.SynMaterialReturn), &resmap)
  360. _,ok := resmap["success"]
  361. if !ok {
  362. return errors.New(string(result_body))
  363. }
  364. if resmap["success"] == "0" {
  365. return errors.New(resmap["message"])
  366. }
  367. return nil
  368. }
  369. func EASWebServicInsertPartsuselaid(SessionId, strJson, billType string) error { //billType采购入库单:006 销售出库单:007 其他入库单:008 其他出库单:009 领料出库单:100 生产入库单:101
  370. client := &http.Client{}
  371. resstr := ReplaceStrWebSevice("InsertPartsuselaidStr", "", "", "", "", "", "", SessionId, strJson, billType)
  372. body := bytes.NewBuffer([]byte(resstr))
  373. req, err := http.NewRequest("POST",
  374. user.Url+"/ormrpc/services/WSSCMWebServiceFacade?wsdl",
  375. body,
  376. )
  377. req.Header.Set("Content-Type", "text/xml")
  378. req.Header.Set("SOAPAction", "application/soap+xml")
  379. if err != nil {
  380. // handle error
  381. }
  382. resp, err := client.Do(req)
  383. if err != nil {
  384. fmt.Println(err)
  385. return err
  386. }
  387. defer resp.Body.Close()
  388. result_body, err := ioutil.ReadAll(resp.Body)
  389. if err != nil {
  390. fmt.Println(err)
  391. return err
  392. }
  393. v := Response{}
  394. err = xml.Unmarshal(result_body, &v)
  395. if err != nil {
  396. fmt.Println(err.Error())
  397. }
  398. resmap := make(map[string]interface{}, 0)
  399. if v.Body.ImportBillResponse.ImportBillReturn != "" {
  400. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
  401. fmt.Println(v.Body.ImportBillResponse.ImportBillReturn)
  402. //_,ok := resmap["billErrors"]
  403. //if !ok {
  404. // println(string(result_body))
  405. // return errors.New(string(result_body))
  406. //}
  407. _,ok := resmap["status"]
  408. if !ok {
  409. println(string(result_body))
  410. return errors.New(string(result_body))
  411. }
  412. //if len(resmap["billErrors"].([]interface{})) > 0 || resmap["status"] != "1" {
  413. if resmap["status"] != "0" {
  414. resmap := make(map[string]interface{}, 0)
  415. err = json.Unmarshal([]byte(v.Body.ImportBillResponse.ImportBillReturn), &resmap)
  416. switch resmap["billErrors"].(type) {
  417. case []interface{}:
  418. if len(resmap["billErrors"].([]interface{}))>0{
  419. switch resmap["billErrors"].([]interface{})[0].(type) {
  420. case map[string]interface{} :
  421. str := resmap["billErrors"].([]interface{})[0].(map[string]interface{})["errorMsgs"]
  422. switch str.(type) {
  423. case []interface{}:
  424. if len(str.([]interface{})) >0 {
  425. s := str.([]interface{})[0]
  426. switch s.(type) {
  427. case string :
  428. return errors.New(s.(string))
  429. }
  430. }
  431. }
  432. }
  433. }
  434. }
  435. return errors.New(v.Body.ImportBillResponse.ImportBillReturn)
  436. }
  437. }
  438. return nil
  439. }
  440. func ReplaceStrWebSevice(typeIn, userName, password, slnName, dcName, language, dbType, SessionId, strJson, billType string) string {
  441. res := ""
  442. switch typeIn {
  443. case "login":
  444. res = LoginStr
  445. res = strings.ReplaceAll(res, "userName?", userName)
  446. res = strings.ReplaceAll(res, "password?", password)
  447. res = strings.ReplaceAll(res, "slnName?", slnName)
  448. res = strings.ReplaceAll(res, "dcName?", dcName)
  449. res = strings.ReplaceAll(res, "language?", language)
  450. res = strings.ReplaceAll(res, "dbType?", dbType)
  451. case "logout":
  452. res = LogoutStr
  453. res = strings.ReplaceAll(res, "userName?", userName)
  454. res = strings.ReplaceAll(res, "slnName?", slnName)
  455. res = strings.ReplaceAll(res, "dcName?", dcName)
  456. res = strings.ReplaceAll(res, "language?", language)
  457. case "InsertParts":
  458. res = InsertPartsStr
  459. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  460. res = strings.ReplaceAll(res, "strJson?", strJson)
  461. case "QureyPartsStr":
  462. res = QureyPartsStr
  463. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  464. res = strings.ReplaceAll(res, "strJson?", strJson)
  465. case "InsertPartsuselaidStr":
  466. res = InsertPartsuselaidStr
  467. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  468. res = strings.ReplaceAll(res, "strJson?", strJson)
  469. res = strings.ReplaceAll(res, "billType?", billType)
  470. case "QureyBill":
  471. res = QureyBill
  472. res = strings.ReplaceAll(res, "SessionId?", SessionId)
  473. res = strings.ReplaceAll(res, "strJson?", strJson)
  474. res = strings.ReplaceAll(res, "billType?", billType)
  475. }
  476. return res
  477. }