123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- package fastgpt
- import (
- "bytes"
- "context"
- "encoding/json"
- "fmt"
- "github.com/golang-jwt/jwt/v5"
- "github.com/suyuan32/simple-admin-core/rpc/types/core"
- "net/http"
- "strconv"
- "github.com/zeromicro/go-zero/core/logx"
- "wechat-api/internal/svc"
- "wechat-api/internal/types"
- )
- type SetTokenLogic struct {
- logx.Logger
- ctx context.Context
- svcCtx *svc.ServiceContext
- rw http.ResponseWriter
- }
- func NewSetTokenLogic(ctx context.Context, svcCtx *svc.ServiceContext, rw http.ResponseWriter) *SetTokenLogic {
- return &SetTokenLogic{
- Logger: logx.WithContext(ctx),
- ctx: ctx,
- svcCtx: svcCtx,
- rw: rw,
- }
- }
- func (l *SetTokenLogic) SetToken(username string) (resp *types.BaseMsgResp, err error) {
- claims, err := ParseJWT(username, l.svcCtx.Config.Auth.AccessSecret)
- if err != nil {
- return nil, fmt.Errorf("invalid token")
- }
- data, err := l.svcCtx.CoreRpc.GetUserById(context.TODO(), &core.UUIDReq{Id: claims.UserId})
- token, err := l.getToken(strconv.FormatUint(*data.DepartmentId, 10))
- if err != nil {
- return nil, fmt.Errorf("invalid token")
- }
-
-
-
-
- cookie := &http.Cookie{
- Name: "fastgpt_token",
- Value: token,
- Domain: ".gkscrm.com",
- SameSite: http.SameSiteNoneMode,
- Secure: true,
- HttpOnly: false,
- Path: "/",
- }
-
- http.SetCookie(l.rw, cookie)
-
- resp = &types.BaseMsgResp{
- Code: 0,
- Msg: "Cookie set successfully",
- }
- return
- }
- func (l *SetTokenLogic) getToken(username string) (string, error) {
-
- url := "https://agent.gkscrm.com/api/support/user/account/loginByPassword"
- payload := map[string]string{
- "username": username,
- "password": "578fd6dfa3f71a8fadf5dc60d0e7115881db4c36504f83c4a0f4422107162c36",
- }
-
- jsonPayload, err := json.Marshal(payload)
- if err != nil {
- return "", err
- }
-
- req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonPayload))
- if err != nil {
- return "", err
- }
- req.Header.Set("Content-Type", "application/json")
-
- client := &http.Client{}
- resp, err := client.Do(req)
- if err != nil {
- return "", err
- }
- defer resp.Body.Close()
-
- if resp.StatusCode != http.StatusOK {
- return "", fmt.Errorf("failed to login, status code: %d", resp.StatusCode)
- }
-
- var response map[string]interface{}
- err = json.NewDecoder(resp.Body).Decode(&response)
- if err != nil {
- return "", err
- }
-
- data, ok := response["data"].(map[string]interface{})
- if !ok {
- return "", fmt.Errorf("invalid response format")
- }
- token, ok := data["token"].(string)
- if !ok {
- return "", fmt.Errorf("token not found in response")
- }
- return token, nil
- }
- type Claims struct {
- RoleId string `json:"roleId"`
- UserId string `json:"userId"`
- jwt.RegisteredClaims
- }
- func ParseJWT(tokenString, accessSecret string) (*Claims, error) {
- claims := &Claims{}
- token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
- if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
- return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
- }
- return []byte(accessSecret), nil
- })
- if err != nil {
- return nil, fmt.Errorf("invalid token")
- }
- if !token.Valid {
- return nil, fmt.Errorf("invalid token")
- }
- return claims, nil
- }
|