ActivitiService.go 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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://localhost: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. func (this *ActivitiService) TaskComplete(ActiComplete ActiCompleteVM) string {
  119. jsonParams, err := json.Marshal(ActiComplete)
  120. if err != nil {
  121. fmt.Println(err, "生成json字符串错误")
  122. }
  123. params := string(jsonParams)
  124. fmt.Println(params)
  125. //token = Authorization(this.Username, this.Password)
  126. retVal := this.Post("/task-complete", params, "")
  127. p, _ := ioutil.ReadAll(retVal.Body)
  128. return string(p)
  129. }
  130. /*func (this *ActivitiService) MultiTaskComplete(processKey string, formEntityId string, userNames string, multiOrgAudits []MultiOrgAuditVM, userId string, result string, remarks string, callbackUrl string) string {
  131. var ActiComplete MultiActiCompleteVM
  132. ActiComplete.ProcessKey = processKey
  133. ActiComplete.BusinessKey = formEntityId
  134. ActiComplete.UserNames = userNames
  135. ActiComplete.MultiOrgAudits = multiOrgAudits
  136. ActiComplete.UserId = userId
  137. ActiComplete.Result = result
  138. ActiComplete.Remarks = remarks
  139. ActiComplete.CallbackUrl = callbackUrl*/
  140. func (this *ActivitiService) MultiTaskComplete(ActiComplete MultiActiCompleteVM) string {
  141. jsonParams, err := json.Marshal(ActiComplete)
  142. if err != nil {
  143. fmt.Println(err, "生成json字符串错误")
  144. }
  145. params := string(jsonParams)
  146. fmt.Println(params)
  147. //token = Authorization(this.Username, this.Password)
  148. retVal := this.Post("/multi-task-complete", params, "")
  149. p, _ := ioutil.ReadAll(retVal.Body)
  150. return string(p)
  151. }
  152. func (this *ActivitiService) GetMyTasks(processKey string, userId string) string {
  153. var ActiMyTasks ActiMyTasksVM
  154. ActiMyTasks.ProcessKey = processKey
  155. ActiMyTasks.UserId = userId
  156. json, err := json.Marshal(ActiMyTasks)
  157. if err != nil {
  158. fmt.Println(err, "生成json字符串错误")
  159. }
  160. params := string(json)
  161. fmt.Println(params)
  162. //token = Authorization(this.Username, this.Password)
  163. retVal := this.Post("/my-tasks", params, "")
  164. p, _ := ioutil.ReadAll(retVal.Body)
  165. return string(p)
  166. }
  167. func (this *ActivitiService) GetHistoricTasks(processKey string, businessKey string, processInstanceId string) []ActiHistoricTask {
  168. var ActiProcess ActiProcessVM
  169. ActiProcess.ProcessKey = processKey
  170. ActiProcess.BusinessKey = businessKey
  171. ActiProcess.ProcessInstanceId = processInstanceId
  172. jsonParam, err := json.Marshal(ActiProcess)
  173. if err != nil {
  174. fmt.Println(err, "生成json字符串错误")
  175. }
  176. params := string(jsonParam)
  177. fmt.Println(params)
  178. //token = Authorization(this.Username, this.Password)
  179. retVal := this.Post("/historic-tasks", params, "")
  180. jsonblob, _ := ioutil.ReadAll(retVal.Body)
  181. // var historicTasks []ActiHistoricTask
  182. historicTasks := make([]ActiHistoricTask, 0)
  183. json.Unmarshal(jsonblob, &historicTasks)
  184. return historicTasks
  185. }
  186. func (this *ActivitiService) GetActivitiProccessImage(processInstanceId string) []byte {
  187. var ActiProcess ActiProcessVM
  188. ActiProcess.ProcessInstanceId = processInstanceId
  189. jsonParam, err := json.Marshal(ActiProcess)
  190. if err != nil {
  191. fmt.Println(err, "生成json字符串错误")
  192. }
  193. params := string(jsonParam)
  194. //token = Authorization(this.Username, this.Password)
  195. retVal := this.Post("/historic-tasks-image", params, "")
  196. imgByte, _ := ioutil.ReadAll(retVal.Body)
  197. return imgByte
  198. }
  199. func (this *ActivitiService) GetHistoryMyTasks(processKey string, userId string) string {
  200. var ActiMyTasks ActiMyTasksVM
  201. ActiMyTasks.ProcessKey = processKey
  202. ActiMyTasks.UserId = userId
  203. json, err := json.Marshal(ActiMyTasks)
  204. if err != nil {
  205. fmt.Println(err, "生成json字符串错误")
  206. }
  207. params := string(json)
  208. fmt.Println(params)
  209. //token = Authorization(this.Username, this.Password)
  210. retVal := this.Post("/my-tasks-finished", params, "")
  211. p, _ := ioutil.ReadAll(retVal.Body)
  212. return string(p)
  213. }
  214. func (this *ActivitiService) ExcelToPdf(addressUrl string) string {
  215. var aposeVM AposeVM
  216. aposeVM.AddressUrl = addressUrl
  217. fmt.Println("==addressUrl===", addressUrl)
  218. jsonParam, err := json.Marshal(aposeVM)
  219. if err != nil {
  220. fmt.Println(err, "生成json字符串错误")
  221. }
  222. params := string(jsonParam)
  223. //token = Authorization(this.Username, this.Password)
  224. retVal := this.Post2("/excel-to-pdf", params, "")
  225. if retVal.StatusCode != 200 {
  226. return ""
  227. }
  228. //pdfByte, err := ioutil.ReadAll(retVal.Body)
  229. //ioutil.WriteFile("E:\aa.pdf", pdfByte, 777)
  230. //模板下载到服务器
  231. _dir := utils.Cfg.MustValue("file", "tmplateDir") + "pdf"
  232. utils.CreatePath(_dir)
  233. _dir += "/tmp_" + strconv.Itoa(int(time.Now().Unix())) + ".pdf"
  234. raw := retVal.Body
  235. defer raw.Close()
  236. file, err := os.Create(_dir)
  237. defer file.Close()
  238. writer := bufio.NewWriter(file)
  239. defer writer.Flush()
  240. body, err := ioutil.ReadAll(raw)
  241. writer.Write(body)
  242. if err != nil {
  243. return ""
  244. }
  245. var sw *Seaweed
  246. var filer []string
  247. if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
  248. filer = []string{_filer}
  249. }
  250. sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
  251. //_, fID, err := sw.Upload(retVal.Body, "tmp.pdf", int64(len(pdfByte)), "", "")
  252. _, _, fID, err := sw.UploadFile(_dir, "", "")
  253. retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
  254. os.Remove(_dir)
  255. fmt.Println("==retDocUrl==", retDocUrl)
  256. return retDocUrl
  257. }
  258. func (this *ActivitiService) FillWordTemplate(datas map[string]interface{}, templateUrl string, fileName string) string {
  259. var wordTempVM WordTemplateVM
  260. wordTempVM.Datas = datas
  261. wordTempVM.TemplateUrl = templateUrl
  262. wordTempVM.FileName = fileName
  263. fmt.Println("==templateUrl===", templateUrl)
  264. jsonParam, err := json.Marshal(wordTempVM)
  265. if err != nil {
  266. fmt.Println(err, "生成json字符串错误")
  267. }
  268. params := string(jsonParam)
  269. //token = Authorization(this.Username, this.Password)
  270. retVal := this.PostOrigin("/word/fill-word", params, "")
  271. if retVal.StatusCode != 200 {
  272. return ""
  273. }
  274. //pdfByte, err := ioutil.ReadAll(retVal.Body)
  275. //ioutil.WriteFile("E:\aa.pdf", pdfByte, 777)
  276. //模板下载到服务器
  277. _dir := utils.Cfg.MustValue("file", "tmplateDir") + "word"
  278. utils.CreatePath(_dir)
  279. _dir += "/tmp_" + strconv.Itoa(int(time.Now().Unix())) + ".docx"
  280. raw := retVal.Body
  281. defer raw.Close()
  282. file, err := os.Create(_dir)
  283. defer file.Close()
  284. writer := bufio.NewWriter(file)
  285. defer writer.Flush()
  286. body, err := ioutil.ReadAll(raw)
  287. writer.Write(body)
  288. if err != nil {
  289. return ""
  290. }
  291. var sw *Seaweed
  292. var filer []string
  293. if _filer := os.Getenv("GOSWFS_FILER_URL"); _filer != "" {
  294. filer = []string{_filer}
  295. }
  296. sw = NewSeaweed("http", utils.Cfg.MustValue("file", "upFileHost"), filer, 2*1024*1024, 5*time.Minute)
  297. //_, fID, err := sw.Upload(retVal.Body, "tmp.pdf", int64(len(pdfByte)), "", "")
  298. _, _, fID, err := sw.UploadFile(_dir, "", "")
  299. retDocUrl := utils.Cfg.MustValue("file", "downFileHost") + "/" + fID
  300. os.Remove(_dir)
  301. fmt.Println("==retDocUrl==", retDocUrl)
  302. return retDocUrl
  303. }