client.go 11 KB

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