蛋蛋星球-客户端
Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 

635 рядки
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. 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. }