client.go 11 KB

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