|
|
@ -2,10 +2,9 @@ package client |
|
|
|
|
|
|
|
import ( |
|
|
|
mpoauth2 "github.com/chanxuehong/wechat/mp/oauth2" |
|
|
|
"github.com/chanxuehong/wechat/oauth2" |
|
|
|
"hudongzhuanjia/controllers" |
|
|
|
"hudongzhuanjia/libs/jwt" |
|
|
|
"hudongzhuanjia/libs/wx" |
|
|
|
"hudongzhuanjia/libs/wechat" |
|
|
|
"hudongzhuanjia/models" |
|
|
|
activity_service "hudongzhuanjia/services/activity" |
|
|
|
"hudongzhuanjia/utils/code" |
|
|
@ -48,7 +47,7 @@ func (t *UserCtl) EntryLogin() { |
|
|
|
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") |
|
|
|
|
|
|
|
// 怎讲activity
|
|
|
|
token, err := jwt.GenJwtToken("entry", entryPeople.Id, customer.Id, customer.Pid, entryPeople.AreaId, entryPeople.ActivityId, entryPeople.Name) |
|
|
|
token, err := jwt.GenJwtToken("entry", entryPeople.Id, customer.Id, customer.Pid, entryPeople.AreaId, entryPeople.ActivityId) |
|
|
|
t.CheckErr(err) |
|
|
|
t.SetSession(define.TOKEN, token) |
|
|
|
entryPeople.Token = token |
|
|
@ -59,11 +58,10 @@ func (t *UserCtl) EntryLogin() { |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
// snsapi_userinfo
|
|
|
|
func (t *UserCtl) WxLogin() { |
|
|
|
activityId := t.MustGetInt64("activity_id") |
|
|
|
wxcode := t.MustGet("code") |
|
|
|
//kind, _ := t.Get("type") // mini 小程序登陆 base 静默授权 userinfo 或者为空 非静默授权
|
|
|
|
kind, _ := t.Get("type") // mini 小程序登陆 base 静默授权 userinfo 或者为空 非静默授权
|
|
|
|
|
|
|
|
activity, exist, err := activity_service.GetActivityById(activityId) |
|
|
|
t.CheckErr(err) |
|
|
@ -83,41 +81,57 @@ func (t *UserCtl) WxLogin() { |
|
|
|
t.CheckErr(err) |
|
|
|
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") |
|
|
|
|
|
|
|
token, err := wx.GetToken(wxcode) |
|
|
|
t.CheckErr(err) |
|
|
|
if token.AccessToken == "" || token.Openid == "" { |
|
|
|
t.ERROR("code无效", code.MSG_ERR) |
|
|
|
} |
|
|
|
info, err := wx.GetUserInfo(token.AccessToken, token.Openid) |
|
|
|
t.CheckErr(err) |
|
|
|
user := new(models.User) |
|
|
|
user.Nickname = info.Nickname |
|
|
|
user.Openid = info.Openid |
|
|
|
user.Gender = func() string { |
|
|
|
if info.Sex == 1 { |
|
|
|
return "男" |
|
|
|
} |
|
|
|
return "女" |
|
|
|
}() |
|
|
|
user.ActivityId = activityId |
|
|
|
user.AreaId = area.Id |
|
|
|
user.AreaName = area.Name |
|
|
|
user.Avatar = info.Headimgurl |
|
|
|
user.Unionid = info.Unionid |
|
|
|
user.City = info.City |
|
|
|
user.Province = info.Province |
|
|
|
user.Country = info.Country |
|
|
|
user.CreatedAt = time.Now() |
|
|
|
user.UpdatedAt = time.Now() |
|
|
|
|
|
|
|
exist, err = user.GetUserByOpenid(info.Openid) |
|
|
|
t.CheckErr(err) |
|
|
|
if exist { |
|
|
|
_, err = core.GetXormAuto().Id(user.Id).AllCols().Update(user) |
|
|
|
} else { |
|
|
|
_, err = core.GetXormAuto().InsertOne(user) |
|
|
|
if kind == "mini" { |
|
|
|
session, err := wechat.MiniAuth(wxcode) |
|
|
|
t.CheckErr(err) |
|
|
|
user.Openid = session.OpenId |
|
|
|
|
|
|
|
} else if kind == "base" { |
|
|
|
token, err := wechat.GetToken(wxcode) |
|
|
|
t.CheckErr(err) |
|
|
|
user.Openid = token.OpenId |
|
|
|
} else if kind == "" || kind == "userinfo" { |
|
|
|
//token, err := wx.GetToken(wxcode)
|
|
|
|
token, err := wechat.GetToken(wxcode) |
|
|
|
t.CheckErr(err) |
|
|
|
if token.AccessToken == "" || token.OpenId == "" { |
|
|
|
t.ERROR("code无效", code.MSG_ERR) |
|
|
|
} |
|
|
|
//info, err := wx.GetUserInfo(token.AccessToken, token.OpenId)
|
|
|
|
info, err := wechat.GetUserInfo(token) |
|
|
|
t.CheckErr(err) |
|
|
|
user.Nickname = info.Nickname |
|
|
|
user.Openid = info.OpenId |
|
|
|
user.Gender = func() string { |
|
|
|
if info.Sex == 1 { |
|
|
|
return "男" |
|
|
|
} |
|
|
|
return "女" |
|
|
|
}() |
|
|
|
user.Avatar = info.HeadImageURL |
|
|
|
user.Unionid = info.UnionId |
|
|
|
user.City = info.City |
|
|
|
user.Province = info.Province |
|
|
|
user.Country = info.Country |
|
|
|
user.CreatedAt = time.Now() |
|
|
|
user.UpdatedAt = time.Now() |
|
|
|
//user.Openid = info.OpenId
|
|
|
|
} |
|
|
|
|
|
|
|
err = user.SaveAndUpdateWithOpenId() |
|
|
|
t.CheckErr(err) |
|
|
|
//exist, err = user.GetUserByOpenid(user.Openid)
|
|
|
|
//t.CheckErr(err)
|
|
|
|
//if exist {
|
|
|
|
// _, err = core.GetXormAuto().Id(user.Id).AllCols().Update(user)
|
|
|
|
//} else {
|
|
|
|
// _, err = core.GetXormAuto().InsertOne(user)
|
|
|
|
//}
|
|
|
|
//t.CheckErr(err)
|
|
|
|
|
|
|
|
history := new(models.SignHistory) |
|
|
|
signExist, err := history.GetByUserId(activityId, user.Id, activity.RehearsalId, area.Id) |
|
|
@ -135,7 +149,7 @@ func (t *UserCtl) WxLogin() { |
|
|
|
realSignExist, err := new(models.RealSignHistory).Check(user.Id, activity.Id, activity.RehearsalId) |
|
|
|
t.CheckErr(err) |
|
|
|
|
|
|
|
jwtToken, err := jwt.GenJwtToken(define.TYPE_USER, user.Id, customer.Id, customer.Pid, area.Id, activityId, user.Nickname) |
|
|
|
jwtToken, err := jwt.GenJwtToken(define.TYPE_USER, user.Id, customer.Id, customer.Pid, area.Id, activityId) |
|
|
|
t.CheckErr(err) |
|
|
|
t.SetSession(define.TOKEN, jwtToken) |
|
|
|
|
|
|
@ -176,8 +190,7 @@ func (t *UserCtl) DebugLogin() { |
|
|
|
t.CheckErr(err) |
|
|
|
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") |
|
|
|
|
|
|
|
jwtToken, err := jwt.GenJwtToken("user", user.Id, customer.Id, customer.Pid, |
|
|
|
area.Id, activityId, user.Nickname) |
|
|
|
jwtToken, err := jwt.GenJwtToken("user", user.Id, customer.Id, customer.Pid, area.Id, activityId) |
|
|
|
t.CheckErr(err) |
|
|
|
t.SetSession(define.TOKEN, jwtToken) |
|
|
|
t.JSON(map[string]interface{}{ |
|
|
@ -190,6 +203,29 @@ type UserAuthCtl struct { |
|
|
|
controllers.AuthorCtl |
|
|
|
} |
|
|
|
|
|
|
|
func (t *UserAuthCtl) SaveUserInfo() { |
|
|
|
uid := t.MustGetUID() |
|
|
|
|
|
|
|
info := mpoauth2.UserInfo{} |
|
|
|
t.RequestToStruct(&info) |
|
|
|
user := models.User{} |
|
|
|
user.Nickname = info.Nickname |
|
|
|
user.Gender = func() string { |
|
|
|
if info.Sex == 1 { |
|
|
|
return "男" |
|
|
|
} |
|
|
|
return "女" |
|
|
|
}() |
|
|
|
user.Avatar = info.HeadImageURL |
|
|
|
user.Unionid = info.UnionId |
|
|
|
user.City = info.City |
|
|
|
user.Province = info.Province |
|
|
|
user.Country = info.Country |
|
|
|
_, err := user.SaveUserInfo(uid) |
|
|
|
t.CheckErr(err) |
|
|
|
t.SUCCESS("保存成功") |
|
|
|
} |
|
|
|
|
|
|
|
//退出
|
|
|
|
func (t *UserAuthCtl) Logout() { |
|
|
|
if _, ok := t.Request.SESSION[define.TOKEN]; !ok { |
|
|
|