ActivitiService.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. package workflow
  2. import (
  3. "bufio"
  4. "encoding/json"
  5. "fmt"
  6. "io/ioutil"
  7. "log"
  8. "net/http"
  9. "os"
  10. "strconv"
  11. "strings"
  12. "time"
  13. "dashoo.cn/utils"
  14. . "dashoo.cn/utils/db"
  15. "github.com/go-xorm/xorm"
  16. . "github.com/linxGnu/goseaweedfs"
  17. )
  18. type ActivitiService struct {
  19. BaseUrl string
  20. AposeUrl string
  21. OriginUrl string
  22. Username string
  23. Password string
  24. ServiceBase
  25. }
  26. func GetActivitiService(xormEngine *xorm.Engine) *ActivitiService {
  27. s := new(ActivitiService)
  28. s.DBE = xormEngine
  29. //s.BaseUrl = "http://123.56.168.26:8080/activiti-rest/service"
  30. //s.BaseUrl = "http://192.168.0.167:8081/api/acti"
  31. s.BaseUrl = "http://47.92.212.59:8080/acti-api/api/acti"
  32. //s.BaseUrl = "http://localhost:8081/api/acti"
  33. s.AposeUrl = "http://47.92.212.59:8080/acti-api/api/apose"
  34. s.OriginUrl = "http://localhost:8081/api"
  35. //s.OriginUrl = "http://47.92.212.59:8080/acti-api/api"
  36. //s.AposeUrl = "http://localhost:8081/api/apose"
  37. //s.AposeUrl = "http://192.168.0.171:8081/api/apose"
  38. s.Username = "leader"
  39. s.Password = "123456"
  40. return s
  41. }
  42. func (this *ActivitiService) Post(url string, params string, token string) *http.Response {
  43. client := &http.Client{}
  44. req, err := http.NewRequest("POST", this.BaseUrl+url, strings.NewReader(params))
  45. req.Header.Add("Content-Type", "application/json")
  46. req.Header.Add("Authorization", "Bearer "+token)
  47. resp, err := client.Do(req)
  48. if err != nil {
  49. log.Println("err= ", err)
  50. }
  51. log.Println("resp= ", resp)
  52. return resp
  53. }
  54. func (this *ActivitiService) Post2(url string, params string, token string) *http.Response {
  55. client := &http.Client{}
  56. req, err := http.NewRequest("POST", this.AposeUrl+url, strings.NewReader(params))
  57. req.Header.Add("Content-Type", "application/json")
  58. req.Header.Add("Authorization", "Bearer "+token)
  59. resp, err := client.Do(req)
  60. if err != nil {
  61. log.Println("err= ", err)
  62. }
  63. log.Println("resp= ", resp)
  64. return resp
  65. }
  66. func (this *ActivitiService) PostOrigin(url string, params string, token string) *http.Response {
  67. client := &http.Client{}
  68. req, err := http.NewRequest("POST", this.OriginUrl+url, strings.NewReader(params))
  69. req.Header.Add("Content-Type", "application/json")
  70. req.Header.Add("Authorization", "Bearer "+token)
  71. resp, err := client.Do(req)
  72. if err != nil {
  73. log.Println("err= ", err)
  74. }
  75. log.Println("resp= ", resp)
  76. return resp
  77. }
  78. func (this *ActivitiService) Get(url string, params string, token string) *http.Response {
  79. client := &http.Client{}
  80. req, err := http.NewRequest("Get", this.BaseUrl+url, strings.NewReader(params))
  81. req.Header.Add("Content-Type", "application/json")
  82. req.Header.Add("Authorization", "Bearer "+token)
  83. resp, err := client.Do(req)
  84. if err != nil {
  85. log.Println("err= ", err)
  86. }
  87. log.Println("resp= ", resp)
  88. return resp
  89. }
  90. func (this *ActivitiService) StartProcess(processKey string, formEntityId string, userNames string) string {
  91. /*vars := make(map[string]string)
  92. vars["processDefinitionKey"] = processKey
  93. vars["formEntityId"] = formEntityId*/
  94. /*params := "processDefinitionKey="+processKey+"&formEntityId"+formEntityId*/
  95. var ActiProcess ActiProcessVM
  96. ActiProcess.ProcessKey = processKey
  97. ActiProcess.BusinessKey = formEntityId
  98. ActiProcess.UserNames = userNames
  99. json, err := json.Marshal(ActiProcess)
  100. if err != nil {
  101. fmt.Println(err, "生成json字符串错误")
  102. }
  103. params := string(json)
  104. fmt.Println(params)
  105. //token = Authorization(this.Username, this.Password)
  106. retVal := this.Post("/start-process", params, "")
  107. p, _ := ioutil.ReadAll(retVal.Body)
  108. return string(p)
  109. }
  110. func (this *ActivitiService) TaskComplete(processKey string, formEntityId string, userNames string, userId string, result string, remarks string) string {
  111. var ActiComplete ActiCompleteVM
  112. ActiComplete.ProcessKey = processKey
  113. ActiComplete.BusinessKey = formEntityId
  114. ActiComplete.UserNames = userNames
  115. ActiComplete.UserId = userId
  116. ActiComplete.Result = result
  117. ActiComplete.Remarks = remarks
  118. jsonParams, err := json.Marshal(ActiComplete)
  119. if err != nil {
  120. fmt.Println(err, "生成json字符串错误")
  121. }
  122. params := string(jsonParams)
  123. fmt.Println(params)
  124. //token = Authorization(this.Username, this.Password)
  125. retVal := this.Post("/task-complete", params, "")
  126. p, _ := ioutil.ReadAll(retVal.Body)
  127. return string(p)
  128. }
  129. func (this *ActivitiService) GetMyTasks(processKey string, userId string) string {
  130. var ActiMyTasks ActiMyTasksVM
  131. ActiMyTasks.ProcessKey = processKey
  132. ActiMyTasks.UserId = userId
  133. json, err := json.Marshal(ActiMyTasks)
  134. if err != nil {
  135. fmt.Println(err, "生成json字符串错误")
  136. }
  137. params := string(json)
  138. fmt.Println(params)
  139. //token = Authorization(this.Username, this.Password)
  140. retVal := this.Post("/my-tasks", params, "")
  141. p, _ := ioutil.ReadAll(retVal.Body)
  142. return string(p)
  143. }
  144. func (this *ActivitiService) GetHistoricTasks(processKey string, businessKey string, processInstanceId string) []ActiHistoricTask {
  145. var ActiProcess ActiProcessVM
  146. ActiProcess.ProcessKey = processKey
  147. ActiProcess.BusinessKey = businessKey
  148. ActiProcess.ProcessInstanceId = processInstanceId
  149. jsonParam, err := json.Marshal(ActiProcess)
  150. if err != nil {
  151. fmt.Println(err, "生成json字符串错误")
  152. }
  153. params := string(jsonParam)
  154. fmt.Println(params)
  155. //token = Authorization(this.Username, this.Password)
  156. retVal := this.Post("/historic-tasks", params, "")
  157. jsonblob, _ := ioutil.ReadAll(retVal.Body)
  158. // var historicTasks []ActiHistoricTask
  159. historicTasks := make([]ActiHistoricTask, 0)
  160. json.Unmarshal(jsonblob, &historicTasks)
  161. return historicTasks
  162. }
  163. func (this *ActivitiService) GetActivitiProccessImage(processInstanceId string) []byte {
  164. var ActiProcess ActiProcessVM
  165. ActiProcess.ProcessInstanceId = processInstanceId
  166. jsonParam, err := json.Marshal(ActiProcess)
  167. if err != nil {
  168. fmt.Println(err, "生成json字符串错误")
  169. }
  170. params := string(jsonParam)
  171. //token = Authorization(this.Username, this.Password)
  172. retVal := this.Post("/historic-tasks-image", params, "")
  173. imgByte, _ := ioutil.ReadAll(retVal.Body)
  174. return imgByte
  175. }
  176. func (this *ActivitiService) GetHistoryMyTasks(processKey string, userId string) string {
  177. var ActiMyTasks ActiMyTasksVM
  178. ActiMyTasks.ProcessKey = processKey
  179. ActiMyTasks.UserId = userId
  180. json, err := json.Marshal(ActiMyTasks)
  181. if err != nil {
  182. fmt.Println(err, "生成json字符串错误")
  183. }
  184. params := string(json)
  185. fmt.Println(params)
  186. //token = Authorization(this.Username, this.Password)
  187. retVal := this.Post("/my-tasks-finished", params, "")
  188. p, _ := ioutil.ReadAll(retVal.Body)
  189. return string(p)
  190. }
  191. func (this *ActivitiService) ExcelToPdf(addressUrl string) string {
  192. var aposeVM AposeVM
  193. aposeVM.AddressUrl = addressUrl
  194. fmt.Println("==addressUrl===", addressUrl)
  195. jsonParam, err := json.Marshal(aposeVM)
  196. if err != nil {
  197. fmt.Println(err, "生成json字符串错误")
  198. }
  199. params := string(jsonParam)
  200. //token = Authorization(this.Username, this.Password)
  201. retVal := this.Post2("/excel-to-pdf", params, "")
  202. if retVal.StatusCode != 200 {
  203. return ""
  204. }
  205. //pdfByte, err := ioutil.ReadAll(retVal.Body)
  206. //ioutil.WriteFile("E:\aa.pdf", pdfByte, 777)
  207. //模板下载到服务器
  208. _dir := utils.Cfg.MustValue("file", "tmplateDir") + "pdf"
  209. utils.CreatePath(_dir)
  210. _dir += "/tmp_" + strconv.Itoa(int(time.Now().Unix())) + ".pdf"
  211. raw := retVal.Body
  212. defer raw.Close()
  213. file, err := os.Create(_dir)
  214. defer file.Close()
  215. writer := bufio.NewWriter(file)
  216. defer writer.Flush()
  217. body, err := ioutil.ReadAll(raw)
  218. writer.Write(body)
  219. if err != nil {
  220. return ""
  221. }
  222. var sw *Seaweed
  223. var filer []string
  224. if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
  225. filer = []string{_filer}
  226. }
  227. sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
  228. //_, fID, err := sw.Upload(retVal.Body, "tmp.pdf", int64(len(pdfByte)), "", "")
  229. _, _, fID, err := sw.UploadFile(_dir, "", "")
  230. retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
  231. os.Remove(_dir)
  232. fmt.Println("==retDocUrl==", retDocUrl)
  233. return retDocUrl
  234. }
  235. func (this *ActivitiService) WordToPdf(addressUrl string) string {
  236. var aposeVM AposeVM
  237. aposeVM.AddressUrl = addressUrl
  238. fmt.Println("==addressUrl===", addressUrl)
  239. jsonParam, err := json.Marshal(aposeVM)
  240. if err != nil {
  241. fmt.Println(err, "生成json字符串错误")
  242. }
  243. params := string(jsonParam)
  244. //token = Authorization(this.Username, this.Password)
  245. retVal := this.Post2("/word-to-pdf", params, "")
  246. if retVal.StatusCode != 200 {
  247. return ""
  248. }
  249. //pdfByte, err := ioutil.ReadAll(retVal.Body)
  250. //ioutil.WriteFile("E:\aa.pdf", pdfByte, 777)
  251. //模板下载到服务器
  252. _dir := utils.Cfg.MustValue("file", "tmplateDir") + "pdf"
  253. utils.CreatePath(_dir)
  254. _dir += "/tmp_" + strconv.Itoa(int(time.Now().Unix())) + ".pdf"
  255. raw := retVal.Body
  256. defer raw.Close()
  257. file, err := os.Create(_dir)
  258. defer file.Close()
  259. writer := bufio.NewWriter(file)
  260. defer writer.Flush()
  261. body, err := ioutil.ReadAll(raw)
  262. writer.Write(body)
  263. if err != nil {
  264. return ""
  265. }
  266. var sw *Seaweed
  267. var filer []string
  268. if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
  269. filer = []string{_filer}
  270. }
  271. sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
  272. //_, fID, err := sw.Upload(retVal.Body, "tmp.pdf", int64(len(pdfByte)), "", "")
  273. _, _, fID, err := sw.UploadFile(_dir, "", "")
  274. retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
  275. os.Remove(_dir)
  276. fmt.Println("==retDocUrl==", retDocUrl)
  277. return retDocUrl
  278. }
  279. func (this *ActivitiService) FillWordTemplate(datas map[string]interface{}, templateUrl string, fileName string) string {
  280. var wordTempVM WordTemplateVM
  281. wordTempVM.Datas = datas
  282. wordTempVM.TemplateUrl = templateUrl
  283. wordTempVM.FileName = fileName
  284. fmt.Println("==templateUrl===", templateUrl)
  285. jsonParam, err := json.Marshal(wordTempVM)
  286. if err != nil {
  287. fmt.Println(err, "生成json字符串错误")
  288. }
  289. params := string(jsonParam)
  290. //token = Authorization(this.Username, this.Password)
  291. retVal := this.PostOrigin("/word/fill-word", params, "")
  292. if retVal.StatusCode != 200 {
  293. return ""
  294. }
  295. //pdfByte, err := ioutil.ReadAll(retVal.Body)
  296. //ioutil.WriteFile("E:\aa.pdf", pdfByte, 777)
  297. //模板下载到服务器
  298. _dir := utils.Cfg.MustValue("file", "tmplateDir") + "word"
  299. utils.CreatePath(_dir)
  300. _dir += "/tmp_" + strconv.Itoa(int(time.Now().Unix())) + ".docx"
  301. raw := retVal.Body
  302. defer raw.Close()
  303. file, err := os.Create(_dir)
  304. defer file.Close()
  305. writer := bufio.NewWriter(file)
  306. defer writer.Flush()
  307. body, err := ioutil.ReadAll(raw)
  308. writer.Write(body)
  309. if err != nil {
  310. return ""
  311. }
  312. var sw *Seaweed
  313. var filer []string
  314. if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
  315. filer = []string{_filer}
  316. }
  317. sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
  318. //_, fID, err := sw.Upload(retVal.Body, "tmp.pdf", int64(len(pdfByte)), "", "")
  319. _, _, fID, err := sw.UploadFile(_dir, "", "")
  320. retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
  321. os.Remove(_dir)
  322. fmt.Println("==retDocUrl==", retDocUrl)
  323. return retDocUrl
  324. }