From 63d8ff85fa970b8693c2150d2df2f2205f4858cf Mon Sep 17 00:00:00 2001 From: tommy <3405129587@qq.com> Date: Thu, 2 Apr 2020 09:40:46 +0800 Subject: [PATCH] master --- controllers/client/login.go | 67 +++++++++++++++++++++++++++++++++++++-------- controllers/client/sign.go | 60 ++++++++++++++++++++++++++++++++-------- log/hdzj.log | 3 ++ models/sign_history.go | 5 ++++ 4 files changed, 112 insertions(+), 23 deletions(-) diff --git a/controllers/client/login.go b/controllers/client/login.go index 62dcae9..424b852 100644 --- a/controllers/client/login.go +++ b/controllers/client/login.go @@ -89,19 +89,16 @@ func (t *UserCtl) WxLogin() { session, err := wechat.MiniAuth(wxcode) t.CheckErr(err) user.Openid = session.OpenId - } else if _type == "base" { token, err := wechat.GetToken(wxcode) t.CheckErr(err) user.Openid = token.OpenId - } else if _type == "" || _type == "userinfo" { - //token, err := wx.GetToken(wxcode) + } else if _type == "" { 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 @@ -124,14 +121,6 @@ func (t *UserCtl) WxLogin() { 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) @@ -165,6 +154,60 @@ func (t *UserCtl) WxLogin() { }) } +func (t *UserCtl) Login() { + wxcode := t.MustGet("code") + _type, _ := t.Get("type") + + user := new(models.User) + if _type == "mini" { + session, err := wechat.MiniAuth(wxcode) + t.CheckErr(err) + user.Openid = session.OpenId + } else if _type == "base" { + token, err := wechat.GetToken(wxcode) + t.CheckErr(err) + user.Openid = token.OpenId + } else if _type == "" || _type == "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) + + jwtToken, err := jwt.GenJwtToken(define.TYPE_USER, user.Id, 0, 0, 0, 0) + t.CheckErr(err) + t.SetSession(define.TOKEN, jwtToken) + + t.JSON(map[string]interface{}{ + "user": user, + "token": jwtToken, + }) +} + // 模拟wx login func (t *UserCtl) DebugLogin() { uid := t.DefaultInt64("user_id", 1) diff --git a/controllers/client/sign.go b/controllers/client/sign.go index 07cb61d..d5be999 100644 --- a/controllers/client/sign.go +++ b/controllers/client/sign.go @@ -18,11 +18,40 @@ type SignCtl struct { controllers.AuthorCtl } +func (t *SignCtl) CheckSign() { + activityId := t.MustGetInt64("activity_id") + uid := t.MustGetUID() + + activity := models.Activity{} + exist, err := models.GetById(&activity, activityId) + t.CheckErr(err) + t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "主活动不存在") + + customer := models.Customer{} + exist, err = models.GetById(&customer, activity.CustomerId) + t.CheckErr(err) + t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") + + area := new(models.AreaStore) + if customer.AreaId == 0 { + exist, err = area.GetMainAreaById(activityId) + } else { + exist, err = area.GetAreaStoreById(customer.AreaId) + } + t.CheckErr(err) + t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") + + history := new(models.SignHistory) + signExist, err := history.GetByUserId(activityId, uid, activity.RehearsalId, area.Id) + t.CheckErr(err) + + t.JSON(signExist) +} + //签到动作 func (t *SignCtl) Sign() { uid := t.MustGetUID() activityId := t.MustGetInt64("activity_id") - customerId := t.MustGetInt64("customer_id") _type, _ := t.GetInt("_type") //根据activity_id查找主活动的信息 @@ -32,16 +61,25 @@ func (t *SignCtl) Sign() { t.Assert(exist, code.MSG_ACTIVITY_NOT_EXIST, "互动不存在") t.CheckRunning(activity.Status) - customer := new(models.Customer) - exist, err = models.GetById(customer, customerId) - t.CheckErr(err) - t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") - user := new(models.User) exist, err = models.GetById(user, uid) t.CheckErr(err) t.Assert(exist, code.MSG_USER_NOT_EXIST, "用户不存在") + customer := models.Customer{} + exist, err = models.GetById(customer, activity.CustomerId) + t.CheckErr(err) + t.Assert(exist, code.MSG_CUSTOMER_NOT_EXIST, "客户不存在") + + var area = new(models.AreaStore) + if customer.AreaId == 0 { + exist, err = area.GetMainAreaById(activityId) + } else { + exist, err = area.GetAreaStoreById(customer.AreaId) + } + t.CheckErr(err) + t.Assert(exist, code.MSG_AREASTORE_NOT_EXIST, "地区不存在") + //根据activity_id查找副活动的规则信息 signUp := new(models.SignUp) exist, err = signUp.GetByActivityId(activityId) @@ -79,7 +117,7 @@ func (t *SignCtl) Sign() { signHistory.RehearsalId = activity.RehearsalId signHistory.ActivityId = activityId signHistory.SignRuleId = signUp.Id - signHistory.AreaId = user.AreaId + signHistory.AreaId = area.Id signHistory.IsDelete = false signHistory.UpdatedAt = time.Now() signHistory.CreatedAt = time.Now() @@ -87,9 +125,9 @@ func (t *SignCtl) Sign() { t.CheckErr(err) go ws_send_service.SendSign(fmt.Sprintf("%d", activity.Id), - define.TYPE_CUSTOMER, customerId, map[string]interface{}{ + define.TYPE_CUSTOMER, activity.CustomerId, map[string]interface{}{ "type": "sign_up", - "customer_id": customer.Id, + "customer_id": activity.CustomerId, "data": map[string]interface{}{ "avatar": user.Avatar, "sign_total": signTotal + 1, @@ -100,8 +138,8 @@ func (t *SignCtl) Sign() { } func (t *SignCtl) RealSign() { - aid := t.MustGetActivityId() - uid := t.MustGetUID() + aid := t.MustGetInt64("activity_id") + uid := t.MustGetInt64("user_id") rid := t.MustGetInt64("rehearsal_id") user := new(models.User) diff --git a/log/hdzj.log b/log/hdzj.log index 700b338..1ce7e62 100644 --- a/log/hdzj.log +++ b/log/hdzj.log @@ -6,3 +6,6 @@ 2020-03-12 10:06:16.154 ERROR logger/logger.go:92 check err {"error": "token invalid"} 2020-03-12 10:07:26.864 ERROR logger/logger.go:92 check err {"error": "Error 1146: Table 'hudongzhuanjia.ox_real_sign_history' doesn't exist"} 2020-03-12 15:08:47.495 INFO controllers/base.go:57 get int64 from request error%!(EXTRA *strconv.NumError=strconv.ParseInt: parsing "": invalid syntax) +2020-04-01 18:28:54.848 ERROR logger/logger.go:92 check err {"error": "errcode: 40029, errmsg: invalid code, hints: [ req_id: NIaBpZFFE-LTy2HA ]"} +2020-04-01 18:31:58.104 ERROR logger/logger.go:92 check err {"error": "needs a pointer to a value"} +2020-04-01 18:35:07.725 ERROR logger/logger.go:92 check err {"error": "errcode: 40029, errmsg: invalid code, hints: [ req_id: rIaBI6yFe-2LduEa ]"} diff --git a/models/sign_history.go b/models/sign_history.go index 2661787..ccb9464 100644 --- a/models/sign_history.go +++ b/models/sign_history.go @@ -29,3 +29,8 @@ func (t *SignHistory) GetByUserId(aid, uid, rid, arid int64) (bool, error) { return core.GetXormAuto().Where("is_delete=0 and activity_id=? and user_id=? and "+ "rehearsal_id=? and area_id=?", aid, uid, rid, arid).Get(t) } + +func (t *SignHistory) Count(id, aid, rid interface{}) (int64, error) { + // 签到人数 + return core.GetXormAuto().Where("is_delete=0 and sign_rule_id=? and rehearsal_id=? and activity_id=?", id, aid, rid).Count(t) +}