互动
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

351 lines
9.5 KiB

package client
import (
"fmt"
"hudongzhuanjia/controllers"
"hudongzhuanjia/libs/im"
"hudongzhuanjia/libs/jwt"
"hudongzhuanjia/libs/wechat"
"hudongzhuanjia/logger"
"hudongzhuanjia/models"
activity_service "hudongzhuanjia/services/activity"
"hudongzhuanjia/utils"
"hudongzhuanjia/utils/code"
"hudongzhuanjia/utils/define"
"time"
mpoauth2 "github.com/chanxuehong/wechat/mp/oauth2"
)
//用户
type UserCtl struct {
controllers.BaseCtl
}
// 录入登录
func (t *UserCtl) EntryLogin() {
account := t.MustGet("account")
password := t.MustGet("password")
activityId := t.MustGetInt("activity_id") // 需要获取
entry := new(models.OrderEntryPerson)
err := entry.Auth(account, password, activityId)
t.CheckErr(err)
activity := new(models.Activity)
exist, err := activity.Get(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
area := new(models.AreaStore)
exist, err = area.Get(area, entry.AreaId)
t.CheckErr(err)
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
if activity.MoreAreaMode == define.StatusClose && area.IsMainArea != 1 {
t.ERROR("用户密码不存在", code.MSG_ERR_LOGIN_ERR)
}
customer := new(models.Customer)
exist, err = customer.Get(customer, entry.Pid)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
// 怎讲activity
//token, err := jwt.GenJwtToken(define.TYPE_ENTRYPEOPLE, entry.Id, customer.Id, customer.Pid, entry.AreaId, entry.ActivityId)
//t.CheckErr(err)
if entry.Token == "" {
entry.Token = utils.GenToken(entry.Name)
}
entry.AreaName = area.Name
entry.ActivityName = activity.Name
entry.IsSpecial = customer.IsSpecial
_, err = entry.Update(entry, entry.Id)
t.CheckErr(err)
entry.Token = fmt.Sprintf("%s:%s", define.TYPE_ENTRYPEOPLE, entry.Token)
t.SetSession(define.TOKEN, entry.Token)
t.JSON(map[string]interface{}{
"people": entry,
"more_area_mode": activity.MoreAreaMode,
})
}
func (t *UserCtl) WxLogin() {
activityId := t.MustGetInt("activity_id")
wxcode := t.MustGet("code")
logger.Error("微信code:", wxcode)
activity, exist, err := activity_service.GetActivityById(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
customer := new(models.Customer)
exist, err = models.Get(customer, activity.CustomerId)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
area := new(models.AreaStore)
exist, err = area.GetByCustomerId(customer.Id, activity.Id)
t.CheckErr(err)
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
token, err := wechat.GetToken(wxcode)
t.CheckErr(err)
if token.AccessToken == "" || token.OpenId == "" {
t.ERROR("code无效", code.MSG_ERR)
}
info, err := wechat.GetUserInfo(token)
t.CheckErr(err)
user := new(models.User)
exist, err = user.GetUserByOpenid(info.OpenId)
t.CheckErr(err)
if user.Token == "" {
user.Token = utils.GenToken(user.Openid)
}
user.Nickname = info.Nickname
user.Openid = info.OpenId
user.Gender = utils.GetGender(info.Sex)
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()
if !exist {
_, err = user.Add(user)
} else {
_, err = user.Update(user, user.Id)
}
t.CheckErr(err)
// 校验邀请函是否开启
var requireInvite = true
invitation := new(models.Invitation)
exist, err = invitation.GetInvitationByActivityId(activityId)
t.CheckErr(err)
//t.Assert(exist, code.MSG_INVITE_SETTING_NOT_EXIST, "邀请函设置不存在")
if exist { // 存在邀请函
letter := new(models.InvitationLetter)
exist, err = letter.GetByUserIdAndActivityId(user.Id, activity.Id, activity.ArchId, activity.RehearsalId)
t.CheckErr(err)
if !exist {
requireInvite = false
}
//t.Assert(exist, code.MSG_INVITE_LETTER_NOT_EXIST, "您尚未接受过邀请")
}
sign := new(models.SignUp)
exist, err = sign.GetByActivityId(activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_SIGN_UP_NOT_EXIST, "签到活动不存在")
history := new(models.SignHistory)
signExist, err := history.GetByUserId(activityId, activity.ArchId, user.Id, activity.RehearsalId, area.Id)
t.CheckErr(err)
var signIn = "未签到"
if customer.IsSpecial == 1 {
if signExist {
history = new(models.SignHistory)
history.UserId = user.Id
history.RehearsalId = activity.RehearsalId
history.ActivityId = activityId
history.SignRuleId = sign.Id
history.AreaId = area.Id
history.ArchId = activity.ArchId
history.Status = 2
_, err = models.Add(history)
t.CheckErr(err)
}
signIn = "已签到"
} else {
if signExist && history.Status == 2 { // 存在数据
signIn = "已签到"
}
}
//jwtToken, err := jwt.GenJwtToken(define.TYPE_USER, user.Id, customer.Id, customer.Pid, area.Id, activityId)
//t.CheckErr(err)
user.Token = fmt.Sprintf("%s:%s", define.TYPE_USER, user.Token)
t.SetSession(define.TOKEN, user.Token)
t.JSON(map[string]interface{}{
"user": user,
"token": user.Token,
"activity": activity,
"sign_in": signIn,
"sign": signExist,
"sign_method": sign.SignMethod,
"real_sign_form": sign.RealSignJsonForm,
"real_sign_json": sign.RealSignJsonTitle,
"tag": "activity",
"require_invite": requireInvite,
})
}
func (t *UserCtl) Login() {
wxcode := t.MustGet("code")
activityId := t.MustGetInt("activity_id")
areaId := t.MustGetInt("area_id")
activity := &models.Activity{}
exist, err := models.Get(activity, activityId)
t.CheckErr(err)
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
area := &models.AreaStore{}
exist, err = models.Get(area, areaId)
t.CheckErr(err)
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "此直播间已被删除")
//if activity.MoreAreaMode == define.StatusClose && area.IsMainArea != 1 {
// t.ERROR("多地区功能关闭,链接失效,请打开后再试", code.MSG_AREASTORE_CLOSED)
//}
token, err := wechat.GetToken(wxcode)
t.CheckErr(err)
info, err := wechat.GetUserInfo(token)
t.CheckErr(err)
user := new(models.User)
exist, err = user.GetUserByOpenid(info.OpenId)
t.CheckErr(err)
if user.Token == "" {
user.Token = utils.GenToken(user.Openid)
}
user.Nickname = info.Nickname
user.Openid = info.OpenId
user.Gender = utils.GetGender(info.Sex)
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()
if !exist {
_, err = user.Add(user)
} else {
_, err = user.Update(user, user.Id)
}
t.CheckErr(err)
admin := 0
if username, ok := t.Get("username"); ok && username != "" {
username := t.MustGet("username")
password := t.MustGet("password")
exist, err := area.Login(activityId, username, password)
t.CheckErr(err)
t.Assert(exist, code.MSG_ERR_LOGIN_ERR, "不存在地区管理员")
if area.UserId != user.Id {
area.UserId = user.Id
_, err = models.Update(area.Id, area, "user_id")
t.CheckErr(err)
}
admin = 1
} else {
exist, err := area.GetByUserId(activityId, user.Id)
t.CheckErr(err)
if exist { // 管理员
admin = 1
}
}
user.Token = fmt.Sprintf("%s:%s", define.TYPE_LIVEUSER, user.Token)
t.SetSession(define.TOKEN, user.Token)
sign, err := im.AccountImport(fmt.Sprintf("%d", user.Id), user.Nickname, user.Avatar)
t.CheckErr(err)
user.Sig = sign
t.JSON(map[string]interface{}{
"user": user,
"token": user.Token,
"area": area,
"admin": admin,
})
}
// 模拟wx login
func (t *UserCtl) DebugLogin() {
uid := t.DefaultInt("user_id", 1)
customerId := t.DefaultInt("customer_id", 1)
activityId := t.DefaultInt("activity_id", 5)
user := new(models.User)
exist, err := models.Get(user, uid)
t.CheckErr(err)
t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
customer := new(models.Customer)
exist, err = models.Get(customer, customerId)
t.CheckErr(err)
t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在")
area := new(models.AreaStore)
if customer.Pid == 0 {
exist, err = area.GetMainAreaById(activityId)
} else {
exist, err = area.GetAreaStoreById(customer.AreaId)
}
t.CheckErr(err)
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
jwtToken, err := jwt.GenJwtToken(define.TYPE_USER, user.Id, customer.Id, customer.Pid, area.Id, activityId)
t.CheckErr(err)
t.SetSession(define.TOKEN, jwtToken)
t.JSON(map[string]interface{}{
"user": user,
"token": jwtToken,
})
}
type UserAuthCtl struct {
controllers.AuthorCtl
}
func (t *UserAuthCtl) SaveUserInfo() {
uid := t.GetAccountId()
info := mpoauth2.SessionInfo{}
t.RequestToStruct(&info)
if info.Nickname == "" && info.AvatarUrl == "" {
t.ERROR("参数错误", code.MSG_ERR_Param)
return
}
user := models.User{}
user.Nickname = info.Nickname
user.Gender = func() string {
if info.Gender == 1 {
return "男"
}
return "女"
}()
user.Avatar = info.AvatarUrl
user.Unionid = info.UnionId
user.City = info.City
user.Province = info.Province
user.Country = info.Country
_, err := user.SaveUserInfo(uid)
t.CheckErr(err)
//
//exist, err := models.Get(&user, uid)
//t.CheckErr(err)
//t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
user.Id = uid
sign, err := im.AccountImport(fmt.Sprintf("%d", user.Id), user.Avatar, user.Nickname)
t.CheckErr(err)
user.Sig = sign
t.JSON(user)
}
//退出
func (t *UserAuthCtl) Logout() {
if _, ok := t.Request.SESSION[define.TOKEN]; !ok {
t.ERROR("未登录", code.MSG_ERR_Authority)
}
t.DeleteSession(define.TOKEN)
t.SUCCESS("退出成功")
}