main.go 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. package wallet
  2. import (
  3. "fmt"
  4. "github.com/guonaihong/gout"
  5. "gogs.uu.mdfitnesscao.com/cuiguohai/sdk"
  6. "gogs.uu.mdfitnesscao.com/cuiguohai/sdk/response"
  7. )
  8. const (
  9. WalletTypeMedicalReport = 1 // 报告解读
  10. WalletTypeArchives = 2 // 家庭档案
  11. )
  12. // 钱包消费
  13. func Consume(orderSn string, archivesId string, mechanismId string, walletType int64, quantity int64, remark string) (*sdk.BaseResponse[map[string]any], *response.ErrCode) {
  14. var resp *sdk.BaseResponse[map[string]any]
  15. resp, err := postReq[*sdk.BaseResponse[map[string]any]]("/walletService/openapi/member/card/balance", gout.H{
  16. "orderSn": orderSn,
  17. "archiveSn": archivesId,
  18. "mechanismId": mechanismId,
  19. "type": walletType,
  20. "quantity": quantity,
  21. "remark": remark,
  22. })
  23. if err != nil {
  24. return nil, &response.ErrCode{
  25. Code: response.PLATFORM_ERROR,
  26. Msg: err.Error(),
  27. }
  28. }
  29. if resp == nil {
  30. return nil, response.ErrPlatform
  31. }
  32. if resp.Code != 200 {
  33. return nil, &response.ErrCode{
  34. Code: resp.Code,
  35. Msg: resp.Message,
  36. }
  37. }
  38. return resp, nil
  39. }
  40. // 钱包消费
  41. func ConsumeRollback(orderSn string, remark string) (*sdk.BaseResponse[map[string]any], *response.ErrCode) {
  42. var resp *sdk.BaseResponse[map[string]any]
  43. resp, err := postReq[*sdk.BaseResponse[map[string]any]]("/walletService/openapi/member/card/balance/rollback", gout.H{
  44. "orderSn": orderSn,
  45. "remark": remark,
  46. })
  47. if err != nil {
  48. return nil, &response.ErrCode{
  49. Code: response.PLATFORM_ERROR,
  50. Msg: err.Error(),
  51. }
  52. }
  53. if resp == nil {
  54. return nil, response.ErrPlatform
  55. }
  56. if resp.Code != 200 {
  57. return nil, &response.ErrCode{
  58. Code: resp.Code,
  59. Msg: resp.Message,
  60. }
  61. }
  62. return resp, nil
  63. }
  64. // 注册钱包
  65. func Regist(archivesId string) (*sdk.BaseResponse[map[string]any], *response.ErrCode) {
  66. var resp *sdk.BaseResponse[map[string]any]
  67. resp, err := postReq[*sdk.BaseResponse[map[string]any]]("/walletService/openapi/event/member/register", gout.H{
  68. "archiveSn": archivesId,
  69. })
  70. if err != nil {
  71. return nil, &response.ErrCode{
  72. Code: response.PLATFORM_ERROR,
  73. Msg: err.Error(),
  74. }
  75. }
  76. if resp == nil {
  77. return nil, response.ErrPlatform
  78. }
  79. if resp.Code != 200 {
  80. return nil, &response.ErrCode{
  81. Code: resp.Code,
  82. Msg: resp.Message,
  83. }
  84. }
  85. return resp, nil
  86. }
  87. // 检查是否已授权钱包
  88. func CheckMechanismAuthorize(mechanismId string, walletType int64) (*sdk.BaseResponse[bool], *response.ErrCode) {
  89. var resp *sdk.BaseResponse[bool]
  90. resp, err := getReq[*sdk.BaseResponse[bool]]("/walletService/openapi/walletType/mechanism", gout.H{
  91. "mechanismId": mechanismId,
  92. "walletTypeId": walletType,
  93. })
  94. if err != nil {
  95. return nil, &response.ErrCode{
  96. Code: response.PLATFORM_ERROR,
  97. Msg: err.Error(),
  98. }
  99. }
  100. if resp == nil {
  101. return nil, response.ErrPlatform
  102. }
  103. if resp.Code != 200 {
  104. return nil, &response.ErrCode{
  105. Code: resp.Code,
  106. Msg: resp.Message,
  107. }
  108. }
  109. return resp, nil
  110. }
  111. // 钱包消费
  112. func MemberCardBind(
  113. cardId int64, archivesId string, channelName string, price int64, payType string, outTradeNo string, remark string, mechanismId string,
  114. ) (*sdk.BaseResponse[map[string]any], *response.ErrCode) {
  115. var resp *sdk.BaseResponse[map[string]any]
  116. resp, err := postReq[*sdk.BaseResponse[map[string]any]]("/walletService/openapi/member/card/bind", gout.H{
  117. "cardId": cardId,
  118. "archiveId": archivesId,
  119. "channelName": channelName,
  120. "price": price,
  121. "payType": payType,
  122. "outTradeNo": outTradeNo,
  123. "remark": remark,
  124. "mechanismId": mechanismId,
  125. })
  126. if err != nil {
  127. return nil, &response.ErrCode{
  128. Code: response.PLATFORM_ERROR,
  129. Msg: err.Error(),
  130. }
  131. }
  132. if resp == nil {
  133. return nil, response.ErrPlatform
  134. }
  135. if resp.Code != 200 {
  136. return nil, &response.ErrCode{
  137. Code: resp.Code,
  138. Msg: resp.Message,
  139. }
  140. }
  141. return resp, nil
  142. }
  143. // postReq 发送post请求
  144. func postReq[T any](path string, data gout.H) (T, error) {
  145. var res T
  146. fmt.Println(sdk.GetConfig())
  147. // 检查配置
  148. if sdk.GetConfig().ApiDomain == "" {
  149. return res, fmt.Errorf("请先配置API域名")
  150. }
  151. url := fmt.Sprintf("%s%s", sdk.GetConfig().ApiDomain, path)
  152. fmt.Println("请求地址 ===>", url)
  153. err := gout.POST(url).Debug(sdk.GetConfig().AppDebug).
  154. SetHeader(gout.H{}).
  155. SetJSON(data).
  156. BindJSON(&res).
  157. Do()
  158. if err != nil {
  159. fmt.Println("请求失败: ", err)
  160. return res, err
  161. }
  162. return res, nil
  163. }
  164. // postReq 发送post请求
  165. func getReq[T any](path string, data gout.H) (T, error) {
  166. var res T
  167. fmt.Println(sdk.GetConfig())
  168. // 检查配置
  169. if sdk.GetConfig().ApiDomain == "" {
  170. return res, fmt.Errorf("请先配置API域名")
  171. }
  172. url := fmt.Sprintf("%s%s", sdk.GetConfig().ApiDomain, path)
  173. fmt.Println("请求地址 ===>", url)
  174. err := gout.GET(url).Debug(sdk.GetConfig().AppDebug).
  175. SetHeader(gout.H{}).
  176. SetQuery(data).
  177. BindJSON(&res).
  178. Do()
  179. if err != nil {
  180. fmt.Println("请求失败: ", err)
  181. return res, err
  182. }
  183. return res, nil
  184. }