|
|
@@ -4,8 +4,8 @@ import (
|
|
|
"crypto/md5"
|
|
|
"encoding/hex"
|
|
|
"fmt"
|
|
|
- "fohow.com/libs/lib_redis"
|
|
|
- "strconv"
|
|
|
+ "fohow.com/apps/models/token_model"
|
|
|
+ "fohow.com/libs/tool"
|
|
|
|
|
|
// "math/rand"
|
|
|
// "crypto/md5"
|
|
|
@@ -32,8 +32,6 @@ import (
|
|
|
"fohow.com/cache"
|
|
|
// "fohow.com/libs/tool"
|
|
|
"fohow.com/libs/wx_mp"
|
|
|
- // "fohow.com/libs/wx_open"
|
|
|
- "fohow.com/libs/tool"
|
|
|
)
|
|
|
|
|
|
var (
|
|
|
@@ -55,11 +53,7 @@ func (self *PermitController) Init(ctx *context.Context, controllerName, actionN
|
|
|
//小程序授权
|
|
|
func (self *PermitController) XcxAuthorize() {
|
|
|
params := self.GetString("userinfo")
|
|
|
-
|
|
|
- channel, _ := self.GetInt64("channel", 0)
|
|
|
- //beego.BeeLogger.Warn("XcxAuthorize userinfo: %s", params)
|
|
|
- inviteId, _ := self.GetInt64("invite_id", 0)
|
|
|
- first := false
|
|
|
+ cache, _ := self.GetBool("cache", false)
|
|
|
//beego.BeeLogger.Warn("XcxAuthorize inviteId: %d", inviteId)
|
|
|
type UserInfo struct {
|
|
|
NickName string `json:"nickName"` // 用户的昵称
|
|
|
@@ -87,58 +81,70 @@ func (self *PermitController) XcxAuthorize() {
|
|
|
beego.BeeLogger.Error("XcxAuthorize err: %s, info:%s", err, info)
|
|
|
self.ReturnError(403, apps.ParamsError, "", nil)
|
|
|
}
|
|
|
- //beego.BeeLogger.Warn("XcxAuthorize code: %s", info.Wxlogincode)
|
|
|
-
|
|
|
- sessionKey, _ := self.GetSession(apps.XcxSessionKey).(string)
|
|
|
- //beego.BeeLogger.Warn("sessionKey:%s", sessionKey)
|
|
|
- type EncryptedData struct {
|
|
|
- UnionId string `json:"unionId"`
|
|
|
- OpenId string `json:"openId"`
|
|
|
- NickName string `json:"nickName"`
|
|
|
- }
|
|
|
-
|
|
|
- if sessionKey == "" {
|
|
|
- self.ReturnError(403, apps.UserNeedLogin, "", nil)
|
|
|
- }
|
|
|
- encryptedData := &EncryptedData{}
|
|
|
- encryptedData.OpenId, encryptedData.UnionId = GetXcxLoginInfo(info.Wxlogincode)
|
|
|
- /*
|
|
|
- pc := helpers.WxBizDataCrypt{AppID: beego.AppConfig.String("WxFohowXcxAppId"), SessionKey: sessionKey}
|
|
|
- beego.BeeLogger.Warn("EncryptedData:%s", info.EncryptedData)
|
|
|
- beego.BeeLogger.Warn("Iv:%s", info.Iv)
|
|
|
- result, err := pc.Decrypt(info.EncryptedData, info.Iv, true) //第三个参数解释: 需要返回 JSON 数据类型时 使用 true, 需要返回 map 数据类型时 使用 false
|
|
|
- if err != nil {
|
|
|
- beego.BeeLogger.Error("xcx XcxAuthorize descrypt failed, err:%s", err)
|
|
|
- self.ReturnError(403, apps.XcxAuthorizeError, "", nil)
|
|
|
- }
|
|
|
- beego.BeeLogger.Warn("result:%v", result)
|
|
|
- encryptedData := &EncryptedData{}
|
|
|
+ wxUser := self.GetCurrentWxUser(cache)
|
|
|
+ user := self.GetCurrentUser(cache)
|
|
|
+ if wxUser == nil {
|
|
|
+ self.ReturnError(401, apps.WxUserNotExist, "", nil)
|
|
|
+ }
|
|
|
+ //更新用户信息
|
|
|
+ wxUser.Nickname = info.UserInfo.NickName
|
|
|
+ wxUser.Sex = info.UserInfo.Gender
|
|
|
+ wxUser.City = info.UserInfo.City
|
|
|
+ wxUser.Province = info.UserInfo.Province
|
|
|
+ wxUser.Country = info.UserInfo.Country
|
|
|
+ wxUser.Save()
|
|
|
+ user.Nickname = wxUser.Nickname
|
|
|
+ user.Country = wxUser.Country
|
|
|
+ user.Province = wxUser.Province
|
|
|
+ user.City = wxUser.City
|
|
|
+ user.Sex = wxUser.Sex
|
|
|
+ user.Save()
|
|
|
|
|
|
- json.Unmarshal([]byte(result.(string)), encryptedData)*/
|
|
|
+ beego.BeeLogger.Warn("XcxAuthorize wxUser after save() Nickname:%s, Sex:%s, City:%s, Province:%s, Country:%s ", wxUser.Nickname, wxUser.Sex, wxUser.City, wxUser.Province, wxUser.Country)
|
|
|
|
|
|
- if encryptedData.UnionId == "" || encryptedData.OpenId == "" {
|
|
|
- beego.BeeLogger.Error("encryptedData:%v", encryptedData)
|
|
|
- self.ReturnError(403, apps.UserAuthorizeFailed, "", nil)
|
|
|
- }
|
|
|
- wxUser := user_model.GetWxUserByUnionid(encryptedData.UnionId, false)
|
|
|
- ip := self.Ctx.Input.IP()
|
|
|
- var user *user_model.User
|
|
|
- if wxUser != nil && wxUser.UserId > 0 {
|
|
|
- //查找已注册会员
|
|
|
- user = user_model.GetUserById(wxUser.UserId, false)
|
|
|
- } else {
|
|
|
- //注册会员
|
|
|
- user = user_model.Create("", ip)
|
|
|
+ // 如果微信用户已绑定手机,则找出userId,并且赋值给session[userId]
|
|
|
+ if wxUser != nil {
|
|
|
+ wxUser.UploadHead(info.UserInfo.AvatarUrl)
|
|
|
+ user.CopyWxUserHead(wxUser.Head)
|
|
|
}
|
|
|
- if user == nil {
|
|
|
- self.ReturnError(403, apps.RegisterUserError, "", nil)
|
|
|
+ type Ret struct {
|
|
|
+ Result bool `json:"result"`
|
|
|
+ WxUser *user_model.WxUser `json:"wx_user"`
|
|
|
}
|
|
|
+ self.Data["json"] = &Ret{WxUser: wxUser, Result: true}
|
|
|
+ self.ServeJSON()
|
|
|
+}
|
|
|
|
|
|
+//小程序登录
|
|
|
+func (self *PermitController) XcxLogin() {
|
|
|
+ code := self.GetString("code")
|
|
|
+ inviteId, _ := self.GetInt64("invite_id", 0)
|
|
|
+ //beego.BeeLogger.Warn("XcxLogin code=%s", code)
|
|
|
+ if code == "" {
|
|
|
+ self.ReturnError(403, apps.ParamsRequired, "", nil)
|
|
|
+ }
|
|
|
+ appId := beego.AppConfig.String("WxFohowXcxAppId")
|
|
|
+ appSecret := beego.AppConfig.String("WxFohowXcxAppSecret")
|
|
|
+ key := wx_mp.GetXcxSessionKey(appId, appSecret, code)
|
|
|
+ if key == nil {
|
|
|
+ self.ReturnError(403, apps.XcxGetSessionKeyError, "", nil)
|
|
|
+ }
|
|
|
+ //beego.BeeLogger.Warn("XcxLogin key=%s", key)
|
|
|
+ wxUser := user_model.GetWxUserByUnionid(key.Unionid, false)
|
|
|
if wxUser != nil {
|
|
|
- wxUser.Openid = encryptedData.OpenId
|
|
|
+ if len(wxUser.Head) > 0 {
|
|
|
+ wxUser.FullHead = self.GetFullImgUrl(wxUser.Head)
|
|
|
+ }
|
|
|
+ self.SetSession(apps.SessionWxUserKey, wxUser.Id)
|
|
|
+ self.SetSession(apps.SessionUserKey, wxUser.UserId)
|
|
|
} else {
|
|
|
- //首次授权
|
|
|
- first = true
|
|
|
+ //注册会员
|
|
|
+ ip := self.Ctx.Input.IP()
|
|
|
+ var user *user_model.User
|
|
|
+ //注册会员
|
|
|
+ user = user_model.Create("", ip)
|
|
|
+
|
|
|
+ //注册微信会员
|
|
|
introUserId := int64(1)
|
|
|
depart := int64(0)
|
|
|
if inviteId == int64(0) {
|
|
|
@@ -155,47 +161,32 @@ func (self *PermitController) XcxAuthorize() {
|
|
|
}
|
|
|
depart = inviter.Depart
|
|
|
}
|
|
|
- wxUser = new(user_model.WxUser).QuickCreate(encryptedData.OpenId, encryptedData.UnionId, ip, channel, time.Now().Unix(), user.Id, inviteId, introUserId, depart)
|
|
|
- }
|
|
|
- user.Nickname = wxUser.Nickname
|
|
|
- user.Country = wxUser.Country
|
|
|
- user.Province = wxUser.Province
|
|
|
- user.City = wxUser.City
|
|
|
- user.Sex = wxUser.Sex
|
|
|
- //参数第一,cookie第二
|
|
|
- cId, _ := strconv.ParseInt(self.Ctx.GetCookie("sign_up_channel"), 10, 64)
|
|
|
- user.SignupChannelId = cId
|
|
|
- user.Save()
|
|
|
- if user != nil {
|
|
|
- self.SetSession(apps.SessionUserKey, user.Id)
|
|
|
- }
|
|
|
- //wxUser.UserId = user.Id
|
|
|
- wxUser.Nickname = info.UserInfo.NickName
|
|
|
- wxUser.Sex = info.UserInfo.Gender
|
|
|
- wxUser.City = info.UserInfo.City
|
|
|
- wxUser.Province = info.UserInfo.Province
|
|
|
- wxUser.Country = info.UserInfo.Country
|
|
|
- // beego.BeeLogger.Warn("XcxAuthorize wxUser before save() Nickname:%s, Sex:%s, City:%s, Province:%s, Country:%s ", wxUser.Nickname, wxUser.Sex, wxUser.City, wxUser.Province, wxUser.Country)
|
|
|
- wxUser.Save()
|
|
|
- beego.BeeLogger.Warn("XcxAuthorize wxUser after save() Nickname:%s, Sex:%s, City:%s, Province:%s, Country:%s ", wxUser.Nickname, wxUser.Sex, wxUser.City, wxUser.Province, wxUser.Country)
|
|
|
-
|
|
|
- if wxUser != nil {
|
|
|
+ wxUser = new(user_model.WxUser).QuickCreate(key.Openid, key.Unionid, ip, user.Id, inviteId, introUserId, depart)
|
|
|
self.SetSession(apps.SessionWxUserKey, wxUser.Id)
|
|
|
- }
|
|
|
- //赠送积分
|
|
|
- if first {
|
|
|
- //go wxUser.UploadHead(info.UserInfo.AvatarUrl)
|
|
|
- wxUser.UploadHead(info.UserInfo.AvatarUrl)
|
|
|
- go user.XcxPermitUploadHead(info.UserInfo.AvatarUrl)
|
|
|
+ self.SetSession(apps.SessionUserKey, wxUser.UserId)
|
|
|
+ //赠送积分
|
|
|
go helpers.SendCent(wxUser.Id, inviteId)
|
|
|
}
|
|
|
- // 如果微信用户已绑定手机,则找出userId,并且赋值给session[userId]
|
|
|
- if wxUser != nil && wxUser.UserId > 0 {
|
|
|
- user := user_model.GetUserById(wxUser.UserId, false)
|
|
|
- user.CopyWxUserHead(wxUser.Head)
|
|
|
- self.SetSession(apps.SessionUserKey, wxUser.UserId)
|
|
|
+ self.SetSession(apps.XcxSessionKey, key.SessionKey)
|
|
|
+ //beego.BeeLogger.Warn("XcxLogin SessionKey=%s", key.SessionKey)
|
|
|
+ if self.CruSession == nil {
|
|
|
+ self.ReturnError(401, apps.NoExist, "", nil)
|
|
|
+ }
|
|
|
+ //更新会员token
|
|
|
+ var jwtUser token_model.JwtUser
|
|
|
+ jwtUser.Id = wxUser.Id
|
|
|
+ token, err := token_model.GenerateToken(&jwtUser, 0)
|
|
|
+ if err != nil {
|
|
|
+ self.ReturnError(401, apps.TokenError, "", nil)
|
|
|
+ }
|
|
|
+
|
|
|
+ sId := self.CruSession.SessionID()
|
|
|
+ type Ret struct {
|
|
|
+ TokenKey string `json:"token"`
|
|
|
+ SessionKey string `json:"session_key"`
|
|
|
+ WxUser *user_model.WxUser `json:"wx_user"`
|
|
|
}
|
|
|
- self.Data["json"] = encryptedData
|
|
|
+ self.Data["json"] = &Ret{SessionKey: sId, WxUser: wxUser, TokenKey: token}
|
|
|
self.ServeJSON()
|
|
|
}
|
|
|
|
|
|
@@ -241,106 +232,6 @@ func (self *PermitController) SaveWxuserInfo() {
|
|
|
self.ServeJSON()
|
|
|
}
|
|
|
|
|
|
-//小程序登录
|
|
|
-func (self *PermitController) XcxLogin() {
|
|
|
- code := self.GetString("code")
|
|
|
-
|
|
|
- //beego.BeeLogger.Warn("XcxLogin code=%s", code)
|
|
|
-
|
|
|
- if code == "" {
|
|
|
- self.ReturnError(403, apps.ParamsRequired, "", nil)
|
|
|
- }
|
|
|
- appId := beego.AppConfig.String("WxFohowXcxAppId")
|
|
|
- appSecret := beego.AppConfig.String("WxFohowXcxAppSecret")
|
|
|
- key := wx_mp.GetXcxSessionKey(appId, appSecret, code)
|
|
|
- if key == nil {
|
|
|
- self.ReturnError(403, apps.XcxGetSessionKeyError, "", nil)
|
|
|
- }
|
|
|
- //beego.BeeLogger.Warn("XcxLogin key=%s", key)
|
|
|
- //beego.BeeLogger.Warn("XcxLogin key=%s, key.Openid=%s", key, key.Openid)
|
|
|
- //beego.BeeLogger.Warn("XcxLogin key=%s, key.Unionid=%s", key, key.Unionid)
|
|
|
-
|
|
|
- wxUser := user_model.GetByOpenid(key.Openid, false)
|
|
|
- //beego.BeeLogger.Warn("XcxLogin key=[%s], key.Openid=[%s], wxUser= [%s]", key, key.Openid, wxUser)
|
|
|
-
|
|
|
- if wxUser != nil {
|
|
|
- wxUser.FullHead = self.GetFullImgUrl(wxUser.Head)
|
|
|
- self.SetSession(apps.SessionWxUserKey, wxUser.Id)
|
|
|
- }
|
|
|
-
|
|
|
- // 如果微信用户已绑定手机,则找出userId,并且赋值给session[userId]
|
|
|
- if wxUser != nil && wxUser.UserId > 0 {
|
|
|
- self.SetSession(apps.SessionUserKey, wxUser.UserId)
|
|
|
- }
|
|
|
-
|
|
|
- self.SetSession(apps.XcxSessionKey, key.SessionKey)
|
|
|
- //beego.BeeLogger.Warn("XcxLogin SessionKey=%s", key.SessionKey)
|
|
|
- if self.CruSession == nil {
|
|
|
- self.ReturnError(200, apps.NoExist, "", nil)
|
|
|
- }
|
|
|
- //设置code对应的 redis openId unionId 值
|
|
|
- openid_key := lib_redis.GetOpenIdRedisKey(code)
|
|
|
- unionid_key := lib_redis.GetUnionIdRedisKey(code)
|
|
|
- lib_redis.SetRedisValue(openid_key, key.Openid, time.Duration(30*60)*time.Second)
|
|
|
- lib_redis.SetRedisValue(unionid_key, key.Unionid, time.Duration(30*60)*time.Second)
|
|
|
-
|
|
|
- sId := self.CruSession.SessionID()
|
|
|
- // beego.BeeLogger.Warn("XcxLogin sId=%s", sId)
|
|
|
- type Ret struct {
|
|
|
- SessionKey string `json:"session_key"`
|
|
|
- WxUser *user_model.WxUser `json:"wx_user"`
|
|
|
- }
|
|
|
- self.Data["json"] = &Ret{SessionKey: sId, WxUser: wxUser}
|
|
|
-
|
|
|
- self.ServeJSON()
|
|
|
-}
|
|
|
-
|
|
|
-//小程序登录
|
|
|
-func GetXcxLoginInfo(code string) (openId, unionId string) {
|
|
|
- openid_key := lib_redis.GetOpenIdRedisKey(code)
|
|
|
- unionid_key := lib_redis.GetUnionIdRedisKey(code)
|
|
|
-
|
|
|
- _, openid := lib_redis.GetSimpleValue(openid_key)
|
|
|
- _, unionid := lib_redis.GetSimpleValue(unionid_key)
|
|
|
-
|
|
|
- return openid, unionid
|
|
|
-}
|
|
|
-
|
|
|
-//生成订单ID
|
|
|
-func createUnionId(prefix string) string {
|
|
|
- n := time.Now().Format("20060102150405")
|
|
|
- u := uuid.NewV4().String()
|
|
|
- c := strings.Split(u, "-")
|
|
|
- oId := strings.ToUpper(fmt.Sprintf("%s%s%s", prefix, n, c[0]))
|
|
|
- beego.BeeLogger.Info("createUnionId=%s", oId)
|
|
|
- return oId
|
|
|
-}
|
|
|
-
|
|
|
-func (self *PermitController) XcxTest() {
|
|
|
- wxUser := self.GetCurrentWxUser(true)
|
|
|
- self.Data["json"] = wxUser
|
|
|
- self.ServeJSON()
|
|
|
-}
|
|
|
-
|
|
|
-// // 统一登录路径
|
|
|
-// // 该接口尽量不被前端调用,供服务器开发者使用
|
|
|
-// func (self *PermitController) Login() {
|
|
|
-// cb := self.GetString("cb")
|
|
|
-// url := ""
|
|
|
-// if self.IsWxClient() {
|
|
|
-// url = fmt.Sprintf("%s/login/mp?cb=%s", beego.AppConfig.String("ApiHost"), cb)
|
|
|
-// }
|
|
|
-// self.Redirect(url, 302)
|
|
|
-// return
|
|
|
-// }
|
|
|
-
|
|
|
-// // 退出登录
|
|
|
-// func (self *PermitController) Logout() {
|
|
|
-// self.DelSession(apps.SessionUserKey)
|
|
|
-// self.DelSession(apps.SessionWxUserKey)
|
|
|
-// self.ReturnError(200, apps.HasLogout, "", nil)
|
|
|
-// }
|
|
|
-
|
|
|
//公众号静默授权回调
|
|
|
func (self *PermitController) AfterWxMpAuth() {
|
|
|
// beego.BeeLogger.Warn("AfterWxMpAuth........")
|
|
|
@@ -388,52 +279,6 @@ func (self *PermitController) AfterWxMpAuth() {
|
|
|
self.ServeJSON()
|
|
|
}
|
|
|
|
|
|
-// //公众号静默授权
|
|
|
-// func (self *PermitController) WxMpAuth() {
|
|
|
-// // beego.BeeLogger.Warn("WxMpAuth.......")
|
|
|
-// if !self.IsWxClient() {
|
|
|
-// self.ReturnError(403, apps.NotWeixinClient, "", nil)
|
|
|
-// }
|
|
|
-// _id := self.Ctx.Input.Param(":id")
|
|
|
-// cb := self.GetString("cb")
|
|
|
-// id, _ := strconv.ParseInt(_id, 10, 64)
|
|
|
-// gzh := wx_gongzhonghao_model.GetGZHById(id, true)
|
|
|
-// if gzh == nil {
|
|
|
-// self.ReturnError(403, apps.GongZhongHaoNoExist, "", nil)
|
|
|
-// }
|
|
|
-// appId := gzh.AppId
|
|
|
-// u := strings.Split(uuid.NewV4().String(), "-")[0]
|
|
|
-// state := fmt.Sprintf("AuthCb[%s]", u)
|
|
|
-// cache.Cache.Put(state, cb, 60*time.Second)
|
|
|
-// redirectURI := fmt.Sprintf("%s/auth/mp/%d/after",
|
|
|
-// beego.AppConfig.String("ApiHost"), gzh.Id)
|
|
|
-// scope := "snsapi_base"
|
|
|
-// url := wx_mp.AuthCodeURL(appId, redirectURI, scope, state)
|
|
|
-// // beego.BeeLogger.Warn("wx mp auth, redirect url: %s", url)
|
|
|
-// self.Redirect(url, 302)
|
|
|
-// }
|
|
|
-
|
|
|
-// //check是否授权过某个公众号
|
|
|
-// func (self *PermitController) CheckWxAuth() {
|
|
|
-// _gId := self.Ctx.Input.Param(":id")
|
|
|
-// gId, _ := strconv.ParseInt(_gId, 10, 64)
|
|
|
-// gzh := wx_gongzhonghao_model.GetGZHById(gId, true)
|
|
|
-// type Ret struct {
|
|
|
-// IsAuth int64 `json:"is_auth"`
|
|
|
-// }
|
|
|
-// var auth int64 = 0
|
|
|
-// wxUser := self.GetCurrentWxUser(true)
|
|
|
-// if gzh != nil {
|
|
|
-// authWxUser := user_model.GetAuthWxUserByMpIdAndUnionId(gzh.WxHao, wxUser.Unionid, false)
|
|
|
-// if authWxUser != nil {
|
|
|
-// auth = 1
|
|
|
-// }
|
|
|
-// }
|
|
|
-// // beego.BeeLogger.Warn("check auth: %v", auth)
|
|
|
-// self.Data["json"] = &Ret{IsAuth: auth}
|
|
|
-// self.ServeJSON()
|
|
|
-// }
|
|
|
-
|
|
|
// 微信公众号平台登录
|
|
|
func (self *PermitController) WxMpLogin() {
|
|
|
cb := self.GetString("cb")
|