package weixin import ( "fmt" "net/http" "strings" "zhiyuan/pkg/app" "zhiyuan/pkg/utils" "zhiyuan/pkg/weixin/mp" "zhiyuan/services/admin" "zhiyuan/services/user" "zhiyuan/services/work/worker" "github.com/gin-gonic/gin" ) func GetAccessToken(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "微信id有误", nil) return } client, err := mp.NewClient(id) if err != nil { app.Error(c, err.Error()) return } response := client.RefreshAccessToken() if response.ErrMsg != "" { app.ErrorMsg(c, response.ErrMsg, nil) return } app.Success(c, gin.H{ "access_token": response.AccessToken, "expires_in": response.ExpiresIn, }) } func GetJsapiTicket(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "微信id有误", nil) return } client, err := mp.NewClient(id) if err != nil { app.Error(c, err.Error()) return } response := client.RefreshJsapiTicket() if response.ErrMsg != "" { app.ErrorMsg(c, response.ErrMsg, nil) return } app.Success(c, gin.H{ "ticket": response.Ticket, "expires_in": response.ExpiresIn, }) } func GetJsapiParam(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "微信id有误", nil) return } url := c.Query("url") if url == "" { app.ErrorMsg(c, "url 不能为空", nil) return } client, err := mp.NewClient(id) if err != nil { app.Error(c, err.Error()) return } if res, err := client.GetJsapiParam(url); err == nil { app.Success(c, res) return } else { app.Error(c, err.Error()) return } } func GetOauthUrl(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "微信id有误", nil) return } client, err := mp.NewClient(id) if err != nil { app.Error(c, err.Error()) return } url, err := client.GetOauthUrl(c.Query("scope"), c.Query("url")) if err != nil { app.Error(c, err.Error()) return } app.Success(c, url) } func GetUserInfo(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "微信id有误", nil) return } code := c.Query("code") if code == "" { app.ErrorMsg(c, "code 不能为空", nil) return } if code == "1" { app.Success(c, "test") return } client, err := mp.NewClient(id) if err != nil { app.Error(c, err.Error()) return } response := client.GetOauthToken(code) if response.ErrMsg != "" { app.ErrorMsg(c, response.ErrMsg, nil) return } if c.Query("scope") == "snsapi_base" { app.Success(c, response.OpenID) return } userInfo := client.GetUserInfo(response.AccessToken, response.OpenID) if response.ErrMsg != "" { app.ErrorMsg(c, response.ErrMsg, nil) return } if user, _ := user.GetOne(map[string]interface{}{"openid": response.OpenID}, nil, nil); user != nil { userInfo.IsExists = true } app.Success(c, userInfo) } func GetAdminInfo(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "微信id有误", nil) return } code := c.Query("code") if code == "" { app.ErrorMsg(c, "code 不能为空", nil) return } if code == "1" { app.Success(c, "test") return } client, err := mp.NewClient(id) if err != nil { app.Error(c, err.Error()) return } response := client.GetOauthToken(code) if response.ErrMsg != "" { app.ErrorMsg(c, response.ErrMsg, nil) return } if c.Query("scope") == "snsapi_base" { app.Success(c, response.OpenID) return } userInfo := client.GetUserInfo(response.AccessToken, response.OpenID) if response.ErrMsg != "" { app.ErrorMsg(c, response.ErrMsg, nil) return } if admin, _ := admin.GetOne(map[string]interface{}{"openid": response.OpenID}, nil, nil); admin != nil { userInfo.IsExists = true } app.Success(c, userInfo) } func GetWorkerInfo(c *gin.Context) { id := utils.StrTo(c.Param("id")).MustInt() if id <= 0 { app.ErrorMsg(c, "微信id有误", nil) return } code := c.Query("code") if code == "" { app.ErrorMsg(c, "code 不能为空", nil) return } if code == "1" { app.Success(c, "test") return } client, err := mp.NewClient(id) if err != nil { app.Error(c, err.Error()) return } response := client.GetOauthToken(code) if response.ErrMsg != "" { app.ErrorMsg(c, response.ErrMsg, nil) return } if c.Query("scope") == "snsapi_base" { app.Success(c, response.OpenID) return } userInfo := client.GetUserInfo(response.AccessToken, response.OpenID) if response.ErrMsg != "" { app.ErrorMsg(c, response.ErrMsg, nil) return } if worker, _ := worker.GetOne(map[string]interface{}{"openid": response.OpenID}, nil, nil); worker != nil { userInfo.IsExists = true } app.Success(c, userInfo) } func RedictShop(c *gin.Context) { backUrl := "/" if c.Query("backUrl") != "" { backUrl = c.Query("backUrl") } if strings.Contains(backUrl, "?") { backUrl += "&" } else { backUrl += "?" } baseUrl := "https://shop.nczyzs.com%sscope=%s&back_url=%s&code=%s&state=%s" url := fmt.Sprintf(baseUrl, backUrl, c.Query("scope"), c.Query("back_url"), c.Query("code"), c.Query("state")) c.Redirect(http.StatusMovedPermanently, url) }