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/utils"
  9. "net/http"
  10. "strconv"
  11. "strings"
  12. )
  13. type Filter struct {
  14. GroupOp string `json:"groupOp"`
  15. Rules []*FilterField `json:"rules"`
  16. }
  17. type FilterField struct {
  18. Field string `json:"field"`
  19. Op string `json:"op"`
  20. Data string `json:"data"`
  21. }
  22. type CallbackArg struct {
  23. DefineId string
  24. InstanceId string
  25. DefineName string
  26. FormData string
  27. Choice string
  28. Executor string
  29. UserId string
  30. }
  31. type WFClient struct {
  32. endpoint string
  33. authorization string
  34. userId string
  35. username string
  36. token string
  37. domain string
  38. callbackMap map[string]func(CallbackArg)
  39. }
  40. var instance *WFClient = nil
  41. /**
  42. * @brief: single instance
  43. */
  44. func Instance() *WFClient {
  45. if instance == nil {
  46. instance = &WFClient{}
  47. instance.endpoint = ""
  48. instance.authorization = ""
  49. instance.userId = ""
  50. instance.token = ""
  51. instance.callbackMap = make(map[string]func(CallbackArg))
  52. HttpClientInstance().setRequestInterseptor(instance.wfReqInterseptor)
  53. }
  54. return instance
  55. }
  56. /**
  57. * @brief: init
  58. * @param1 endpoint: endpoint of wf service, do not end with /; such as http://wf.qianqiusoft.com
  59. * @param2 userId: user's id
  60. * @param3 username: username
  61. * @param4 token: token
  62. * @param5 domain: domain, its default value is qianqiusoft.com
  63. * @return1: information of error, nil if everything is all right
  64. */
  65. func (w *WFClient) Init(endpoint, userId, username, token, domain string) error {
  66. if w.token == token && w.authorization != "" {
  67. fmt.Println("wfclient token", w.token, "does already exist, token is", w.authorization)
  68. return nil
  69. }
  70. var err error = nil
  71. w.endpoint = endpoint
  72. w.userId = userId
  73. w.authorization, err = w.createAuthorization(username, token, domain)
  74. w.token = token
  75. if err != nil {
  76. return err
  77. } else {
  78. return nil
  79. }
  80. }
  81. /**
  82. * @brief: add callback
  83. * @param1: key
  84. * @param2: callback
  85. */
  86. func (w *WFClient) AddCallback(key string, cb func(CallbackArg)) {
  87. if _, ok := w.callbackMap[key]; !ok {
  88. w.callbackMap[key] = cb
  89. } else {
  90. fmt.Println("callback", key, "does already exist")
  91. }
  92. }
  93. /**
  94. * @brief: create or update the define,
  95. * @param1 defineId: id of define, if the define with id does already exist, update the define
  96. * @param2 defineName: name of define
  97. * @param3 defineDesc: description of define
  98. * @param4 diagram: diagram of define
  99. * @param5 formName: name of the form
  100. * @return1 information of error
  101. */
  102. func (w *WFClient) CreateOrUpdateDefine(defineId, defineName, defineDesc, diagram, formName, tag, code string) ([]byte, error) {
  103. if defineId == "" {
  104. defineId = utils.NewUUID()
  105. }
  106. url := w.getFullUrl(fmt.Sprintf("api/wf_define/%s", defineId))
  107. fmt.Println("----url:", url)
  108. params := make(map[string]string)
  109. params["define_id"] = defineId
  110. params["name"] = defineName
  111. params["descript"] = defineDesc
  112. params["data"] = diagram
  113. params["form"] = formName
  114. params["code"] = code
  115. params["tag"] = tag
  116. return HttpClientInstance().post(url, params, nil)
  117. }
  118. func (w *WFClient) FetchDefine(defineId string) ([]byte, error) {
  119. url := w.getFullUrl("api/wf_define/" + defineId)
  120. fmt.Println(url)
  121. return HttpClientInstance().get(url, nil, nil)
  122. }
  123. func (w *WFClient) FetchAllDefines() ([]byte, error) {
  124. url := w.getFullUrl("api/wf_define/all")
  125. return HttpClientInstance().get(url, nil, nil)
  126. }
  127. /**
  128. * @brief: get the flow define by tag
  129. * @param1 tag: tag of the define, split by ,
  130. * @return1: content of response
  131. * @return2: information of error, nil if everything is all right
  132. */
  133. func (w *WFClient) FetchDefinesByTag(tag string) ([]byte, error) {
  134. url := w.getFullUrl("api/wf_define/list/tag")
  135. params := make(map[string]string)
  136. params["tag"] = tag
  137. return HttpClientInstance().get(url, params, nil)
  138. }
  139. /**
  140. * @brief: get the flow design diagram
  141. * @param1 id: id of flow define
  142. * @return1: content of response
  143. * @return2: information of error, nil if everything is all right
  144. */
  145. func (w *WFClient) FetchDesignDiagram(defineId string) ([]byte, error) {
  146. url := w.getFullUrl(fmt.Sprintf("api/wf_define/designer/%s", defineId))
  147. return HttpClientInstance().get(url, nil, nil)
  148. }
  149. /**
  150. * @brief: create a wf instance
  151. * @param1 defineId: id of wf define
  152. * @param2 name: name of instance
  153. * @param3 formData: data to submit
  154. * @return1: content of response
  155. * @return2: error
  156. */
  157. func (w *WFClient) CreateInstance(defineId, name, formData string) ([]byte, error) {
  158. url := w.getFullUrl("api/wf_instance")
  159. params := make(map[string]string)
  160. params["define_id"] = defineId
  161. params["name"] = name
  162. params["form_data"] = formData
  163. return HttpClientInstance().post(url, params, nil)
  164. }
  165. type Choice struct {
  166. XMLName xml.Name `xml:"choice"json:"-"`
  167. Name string `json:"name"xml:"name"`
  168. Trans Transition `json:"trans"xml:"transition"`
  169. }
  170. type Transition struct {
  171. XMLName xml.Name `xml:"transition"json:"-"`
  172. Name string `json:"name"xml:"name"`
  173. To string `json:"to"xml:"to"`
  174. Points string `json:"-"xml:"points"`
  175. Conditions []*Condition `json:"conditions,omitempty"`
  176. Callback string `json:"callback"xml:"callback"`
  177. }
  178. type Condition struct {
  179. Operator string `json:"operator"xml:"operator"`
  180. DataKey string `json:"data_key"xml:"data_key"`
  181. Value string `json:"value"xml:"value"`
  182. Logic string `json:"logic"xml:"logic"`
  183. }
  184. /**
  185. * @brief: run the wf instance
  186. * @param1 instanceId: id of instance
  187. * @param2 userId: approver id
  188. * @param3 choice: choice
  189. * @param4 options: options
  190. * @return1 content of response
  191. * @return2 error
  192. */
  193. func (w *WFClient) Run(instanceId, userId, choice, options, nextStep string) ([]byte, error) {
  194. url := w.getFullUrl("api/wf_instance/run")
  195. params := make(map[string]string)
  196. params["instance_id"] = instanceId
  197. params["users"] = userId
  198. if choice != "" {
  199. params["choice"] = choice
  200. }
  201. if options != "" {
  202. params["opinion"] = options
  203. }
  204. if nextStep != "" {
  205. params["nextStep"] = nextStep
  206. }
  207. var RunRespInfo struct {
  208. DefineId string `json:"define_id"`
  209. InstanceId string `json:"instance_id"`
  210. DefineName string `json:"define_name"`
  211. Choices []*Choice `json:"choices"`
  212. Transition []*Transition `json:"transition"`
  213. StepType string `json:"step_type"`
  214. FormData string `json:"form_data"`
  215. StepName string `json:"step_name"`
  216. ActorType string `json:"actor_type"`
  217. Executor string `json:"executor"`
  218. StartCallback string `json:"start_callback"`
  219. }
  220. bytess, err := HttpClientInstance().post(url, params, nil)
  221. if err != nil {
  222. return nil, err
  223. }
  224. fmt.Println("------------->>>>>--------", string(bytess))
  225. err = json.Unmarshal(bytess, &RunRespInfo)
  226. if err != nil {
  227. return nil, err
  228. }
  229. fmt.Println("------------------------------------->121", RunRespInfo.StartCallback)
  230. // 节点回调
  231. callbacks := strings.Split(RunRespInfo.StartCallback, ",")
  232. for _, callbackKye := range callbacks {
  233. callback, ok := w.callbackMap[callbackKye]
  234. if ok {
  235. fmt.Println("------------------------------------->121")
  236. callback(CallbackArg{
  237. DefineId: RunRespInfo.DefineId,
  238. InstanceId: RunRespInfo.InstanceId,
  239. DefineName: RunRespInfo.DefineName,
  240. FormData: RunRespInfo.FormData,
  241. Choice: choice,
  242. Executor: RunRespInfo.Executor,
  243. UserId: w.userId,
  244. })
  245. }
  246. }
  247. // 连线回调
  248. linkCallbacks := []string{}
  249. if RunRespInfo.StepType == "begin" || RunRespInfo.StepType == "normal" {
  250. for _, c := range RunRespInfo.Transition {
  251. if c.Name == choice {
  252. linkCallbacks = strings.Split(c.Callback, ",")
  253. }
  254. }
  255. } else {
  256. for _, c := range RunRespInfo.Choices {
  257. if c.Name == choice {
  258. linkCallbacks = strings.Split(c.Trans.Callback, ",")
  259. }
  260. }
  261. }
  262. for _, callbackKye := range linkCallbacks {
  263. callback, ok := w.callbackMap[callbackKye]
  264. if ok {
  265. callback(CallbackArg{
  266. DefineId: RunRespInfo.DefineId,
  267. InstanceId: RunRespInfo.InstanceId,
  268. DefineName: RunRespInfo.DefineName,
  269. FormData: RunRespInfo.FormData,
  270. Choice: choice,
  271. Executor: RunRespInfo.Executor,
  272. UserId: w.userId,
  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. }