client.go 13 KB

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