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/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") // 需要获取 entryPeople := new(models.OrderEntryPerson) exist, err := entryPeople.Auth(account, password, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ENTRYPEOPLE_NOT_EXIST, "录入人员不存在") area := new(models.AreaStore) exist, err = models.Get(area, entryPeople.AreaId) t.CheckErr(err) t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") activity := new(models.Activity) exist, err = models.Get(activity, activityId) t.CheckErr(err) t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") customer := new(models.Customer) exist, err = models.Get(customer, entryPeople.Pid) t.CheckErr(err) t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") // 怎讲activity token, err := jwt.GenJwtToken(define.TYPE_ENTRYPEOPLE, entryPeople.Id, customer.Id, customer.Pid, entryPeople.AreaId, entryPeople.ActivityId) t.CheckErr(err) t.SetSession(define.TOKEN, token) entryPeople.Token = token entryPeople.AreaName = area.Name entryPeople.ActivityName = activity.Name entryPeople.IsSpecial = customer.IsSpecial t.JSON(map[string]interface{}{ "people": entryPeople, }) } 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) 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() if !exist { _, err = models.Add(user) } else { _, err = user.SaveUserInfo(user.Id) } t.CheckErr(err) 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.Type = 0 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_H5USER, 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, "activity": activity, "sign_in": signIn, "sign": signExist, "sign_method": sign.SignMethod, "real_sign_form": sign.RealSignJsonForm, "real_sign_json": sign.RealSignJsonTitle, "tag": "activity", }) } func (t *UserCtl) Login() { activityId := t.MustGetInt("activity_id") wxcode := t.MustGet("code") admin := t.DefaultInt("admin", 0) user := new(models.User) token, err := wechat.GetToken(wxcode) t.CheckErr(err) user.Openid = token.OpenId exist, err := user.GetUserByOpenid(user.Openid) t.CheckErr(err) if !exist { _, err = models.Add(user) t.CheckErr(err) } area := &models.AreaStore{} if admin == 1 { username := t.MustGet("username") password := t.MustGet("password") exist, err := area.Login(activityId, username, password) t.CheckErr(err) t.Assert(exist, code.MSG_ERR_Authority, "不存在地区管理员") if area.UserId != user.Id { area.UserId = user.Id _, err = models.Update(area.Id, area, "user_id") t.CheckErr(err) } } else { exist, err := area.GetByUserId(activityId, user.Id) t.CheckErr(err) if exist { // 管理员 admin = 1 } } jwtToken, err := jwt.GenJwtToken(define.TYPE_H5USER, user.Id, 0, 0, 0, 0) t.CheckErr(err) t.SetSession(define.TOKEN, jwtToken) 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": jwtToken, "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_H5USER, 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("退出成功") }