pass_through.go 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667
  1. package pass_through
  2. import (
  3. "git.sxidc.com/go-framework/baize/framework/core/api"
  4. "git.sxidc.com/go-framework/baize/framework/gateway"
  5. "git.sxidc.com/go-tools/utils/http_client"
  6. "git.sxidc.com/go-tools/utils/strutils"
  7. "net/http"
  8. )
  9. // PostRoute POST直传API
  10. // 参数:
  11. // - builder: 该网关API构建器
  12. // - params: 网关直通参数
  13. // - opts: 网关直通选项
  14. // 返回值: 无
  15. func PostRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  16. params.passThrough(builder, http.MethodPost, opts...)
  17. }
  18. // DeleteRoute DELETE直传API
  19. // 参数:
  20. // - builder: 该网关API构建器
  21. // - params: 网关直通参数
  22. // - opts: 网关直通选项
  23. // 返回值: 无
  24. func DeleteRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  25. params.passThrough(builder, http.MethodDelete, opts...)
  26. }
  27. // PutRoute PUT直传API
  28. // 参数:
  29. // - builder: 该网关API构建器
  30. // - params: 网关直通参数
  31. // - opts: 网关直通选项
  32. // 返回值: 无
  33. func PutRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  34. params.passThrough(builder, http.MethodPut, opts...)
  35. }
  36. // GetRoute GET直传API
  37. // 参数:
  38. // - builder: 该网关API构建器
  39. // - params: 网关直通参数
  40. // - opts: 网关直通选项
  41. // 返回值: 无
  42. func GetRoute(builder *gateway.Builder, params *Simple, opts ...Option) {
  43. params.passThrough(builder, http.MethodGet, opts...)
  44. }
  45. // PostRouteWithTenantIDAndUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
  46. // 参数:
  47. // - builder: 该网关API构建器
  48. // - params: 网关直通参数
  49. // - opts: 网关直通选项
  50. // 返回值: 无
  51. func PostRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  52. params.passThrough(builder, http.MethodPost, append(opts,
  53. WithTenantIDParamsName("tenantId"),
  54. WithUserIDParamsName("userId"))...)
  55. }
  56. // PostRouteWithTenantIDAndCreateUserID POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
  57. // 参数:
  58. // - builder: 该网关API构建器
  59. // - params: 网关直通参数
  60. // - opts: 网关直通选项
  61. // 返回值: 无
  62. func PostRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  63. params.passThrough(builder, http.MethodPost, append(opts,
  64. WithTenantIDParamsName("tenantId"),
  65. WithUserIDParamsName("createUserId"))...)
  66. }
  67. // DeleteRouteWithTenantIDAndUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
  68. // 参数:
  69. // - builder: 该网关API构建器
  70. // - params: 网关直通参数
  71. // - opts: 网关直通选项
  72. // 返回值: 无
  73. func DeleteRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  74. params.passThrough(builder, http.MethodDelete, append(opts,
  75. WithTenantIDParamsName("tenantId"),
  76. WithUserIDParamsName("userId"))...)
  77. }
  78. // DeleteRouteWithTenantIDAndDeleteUserID DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
  79. // 参数:
  80. // - builder: 该网关API构建器
  81. // - params: 网关直通参数
  82. // - opts: 网关直通选项
  83. // 返回值: 无
  84. func DeleteRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  85. params.passThrough(builder, http.MethodDelete, append(opts,
  86. WithTenantIDParamsName("tenantId"),
  87. WithUserIDParamsName("deleteUserId"))...)
  88. }
  89. // PutRouteWithTenantIDAndUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和userId
  90. // 参数:
  91. // - builder: 该网关API构建器
  92. // - params: 网关直通参数
  93. // - opts: 网关直通选项
  94. // 返回值: 无
  95. func PutRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  96. params.passThrough(builder, http.MethodPut, append(opts,
  97. WithTenantIDParamsName("tenantId"),
  98. WithUserIDParamsName("userId"))...)
  99. }
  100. // PutRouteWithTenantIDAndUpdateUserID PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
  101. // 参数:
  102. // - builder: 该网关API构建器
  103. // - params: 网关直通参数
  104. // - opts: 网关直通选项
  105. // 返回值: 无
  106. func PutRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  107. params.passThrough(builder, http.MethodPut, append(opts,
  108. WithTenantIDParamsName("tenantId"),
  109. WithUserIDParamsName("updateUserId"))...)
  110. }
  111. // PostRouteWithTenantIDAndUserInfo POST直传API,请求Body是JsonBody,且会添加租户ID,用户ID以及操作者用户名字段,字段名分别为tenantId,userId和operatorUserName
  112. // 参数:
  113. // - builder: 该网关API构建器
  114. // - params: 网关直通参数
  115. // - opts: 网关直通选项
  116. // 返回值: 无
  117. func PostRouteWithTenantIDAndUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  118. params.passThrough(builder, http.MethodPost, append(opts,
  119. WithTenantIDParamsName("tenantId"),
  120. WithUserIDParamsName("userId"),
  121. WithUserNameParamsName("operatorUserName"))...)
  122. }
  123. // PostRouteWithTenantIDAndCreateUserInfo POST直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  124. // 参数:
  125. // - builder: 该网关API构建器
  126. // - params: 网关直通参数
  127. // - opts: 网关直通选项
  128. // 返回值: 无
  129. func PostRouteWithTenantIDAndCreateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  130. params.passThrough(builder, http.MethodPost, append(opts,
  131. WithTenantIDParamsName("tenantId"),
  132. WithUserIDParamsName("createUserId"),
  133. WithUserNameParamsName("operatorUserName"))...)
  134. }
  135. // DeleteRouteWithTenantIDAndUserInfo DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  136. // 参数:
  137. // - builder: 该网关API构建器
  138. // - params: 网关直通参数
  139. // - opts: 网关直通选项
  140. // 返回值: 无
  141. func DeleteRouteWithTenantIDAndUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  142. params.passThrough(builder, http.MethodDelete, append(opts,
  143. WithTenantIDParamsName("tenantId"),
  144. WithUserIDParamsName("userId"),
  145. WithUserNameParamsName("operatorUserName"))...)
  146. }
  147. // DeleteRouteWithTenantIDAndDeleteUserInfo DELETE直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  148. // 参数:
  149. // - builder: 该网关API构建器
  150. // - params: 网关直通参数
  151. // - opts: 网关直通选项
  152. // 返回值: 无
  153. func DeleteRouteWithTenantIDAndDeleteUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  154. params.passThrough(builder, http.MethodDelete, append(opts,
  155. WithTenantIDParamsName("tenantId"),
  156. WithUserIDParamsName("deleteUserId"),
  157. WithUserNameParamsName("operatorUserName"))...)
  158. }
  159. // PutRouteWithTenantIDAndUserInfo PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  160. // 参数:
  161. // - builder: 该网关API构建器
  162. // - params: 网关直通参数
  163. // - opts: 网关直通选项
  164. // 返回值: 无
  165. func PutRouteWithTenantIDAndUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  166. params.passThrough(builder, http.MethodPut, append(opts,
  167. WithTenantIDParamsName("tenantId"),
  168. WithUserIDParamsName("userId"),
  169. WithUserNameParamsName("operatorUserName"))...)
  170. }
  171. // PutRouteWithTenantIDAndUpdateUserInfo PUT直传API,请求Body是JsonBody,且会添加租户ID和用户ID字段,字段名分别为为tenantId,userId和operatorUserName
  172. // 参数:
  173. // - builder: 该网关API构建器
  174. // - params: 网关直通参数
  175. // - opts: 网关直通选项
  176. // 返回值: 无
  177. func PutRouteWithTenantIDAndUpdateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  178. params.passThrough(builder, http.MethodPut, append(opts,
  179. WithTenantIDParamsName("tenantId"),
  180. WithUserIDParamsName("updateUserId"),
  181. WithUserNameParamsName("operatorUserName"))...)
  182. }
  183. // GetRouteWithTenantIDAndUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和userId
  184. // 参数:
  185. // - builder: 该网关API构建器
  186. // - params: 网关直通参数
  187. // - opts: 网关直通选项
  188. // 返回值: 无
  189. func GetRouteWithTenantIDAndUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  190. params.passThrough(builder, http.MethodGet, append(opts,
  191. WithTenantIDParamsName("tenantId"),
  192. WithUserIDParamsName("userId"))...)
  193. }
  194. // GetRouteWithTenantIDAndCreateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和createUserId
  195. // 参数:
  196. // - builder: 该网关API构建器
  197. // - params: 网关直通参数
  198. // - opts: 网关直通选项
  199. // 返回值: 无
  200. func GetRouteWithTenantIDAndCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  201. params.passThrough(builder, http.MethodGet, append(opts,
  202. WithTenantIDParamsName("tenantId"),
  203. WithUserIDParamsName("createUserId"))...)
  204. }
  205. // GetRouteWithTenantIDAndDeleteUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和deleteUserId
  206. // 参数:
  207. // - builder: 该网关API构建器
  208. // - params: 网关直通参数
  209. // - opts: 网关直通选项
  210. // 返回值: 无
  211. func GetRouteWithTenantIDAndDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  212. params.passThrough(builder, http.MethodGet, append(opts,
  213. WithTenantIDParamsName("tenantId"),
  214. WithUserIDParamsName("deleteUserId"))...)
  215. }
  216. // GetRouteWithTenantIDAndUpdateUserID GET直传API,会在查询参数添加租户ID和用户ID字段,字段名分别为tenantId和updateUserId
  217. // 参数:
  218. // - builder: 该网关API构建器
  219. // - params: 网关直通参数
  220. // - opts: 网关直通选项
  221. // 返回值: 无
  222. func GetRouteWithTenantIDAndUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  223. params.passThrough(builder, http.MethodGet, append(opts,
  224. WithTenantIDParamsName("tenantId"),
  225. WithUserIDParamsName("updateUserId"))...)
  226. }
  227. // PostRouteWithTenantID POST直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
  228. // 参数:
  229. // - builder: 该网关API构建器
  230. // - params: 网关直通参数
  231. // - opts: 网关直通选项
  232. // 返回值: 无
  233. func PostRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  234. params.passThrough(builder, http.MethodPost, append(opts,
  235. WithTenantIDParamsName("tenantId"))...)
  236. }
  237. // DeleteRouteWithTenantID DELETE直传API,会在查询参数添加租户ID字段,字段名分别为tenantId
  238. // 参数:
  239. // - builder: 该网关API构建器
  240. // - params: 网关直通参数
  241. // - opts: 网关直通选项
  242. // 返回值: 无
  243. func DeleteRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  244. params.passThrough(builder, http.MethodDelete, append(opts,
  245. WithTenantIDParamsName("tenantId"))...)
  246. }
  247. // PutRouteWithTenantID PUT直传API,请求Body是JsonBody,且会添加租户ID字段,字段名分别为tenantId
  248. // 参数:
  249. // - builder: 该网关API构建器
  250. // - params: 网关直通参数
  251. // - opts: 网关直通选项
  252. // 返回值: 无
  253. func PutRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  254. params.passThrough(builder, http.MethodPut, append(opts,
  255. WithTenantIDParamsName("tenantId"))...)
  256. }
  257. // GetRouteWithTenantID GET直传API,会在查询参数添加租户ID字段,字段名分别为tenantId
  258. // 参数:
  259. // - builder: 该网关API构建器
  260. // - params: 网关直通参数
  261. // - opts: 网关直通选项
  262. // 返回值: 无
  263. func GetRouteWithTenantID(builder *gateway.Builder, params *Simple, opts ...Option) {
  264. params.passThrough(builder, http.MethodGet, append(opts,
  265. WithTenantIDParamsName("tenantId"))...)
  266. }
  267. // PostRouteWithUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为userId
  268. // 参数:
  269. // - builder: 该网关API构建器
  270. // - params: 网关直通参数
  271. // - opts: 网关直通选项
  272. // 返回值: 无
  273. func PostRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  274. params.passThrough(builder, http.MethodPost, append(opts,
  275. WithUserIDParamsName("userId"))...)
  276. }
  277. // PostRouteWithCreateUserID POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId
  278. // 参数:
  279. // - builder: 该网关API构建器
  280. // - params: 网关直通参数
  281. // - opts: 网关直通选项
  282. // 返回值: 无
  283. func PostRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  284. params.passThrough(builder, http.MethodPost, append(opts,
  285. WithUserIDParamsName("createUserId"))...)
  286. }
  287. // DeleteRouteWithUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为userId
  288. // 参数:
  289. // - builder: 该网关API构建器
  290. // - params: 网关直通参数
  291. // - opts: 网关直通选项
  292. // 返回值: 无
  293. func DeleteRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  294. params.passThrough(builder, http.MethodDelete, append(opts,
  295. WithUserIDParamsName("userId"))...)
  296. }
  297. // DeleteRouteWithDeleteUserID DELETE直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
  298. // 参数:
  299. // - builder: 该网关API构建器
  300. // - params: 网关直通参数
  301. // - opts: 网关直通选项
  302. // 返回值: 无
  303. func DeleteRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  304. params.passThrough(builder, http.MethodDelete, append(opts,
  305. WithUserIDParamsName("deleteUserId"))...)
  306. }
  307. // PutRouteWithUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId
  308. // 参数:
  309. // - builder: 该网关API构建器
  310. // - params: 网关直通参数
  311. // - opts: 网关直通选项
  312. // 返回值: 无
  313. func PutRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  314. params.passThrough(builder, http.MethodPut, append(opts,
  315. WithUserIDParamsName("userId"))...)
  316. }
  317. // PutRouteWithUpdateUserID PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId
  318. // 参数:
  319. // - builder: 该网关API构建器
  320. // - params: 网关直通参数
  321. // - opts: 网关直通选项
  322. // 返回值: 无
  323. func PutRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  324. params.passThrough(builder, http.MethodPut, append(opts,
  325. WithUserIDParamsName("updateUserId"))...)
  326. }
  327. // PostRouteWithUserInfo POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为userId,还会添加操作者用户名,字段名为operatorUserName
  328. // 参数:
  329. // - builder: 该网关API构建器
  330. // - params: 网关直通参数
  331. // - opts: 网关直通选项
  332. // 返回值: 无
  333. func PostRouteWithUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  334. params.passThrough(builder, http.MethodPost, append(opts,
  335. WithUserIDParamsName("userId"),
  336. WithUserNameParamsName("operatorUserName"))...)
  337. }
  338. // PostRouteWithCreateUserInfo POST直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId,还会添加操作者用户名,字段名为operatorUserName
  339. // 参数:
  340. // - builder: 该网关API构建器
  341. // - params: 网关直通参数
  342. // - opts: 网关直通选项
  343. // 返回值: 无
  344. func PostRouteWithCreateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  345. params.passThrough(builder, http.MethodPost, append(opts,
  346. WithUserIDParamsName("createUserId"),
  347. WithUserNameParamsName("operatorUserName"))...)
  348. }
  349. // DeleteRouteWithUserInfo DELETE直传API,会在查询参数添加用户ID字段,字段名分别为userId,还会添加操作者用户名,字段名为operatorUserName
  350. // 参数:
  351. // - builder: 该网关API构建器
  352. // - params: 网关直通参数
  353. // - opts: 网关直通选项
  354. // 返回值: 无
  355. func DeleteRouteWithUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  356. params.passThrough(builder, http.MethodDelete, append(opts,
  357. WithUserIDParamsName("userId"),
  358. WithUserNameParamsName("operatorUserName"))...)
  359. }
  360. // DeleteRouteWithDeleteUserInfo DELETE直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId,还会添加操作者用户名,字段名为operatorUserName
  361. // 参数:
  362. // - builder: 该网关API构建器
  363. // - params: 网关直通参数
  364. // - opts: 网关直通选项
  365. // 返回值: 无
  366. func DeleteRouteWithDeleteUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  367. params.passThrough(builder, http.MethodDelete, append(opts,
  368. WithUserIDParamsName("deleteUserId"),
  369. WithUserNameParamsName("operatorUserName"))...)
  370. }
  371. // PutRouteWithUserInfo PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为createUserId,还会添加操作者用户名,字段名为operatorUserName
  372. // 参数:
  373. // - builder: 该网关API构建器
  374. // - params: 网关直通参数
  375. // - opts: 网关直通选项
  376. // 返回值: 无
  377. func PutRouteWithUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  378. params.passThrough(builder, http.MethodPut, append(opts,
  379. WithUserIDParamsName("userId"),
  380. WithUserNameParamsName("operatorUserName"))...)
  381. }
  382. // PutRouteWithUpdateUserInfo PUT直传API,请求Body是JsonBody,且会添加用户ID字段,字段名分别为updateUserId,还会添加操作者用户名,字段名为operatorUserName
  383. // 参数:
  384. // - builder: 该网关API构建器
  385. // - params: 网关直通参数
  386. // - opts: 网关直通选项
  387. // 返回值: 无
  388. func PutRouteWithUpdateUserInfo(builder *gateway.Builder, params *Simple, opts ...Option) {
  389. params.passThrough(builder, http.MethodPut, append(opts,
  390. WithUserIDParamsName("updateUserId"),
  391. WithUserNameParamsName("operatorUserName"))...)
  392. }
  393. // GetRouteWithUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为userId
  394. // 参数:
  395. // - builder: 该网关API构建器
  396. // - params: 网关直通参数
  397. // - opts: 网关直通选项
  398. // 返回值: 无
  399. func GetRouteWithUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  400. params.passThrough(builder, http.MethodGet, append(opts,
  401. WithUserIDParamsName("userId"))...)
  402. }
  403. // GetRouteWithCreateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为createUserId
  404. // 参数:
  405. // - builder: 该网关API构建器
  406. // - params: 网关直通参数
  407. // - opts: 网关直通选项
  408. // 返回值: 无
  409. func GetRouteWithCreateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  410. params.passThrough(builder, http.MethodGet, append(opts,
  411. WithUserIDParamsName("createUserId"))...)
  412. }
  413. // GetRouteWithDeleteUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为deleteUserId
  414. // 参数:
  415. // - builder: 该网关API构建器
  416. // - params: 网关直通参数
  417. // - opts: 网关直通选项
  418. // 返回值: 无
  419. func GetRouteWithDeleteUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  420. params.passThrough(builder, http.MethodGet, append(opts,
  421. WithUserIDParamsName("deleteUserId"))...)
  422. }
  423. // GetRouteWithUpdateUserID GET直传API,会在查询参数添加用户ID字段,字段名分别为updateUserId
  424. // 参数:
  425. // - builder: 该网关API构建器
  426. // - params: 网关直通参数
  427. // - opts: 网关直通选项
  428. // 返回值: 无
  429. func GetRouteWithUpdateUserID(builder *gateway.Builder, params *Simple, opts ...Option) {
  430. params.passThrough(builder, http.MethodGet, append(opts,
  431. WithUserIDParamsName("updateUserId"))...)
  432. }
  433. // Simple 参数
  434. type Simple struct {
  435. // RelativePath 网关开放API的RelativePath
  436. RelativePath string
  437. // 服务的URL
  438. ServiceUrl string
  439. }
  440. func (params *Simple) passThrough(builder *gateway.Builder, httpMethod string, opts ...Option) {
  441. options := new(Options)
  442. for _, opt := range opts {
  443. opt(options)
  444. }
  445. builder.AddRoute(httpMethod, params.RelativePath,
  446. func(requestBuilder *gateway.RequestBuilder) {
  447. if options.responseSuccessCallback != nil {
  448. requestBuilder.ResponseSuccessCallback(func(c *api.Context, historyRequests []gateway.Request, resultMap map[string]any) {
  449. err := options.responseSuccessCallback(c, historyRequests, resultMap)
  450. if err != nil {
  451. requestBuilder.ResponseError(err)
  452. return
  453. }
  454. })
  455. }
  456. if options.responseErrorCallback != nil {
  457. requestBuilder.ResponseErrorCallback(func(c *api.Context, historyRequests []gateway.Request, resultMap map[string]any, err error) {
  458. retErr := options.responseErrorCallback(c, historyRequests, resultMap, err)
  459. if retErr != nil {
  460. requestBuilder.ResponseError(retErr)
  461. return
  462. }
  463. })
  464. }
  465. if strutils.IsStringNotEmpty(options.tenantIDParamsName) ||
  466. strutils.IsStringNotEmpty(options.userIDParamsName) {
  467. if httpMethod == http.MethodPost || httpMethod == http.MethodPut {
  468. err := gateway.AddJsonBodyTenantIDAndUserInfo(requestBuilder, options.tenantIDParamsName, options.userIDParamsName, options.userNameParamsName)
  469. if err != nil {
  470. requestBuilder.ResponseError(err)
  471. return
  472. }
  473. }
  474. if httpMethod == http.MethodDelete || httpMethod == http.MethodGet {
  475. err := gateway.AddQueryParamsTenantIDAndUserInfo(requestBuilder, options.tenantIDParamsName, options.userIDParamsName, options.userNameParamsName)
  476. if err != nil {
  477. requestBuilder.ResponseError(err)
  478. return
  479. }
  480. }
  481. }
  482. requestOptions := make([]gateway.RequestOption, 0)
  483. if options.beforeRequestCallback != nil {
  484. requestOptions = append(requestOptions, gateway.WithBeforeRequestCallback(
  485. func(c *api.Context, historyRequests []gateway.Request, resultMap map[string]any) error {
  486. err := options.beforeRequestCallback(c, historyRequests, resultMap)
  487. if err != nil {
  488. return err
  489. }
  490. return nil
  491. }))
  492. }
  493. if options.afterRequestCallback != nil {
  494. requestOptions = append(requestOptions, gateway.WithRequestResponseCallback(
  495. func(c *api.Context, historyRequests []gateway.Request, resultMap map[string]any, response *http_client.Response) error {
  496. err := options.afterRequestCallback(c, historyRequests, resultMap)
  497. if err != nil {
  498. return err
  499. }
  500. return nil
  501. }))
  502. }
  503. switch httpMethod {
  504. case http.MethodPost:
  505. requestBuilder.Post(&gateway.PostRequest{
  506. Url: params.ServiceUrl,
  507. }, requestOptions...)
  508. case http.MethodDelete:
  509. requestBuilder.Delete(&gateway.DeleteRequest{
  510. Url: params.ServiceUrl,
  511. }, requestOptions...)
  512. case http.MethodPut:
  513. requestBuilder.Put(&gateway.PutRequest{
  514. Url: params.ServiceUrl,
  515. }, requestOptions...)
  516. case http.MethodGet:
  517. requestBuilder.Get(&gateway.GetRequest{
  518. Url: params.ServiceUrl,
  519. }, requestOptions...)
  520. default:
  521. panic("不支持的请求类型")
  522. }
  523. requestBuilder.Request()
  524. }, options.middlewares...)
  525. }
  526. type RequestBuilderCallback func(c *api.Context, historyRequests []gateway.Request, resultMap map[string]any) error
  527. type RequestBuilderErrCallback func(c *api.Context, historyRequests []gateway.Request, resultMap map[string]any, err error) error
  528. type Option func(options *Options)
  529. type Options struct {
  530. // tenantIDParamsName 租户ID请求参数名称
  531. tenantIDParamsName string
  532. // userIDParamsName 用户ID请求参数名称
  533. userIDParamsName string
  534. // userNameParamsName 用户名请求参数名称
  535. userNameParamsName string
  536. // beforeRequestCallback 请求前回调
  537. beforeRequestCallback RequestBuilderCallback
  538. // afterRequestCallback 请求后回调
  539. afterRequestCallback RequestBuilderCallback
  540. // responseSuccessCallback 成功响应回调
  541. responseSuccessCallback RequestBuilderCallback
  542. // responseErrorCallback 失败响应回调
  543. responseErrorCallback RequestBuilderErrCallback
  544. // 中间件
  545. middlewares []gateway.Handler
  546. }
  547. // WithTenantIDParamsName 设置请求参数中的租户ID参数的名称
  548. func WithTenantIDParamsName(tenantIDParamsName string) Option {
  549. return func(options *Options) {
  550. options.tenantIDParamsName = tenantIDParamsName
  551. }
  552. }
  553. // WithUserIDParamsName 设置请求参数中的用户ID参数的名称
  554. func WithUserIDParamsName(userIDParamsName string) Option {
  555. return func(options *Options) {
  556. options.userIDParamsName = userIDParamsName
  557. }
  558. }
  559. // WithUserNameParamsName 设置请求参数中的用户名参数的名称
  560. func WithUserNameParamsName(userNameParamsName string) Option {
  561. return func(options *Options) {
  562. options.userNameParamsName = userNameParamsName
  563. }
  564. }
  565. // WithBeforeRequestCallback 设置请求前回调
  566. func WithBeforeRequestCallback(callback RequestBuilderCallback) Option {
  567. return func(options *Options) {
  568. options.beforeRequestCallback = callback
  569. }
  570. }
  571. // WithAfterRequestCallback 设置请求后回调
  572. func WithAfterRequestCallback(callback RequestBuilderCallback) Option {
  573. return func(options *Options) {
  574. options.afterRequestCallback = callback
  575. }
  576. }
  577. // WithResponseSuccessCallback 设置成功响应回调,默认回调会将服务响应作为网关API的响应返回
  578. func WithResponseSuccessCallback(callback RequestBuilderCallback) Option {
  579. return func(options *Options) {
  580. options.responseSuccessCallback = callback
  581. }
  582. }
  583. // WithResponseErrorCallback 设置失败响应回调,默认回调会按照是否存在错误返回MsgResponse
  584. func WithResponseErrorCallback(callback RequestBuilderErrCallback) Option {
  585. return func(options *Options) {
  586. options.responseErrorCallback = callback
  587. }
  588. }
  589. // WithMiddlewares 设置中间件
  590. func WithMiddlewares(middlewares ...gateway.Handler) Option {
  591. return func(options *Options) {
  592. options.middlewares = middlewares
  593. }
  594. }