client.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. package wfclient
  2. import (
  3. "encoding/base64"
  4. "encoding/json"
  5. "errors"
  6. "fmt"
  7. "git.qianqiusoft.com/qianqiusoft/light-apiengine/utils"
  8. "net/http"
  9. "strconv"
  10. "strings"
  11. )
  12. type Filter struct {
  13. GroupOp string `json:"groupOp"`
  14. Rules []*FilterField `json:"rules"`
  15. }
  16. type FilterField struct {
  17. Field string `json:"field"`
  18. Op string `json:"op"`
  19. Data string `json:"data"`
  20. }
  21. type CallbackArg struct {
  22. DefineId string
  23. InstanceId string
  24. DefineName string
  25. FormData string
  26. Choice string
  27. Executor string
  28. UserId string
  29. }
  30. type WFClient struct {
  31. endpoint string
  32. authorization string
  33. userId string
  34. username string
  35. token string
  36. domain string
  37. callbackMap map[string]func(CallbackArg)
  38. }
  39. var instance *WFClient = nil
  40. /**
  41. * @brief: single instance
  42. */
  43. func Instance() *WFClient {
  44. if instance == nil {
  45. instance = &WFClient{}
  46. instance.endpoint = ""
  47. instance.authorization = ""
  48. instance.userId = ""
  49. instance.token = ""
  50. instance.callbackMap = make(map[string]func(CallbackArg))
  51. HttpClientInstance().setRequestInterseptor(instance.wfReqInterseptor)
  52. }
  53. return instance
  54. }
  55. /**
  56. * @brief: init
  57. * @param1 endpoint: endpoint of wf service, do not end with /; such as http://wf.qianqiusoft.com
  58. * @param2 userId: user's id
  59. * @param3 username: username
  60. * @param4 token: token
  61. * @param5 domain: domain, its default value is qianqiusoft.com
  62. * @return1: information of error, nil if everything is all right
  63. */
  64. func (w *WFClient) Init(endpoint, userId, username, token, domain string) error {
  65. if w.token == token && w.authorization != "" {
  66. fmt.Println("wfclient token", w.token, "does already exist, token is", w.authorization)
  67. return nil
  68. }
  69. var err error = nil
  70. w.endpoint = endpoint
  71. w.userId = userId
  72. w.authorization, err = w.createAuthorization(username, token, domain)
  73. w.token = token
  74. if err != nil {
  75. return err
  76. } else {
  77. return nil
  78. }
  79. }
  80. /**
  81. * @brief: add callback
  82. * @param1: key
  83. * @param2: callback
  84. */
  85. func (w *WFClient) AddCallback(key string, cb func(CallbackArg)) {
  86. if _, ok := w.callbackMap[key]; !ok {
  87. w.callbackMap[key] = cb
  88. } else {
  89. fmt.Println("callback", key, "does already exist")
  90. }
  91. }
  92. /**
  93. * @brief: create or update the define,
  94. * @param1 defineId: id of define, if the define with id does already exist, update the define
  95. * @param2 defineName: name of define
  96. * @param3 defineDesc: description of define
  97. * @param4 diagram: diagram of define
  98. * @param5 formName: name of the form
  99. * @return1 information of error
  100. */
  101. func (w *WFClient) CreateOrUpdateDefine(defineId, defineName, defineDesc, diagram, formName, tag, code string) ([]byte, error) {
  102. if defineId == "" {
  103. defineId = utils.NewUUID()
  104. }
  105. url := w.getFullUrl(fmt.Sprintf("api/wf_define/%s", defineId))
  106. fmt.Println("----url:", url)
  107. params := make(map[string]string)
  108. params["define_id"] = defineId
  109. params["name"] = defineName
  110. params["descript"] = defineDesc
  111. params["data"] = diagram
  112. params["form"] = formName
  113. params["code"] = code
  114. params["tag"] = tag
  115. return HttpClientInstance().post(url, params, nil)
  116. }
  117. func (w *WFClient) FetchDefine(defineId string) ([]byte, error) {
  118. url := w.getFullUrl("api/wf_define/" + defineId)
  119. fmt.Println(url)
  120. return HttpClientInstance().get(url, nil, nil)
  121. }
  122. func (w *WFClient) FetchAllDefines() ([]byte, error) {
  123. url := w.getFullUrl("api/wf_define/all")
  124. return HttpClientInstance().get(url, nil, nil)
  125. }
  126. /**
  127. * @brief: get the flow define by tag
  128. * @param1 tag: tag of the define, split by ,
  129. * @return1: content of response
  130. * @return2: information of error, nil if everything is all right
  131. */
  132. func (w *WFClient) FetchDefinesByTag(tag string) ([]byte, error) {
  133. url := w.getFullUrl("api/wf_define/list/tag")
  134. params := make(map[string]string)
  135. params["tag"] = tag
  136. return HttpClientInstance().get(url, params, nil)
  137. }
  138. /**
  139. * @brief: get the flow design diagram
  140. * @param1 id: id of flow define
  141. * @return1: content of response
  142. * @return2: information of error, nil if everything is all right
  143. */
  144. func (w *WFClient) FetchDesignDiagram(defineId string) ([]byte, error) {
  145. url := w.getFullUrl(fmt.Sprintf("api/wf_define/designer/%s", defineId))
  146. return HttpClientInstance().get(url, nil, nil)
  147. }
  148. /**
  149. * @brief: create a wf instance
  150. * @param1 defineId: id of wf define
  151. * @param2 name: name of instance
  152. * @param3 formData: data to submit
  153. * @return1: content of response
  154. * @return2: error
  155. */
  156. func (w *WFClient) CreateInstance(defineId, name, formData string) ([]byte, error) {
  157. url := w.getFullUrl("api/wf_instance")
  158. params := make(map[string]string)
  159. params["define_id"] = defineId
  160. params["name"] = name
  161. params["form_data"] = formData
  162. return HttpClientInstance().post(url, params, nil)
  163. }
  164. /**
  165. * @brief: run the wf instance
  166. * @param1 instanceId: id of instance
  167. * @param2 userId: approver id
  168. * @param3 choice: choice
  169. * @param4 options: options
  170. * @return1 content of response
  171. * @return2 error
  172. */
  173. func (w *WFClient) Run(instanceId, userId, choice, options, nextStep string) ([]byte, error) {
  174. url := w.getFullUrl("api/wf_instance/run")
  175. params := make(map[string]string)
  176. params["instance_id"] = instanceId
  177. params["users"] = userId
  178. if choice != "" {
  179. params["choice"] = choice
  180. }
  181. if options != "" {
  182. params["opinion"] = options
  183. }
  184. if nextStep != "" {
  185. params["nextStep"] = nextStep
  186. }
  187. var RunRespInfo struct {
  188. DefineId string `json:"define_id"`
  189. InstanceId string `json:"instance_id"`
  190. DefineName string `json:"define_name"`
  191. Choice string `json:"choice"`
  192. FormData string `json:"form_data"`
  193. StepName string `json:"step_name"`
  194. ActorType string `json:"actor_type"`
  195. Executor string `json:"executor"`
  196. StartCallback string `json:"start_callback"`
  197. }
  198. bytess, err := HttpClientInstance().post(url, params, nil)
  199. if err != nil {
  200. return nil, err
  201. }
  202. err = json.Unmarshal(bytess, &RunRespInfo)
  203. if err != nil {
  204. return nil, err
  205. }
  206. fmt.Println("------------------------------------->121", RunRespInfo.StartCallback)
  207. callbacks := strings.Split(RunRespInfo.StartCallback, ",")
  208. for _, callbackKye := range callbacks {
  209. callback, ok := w.callbackMap[callbackKye]
  210. if ok {
  211. fmt.Println("------------------------------------->121")
  212. callback(CallbackArg{
  213. DefineId: RunRespInfo.DefineId,
  214. InstanceId: RunRespInfo.InstanceId,
  215. DefineName: RunRespInfo.DefineName,
  216. FormData: RunRespInfo.FormData,
  217. Choice: RunRespInfo.Choice,
  218. Executor: RunRespInfo.Executor,
  219. UserId: w.userId,
  220. })
  221. }
  222. }
  223. return []byte{}, nil
  224. }
  225. /**
  226. * @brief: pre the wf instance
  227. * @param1 instanceId: id of instance
  228. * @param2 choice: choice of step
  229. * @return1 content of response
  230. * @return2 error
  231. */
  232. func (w *WFClient) PreRun(instanceId, choice string) ([]byte, error) {
  233. url := w.getFullUrl("api/wf_instance/prerun")
  234. params := make(map[string]string)
  235. params["instance_id"] = instanceId
  236. params["choice"] = choice
  237. return HttpClientInstance().post(url, params, nil)
  238. }
  239. /**
  240. * @brief: fetch my instances
  241. * @param1 page: page num, start from 1
  242. * @param2 rows: count per page, its default value is 10000
  243. * @return1: content of response
  244. * @return2: information of error
  245. */
  246. func (w *WFClient) FetchMyInstances(page, rows int) ([]byte, error) {
  247. url := w.getFullUrl("/api/wf_instance/mime")
  248. params := make(map[string]string)
  249. params["page"] = strconv.FormatInt(int64(page), 10)
  250. params["pageSize"] = strconv.FormatInt(int64(rows), 10)
  251. return HttpClientInstance().get(url, params, nil)
  252. }
  253. /**
  254. * @brief: fetch current step of login user
  255. * @param1 instanceId: id of instance
  256. * @return1 content of response
  257. * @return2 error
  258. */
  259. func (w *WFClient) FetchCurrentStepByLoginUser(instanceId string) ([]byte, error) {
  260. url := w.getFullUrl("/api/wf_instance/user/current")
  261. params := make(map[string]string)
  262. params["instance_id"] = instanceId
  263. return HttpClientInstance().get(url, params, nil)
  264. }
  265. /**
  266. * @brief: fetch current step of instance
  267. * @param1 instanceId: id of instance
  268. * @return1 content of response
  269. * @return2 error
  270. */
  271. func (w *WFClient) FetchCurrentStep(instanceId string) ([]byte, error) {
  272. url := w.getFullUrl("/api/wf_instance/current")
  273. params := make(map[string]string)
  274. params["instance_id"] = instanceId
  275. return HttpClientInstance().get(url, params, nil)
  276. }
  277. /**
  278. * @brief: fetch my to do list
  279. * @param1 page: page num, start from 1
  280. * @param2 rows: count per page, its default value is 10000
  281. * @return1: content of response
  282. * @return2: information of error
  283. */
  284. func (w *WFClient) FetchToDoList(page, rows int) ([]byte, error) {
  285. url := w.getFullUrl("/api/wf_instance/todo")
  286. params := make(map[string]string)
  287. params["page"] = strconv.FormatInt(int64(page), 10)
  288. params["pageSize"] = strconv.FormatInt(int64(rows), 10)
  289. return HttpClientInstance().get(url, params, nil)
  290. }
  291. /**
  292. * @brief: fetch my done list
  293. * @param1 page: page num, start from 1
  294. * @param2 rows: count per page, its default value is 10000
  295. * @return1: content of response
  296. * @return2: information of error
  297. */
  298. func (w *WFClient) FetchDoneList(page, rows int) ([]byte, error) {
  299. url := w.getFullUrl("/api/wf_instance/done")
  300. params := make(map[string]string)
  301. params["page"] = strconv.FormatInt(int64(page), 10)
  302. params["pageSize"] = strconv.FormatInt(int64(rows), 10)
  303. return HttpClientInstance().get(url, params, nil)
  304. }
  305. func (w *WFClient) FetchWFINstances(page, rows int, filters, sidx, sord string) ([]byte, error) {
  306. url := w.getFullUrl("/api/wf_instance/list")
  307. fmt.Println("wf url: ", url)
  308. params := make(map[string]string)
  309. params["page"] = strconv.FormatInt(int64(page), 10)
  310. params["rows"] = strconv.FormatInt(int64(rows), 10)
  311. params["sidx"] = sidx
  312. params["sord"] = sord
  313. params["filters"] = filters
  314. return HttpClientInstance().get(url, params, nil)
  315. }
  316. /**
  317. * @brief: wf req interseptor
  318. * @param1 r: http req
  319. */
  320. func (w *WFClient) wfReqInterseptor(r *http.Request) {
  321. //r.Header.Add("Authorization", w.authorization)
  322. fmt.Println("header add token", w.token)
  323. r.Header.Add("token", w.token)
  324. }
  325. /**
  326. * @brief: create the authorization by username, token and domain
  327. * @param2 username: username
  328. * @param3 token: token
  329. * @param4 domain: domain, its default value is qianqiusoft.com
  330. * @return1 authorization, such as Bearer adfeadfsdfsdffds
  331. * @return2 information of error, nil if everything is all right
  332. */
  333. func (w *WFClient) createAuthorization(username, token, domain string) (string, error) {
  334. if username == "" || token == "" {
  335. return "", errors.New("username or token is empty")
  336. }
  337. if domain == "" {
  338. domain = "qianqiusoft.com"
  339. }
  340. w.username = username
  341. w.token = token
  342. w.domain = domain
  343. tstr := fmt.Sprintf("%s:%s:%s", username, token, domain)
  344. tbase64str := base64.StdEncoding.EncodeToString([]byte(tstr))
  345. finalStr := "sso-auth-token:" + tbase64str
  346. return "Bearer " + base64.StdEncoding.EncodeToString([]byte(finalStr)), nil
  347. }
  348. /**
  349. * @brief: get full url
  350. * @param1 path: api path, such as /api/wf_instance/done
  351. * @return1 url with query params
  352. */
  353. func (w *WFClient) getFullUrl(path string) string {
  354. path = strings.TrimLeft(path, "/")
  355. return fmt.Sprintf("%s/%s", w.endpoint, path)
  356. }
  357. //撤回
  358. func (w *WFClient) Recall(definedId string) ([]byte, error) {
  359. url := w.getFullUrl("/api/wf_instance/recall")
  360. params := make(map[string]string)
  361. params["instance_id"] = definedId
  362. return HttpClientInstance().post(url, params, nil)
  363. }