client.go 13 KB

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