Browse Source

user

master
黄梓健 5 years ago
parent
commit
3c7fcdb244
  1. 4
      controllers/author.go
  2. 110
      controllers/client/login.go
  3. 2
      go.mod
  4. 4
      libs/jwt/jwt_go.go
  5. 27
      libs/wechat/wx.go
  6. 16
      models/user.go

4
controllers/author.go

@ -17,9 +17,9 @@ func (t *AuthorCtl) Prepare() {
token := ""
if tokenStr, ok := t.Request.SESSION[define.TOKEN]; ok {
token = tokenStr
} else if tokenStr, ok := t.Request.REQUEST[define.TOKEN]; ok {
} else if tokenStr, ok = t.Request.REQUEST[define.TOKEN]; ok {
token = tokenStr
} else if tokenStr, ok := t.Request.HEADER[define.TOKEN]; ok {
} else if tokenStr, ok = t.Request.HEADER[define.TOKEN]; ok {
token = tokenStr
} else {
var param = make(map[string]interface{}, 0)

110
controllers/client/login.go

@ -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 {

2
go.mod

@ -6,7 +6,7 @@ require (
github.com/360EntSecGroup-Skylar/excelize/v2 v2.0.1
github.com/agrison/go-tablib v0.0.0-20160310143025-4930582c22ee
github.com/alecthomas/template v0.0.0-20190718012654-fb15b899a751
github.com/chanxuehong/wechat v0.0.0-20190521093015-fafb751f9916 // indirect
github.com/chanxuehong/wechat v0.0.0-20190521093015-fafb751f9916
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/elazarl/go-bindata-assetfs v1.0.0 // indirect
github.com/go-bindata/go-bindata v3.1.2+incompatible // indirect

4
libs/jwt/jwt_go.go

@ -14,19 +14,17 @@ type Claims struct {
AccountId int64
CustomerId int64
CustomerPid int64
Username string
ActivityId int64
AreaId int64
jwt.StandardClaims
}
func GenJwtToken(accountType string, accountId, customerId, customerPid, areaId, activityId int64, username string) (string, error) {
func GenJwtToken(accountType string, accountId, customerId, customerPid, areaId, activityId int64) (string, error) {
claims := Claims{
accountType,
accountId,
customerId,
customerPid,
username,
activityId,
areaId,
jwt.StandardClaims{

27
libs/wechat/wx.go

@ -1 +1,28 @@
package wechat
import (
mpoauth2 "github.com/chanxuehong/wechat/mp/oauth2"
"github.com/chanxuehong/wechat/oauth2"
)
var (
Appid = "wx7b0bcf476552c5e9"
Secret = "f6aabdd40ea25272f4442603a7dc8028"
)
var Endpoint = mpoauth2.NewEndpoint(Appid, Secret)
func GetToken(code string) (*oauth2.Token, error) {
client := oauth2.Client{
Endpoint: Endpoint,
}
return client.ExchangeToken(code)
}
func GetUserInfo(tk *oauth2.Token) (*mpoauth2.UserInfo, error) {
return mpoauth2.GetUserInfo(tk.AccessToken, tk.OpenId, "", nil)
}
func MiniAuth(code string) (*mpoauth2.Session, error) {
return mpoauth2.GetSession(Endpoint, code)
}

16
models/user.go

@ -6,7 +6,7 @@ import (
"github.com/ouxuanserver/osmanthuswine/src/core"
)
const UserTableName = TableNamePrefix + "user"
const UserTN = TableNamePrefix + "user"
//用户
type User struct {
@ -32,7 +32,7 @@ type User struct {
}
func (t *User) TableName() string {
return UserTableName
return UserTN
}
func (t *User) Alias(name string) string {
@ -44,15 +44,19 @@ func (t *User) GetUserByOpenid(openId string) (bool, error) {
}
func (t *User) SaveAndUpdateWithOpenId() error {
exist, err := core.GetXormAuto().Table("ox_user").
Where("openid=?", t.Openid).Exist()
exist, err := core.GetXormAuto().Table(UserTN).Where("openid=?", t.Openid).Exist()
if err != nil {
return err
}
if !exist {
_, err := core.GetXormAuto().Insert(t)
_, err = core.GetXormAuto().Insert(t)
return err
}
_, err = core.GetXormAuto().Where("openid=?", t.Openid).AllCols().Update(t)
_, err = core.GetXormAuto().Where("openid=?", t.Openid).Update(t)
return err
}
func (t *User) SaveUserInfo(uid interface{}) (int64, error) {
return core.GetXormAuto().Where("id=?", uid).Cols("nickname", "gender",
"avatar", "unionid", "city", "province", "country").Update(t)
}
Loading…
Cancel
Save