|
@@ -7,11 +7,42 @@ import (
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/silenceper/wechat/v2/officialaccount/message"
|
|
"github.com/silenceper/wechat/v2/officialaccount/message"
|
|
"go.uber.org/zap"
|
|
"go.uber.org/zap"
|
|
|
|
+ "net/http"
|
|
|
|
+ "net/url"
|
|
)
|
|
)
|
|
|
|
|
|
type WechatApi struct {
|
|
type WechatApi struct {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func (b *WechatApi) Auth(c *gin.Context) {
|
|
|
|
+ code := c.Query("code")
|
|
|
|
+ state := c.Query("state")
|
|
|
|
+ global.GVA_LOG.Info("用户授权", zap.Any("code", code), zap.Any("state", state))
|
|
|
|
+ if acc, err := global.GVA_WECHAT.GetOauth().GetUserAccessToken(code); err != nil {
|
|
|
|
+ global.GVA_LOG.Error("用户授权异常", zap.Any("err", err))
|
|
|
|
+ c.String(http.StatusOK, "用户授权异常,请尝试重新点击授权链接")
|
|
|
|
+ } else {
|
|
|
|
+ if userInfo, err := global.GVA_WECHAT.GetOauth().GetUserInfo(acc.AccessToken, acc.OpenID, "zh_CN"); err != nil {
|
|
|
|
+ global.GVA_LOG.Error("用户获取用户信息异常", zap.Any("err", err))
|
|
|
|
+ c.String(http.StatusOK, "获取用户信息异常,请尝试重新点击授权链接")
|
|
|
|
+ } else {
|
|
|
|
+ global.GVA_LOG.Info("用户获取授权信息", zap.Any("res", userInfo))
|
|
|
|
+ if err, userSys := userService.GetUserInfoByUnionId(userInfo.Unionid); err != nil {
|
|
|
|
+ authorityId := "100"
|
|
|
|
+ user := &system.SysUser{NickName: userInfo.Nickname, HeaderImg: userInfo.HeadImgURL, WechatId: userInfo.OpenID, UnionId: userInfo.Unionid, Password: utils.RandomString(10), AuthorityId: authorityId, Authorities: nil}
|
|
|
|
+ userService.Register(*user)
|
|
|
|
+ } else {
|
|
|
|
+ //已有用户,更新信息
|
|
|
|
+ userSys.NickName = userInfo.Nickname
|
|
|
|
+ userSys.HeaderImg = userInfo.HeadImgURL
|
|
|
|
+ userSys.WechatId = userInfo.OpenID
|
|
|
|
+ userService.SetUserInfo(userSys)
|
|
|
|
+ }
|
|
|
|
+ c.String(http.StatusOK, "您已成功授权《经开创城进行时》!")
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
func (b *WechatApi) Req(c *gin.Context) {
|
|
func (b *WechatApi) Req(c *gin.Context) {
|
|
// 传入request和responseWriter
|
|
// 传入request和responseWriter
|
|
server := global.GVA_WECHAT.GetServer(c.Request, c.Writer)
|
|
server := global.GVA_WECHAT.GetServer(c.Request, c.Writer)
|
|
@@ -27,7 +58,7 @@ func (b *WechatApi) Req(c *gin.Context) {
|
|
if err, sysUser := userService.GetUserInfoByUnionId(user.UnionID); err != nil {
|
|
if err, sysUser := userService.GetUserInfoByUnionId(user.UnionID); err != nil {
|
|
//不存在则新增用户
|
|
//不存在则新增用户
|
|
authorityId := "100"
|
|
authorityId := "100"
|
|
- user := &system.SysUser{Username: user.OpenID, WechatId: user.OpenID, UnionId: user.UnionID, NickName: user.UnionID, Password: utils.RandomString(10), AuthorityId: authorityId, Authorities: nil}
|
|
|
|
|
|
+ user := &system.SysUser{WechatId: user.OpenID, UnionId: user.UnionID, Password: utils.RandomString(10), AuthorityId: authorityId, Authorities: nil}
|
|
userService.Register(*user)
|
|
userService.Register(*user)
|
|
} else {
|
|
} else {
|
|
//已存在小程序用户更新user 微信id到表
|
|
//已存在小程序用户更新user 微信id到表
|
|
@@ -40,7 +71,14 @@ func (b *WechatApi) Req(c *gin.Context) {
|
|
} else {
|
|
} else {
|
|
global.GVA_LOG.Info("用户已存在", zap.Any("err", err), zap.Any("sysUser", sysUser))
|
|
global.GVA_LOG.Info("用户已存在", zap.Any("err", err), zap.Any("sysUser", sysUser))
|
|
}
|
|
}
|
|
- return nil
|
|
|
|
|
|
+ text := message.NewText("【经开创城进行时】感谢您的关注!授权请点击此链接:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx66e0fbb5a735bcc4&redirect_uri=" + url.QueryEscape(
|
|
|
|
+ global.GVA_CONFIG.Wxxcx.AuthUrl+"/wechat/auth") + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect")
|
|
|
|
+ return &message.Reply{MsgType: message.MsgTypeText, MsgData: text}
|
|
|
|
+ }
|
|
|
|
+ if msg.Content == "授权" {
|
|
|
|
+ text := message.NewText("授权请点击此链接:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx66e0fbb5a735bcc4&redirect_uri=" + url.QueryEscape(
|
|
|
|
+ global.GVA_CONFIG.Wxxcx.AuthUrl+"/wechat/auth") + "&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect")
|
|
|
|
+ return &message.Reply{MsgType: message.MsgTypeText, MsgData: text}
|
|
}
|
|
}
|
|
global.GVA_LOG.Info("用户消息", zap.Any("openId", msg.FromUserName), zap.Any("msg", msg))
|
|
global.GVA_LOG.Info("用户消息", zap.Any("openId", msg.FromUserName), zap.Any("msg", msg))
|
|
text := message.NewText(msg.Content)
|
|
text := message.NewText(msg.Content)
|