applet_customer.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package service
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "fmt"
  6. "gas-cylinder-api/app/admin/model"
  7. "gas-cylinder-api/app/admin/service/dto"
  8. "gas-cylinder-api/conf"
  9. "github.com/dgrijalva/jwt-go"
  10. "github.com/gin-gonic/gin"
  11. "gogs.baozhida.cn/zoie/OAuth-core/pkg/utils"
  12. "gogs.baozhida.cn/zoie/OAuth-core/service"
  13. "gorm.io/gorm"
  14. "net/http"
  15. "time"
  16. )
  17. type AppletCustomer struct {
  18. service.Service
  19. }
  20. func GetAppletCustomerId(c *gin.Context) string {
  21. customerId, exists := c.Get("customer_id")
  22. if !exists {
  23. return ""
  24. }
  25. return customerId.(string)
  26. }
  27. // GetPage 获取Customer列表
  28. func (e *AppletCustomer) Login(c *dto.AppletCustomerLoginReq) (token, expiresAt string, err error) {
  29. var data model.Customer
  30. url := "https://api.weixin.qq.com/sns/jscode2session?appid=%s&secret=%s&js_code=%s&grant_type=authorization_code"
  31. appid := conf.ExtConfig.Applet.Appid
  32. appSecret := conf.ExtConfig.Applet.AppSecret
  33. url = fmt.Sprintf(url, appid, appSecret, c.Code)
  34. // 发起请求
  35. res, _ := http.Get(url)
  36. // 成功后获取openId
  37. wxRes := dto.AppletCustomerLoginResp{}
  38. err = json.NewDecoder(res.Body).Decode(&wxRes)
  39. if err != nil || wxRes.Openid == "" {
  40. return token, expiresAt, errors.New("获取openid失败")
  41. }
  42. err = e.Orm.Where("principal_phone = ?", c.Phone).First(&data).Error
  43. id := data.Id
  44. if err != nil {
  45. if errors.Is(err, gorm.ErrRecordNotFound) {
  46. // 添加客户
  47. for {
  48. var count int64
  49. id = utils.GetUUID()
  50. var i int64
  51. err = e.Orm.Model(&data).Where("id = ?", id).Count(&count).Error
  52. if err != nil {
  53. continue
  54. }
  55. if i == 0 {
  56. break
  57. }
  58. }
  59. err = e.Orm.Create(&model.Customer{
  60. ProvCustomer: model.ProvCustomer{
  61. Id: id,
  62. PrincipalPhone: c.Phone,
  63. },
  64. Openid: wxRes.Openid,
  65. }).Error
  66. } else {
  67. return token, expiresAt, errors.New("获取个人信息失败")
  68. }
  69. }
  70. if data.Openid != wxRes.Openid {
  71. data.Openid = wxRes.Openid
  72. err = e.Orm.Save(&data).Error
  73. if err != nil {
  74. return token, expiresAt, errors.New("同步个人信息失败")
  75. }
  76. }
  77. token, expiresAt, _ = e.GeneratorToken(id)
  78. return token, expiresAt, nil
  79. }
  80. func (e *AppletCustomer) GeneratorToken(customerId string) (string, string, error) {
  81. // 创建一个我们自己的声明
  82. var TokenExpireDuration = time.Second * 24 * 3600 * time.Duration(conf.ExtConfig.Applet.TokenExpire)
  83. expiresAt := time.Now().Add(TokenExpireDuration)
  84. c := model.CustomerClaims{
  85. customerId,
  86. jwt.StandardClaims{
  87. ExpiresAt: expiresAt.Unix(), // 过期时间
  88. Issuer: "customer", // 签发人
  89. },
  90. }
  91. // 使用指定的签名方法创建签名对象
  92. token := jwt.NewWithClaims(jwt.SigningMethodHS256, c)
  93. // 使用指定的secret签名并获得完整的编码后的字符串token
  94. tokenStr, err := token.SignedString(model.AppletCustomerSecret)
  95. return tokenStr, expiresAt.Format("2006-01-02 15:04:05"), err
  96. }