123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- 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)
- }
|