蛋蛋星球RabbitMq消费项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

265 rivejä
8.3 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. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  13. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  14. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  15. "code.fnuoos.com/EggPlanet/egg_system_rules.git/svc/sys_cfg"
  16. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  17. "context"
  18. "encoding/json"
  19. "errors"
  20. "fmt"
  21. "github.com/go-pay/gopay"
  22. "github.com/go-pay/gopay/alipay"
  23. "github.com/go-pay/xlog"
  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, ch)
  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. data.EggFinWithdrawApplyData = *msg
  73. }
  74. ch.Publish(md2.EggAppExchange, data, md2.EggFinWithdrawApplyError)
  75. }
  76. //_ = res.Reject(false)
  77. err = res.Ack(true)
  78. fmt.Println("err ::: ", err)
  79. } else {
  80. panic(errors.New("error getting message"))
  81. }
  82. }
  83. }
  84. func handleEggFinWithdrawApplyDataConsume(msgData []byte, client *alipay.Client, ch *rabbit.Channel) error {
  85. time.Sleep(time.Duration(100) * time.Millisecond) //休眠100毫秒
  86. // 1.解析mq中queue的数据结构体
  87. var msg *md2.EggFinWithdrawApplyData
  88. err := json.Unmarshal(msgData, &msg)
  89. if err != nil {
  90. return err
  91. }
  92. return errors.New("测试")
  93. applyDb := implement.NewFinWithdrawApplyDb(db.Db)
  94. apply, err := applyDb.FinWithdrawApplyGet(msg.Id)
  95. if err != nil {
  96. return err
  97. }
  98. if apply == nil {
  99. return errors.New("提现记录不存在~")
  100. }
  101. // 如果不在队列处理阶段或不是自动处理订单,直接返回
  102. if apply.State != 4 && apply.Type != 2 {
  103. return errors.New("提现记录无需处理~")
  104. }
  105. session := db.Db.NewSession()
  106. defer func() {
  107. session.Close()
  108. if err := recover(); err != nil {
  109. _ = logx.Error(err)
  110. }
  111. }()
  112. session.Begin()
  113. // 2.3 更新 apply 信息
  114. apply.State = 2
  115. affected, err2 := applyDb.UpdateFinWithdrawApplyBySession(session, apply, "state")
  116. if err2 != nil {
  117. return err2
  118. }
  119. if affected == 0 {
  120. return errors.New("更新 apply 状态失败")
  121. }
  122. if msg.WithdrawKind == 1 {
  123. // 3.支付宝提现
  124. alipayUserInfoDb := implement.NewAlipayUserInfoDb(db.Db)
  125. userInfo, err1 := alipayUserInfoDb.GetAlipayUserInfo(msg.Uid)
  126. if err != nil {
  127. return err1
  128. }
  129. bm := make(gopay.BodyMap)
  130. bm.Set("out_biz_no", msg.Id).
  131. Set("trans_amount", msg.Amount).
  132. Set("biz_scene", "DIRECT_TRANSFER").
  133. Set("product_code", "TRANS_ACCOUNT_NO_PWD").
  134. SetBodyMap("payee_info", func(bm gopay.BodyMap) {
  135. bm.Set("identity", userInfo.OpenId)
  136. bm.Set("identity_type", "ALIPAY_OPEN_ID")
  137. })
  138. _, err = client.FundTransUniTransfer(context.Background(), bm)
  139. if err != nil {
  140. // 如果需要重试 推回队尾
  141. if err.Error() == "Lock wait timeout exceeded; try restarting transaction" {
  142. ch.Publish(md2.EggAppExchange, msg, md2.EggFinWithdrawApply)
  143. return nil
  144. }
  145. //TODO::处理提现失败
  146. err = dealFinWithdrawApplyStateForBad(apply)
  147. if err != nil {
  148. return err
  149. }
  150. session.Rollback()
  151. return err
  152. }
  153. } else if msg.WithdrawKind == 2 {
  154. }
  155. return session.Commit()
  156. }
  157. func dealFinWithdrawApplyStateForBad(finWithdrawApply *model.FinWithdrawApply) error {
  158. session := db.Db.NewSession()
  159. defer session.Close()
  160. session.Begin()
  161. finWithdrawApplyDb := implement.NewFinWithdrawApplyDb(db.Db)
  162. finWithdrawApply.State = int(enum.FinWithdrawApplyStateForBad)
  163. updateAffected, err1 := finWithdrawApplyDb.UpdateFinWithdrawApplyBySession(session, finWithdrawApply, "state")
  164. if err1 != nil {
  165. return err1
  166. }
  167. if updateAffected <= 0 {
  168. return errors.New("更新提现单状态失败")
  169. }
  170. dealUserWalletReq := md3.DealUserWalletReq{
  171. Direction: "sub",
  172. Kind: int(enum.UserWithdrawBad),
  173. Title: enum.UserWithdrawBad.String(),
  174. Uid: finWithdrawApply.Uid,
  175. Amount: utils2.StrToFloat64(finWithdrawApply.Amount),
  176. }
  177. err := rule.DealUserWallet(session, dealUserWalletReq)
  178. if err != nil {
  179. session.Rollback()
  180. return err
  181. }
  182. err = session.Commit()
  183. if err != nil {
  184. _ = session.Rollback()
  185. }
  186. return err
  187. }
  188. type InitAlipayStruct struct {
  189. IsProd bool `json:"is_prod" label:"是否生产环境"`
  190. AlipayAppId string `json:"alipay_app_id" label:"支付宝商家应用appid"`
  191. AlipayPrivateKey string `json:"alipay_private_key" label:"支付宝商家应用私钥"`
  192. AlipayPublicKey string `json:"alipay_public_key" label:"支付宝商家应用公钥"`
  193. AlipayPublicContentRSA2 []byte `json:"alipay_public_content_rsa_2" label:"支付宝公钥证书"`
  194. AlipayRootContent []byte `json:"alipay_root_content" label:"支付宝根证书"`
  195. AppPublicContent []byte `json:"app_public_content" label:"应用公钥证书"`
  196. }
  197. // InitAlipay 初始化支付宝客户端
  198. // appid:应用ID
  199. // privateKey:应用私钥,支持PKCS1和PKCS8
  200. // isProd:是否是正式环境,沙箱环境请选择新版沙箱应用。
  201. func InitAlipay(initData *InitAlipayStruct) (client *alipay.Client, err error) {
  202. if initData == nil {
  203. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  204. sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.AlipayPrivateKey, enum.AlipayPublicKey, enum.AlipayPublicContentRSA2, enum.AlipayRootContent, enum.AppPublicContent)
  205. initData = &InitAlipayStruct{
  206. IsProd: true,
  207. AlipayAppId: sysCfgMap[enum.AlipayAppId],
  208. AlipayPrivateKey: sysCfgMap[enum.AlipayPrivateKey],
  209. AlipayPublicKey: sysCfgMap[enum.AlipayPublicKey],
  210. AlipayPublicContentRSA2: []byte(sysCfgMap[enum.AlipayPublicContentRSA2]),
  211. AlipayRootContent: []byte(sysCfgMap[enum.AlipayRootContent]),
  212. AppPublicContent: []byte(sysCfgMap[enum.AppPublicContent]),
  213. }
  214. }
  215. client, err = alipay.NewClient(initData.AlipayAppId, initData.AlipayPrivateKey, initData.IsProd)
  216. if err != nil {
  217. xlog.Error(err)
  218. return
  219. }
  220. // 自定义配置http请求接收返回结果body大小,默认 10MB
  221. client.SetBodySize(10) // 没有特殊需求,可忽略此配置
  222. // 打开Debug开关,输出日志,默认关闭
  223. client.DebugSwitch = gopay.DebugOn
  224. client.SetLocation(alipay.LocationShanghai). // 设置时区,不设置或出错均为默认服务器时间
  225. SetCharset(alipay.UTF8). // 设置字符编码,不设置默认 utf-8
  226. SetSignType(alipay.RSA2) // 设置签名类型,不设置默认 RSA2
  227. // SetAppAuthToken("") //授权token
  228. //SetReturnUrl("https://www.fmm.ink"). // 设置返回URL
  229. // SetNotifyUrl("https://www.fmm.ink"). // 设置异步通知URL
  230. // 传入 支付宝公钥证书 alipayPublicCert.crt 内容
  231. client.AutoVerifySign(initData.AlipayPublicContentRSA2)
  232. // 传入证书内容
  233. err = client.SetCertSnByContent(initData.AppPublicContent, initData.AlipayRootContent, initData.AlipayPublicContentRSA2)
  234. if err != nil {
  235. xlog.Debug("SetCertSn:", err)
  236. return
  237. }
  238. return
  239. }