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

hdl_wallet.go 18 KiB

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