蛋蛋星球RabbitMq消费项目
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.
 
 
 
 
 
 

212 lignes
6.9 KiB

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