蛋蛋星球-客户端
Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 

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