蛋蛋星球-客户端
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.
 
 
 
 
 

656 lines
19 KiB

  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. if utils.StrToFloat64(req.Amount) <= 0 {
  159. e.OutErr(c, 400, e.NewErr(400, "请输入金额"))
  160. return
  161. }
  162. user := svc.GetUser(c)
  163. var userId, openId string
  164. var kind int
  165. //sysCfgDb := implement.NewSysCfgDb(db.Db, cache.GetPool().Get())
  166. //sysCfgMap := sysCfgDb.SysCfgFindWithDb(enum.AlipayAppId, enum.WxAppId)
  167. if req.Kind == enum.FinWithdrawApplyWithdrawKindForAli.String() {
  168. alipayUserInfoDb := implement.NewAlipayUserInfoDb(db.Db)
  169. aliInfo, err := alipayUserInfoDb.GetAlipayUserInfo(user.Id)
  170. if err != nil {
  171. e.OutErr(c, e.ERR, err.Error())
  172. return
  173. }
  174. if aliInfo == nil {
  175. e.OutErr(c, e.ERR, "支付宝用户信息未授权")
  176. return
  177. }
  178. if aliInfo.OpenId == "" {
  179. e.OutErr(c, e.ERR, "支付宝用户授权信息有误")
  180. return
  181. }
  182. //appId = sysCfgMap[enum.AlipayAppId]
  183. userId = aliInfo.UserId
  184. openId = aliInfo.OpenId
  185. kind = int(enum.FinWithdrawApplyWithdrawKindForAli)
  186. } else if req.Kind == enum.FinWithdrawApplyWithdrawKindForWx.String() {
  187. e.OutErr(c, 400, "温馨提示:请选择支付宝提现~")
  188. return
  189. wxUserInfoDb := implement.NewWxUserInfoDb(db.Db)
  190. wxInfo, err := wxUserInfoDb.GetWxUserInfo(user.Id)
  191. if err != nil {
  192. e.OutErr(c, e.ERR, err.Error())
  193. return
  194. }
  195. if wxInfo == nil {
  196. e.OutErr(c, e.ERR, "微信用户信息未授权")
  197. return
  198. }
  199. //appId = sysCfgMap[enum.WxAppId]
  200. userId = wxInfo.UserId
  201. openId = wxInfo.OpenId
  202. kind = int(enum.FinWithdrawApplyWithdrawKindForWx)
  203. } else {
  204. e.OutErr(c, e.ERR, "未知的提现类型")
  205. return
  206. }
  207. //1、判断是否可以提现
  208. err, realAmount, fee, isAuto, isFirst := svc.CheckWithdraw(c, req.Amount)
  209. if err != nil {
  210. e.OutErr(c, e.ERR, err.Error())
  211. return
  212. }
  213. // 2、加锁 防止并发提取
  214. mutexKey := fmt.Sprintf("egg_app_withdraw_apply:%s", utils.Int64ToStr(user.Id))
  215. withdrawAvailable, err := cache.Do("SET", mutexKey, 1, "EX", 5, "NX")
  216. if err != nil {
  217. e.OutErr(c, e.ERR, err)
  218. return
  219. }
  220. if withdrawAvailable != "OK" {
  221. e.OutErr(c, e.ERR, e.NewErr(400000, "请求过于频繁,请稍后再试"))
  222. return
  223. }
  224. // 开启事务
  225. session := db.Db.NewSession()
  226. defer session.Close()
  227. err = session.Begin()
  228. if err != nil {
  229. session.Rollback()
  230. e.OutErr(c, e.ERR_DB_ORM, err)
  231. return
  232. }
  233. //3、处理用户余额
  234. dealUserWalletReq := md2.DealUserWalletReq{
  235. Direction: "sub",
  236. Kind: int(enum.UserWithdrawApply),
  237. Title: enum.UserWithdrawApply.String(),
  238. Uid: user.Id,
  239. Amount: utils.StrToFloat64(req.Amount),
  240. }
  241. rule2.Init(cfg.RedisAddr)
  242. err = rule.DealUserWallet(session, dealUserWalletReq)
  243. if err != nil {
  244. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  245. session.Rollback()
  246. return
  247. }
  248. //4、新增提现记录
  249. now := time.Now()
  250. id := utils.StrToInt64(utils.OrderUUID(int(user.Id)))
  251. finWithdrawApplyDb := implement.NewFinWithdrawApplyDb(db.Db)
  252. finWithdrawApply := &model.FinWithdrawApply{
  253. Id: id,
  254. Uid: user.Id,
  255. AdmId: 0,
  256. Amount: req.Amount,
  257. RealAmount: realAmount,
  258. Fee: fee,
  259. Type: func(isAuto bool) int {
  260. if isAuto {
  261. return 2
  262. }
  263. return 1
  264. }(isAuto),
  265. WithdrawAccount: userId,
  266. WithdrawName: openId,
  267. Reason: 0,
  268. PaymentDate: "",
  269. State: 0,
  270. WithdrawKind: kind,
  271. IsFirst: func(isFirst bool) int {
  272. if isFirst {
  273. return 1
  274. }
  275. return 0
  276. }(isFirst),
  277. Memo: "",
  278. UpdateAt: now.Format("2006-01-02 15:04:05"),
  279. CreateAt: now.Format("2006-01-02 15:04:05"),
  280. }
  281. insertAffected, err := finWithdrawApplyDb.FinWithdrawApplyInsertOneBySession(session, finWithdrawApply)
  282. if err != nil {
  283. session.Rollback()
  284. e.OutErr(c, e.ERR_DB_ORM, err)
  285. return
  286. }
  287. if insertAffected <= 0 {
  288. session.Rollback()
  289. e.OutErr(c, e.ERR_DB_ORM, "生成提现单失败")
  290. return
  291. }
  292. err = session.Begin()
  293. if err != nil {
  294. session.Rollback()
  295. e.OutErr(c, e.ERR_DB_ORM, err)
  296. return
  297. }
  298. err = session.Commit()
  299. if err != nil {
  300. _ = session.Rollback()
  301. e.OutErr(c, e.ERR_DB_ORM, err)
  302. return
  303. }
  304. //5、推入mq
  305. if isAuto {
  306. //更改提现单记录状态
  307. finWithdrawApply.State = int(enum.FinWithdrawApplyStateForIng)
  308. updateAffected, err1 := finWithdrawApplyDb.UpdateFinWithdrawApply(finWithdrawApply, "state")
  309. if err1 != nil {
  310. e.OutErr(c, e.ERR_DB_ORM, err1.Error())
  311. return
  312. }
  313. if updateAffected <= 0 {
  314. e.OutErr(c, e.ERR_DB_ORM, "更新提现单状态失败")
  315. return
  316. }
  317. ch, err1 := rabbit.Cfg.Pool.GetChannel()
  318. if err1 != nil {
  319. e.OutErr(c, e.ERR_INIT_RABBITMQ, err1.Error())
  320. return
  321. }
  322. defer ch.Release()
  323. var data md3.EggFinWithdrawApplyData
  324. err = copier.Copy(&data, &finWithdrawApply)
  325. if err != nil {
  326. e.OutErr(c, e.ERR, err.Error())
  327. return
  328. }
  329. ch.Publish(md3.EggAppExchange, data, md3.EggFinWithdrawApply)
  330. }
  331. e.OutSuc(c, "success", nil)
  332. }
  333. // GetWithdrawCondition
  334. // @Summary 蛋蛋星球-钱包-提现条件(获取)
  335. // @Tags 钱包
  336. // @Description 提现条件(获取)
  337. // @Accept json
  338. // @Produce json
  339. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  340. // @Success 200 {object} md.GetWithdrawConditionResp "具体数据"
  341. // @Failure 400 {object} md.Response "具体错误"
  342. // @Router /api/v1/wallet/withdraw/condition [GET]
  343. func GetWithdrawCondition(c *gin.Context) {
  344. user := svc.GetUser(c)
  345. settingDb := implement.NewFinWithdrawSettingDb(db.Db)
  346. setting, err := settingDb.FinWithdrawSettingGetOne()
  347. if err != nil {
  348. e.OutErr(c, e.ERR_DB_ORM, err)
  349. return
  350. }
  351. //1. 判断是否为第一次提现
  352. isFirst := false
  353. has, err := db.Db.Where("uid = ?", user.Id).Get(&model.FinWithdrawApply{})
  354. if !has { //第一次提现
  355. isFirst = true
  356. }
  357. resp := svc.GetWithdrawCondition(user, setting, isFirst)
  358. alipayUserInfoDb := implement.NewAlipayUserInfoDb(db.Db)
  359. alipayInfo, err := alipayUserInfoDb.GetAlipayUserInfo(user.Id)
  360. if err != nil {
  361. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  362. return
  363. }
  364. if alipayInfo != nil {
  365. resp.IsBindAlipay = true
  366. }
  367. userInfoDb := implement.NewWxUserInfoDb(db.Db)
  368. wxUserInfo, err := userInfoDb.GetWxUserInfo(user.Id)
  369. if err != nil {
  370. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  371. return
  372. }
  373. if wxUserInfo != nil {
  374. resp.IsBindWx = true
  375. }
  376. e.OutSuc(c, resp, nil)
  377. }
  378. // LaunchBindAlipayAccount
  379. // @Summary 蛋蛋星球-钱包-发起绑定支付宝获得URL
  380. // @Tags 钱包
  381. // @Description 发起绑定支付宝获得URL
  382. // @Accept json
  383. // @Produce json
  384. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  385. // @Success 200 {string} "Url"
  386. // @Failure 400 {object} md.Response "具体错误"
  387. // @Router /api/v1/wallet/withdraw/launchBindAlipay [GET]
  388. func LaunchBindAlipayAccount(c *gin.Context) {
  389. client, err := alipay.InitAlipay(nil)
  390. if err != nil {
  391. e.OutErr(c, e.ERR, err.Error())
  392. return
  393. }
  394. appId := client.AppId
  395. scope := "auth_user"
  396. sysCfgDb := sys_cfg.NewSysCfgDb(db.Db)
  397. sysCfgs, err := sysCfgDb.SysCfgGetAll()
  398. if err != nil {
  399. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  400. return
  401. }
  402. if sysCfgs == nil {
  403. e.OutErr(c, e.ERR_CFG_CACHE, nil)
  404. return
  405. }
  406. cfgMap := make(map[string]string, len(*sysCfgs))
  407. for _, cfg := range *sysCfgs {
  408. cfgMap[cfg.Key] = cfg.Val
  409. }
  410. targetId := utils.UUIDHexString()
  411. alipayPrivateKey := cfgMap[enum2.AlipayPrivateKey]
  412. pid := cfgMap[enum2.AlipayPid]
  413. bm := make(gopay.BodyMap)
  414. bm.Set("apiname", "com.alipay.account.auth")
  415. bm.Set("app_id", appId)
  416. bm.Set("app_name", "mc")
  417. bm.Set("auth_type", "AUTHACCOUNT")
  418. bm.Set("biz_type", "openservice")
  419. bm.Set("pid", pid)
  420. bm.Set("product_id", "APP_FAST_LOGIN")
  421. bm.Set("scope", scope)
  422. bm.Set("sign_type", "RSA2")
  423. bm.Set("method", "alipay.open.auth.sdk.code.get")
  424. bm.Set("target_id", targetId)
  425. privateKey, err := utils.StringToPrivateKey(alipayPrivateKey)
  426. if err != nil {
  427. e.OutErr(c, e.ERR, err.Error())
  428. return
  429. }
  430. sign, err := alipay2.GetRsaSign(bm, alipay2.RSA2, privateKey)
  431. if err != nil {
  432. e.OutErr(c, e.ERR, err.Error())
  433. return
  434. }
  435. resUrl := fmt.Sprintf("apiname=com.alipay.account.auth&app_id=%s&app_name=mc&"+
  436. "auth_type=AUTHACCOUNT&biz_type=openservice&method=alipay.open.auth.sdk.code.get"+
  437. "&pid=%s&product_id=APP_FAST_LOGIN&scope=%s&sign_type=RSA2&"+
  438. "target_id=%s&sign=%s", appId, pid, scope, targetId, sign)
  439. e.OutSuc(c, resUrl, nil)
  440. }
  441. // BindAlipayAccount
  442. // @Summary 蛋蛋星球-钱包-绑定支付宝
  443. // @Tags 钱包
  444. // @Description 绑定支付宝
  445. // @Accept json
  446. // @Produce json
  447. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  448. // @Param req body md.BindAlipayAccountReq true "具体参数"
  449. // @Success 200 {string} "success"
  450. // @Failure 400 {object} md.Response "具体错误"
  451. // @Router /api/v1/wallet/withdraw/bindAlipay [POST]
  452. func BindAlipayAccount(c *gin.Context) {
  453. var req md.BindAlipayAccountReq
  454. err := c.ShouldBindJSON(&req)
  455. if err != nil {
  456. utils.FilePutContents("BindAlipayAccount_err", "_1")
  457. utils.FilePutContents("BindAlipayAccount_err", err.Error())
  458. err = svc.HandleValidateErr(err)
  459. err1 := err.(e.E)
  460. e.OutErr(c, err1.Code, err1.Error())
  461. return
  462. }
  463. user := svc.GetUser(c)
  464. var alipayStruct *alipay.InitAlipayStruct
  465. client, err := alipay.InitAlipay(alipayStruct)
  466. if err != nil {
  467. utils.FilePutContents("BindAlipayAccount_err", utils.Int64ToStr(user.Id)+"_2")
  468. utils.FilePutContents("BindAlipayAccount_err", err.Error())
  469. e.OutErr(c, e.ERR, err.Error())
  470. return
  471. }
  472. bm := make(gopay.BodyMap)
  473. bm = bm.Set("grant_type", "authorization_code")
  474. bm = bm.Set("code", req.AuthCode)
  475. systemOauthToken, err := client.SystemOauthToken(c, bm)
  476. if err != nil {
  477. utils.FilePutContents("BindAlipayAccount_err", utils.Int64ToStr(user.Id)+"_3")
  478. utils.FilePutContents("BindAlipayAccount_err", err.Error())
  479. e.OutErr(c, e.ERR, err.Error())
  480. return
  481. }
  482. info, err := client.UserInfoShare(c, systemOauthToken.Response.AccessToken)
  483. if err != nil {
  484. utils.FilePutContents("BindAlipayAccount_err", utils.Int64ToStr(user.Id)+"_4")
  485. utils.FilePutContents("BindAlipayAccount_err", err.Error())
  486. e.OutErr(c, e.ERR, err.Error())
  487. return
  488. }
  489. fmt.Println("BindAlipayAccount>>>>>>>>", info)
  490. utils.FilePutContents("BindAlipayAccount", utils.SerializeStr(map[string]interface{}{
  491. "info": info,
  492. "uid": user.Id,
  493. "systemOauthToken": systemOauthToken,
  494. "req": req,
  495. }))
  496. infoDb := implement.NewAlipayUserInfoDb(db.Db)
  497. userInfo, err := infoDb.GetAlipayUserInfo(user.Id)
  498. if err != nil {
  499. utils.FilePutContents("BindAlipayAccount_err", utils.Int64ToStr(user.Id)+"_5")
  500. utils.FilePutContents("BindAlipayAccount_err", err.Error())
  501. e.OutErr(c, e.ERR, err.Error())
  502. return
  503. }
  504. now := time.Now()
  505. if userInfo == nil {
  506. m := model.AlipayUserInfo{
  507. Uid: user.Id,
  508. UserId: info.Response.UserId,
  509. OpenId: systemOauthToken.Response.OpenId,
  510. AppId: client.AppId,
  511. UserName: info.Response.NickName,
  512. Ext: "",
  513. CreateAt: now.Format("2006-01-02 15:04:05"),
  514. UpdateAt: now.Format("2006-01-02 15:04:05"),
  515. }
  516. _, err = infoDb.AlipayUserInfoInsert(&m)
  517. if err != nil {
  518. utils.FilePutContents("BindAlipayAccount_err", utils.Int64ToStr(user.Id)+"_6")
  519. utils.FilePutContents("BindAlipayAccount_err", err.Error())
  520. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  521. return
  522. }
  523. } else {
  524. cols := []string{"open_id", "app_id", "user_id", "user_name"}
  525. m := model.AlipayUserInfo{
  526. Id: userInfo.Id,
  527. Uid: userInfo.Uid,
  528. UserId: info.Response.UserId,
  529. OpenId: info.Response.OpenId,
  530. AppId: client.AppId,
  531. UserName: info.Response.NickName,
  532. Ext: "",
  533. CreateAt: userInfo.CreateAt,
  534. UpdateAt: now.Format("2006-01-02 15:04:05"),
  535. }
  536. _, err := infoDb.UpdateAlipayUserInfo(&m, cols...)
  537. if err != nil {
  538. utils.FilePutContents("BindAlipayAccount_err", utils.Int64ToStr(user.Id)+"_7")
  539. utils.FilePutContents("BindAlipayAccount_err", err.Error())
  540. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  541. return
  542. }
  543. }
  544. e.OutSuc(c, "success", nil)
  545. }
  546. // BindWxPayAccount
  547. // @Summary 蛋蛋星球-钱包-绑定微信支付
  548. // @Tags 钱包
  549. // @Description 绑定微信支付
  550. // @Accept json
  551. // @Produce json
  552. // @param Authorization header string true "验证参数Bearer和token空格拼接"
  553. // @Param req body md.BindWxPayAccountReq true "具体参数"
  554. // @Success 200 {string} "success"
  555. // @Failure 400 {object} md.Response "具体错误"
  556. // @Router /api/v1/wallet/withdraw/bindWxPay [POST]
  557. func BindWxPayAccount(c *gin.Context) {
  558. var req md.BindWxPayAccountReq
  559. err := c.ShouldBindJSON(&req)
  560. if err != nil {
  561. err = svc.HandleValidateErr(err)
  562. err1 := err.(e.E)
  563. e.OutErr(c, err1.Code, err1.Error())
  564. return
  565. }
  566. user := svc.GetUser(c)
  567. wxUserInfoDb := implement.NewWxUserInfoDb(db.Db)
  568. wxUserInfo, err := wxUserInfoDb.GetWxUserInfo(user.Id)
  569. if err != nil {
  570. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  571. return
  572. }
  573. if wxUserInfo != nil {
  574. e.OutErr(c, e.ERR, errors.New("用户已绑定过微信").Error())
  575. return
  576. }
  577. appid := svc.GetSysCfgStr("wechat_appid")
  578. secret := svc.GetSysCfgStr("wechat_secret")
  579. 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)
  580. wechatResp, err := utils.CurlGet(wxUrl, nil)
  581. if err != nil {
  582. e.OutErr(c, 400, e.NewErr(400, "获取微信信息失败"))
  583. return
  584. }
  585. utils.FilePutContents("WithdrawalBinding", utils.SerializeStr(wechatResp))
  586. openId := gjson.Get(string(wechatResp), "openid").String()
  587. wechatToken := gjson.Get(string(wechatResp), "access_token").String()
  588. wechatInfoUrl := "https://api.weixin.qq.com/sns/userinfo?access_token=%s&openid=%s"
  589. wechatInfoUrl = fmt.Sprintf(wechatInfoUrl, wechatToken, openId)
  590. wechatInfoResp, _ := utils.CurlGet(wechatInfoUrl, nil)
  591. now := time.Now()
  592. newWxUserInfo := model.WxUserInfo{
  593. Uid: user.Id,
  594. OpenId: openId,
  595. UserId: gjson.Get(string(wechatResp), "unionid").String(),
  596. UserName: gjson.Get(string(wechatInfoResp), "nickname").String(),
  597. AppId: appid,
  598. CreateAt: now.Format("2006-01-02 15:04:05"),
  599. UpdateAt: now.Format("2006-01-02 15:04:05"),
  600. }
  601. affected, err := wxUserInfoDb.WxUserInfoInsert(&newWxUserInfo)
  602. if err != nil {
  603. e.OutErr(c, e.ERR_DB_ORM, err.Error())
  604. return
  605. }
  606. if affected <= 0 {
  607. e.OutErr(c, e.ERR, errors.New("绑定失败"))
  608. }
  609. e.OutSuc(c, "success", nil)
  610. }