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.
193 lines
5.3 KiB
193 lines
5.3 KiB
package client
|
|
|
|
import (
|
|
"hudongzhuanjia/controllers"
|
|
"hudongzhuanjia/libs/jwt"
|
|
"hudongzhuanjia/libs/wx"
|
|
"hudongzhuanjia/models"
|
|
activity_service "hudongzhuanjia/services/activity"
|
|
"hudongzhuanjia/utils/code"
|
|
"hudongzhuanjia/utils/define"
|
|
"time"
|
|
|
|
"github.com/ouxuanserver/osmanthuswine/src/core"
|
|
)
|
|
|
|
//用户
|
|
type UserCtl struct {
|
|
controllers.BaseCtl
|
|
}
|
|
|
|
// 录入登录
|
|
func (t *UserCtl) EntryLogin() {
|
|
account := t.MustGet("account")
|
|
password := t.MustGet("password")
|
|
activityId := t.MustGetInt64("activity_id") // 需要获取
|
|
|
|
entryPeople := new(models.OrderEntryPerson)
|
|
exist, err := core.GetXormAuto().Where("is_delete=0 and account = ? and password = ? and activity_id = ?",
|
|
account, password, activityId).Get(entryPeople)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在")
|
|
|
|
area := new(models.AreaStore)
|
|
exist, err = models.GetById(area, entryPeople.AreaId)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在")
|
|
|
|
activity := new(models.Activity)
|
|
exist, err = models.GetById(activity, activityId)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在")
|
|
|
|
customer := new(models.Customer)
|
|
exist, err = models.GetById(customer, entryPeople.Pid)
|
|
t.CheckErr(err)
|
|
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)
|
|
t.CheckErr(err)
|
|
t.SetSession(define.TOKEN, token)
|
|
entryPeople.Token = token
|
|
entryPeople.AreaName = area.Name
|
|
entryPeople.ActivityName = activity.Name
|
|
t.JSON(map[string]interface{}{
|
|
"people": entryPeople,
|
|
})
|
|
}
|
|
|
|
func (t *UserCtl) WxLogin() {
|
|
activityId := t.MustGetInt64("activity_id")
|
|
wxcode := t.MustGet("code")
|
|
|
|
activity, exist, err := activity_service.GetActivityById(activityId)
|
|
t.CheckErr(err)
|
|
|
|
customerId := t.DefaultInt64("customer_id", activity.CustomerId)
|
|
customer := new(models.Customer)
|
|
exist, err = models.GetById(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, "地区不存在")
|
|
|
|
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)
|
|
exist, err = user.GetUserByOpenid(info.Openid)
|
|
t.CheckErr(err)
|
|
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()
|
|
if exist {
|
|
core.GetXormAuto().Id(user.Id).AllCols().Update(user)
|
|
} else {
|
|
core.GetXormAuto().InsertOne(user)
|
|
}
|
|
t.CheckErr(err)
|
|
|
|
signIn := "未签到"
|
|
history := new(models.SignHistory)
|
|
exist, err = history.GetByUserId(activityId, user.Id, activity.RehearsalId, area.Id)
|
|
t.CheckErr(err)
|
|
if exist { // 存在数据
|
|
signIn = "已签到"
|
|
}
|
|
sign := new(models.SignUp)
|
|
exist, err = sign.GetByActivityId(activityId)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_SIGN_UP_NOT_EXIST, "签到活动不存在")
|
|
|
|
realSignExist, err := new(models.RealSignList).CheckSignIn(user.Id, activityId)
|
|
t.CheckErr(err)
|
|
|
|
jwtToken, err := jwt.GenJwtToken(define.TYPE_USER, user.Id, customer.Id, customer.Pid, area.Id, activityId, user.Nickname)
|
|
t.CheckErr(err)
|
|
t.SetSession(define.TOKEN, jwtToken)
|
|
|
|
t.JSON(map[string]interface{}{
|
|
"user": user,
|
|
"token": jwtToken,
|
|
"activity": activity,
|
|
"sign_in": signIn,
|
|
"real_sign": realSignExist,
|
|
"real_sign_form": sign.RealSignJsonForm,
|
|
"tag": "activity",
|
|
})
|
|
}
|
|
|
|
// 模拟wx login
|
|
func (t *UserCtl) DebugLogin() {
|
|
uid := t.DefaultInt64("user_id", 1)
|
|
customerId := t.DefaultInt64("customer_id", 1)
|
|
activityId := t.DefaultInt64("activity_id", 5)
|
|
|
|
user := new(models.User)
|
|
exist, err := models.GetById(user, uid)
|
|
t.CheckErr(err)
|
|
t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在")
|
|
|
|
customer := new(models.Customer)
|
|
exist, err = models.GetById(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("user", user.Id, customer.Id, customer.Pid,
|
|
area.Id, activityId, user.Nickname)
|
|
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) Logout() {
|
|
if _, ok := t.Request.SESSION[define.TOKEN]; !ok {
|
|
t.ERROR("未登录", code.MSG_ERR_Authority)
|
|
}
|
|
t.DeleteSession(define.TOKEN)
|
|
t.SUCCESS("退出成功")
|
|
}
|