蛋蛋星球RabbitMq消费项目
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。
 
 
 
 
 
 

205 行
6.7 KiB

  1. package consume
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. utils2 "applet/app/utils"
  6. "applet/app/utils/logx"
  7. "applet/consume/md"
  8. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  9. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  10. "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  11. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  12. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  13. "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc/sys_cfg"
  14. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  15. "context"
  16. "encoding/json"
  17. "errors"
  18. "fmt"
  19. "github.com/go-pay/gopay"
  20. "github.com/go-pay/gopay/alipay"
  21. "github.com/go-pay/xlog"
  22. "github.com/jinzhu/copier"
  23. "github.com/streadway/amqp"
  24. "time"
  25. )
  26. func EggFinWithdrawApplyDataConsume(queue md.MqQueue) {
  27. fmt.Println(">>>>>>>>>>>>EggFinWithdrawApplyDataConsume>>>>>>>>>>>>")
  28. ch, err := rabbit.Cfg.Pool.GetChannel()
  29. if err != nil {
  30. logx.Error(err)
  31. return
  32. }
  33. defer ch.Release()
  34. //1、将自己绑定到交换机上
  35. ch.Bind(queue.Name, queue.ExchangeName, queue.RoutKey)
  36. //2、取出数据进行消费
  37. ch.Qos(1)
  38. delivery := ch.Consume(queue.Name, false)
  39. egg_system_rules.Init(cfg.RedisAddr)
  40. var alipayStruct *InitAlipayStruct
  41. client, err := InitAlipay(alipayStruct)
  42. if err != nil {
  43. fmt.Println("EggFinWithdrawApplyDataConsume:::::", err.Error())
  44. utils2.FilePutContents("EggFinWithdrawApplyDataConsume", utils2.SerializeStr(map[string]interface{}{
  45. "err": err.Error(),
  46. }))
  47. return
  48. }
  49. var res amqp.Delivery
  50. var ok bool
  51. for {
  52. res, ok = <-delivery
  53. if ok == true {
  54. err = handleEggFinWithdrawApplyDataConsume(res.Body, client)
  55. if err != nil {
  56. fmt.Println("EggFinWithdrawApplyDataConsume:::::", err.Error())
  57. utils2.FilePutContents("EggFinWithdrawApplyDataConsume", utils2.SerializeStr(map[string]interface{}{
  58. "body": res.Body,
  59. "err": err.Error(),
  60. }))
  61. var data md2.EggFinWithdrawApplyErrorData
  62. data.ErrorInfo = err.Error()
  63. // 尝试解析数据结构体
  64. var msg *md2.EggFinWithdrawApplyData
  65. err1 := json.Unmarshal(res.Body, &msg)
  66. if err1 != nil {
  67. // 无法解析 直接将[]byte推进队列
  68. data.Ext = res.Body
  69. } else {
  70. // 解析成功
  71. err2 := copier.Copy(&data, &msg)
  72. if err2 != nil {
  73. data.Ext = res.Body
  74. }
  75. }
  76. ch.Publish(md2.EggAppExchange, utils2.SerializeStr(data), md2.EggFinWithdrawApplyError)
  77. }
  78. //_ = res.Reject(false)
  79. err = res.Ack(true)
  80. fmt.Println("err ::: ", err)
  81. } else {
  82. panic(errors.New("error getting message"))
  83. }
  84. }
  85. }
  86. func handleEggFinWithdrawApplyDataConsume(msgData []byte, client *alipay.Client) error {
  87. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  88. // 1.解析mq中queue的数据结构体
  89. var msg *md2.EggFinWithdrawApplyData
  90. err := json.Unmarshal(msgData, &msg)
  91. if err != nil {
  92. return err
  93. }
  94. // 如果不在队列处理阶段或不是自动处理订单,直接返回
  95. if msg.State != 4 && msg.Type != 2 {
  96. return nil
  97. }
  98. applyDb := implement.NewFinWithdrawApplyDb(db.Db)
  99. if msg.WithdrawKind == 1 {
  100. // 2.支付宝提现
  101. // 2.1 提现
  102. alipayUserInfoDb := implement.NewAlipayUserInfoDb(db.Db)
  103. userInfo, err := alipayUserInfoDb.GetAlipayUserInfo(msg.Uid)
  104. if err != nil {
  105. return err
  106. }
  107. bm := make(gopay.BodyMap)
  108. bm.Set("out_biz_no", msg.Id).
  109. Set("trans_amount", msg.Amount).
  110. Set("biz_scene", "DIRECT_TRANSFER").
  111. Set("product_code", "TRANS_ACCOUNT_NO_PWD").
  112. SetBodyMap("payee_info", func(bm gopay.BodyMap) {
  113. bm.Set("identity", userInfo.OpenId)
  114. bm.Set("identity_type", "ALIPAY_OPEN_ID")
  115. })
  116. _, err = client.FundTransUniTransfer(context.Background(), bm)
  117. if err != nil {
  118. return err
  119. }
  120. // 2.3 更新 apply 信息
  121. affected, err := applyDb.UpdateFinWithdrawApply(&model.FinWithdrawApply{State: 2}, "state")
  122. if err != nil {
  123. return err
  124. }
  125. if affected == 0 {
  126. return errors.New("更新 apply 状态失败")
  127. }
  128. } else if msg.WithdrawKind == 2 {
  129. }
  130. return nil
  131. }
  132. type InitAlipayStruct struct {
  133. IsProd bool `json:"is_prod" label:"是否生产环境"`
  134. AlipayAppId string `json:"alipay_app_id" label:"支付宝商家应用appid"`
  135. AlipayPrivateKey string `json:"alipay_private_key" label:"支付宝商家应用私钥"`
  136. AlipayPublicKey string `json:"alipay_public_key" label:"支付宝商家应用公钥"`
  137. AlipayPublicContentRSA2 []byte `json:"alipay_public_content_rsa_2" label:"支付宝公钥证书"`
  138. AlipayRootContent []byte `json:"alipay_root_content" label:"支付宝根证书"`
  139. AppPublicContent []byte `json:"app_public_content" label:"应用公钥证书"`
  140. }
  141. // InitAlipay 初始化支付宝客户端
  142. // appid:应用ID
  143. // privateKey:应用私钥,支持PKCS1和PKCS8
  144. // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。
  145. func InitAlipay(initData *InitAlipayStruct) (client *alipay.Client, err error) {
  146. if initData == nil {
  147. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  148. sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.AlipayPrivateKey, enum.AlipayPublicKey, enum.AlipayPublicContentRSA2, enum.AlipayRootContent, enum.AppPublicContent)
  149. initData = &InitAlipayStruct{
  150. IsProd: true,
  151. AlipayAppId: sysCfgMap[enum.AlipayAppId],
  152. AlipayPrivateKey: sysCfgMap[enum.AlipayPrivateKey],
  153. AlipayPublicKey: sysCfgMap[enum.AlipayPublicKey],
  154. AlipayPublicContentRSA2: []byte(sysCfgMap[enum.AlipayPublicContentRSA2]),
  155. AlipayRootContent: []byte(sysCfgMap[enum.AlipayRootContent]),
  156. AppPublicContent: []byte(sysCfgMap[enum.AppPublicContent]),
  157. }
  158. }
  159. client, err = alipay.NewClient(initData.AlipayAppId, initData.AlipayPrivateKey, initData.IsProd)
  160. if err != nil {
  161. xlog.Error(err)
  162. return
  163. }
  164. // 自定义配置http请求接收返回结果body大小,默认 10MB
  165. client.SetBodySize(10) // 没有特殊需求,可忽略此配置
  166. // 打开Debug开关,输出日志,默认关闭
  167. client.DebugSwitch = gopay.DebugOn
  168. client.SetLocation(alipay.LocationShanghai). // 设置时区,不设置或出错均为默认服务器时间
  169. SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8
  170. SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2
  171. // SetAppAuthToken("") //授权token
  172. //SetReturnUrl("https://www.fmm.ink"). // 设置返回URL
  173. // SetNotifyUrl("https://www.fmm.ink"). // 设置异步通知URL
  174. // 传入 支付宝公钥证书 alipayPublicCert.crt 内容
  175. client.AutoVerifySign(initData.AlipayPublicContentRSA2)
  176. // 传入证书内容
  177. err = client.SetCertSnByContent(initData.AppPublicContent, initData.AlipayRootContent, initData.AlipayPublicContentRSA2)
  178. if err != nil {
  179. xlog.Debug("SetCertSn:", err)
  180. return
  181. }
  182. return
  183. }