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

hdl_wallet.go 16 KiB

2週間前
3週間前
1週間前
3週間前
2週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
4週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
2週間前
3週間前
2週間前
3週間前
3週間前
2週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
2週間前
3週間前
3週間前
1週間前
3週間前
3週間前
3週間前
3週間前
3週間前
1週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
1週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
3週間前
2週間前
3週間前
2週間前
3週間前
2週間前
3週間前
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606
  1. package hdl
  2. import (
  3. "applet/app/cfg"
  4. "applet/app/db"
  5. "applet/app/e"
  6. alipay "applet/app/lib/gopay"
  7. "applet/app/md"
  8. "applet/app/svc"
  9. "applet/app/svc/sys_cfg"
  10. "applet/app/utils"
  11. "applet/app/utils/cache"
  12. "code.fnuoos.com/EggPlanet/egg_models.git/src/implement"
  13. "code.fnuoos.com/EggPlanet/egg_models.git/src/model"
  14. rule2 "code.fnuoos.com/EggPlanet/egg_system_rules.git"
  15. "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  16. enum2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/enum"
  17. md2 "code.fnuoos.com/EggPlanet/egg_system_rules.git/md"
  18. "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule"
  19. md3 "code.fnuoos.com/EggPlanet/egg_system_rules.git/rule/egg_energy/md"
  20. "code.fnuoos.com/go_rely_warehouse/zyos_go_mq.git/rabbit"
  21. "errors"
  22. "fmt"
  23. "github.com/gin-gonic/gin"
  24. "github.com/go-pay/gopay"
  25. alipay2 "github.com/go-pay/gopay/alipay"
  26. "github.com/jinzhu/copier"
  27. "time"
  28. )
  29. // GetAmountFlow
  30. // @Summary 蛋蛋星球-钱包-余额明细(获取)
  31. // @Tags 钱包
  32. // @Description 余额明细(获取)
  33. // @Accept json
  34. // @Produce json
  35. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  36. // @Param limit query string true "每页大小"
  37. // @Param page query string true "页数"
  38. // @Param startAt query string false "开始时间"
  39. // @Param endAt query string false "结束时间"
  40. // @Param direction query string false "流水方向(1.收入 2.支出 0.全部)"
  41. // @Success 200 {object} md.GetAmountFlowResp "具体数据"
  42. // @Failure 400 {object} md.Response "具体错误"
  43. // @Router /api/v1/wallet/amountFlow [GET]
  44. func GetAmountFlow(c *gin.Context) {
  45. pageStr := c.DefaultQuery("page", "1")
  46. limitStr := c.DefaultQuery("limit", "10")
  47. startAt := c.Query("startAt")
  48. endAt := c.Query("endAt")
  49. directionStr := c.Query("direction")
  50. val, exists := c.Get("user")
  51. if !exists {
  52. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  53. return
  54. }
  55. user, ok := val.(*model.User)
  56. if !ok {
  57. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  58. return
  59. }
  60. direction := 0
  61. switch directionStr {
  62. case "1":
  63. direction = 1
  64. case "2":
  65. direction = 2
  66. }
  67. page := utils.StrToInt(pageStr)
  68. limit := utils.StrToInt(limitStr)
  69. flowDb := implement.NewUserWalletFlowDb(db.Db)
  70. flows, total, err := flowDb.UserWalletFlowFindByCoinAndUser(page, limit, user.Id, startAt, endAt, direction, false, 0, 0)
  71. if err != nil {
  72. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  73. return
  74. }
  75. list := make([]md.WalletFlowNode, 0, len(flows))
  76. for _, flow := range flows {
  77. temp := md.WalletFlowNode{
  78. Id: flow.Id,
  79. Uid: flow.Uid,
  80. Direction: flow.Direction,
  81. Amount: flow.Amount,
  82. BeforeAmount: flow.BeforeAmount,
  83. AfterAmount: flow.AfterAmount,
  84. SysFee: flow.SysFee,
  85. OrdId: flow.OrdId,
  86. Title: flow.Title,
  87. Kind: flow.Kind,
  88. State: flow.State,
  89. Memo: flow.Memo,
  90. CreateTime: flow.CreateAt,
  91. UpdateTime: flow.UpdateAt,
  92. }
  93. list = append(list, temp)
  94. }
  95. resp := md.GetAmountFlowResp{
  96. List: list,
  97. Paginate: md.Paginate{
  98. Limit: limit,
  99. Page: page,
  100. Total: total,
  101. },
  102. }
  103. e.OutSuc(c, resp, nil)
  104. }
  105. // WithdrawGetAmount
  106. // @Summary 蛋蛋星球-钱包-提现余额(获取)
  107. // @Tags 钱包
  108. // @Description 提现余额(获取)
  109. // @Accept json
  110. // @Produce json
  111. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  112. // @Success 200 {object} md.WithdrawGetAmountResp "具体数据"
  113. // @Failure 400 {object} md.Response "具体错误"
  114. // @Router /api/v1/wallet/withdraw/index [GET]
  115. func WithdrawGetAmount(c *gin.Context) {
  116. val, exists := c.Get("user")
  117. if !exists {
  118. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  119. return
  120. }
  121. user, ok := val.(*model.User)
  122. if !ok {
  123. e.OutErr(c, e.ERR_USER_CHECK_ERR, nil)
  124. return
  125. }
  126. walletDb := implement.NewUserWalletDb(db.Db)
  127. wallet, err := walletDb.GetUserVirtualWallet(user.Id)
  128. if err != nil {
  129. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  130. return
  131. }
  132. resp := md.WithdrawGetAmountResp{
  133. Amount: wallet.Amount,
  134. }
  135. e.OutSuc(c, resp, nil)
  136. }
  137. // WithdrawApply
  138. // @Summary 蛋蛋星球-钱包-发起提现
  139. // @Tags 钱包
  140. // @Description 发起提现
  141. // @Accept json
  142. // @Produce json
  143. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  144. // @Param req body md.WithdrawApplyReq true "具体参数"
  145. // @Success 200 {string} "success"
  146. // @Failure 400 {object} md.Response "具体错误"
  147. // @Router /api/v1/wallet/withdraw/apply [POST]
  148. func WithdrawApply(c *gin.Context) {
  149. var req md.WithdrawApplyReq
  150. err := c.ShouldBindJSON(&req)
  151. if err != nil {
  152. err = svc.HandleValidateErr(err)
  153. err1 := err.(e.E)
  154. e.OutErr(c, err1.Code, err1.Error())
  155. return
  156. }
  157. user := svc.GetUser(c)
  158. var userId, openId string
  159. var kind int
  160. //sysCfgDb := implement.NewSysCfgDb(db.Db, cache.GetPool().Get())
  161. //sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.WxAppId)
  162. if req.Kind == enum.FinWithdrawApplyWithdrawKindForAli.String() {
  163. alipayUserInfoDb := implement.NewAlipayUserInfoDb(db.Db)
  164. aliInfo, err := alipayUserInfoDb.GetAlipayUserInfo(user.Id)
  165. if err != nil {
  166. e.OutErr(c, e.ERR, err.Error())
  167. return
  168. }
  169. if aliInfo == nil {
  170. e.OutErr(c, e.ERR, "支付宝用户信息未授权")
  171. return
  172. }
  173. //appId = sysCfgMap[enum.AlipayAppId]
  174. userId = aliInfo.UserId
  175. openId = aliInfo.OpenId
  176. kind = int(enum.FinWithdrawApplyWithdrawKindForAli)
  177. } else if req.Kind == enum.FinWithdrawApplyWithdrawKindForWx.String() {
  178. wxUserInfoDb := implement.NewWxUserInfoDb(db.Db)
  179. wxInfo, err := wxUserInfoDb.GetWxUserInfo(user.Id)
  180. if err != nil {
  181. e.OutErr(c, e.ERR, err.Error())
  182. return
  183. }
  184. if wxInfo == nil {
  185. e.OutErr(c, e.ERR, "微信用户信息未授权")
  186. return
  187. }
  188. //appId = sysCfgMap[enum.WxAppId]
  189. userId = wxInfo.UserId
  190. openId = wxInfo.OpenId
  191. kind = int(enum.FinWithdrawApplyWithdrawKindForWx)
  192. } else {
  193. e.OutErr(c, e.ERR, "未知的提现类型")
  194. return
  195. }
  196. //1、判断是否可以提现
  197. err, realAmount, fee, isAuto, isFirst := svc.CheckWithdraw(c, req.Amount)
  198. if err != nil {
  199. e.OutErr(c, e.ERR, err.Error())
  200. return
  201. }
  202. // 2、加锁 防止并发提取
  203. mutexKey := fmt.Sprintf("egg_app_withdraw_apply:%s", utils.Int64ToStr(user.Id))
  204. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  205. if err != nil {
  206. e.OutErr(c, e.ERR, err)
  207. return
  208. }
  209. if withdrawAvailable != "OK" {
  210. e.OutErr(c, e.ERR, e.NewErr(400000, "请求过于频繁,请稍后再试"))
  211. return
  212. }
  213. // 开启事务
  214. session := db.Db.NewSession()
  215. defer session.Close()
  216. err = session.Begin()
  217. if err != nil {
  218. session.Rollback()
  219. e.OutErr(c, e.ERR_DB_ORM, err)
  220. return
  221. }
  222. //3、处理用户余额
  223. dealUserWalletReq := md2.DealUserWalletReq{
  224. Direction: "sub",
  225. Kind: int(enum.UserWithdrawApply),
  226. Title: enum.UserWithdrawApply.String(),
  227. Uid: user.Id,
  228. Amount: utils.StrToFloat64(req.Amount),
  229. }
  230. rule2.Init(cfg.RedisAddr)
  231. err = rule.DealUserWallet(session, dealUserWalletReq)
  232. if err != nil {
  233. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  234. session.Rollback()
  235. return
  236. }
  237. //4、新增提现记录
  238. now := time.Now()
  239. id := utils.StrToInt64(utils.OrderUUID(int(user.Id)))
  240. finWithdrawApplyDb := implement.NewFinWithdrawApplyDb(db.Db)
  241. finWithdrawApply := &model.FinWithdrawApply{
  242. Id: id,
  243. Uid: user.Id,
  244. AdmId: 0,
  245. Amount: req.Amount,
  246. RealAmount: realAmount,
  247. Fee: fee,
  248. Type: func(isAuto bool) int {
  249. if isAuto {
  250. return 2
  251. }
  252. return 1
  253. }(isAuto),
  254. WithdrawAccount: userId,
  255. WithdrawName: openId,
  256. Reason: 0,
  257. PaymentDate: "",
  258. State: 0,
  259. WithdrawKind: kind,
  260. IsFirst: func(isFirst bool) int {
  261. if isFirst {
  262. return 1
  263. }
  264. return 0
  265. }(isFirst),
  266. Memo: "",
  267. UpdateAt: now.Format("2006-01-02 15:04:05"),
  268. CreateAt: now.Format("2006-01-02 15:04:05"),
  269. }
  270. insertAffected, err := finWithdrawApplyDb.FinWithdrawApplyInsertOneBySession(session, finWithdrawApply)
  271. if err != nil {
  272. session.Rollback()
  273. e.OutErr(c, e.ERR_DB_ORM, err)
  274. return
  275. }
  276. if insertAffected <= 0 {
  277. session.Rollback()
  278. e.OutErr(c, e.ERR_DB_ORM, "生成提现单失败")
  279. return
  280. }
  281. err = session.Begin()
  282. if err != nil {
  283. session.Rollback()
  284. e.OutErr(c, e.ERR_DB_ORM, err)
  285. return
  286. }
  287. err = session.Commit()
  288. if err != nil {
  289. _ = session.Rollback()
  290. e.OutErr(c, e.ERR_DB_ORM, err)
  291. return
  292. }
  293. //5、推入mq
  294. if isAuto {
  295. //更改提现单记录状态
  296. finWithdrawApply.State = int(enum.FinWithdrawApplyStateForIng)
  297. updateAffected, err1 := finWithdrawApplyDb.UpdateFinWithdrawApply(finWithdrawApply, "state")
  298. if err1 != nil {
  299. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  300. return
  301. }
  302. if updateAffected <= 0 {
  303. e.OutErr(c, e.ERR_DB_ORM, "更新提现单状态失败")
  304. return
  305. }
  306. ch, err1 := rabbit.Cfg.Pool.GetChannel()
  307. if err1 != nil {
  308. e.OutErr(c, e.ERR_INIT_RABBITMQ, err1.Error())
  309. return
  310. }
  311. defer ch.Release()
  312. var data md3.EggFinWithdrawApplyData
  313. err = copier.Copy(&data, &finWithdrawApply)
  314. if err != nil {
  315. e.OutErr(c, e.ERR, err.Error())
  316. return
  317. }
  318. ch.Publish(md3.EggAppExchange, data, md3.EggFinWithdrawApply)
  319. }
  320. e.OutSuc(c, "success", nil)
  321. }
  322. // GetWithdrawCondition
  323. // @Summary 蛋蛋星球-钱包-提现条件(获取)
  324. // @Tags 钱包
  325. // @Description 提现条件(获取)
  326. // @Accept json
  327. // @Produce json
  328. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  329. // @Success 200 {object} md.GetWithdrawConditionResp "具体数据"
  330. // @Failure 400 {object} md.Response "具体错误"
  331. // @Router /api/v1/wallet/withdraw/condition [GET]
  332. func GetWithdrawCondition(c *gin.Context) {
  333. user := svc.GetUser(c)
  334. settingDb := implement.NewFinWithdrawSettingDb(db.Db)
  335. setting, err := settingDb.FinWithdrawSettingGetOne()
  336. if err != nil {
  337. e.OutErr(c, e.ERR_DB_ORM, err)
  338. return
  339. }
  340. //1. 判断是否为第一次提现
  341. isFirst := false
  342. has, err := db.Db.Where("uid = ?", user.Id).Get(model.FinWithdrawApply{})
  343. if !has { //第一次提现
  344. isFirst = true
  345. }
  346. resp := svc.GetWithdrawCondition(user, setting, isFirst)
  347. alipayUserInfoDb := implement.NewAlipayUserInfoDb(db.Db)
  348. alipayInfo, err := alipayUserInfoDb.GetAlipayUserInfo(user.Id)
  349. if err != nil {
  350. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  351. return
  352. }
  353. if alipayInfo != nil {
  354. resp.IsBindAlipay = true
  355. }
  356. userInfoDb := implement.NewWxUserInfoDb(db.Db)
  357. wxUserInfo, err := userInfoDb.GetWxUserInfo(user.Id)
  358. if err != nil {
  359. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  360. return
  361. }
  362. if wxUserInfo != nil {
  363. resp.IsBindWx = true
  364. }
  365. e.OutSuc(c, resp, nil)
  366. }
  367. // LaunchBindAlipayAccount
  368. // @Summary 蛋蛋星球-钱包-发起绑定支付宝获得URL
  369. // @Tags 钱包
  370. // @Description 发起绑定支付宝获得URL
  371. // @Accept json
  372. // @Produce json
  373. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  374. // @Success 200 {string} "Url"
  375. // @Failure 400 {object} md.Response "具体错误"
  376. // @Router /api/v1/wallet/withdraw/launchBindAlipay [GET]
  377. func LaunchBindAlipayAccount(c *gin.Context) {
  378. client, err := alipay.InitAlipay(nil)
  379. if err != nil {
  380. e.OutErr(c, e.ERR, err.Error())
  381. return
  382. }
  383. appId := client.AppId
  384. scope := "auth_user"
  385. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  386. sysCfgs, err := sysCfgDb.SysCfgGetAll()
  387. if err != nil {
  388. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  389. return
  390. }
  391. if sysCfgs == nil {
  392. e.OutErr(c, e.ERR_CFG_CACHE, nil)
  393. return
  394. }
  395. cfgMap := make(map[string]string, len(*sysCfgs))
  396. for _, cfg := range *sysCfgs {
  397. cfgMap[cfg.Key] = cfg.Val
  398. }
  399. targetId := utils.UUIDHexString()
  400. alipayPrivateKey := cfgMap[enum2.AlipayPrivateKey]
  401. pid := cfgMap[enum2.AlipayPid]
  402. bm := make(gopay.BodyMap)
  403. bm.Set("apiname", "com.alipay.account.auth")
  404. bm.Set("app_id", appId)
  405. bm.Set("app_name", "mc")
  406. bm.Set("auth_type", "AUTHACCOUNT")
  407. bm.Set("biz_type", "openservice")
  408. bm.Set("pid", pid)
  409. bm.Set("product_id", "APP_FAST_LOGIN")
  410. bm.Set("scope", scope)
  411. bm.Set("sign_type", "RSA2")
  412. bm.Set("method", "alipay.open.auth.sdk.code.get")
  413. bm.Set("target_id", targetId)
  414. privateKey, err := utils.StringToPrivateKey(alipayPrivateKey)
  415. if err != nil {
  416. e.OutErr(c, e.ERR, err.Error())
  417. return
  418. }
  419. sign, err := alipay2.GetRsaSign(bm, alipay2.RSA2, privateKey)
  420. if err != nil {
  421. e.OutErr(c, e.ERR, err.Error())
  422. return
  423. }
  424. resUrl := fmt.Sprintf("apiname=com.alipay.account.auth&app_id=%s&app_name=mc&"+
  425. "auth_type=AUTHACCOUNT&biz_type=openservice&method=alipay.open.auth.sdk.code.get"+
  426. "&pid=%s&product_id=APP_FAST_LOGIN&scope=%s&sign_type=RSA2&"+
  427. "target_id=%s&sign=%s", appId, pid, scope, targetId, sign)
  428. e.OutSuc(c, resUrl, nil)
  429. }
  430. // BindAlipayAccount
  431. // @Summary 蛋蛋星球-钱包-绑定支付宝
  432. // @Tags 钱包
  433. // @Description 绑定支付宝
  434. // @Accept json
  435. // @Produce json
  436. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  437. // @Param req body md.BindAlipayAccountReq true "具体参数"
  438. // @Success 200 {string} "success"
  439. // @Failure 400 {object} md.Response "具体错误"
  440. // @Router /api/v1/wallet/withdraw/bindAlipay [POST]
  441. func BindAlipayAccount(c *gin.Context) {
  442. var req md.BindAlipayAccountReq
  443. err := c.ShouldBindJSON(&req)
  444. if err != nil {
  445. err = svc.HandleValidateErr(err)
  446. err1 := err.(e.E)
  447. e.OutErr(c, err1.Code, err1.Error())
  448. return
  449. }
  450. user := svc.GetUser(c)
  451. var alipayStruct *alipay.InitAlipayStruct
  452. client, err := alipay.InitAlipay(alipayStruct)
  453. if err != nil {
  454. e.OutErr(c, e.ERR, err.Error())
  455. return
  456. }
  457. bm := make(gopay.BodyMap)
  458. bm = bm.Set("grant_type", "authorization_code")
  459. bm = bm.Set("code", req.AuthCode)
  460. systemOauthToken, err := client.SystemOauthToken(c, bm)
  461. if err != nil {
  462. e.OutErr(c, e.ERR, err.Error())
  463. return
  464. }
  465. info, err := client.UserInfoShare(c, systemOauthToken.Response.AccessToken)
  466. if err != nil {
  467. e.OutErr(c, e.ERR, err.Error())
  468. return
  469. }
  470. infoDb := implement.NewAlipayUserInfoDb(db.Db)
  471. userInfo, err := infoDb.GetAlipayUserInfo(user.Id)
  472. if err != nil {
  473. e.OutErr(c, e.ERR, err.Error())
  474. return
  475. }
  476. now := time.Now()
  477. if userInfo == nil {
  478. m := model.AlipayUserInfo{
  479. Uid: user.Id,
  480. UserId: info.Response.UserId,
  481. OpenId: info.Response.OpenId,
  482. AppId: client.AppId,
  483. UserName: info.Response.NickName,
  484. Ext: "",
  485. CreateAt: now.Format("2006-01-02 15:04:05"),
  486. UpdateAt: now.Format("2006-01-02 15:04:05"),
  487. }
  488. _, err = infoDb.AlipayUserInfoInsert(&m)
  489. if err != nil {
  490. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  491. return
  492. }
  493. } else {
  494. cols := []string{"open_id", "app_id", "user_id", "user_name"}
  495. m := model.AlipayUserInfo{
  496. Id: userInfo.Id,
  497. Uid: userInfo.Uid,
  498. UserId: info.Response.UserId,
  499. OpenId: info.Response.OpenId,
  500. AppId: client.AppId,
  501. UserName: info.Response.NickName,
  502. Ext: "",
  503. CreateAt: userInfo.CreateAt,
  504. UpdateAt: now.Format("2006-01-02 15:04:05"),
  505. }
  506. _, err := infoDb.UpdateAlipayUserInfo(&m, cols...)
  507. if err != nil {
  508. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  509. return
  510. }
  511. }
  512. e.OutSuc(c, "success", nil)
  513. }
  514. // BindWxPayAccount
  515. // @Summary 蛋蛋星球-钱包-绑定微信支付
  516. // @Tags 钱包
  517. // @Description 绑定微信支付
  518. // @Accept json
  519. // @Produce json
  520. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  521. // @Param req body md.BindWxPayAccountReq true "具体参数"
  522. // @Success 200 {string} "success"
  523. // @Failure 400 {object} md.Response "具体错误"
  524. // @Router /api/v1/wallet/withdraw/bindWxPay [POST]
  525. func BindWxPayAccount(c *gin.Context) {
  526. var req md.BindWxPayAccountReq
  527. err := c.ShouldBindJSON(&req)
  528. if err != nil {
  529. err = svc.HandleValidateErr(err)
  530. err1 := err.(e.E)
  531. e.OutErr(c, err1.Code, err1.Error())
  532. return
  533. }
  534. user := svc.GetUser(c)
  535. wxUserInfoDb := implement.NewWxUserInfoDb(db.Db)
  536. wxUserInfo, err := wxUserInfoDb.GetWxUserInfo(user.Id)
  537. if err != nil {
  538. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  539. return
  540. }
  541. if wxUserInfo != nil {
  542. e.OutErr(c, e.ERR, errors.New("用户已绑定过微信").Error())
  543. return
  544. }
  545. now := time.Now()
  546. newWxUserInfo := model.WxUserInfo{
  547. Uid: user.Id,
  548. UserId: req.UserId,
  549. OpenId: req.OpenId,
  550. AppId: req.AppId,
  551. Ext: req.Ext,
  552. CreateAt: now.Format("2006-01-02 15:04:05"),
  553. UpdateAt: now.Format("2006-01-02 15:04:05"),
  554. }
  555. affected, err := wxUserInfoDb.WxUserInfoInsert(&newWxUserInfo)
  556. if err != nil {
  557. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  558. return
  559. }
  560. if affected <= 0 {
  561. e.OutErr(c, e.ERR, errors.New("绑定失败"))
  562. }
  563. e.OutSuc(c, "success", nil)
  564. }