- auto create weixin user.

This commit is contained in:
gazebo
2018-09-19 11:30:57 +08:00
parent 57b60208e2
commit 5d8017f4a6
5 changed files with 76 additions and 31 deletions

View File

@@ -5,9 +5,6 @@ import (
"time"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/mobile"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
)
@@ -81,17 +78,3 @@ func GetUserInfo(token string) (loginInfo *LoginInfo, err error) {
}
return nil, err
}
func BindMobile(token, mobileNum, code string) (err error) {
loginInfo := new(LoginInfo)
if err = globals.Cacher.GetAs(token, loginInfo); err == nil {
if mobile.VerifyCode(mobileNum, code) {
user := &model.WeiXins{
OpenID: loginInfo.ID,
}
_, err = dao.UpdateEntityByKV(nil, user, utils.Params2Map("Tel", mobileNum), utils.Params2Map("OpenID", loginInfo.ID))
}
err = errors.New("验证码错")
}
return err
}

View File

@@ -8,10 +8,12 @@ import (
"git.rosy.net.cn/baseapi/platformapi/weixinsnsapi"
"git.rosy.net.cn/baseapi/utils"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth"
"git.rosy.net.cn/jx-callback/business/jxcallback/auth/mobile"
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"git.rosy.net.cn/jx-callback/globals/api"
"github.com/astaxie/beego/orm"
)
const (
@@ -24,6 +26,10 @@ var (
StrStateIsWrong = "state:%s状态不对"
)
var (
auther *Auther
)
type Auther struct {
}
@@ -33,7 +39,8 @@ type UserInfoExt struct {
}
func init() {
auth.RegisterAuther(LoginType, new(Auther))
auther = new(Auther)
auth.RegisterAuther(LoginType, auther)
}
func GetUserInfo(code string, state string) (token *UserInfoExt, err error) {
@@ -77,3 +84,27 @@ func (a *Auther) Login(openid, password string) (err error) {
func (a *Auther) Logout(openid string) error {
return globals.Cacher.Del(openid)
}
func BindMobile(token, mobileNum, code, nickname string) (err error) {
loginInfo := new(auth.LoginInfo)
if err = globals.Cacher.GetAs(token, loginInfo); err == nil {
if mobile.VerifyCode(mobileNum, code) {
user := &model.WeiXins{
OpenID: loginInfo.ID,
Tel: mobileNum,
NickName: nickname,
}
db := dao.GetDB()
if err = dao.GetEntity(db, user, "OpenID"); err == nil {
user.Tel = mobileNum
user.NickName = nickname
_, err = dao.UpdateEntity(db, user, "Tel", "NickName")
} else if err == orm.ErrNoRows {
err = dao.CreateEntity(db, user)
}
} else {
err = errors.New("验证码错")
}
}
return err
}

View File

@@ -7,6 +7,7 @@ import (
"git.rosy.net.cn/jx-callback/business/model"
"git.rosy.net.cn/jx-callback/business/model/dao"
"git.rosy.net.cn/jx-callback/globals"
"github.com/astaxie/beego/orm"
)
type StoreUserInfo struct {
@@ -52,20 +53,26 @@ func GetUserInfo(mobile string) (storeUserInfo *StoreUserInfo, err error) {
func UnbindMobile(mobile string) (num int64, err error) {
return dao.UpdateEntityByKV(nil, &model.WeiXins{}, map[string]interface{}{
"Jxstoreid": nil,
"Parentid": -1,
"JxStoreID": nil,
"ParentID": -1,
}, map[string]interface{}{
"Tel": mobile,
})
}
func BindMobile2Store(mobile string, storeID int) (num int64, err error) {
return dao.UpdateEntityByKV(nil, &model.WeiXins{}, map[string]interface{}{
"Jxstoreid": storeID,
"Parentid": -1,
}, map[string]interface{}{
"Tel": mobile,
})
db := dao.GetDB()
user, err2 := verifyMobileIsBlank(db, mobile)
if err = err2; err == nil || err == orm.ErrNoRows {
user.JxStoreID = storeID
if err == nil {
num, err = dao.UpdateEntity(db, user, "JxStoreID")
} else {
err = dao.CreateEntity(db, user)
num = 1
}
}
return num, err
}
func AddMobile2Mobile(parentMobile, mobile string) (num int64, err error) {
@@ -75,7 +82,16 @@ func AddMobile2Mobile(parentMobile, mobile string) (num int64, err error) {
if err = dao.GetEntity(db, parentUser, "Tel"); err == nil {
if parentUser.ParentID == -1 {
globals.SugarLogger.Debug(parentUser)
num, err = dao.UpdateEntityByKV(db, &model.WeiXins{}, utils.Params2Map("ParentID", parentUser.ID), utils.Params2Map("Tel", mobile))
user, err2 := verifyMobileIsBlank(db, mobile)
if err = err2; err == nil || err == orm.ErrNoRows {
user.ParentID = parentUser.ID
if err == nil {
num, err = dao.UpdateEntity(db, user, "ParentID")
} else {
err = dao.CreateEntity(db, user)
num = 1
}
}
} else {
err = fmt.Errorf("%s本身是成员", parentMobile)
}
@@ -90,3 +106,17 @@ func ChangeMobile(curMobile, expectedMobile string) (num int64, err error) {
"Tel": curMobile,
})
}
func verifyMobileIsBlank(db *dao.DaoDB, mobile string) (user *model.WeiXins, err error) {
user = &model.WeiXins{
Tel: mobile,
}
if err = dao.GetEntity(db, user, "Tel"); err == nil {
if user.ParentID != -1 && user.ParentID != 0 {
err = fmt.Errorf("%s已经是小组成员", mobile)
} else if user.JxStoreID != 0 {
err = fmt.Errorf("%s本身已经是%d的组长", mobile, user.JxStoreID)
}
}
return user, err
}

View File

@@ -3,9 +3,9 @@ package model
type WeiXins struct {
ID int `orm:"column(id)" json:"id"`
JxStoreID int `orm:"column(jxstoreid)" json:"storeID"`
OpenID string `orm:"column(openid);size(70);index" json:"openID"`
Tel string `orm:"size(15);index" json:"tel"`
ParentID int `orm:"column(parentid)" json:"parentID"`
OpenID string `orm:"column(openid);size(70);unique;default(null)" json:"openID"`
Tel string `orm:"size(15);unique" json:"tel"`
ParentID int `orm:"column(parentid);default(-1)" json:"parentID"`
NickName string `orm:"column(nickname);size(30)" json:"nickname"`
}

View File

@@ -122,12 +122,13 @@ func (c *AuthController) SendMobileVerifyCode() {
// @Param token header string true "认证token"
// @Param mobile formData string true "手机号"
// @Param code formData string true "验证码"
// @Param nickname formData string true "用户名"
// @Success 200 {object} controllers.CallResult
// @Failure 200 {object} controllers.CallResult
// @router /BindMobile [post]
func (c *AuthController) BindMobile() {
c.callBindMobile(func(params *tAuthBindMobileParams) (retVal interface{}, errCode string, err error) {
err = auth.BindMobile(params.Token, params.Mobile, params.Code)
err = weixin.BindMobile(params.Token, params.Mobile, params.Code, params.Nickname)
return retVal, "", err
})
}