user.open.id.set.go 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package user
  2. import (
  3. "duoduo/apis/pdd"
  4. "duoduo/conf"
  5. "duoduo/models/mysql"
  6. "duoduo/models/user"
  7. "duoduo/tools"
  8. "duoduo/tools/app"
  9. "fmt"
  10. "github.com/gin-gonic/gin"
  11. )
  12. func OpenIdSet(c *gin.Context) {
  13. var inData user.OpenIdSetRequest
  14. var outData user.OpenIdSetResponse
  15. var sqlData mysql.User
  16. var wallet mysql.Wallet
  17. err := c.ShouldBindJSON(&inData)
  18. if err != nil {
  19. app.Error(c, 400, err, err.Error())
  20. return
  21. }
  22. confIni, err := conf.ConnIni()
  23. if err != nil {
  24. app.Error(c, 400, err, err.Error())
  25. return
  26. }
  27. url := fmt.Sprintf("https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code", confIni.MustValue("wx", "app_id"), confIni.MustValue("wx", "secret"), inData.Code)
  28. val, err := pdd.DuoDuoGet(url)
  29. if err != nil {
  30. app.Error(c, 500, err, err.Error())
  31. return
  32. }
  33. fmt.Print(val)
  34. err = tools.JsonUnmarshal(val, &outData)
  35. if err != nil {
  36. app.Error(c, 500, err, err.Error())
  37. return
  38. }
  39. if outData.Errcode != 0 {
  40. app.Error(c, outData.Errcode, err, outData.Errmsg)
  41. return
  42. }
  43. //查询数据
  44. sqlData.OpenID = outData.OpenId
  45. num := sqlData.GetNum()
  46. if num == 0 {
  47. //创建用户
  48. sqlData.CreateTime = tools.GetCurrntTimeStr()
  49. sqlData.UpdateTime = tools.GetCurrntTimeStr()
  50. sqlData.Create()
  51. //创建钱包
  52. wallet.CreateTime = tools.GetCurrntTimeStr()
  53. wallet.UpdateTime = tools.GetCurrntTimeStr()
  54. wallet.OpenID = outData.OpenId
  55. wallet.Create()
  56. }
  57. app.OK(c, outData, app.Success)
  58. }