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.
 
 
 

235 lines
5.6 KiB

package routehb
import (
"GoClouds/core/bean/sysBean"
"GoClouds/core/cloud/commCloud"
"GoClouds/core/comms"
"GoClouds/core/utils"
"GoClouds/models"
"GoClouds/service"
"GoClouds/service/userService"
"fmt"
"strings"
"time"
hbtp "github.com/mgr9525/HyperByte-Transfer-Protocol"
gocloud "github.com/mgr9525/go-cloud"
ruisUtil "github.com/mgr9525/go-ruisutil"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson/primitive"
)
type UserRPC struct{}
func (UserRPC) AuthFun() hbtp.AuthFun {
return nil
}
func (UserRPC) GetUsrPerms(c *hbtp.Context, uid string) {
c.ResJson(hbtp.ResStatusOk, userService.FindUserPermissionCache(uid))
}
func (UserRPC) Info(c *hbtp.Context, id string) {
if id == "" {
c.ResString(hbtp.ResStatusErr, "param err")
return
}
e := &models.SysUser{}
key := fmt.Sprintf("uinfo:%s", id)
if err := gocloud.CacheGets(key, e); err != nil {
e = userService.FindXid(id)
gocloud.CacheSets(key, e, time.Hour*2)
}
c.ResJson(hbtp.ResStatusOk, e)
}
func (UserRPC) CheckInfo(c *hbtp.Context, m *ruisUtil.Map) {
xid := m.GetString("xid")
hash := m.GetString("hash")
if xid == "" || hash == "" {
c.ResString(hbtp.ResStatusErr, "param err")
return
}
e := &models.SysUser{}
key := fmt.Sprintf("uinfo:%s", xid)
if err := gocloud.CacheGets(key, e); err != nil {
e = userService.FindXid(xid)
gocloud.CacheSets(key, e, time.Hour*2)
}
if hash != utils.CacLoginHash(e.Pass) {
c.ResString(hbtp.ResStatusErr, "param err")
return
}
c.ResJson(hbtp.ResStatusOk, e)
}
func (UserRPC) Login(c *hbtp.Context, m *sysBean.LoginReq) {
if m.Name == "" {
c.ResString(hbtp.ResStatusErr, "param err:name!")
return
}
if m.Pass == "" {
c.ResString(hbtp.ResStatusErr, "param err:pass!")
return
}
ret := &sysBean.LoginRes{}
usr := userService.FindName(m.Name)
if usr == nil {
ret.Errs = "未找到用户"
c.ResJson(hbtp.ResStatusOk, ret)
return
}
if len(usr.Xid) <= 0 {
usr.Xid = fmt.Sprintf("%d", usr.Id)
comms.DbSysHelper.GetDB().Cols("xid").Where("id=?", usr.Id).Update(usr)
}
if usr.Pass != strings.ToUpper(ruisUtil.Md5String(m.Pass)) {
ret.Errs = "密码错误"
c.ResJson(hbtp.ResStatusOk, ret)
return
}
/*if !sysCloud.CheckPermission(usr.Xid, "login") {
c.PlainText(514, []byte("帐号禁止登录"))
return
}*/
usr.Logintm = time.Now()
comms.DbSysHelper.GetDB().Cols("logintm").Where("xid=?", usr.Xid).Update(usr)
ret.Stat = 1
ret.Uid = usr.Id
ret.Xid = usr.Xid
ret.Name = usr.Name
ret.SetHash(usr.Pass)
gocloud.CacheSets(fmt.Sprintf("uinfo:%s", usr.Xid), nil)
c.ResJson(hbtp.ResStatusOk, ret)
}
func (UserRPC) Reg(c *hbtp.Context, m *sysBean.RegReq) {
if m.Name == "" {
c.ResString(hbtp.ResStatusErr, "param err:name!")
return
}
ret := &sysBean.LoginRes{}
ne := userService.FindName(m.Name)
if ne != nil {
ret.Errs = "用户已注册过"
c.ResJson(hbtp.ResStatusOk, ret)
return
}
ne = &models.SysUser{}
ne.Xid = ruisUtil.NewXid()
ne.Name = m.Name
ne.Phone = m.Name
if len(m.Pass) > 0 {
ne.Pass = strings.ToUpper(ruisUtil.Md5String(m.Pass))
}
ne.Times = time.Now()
ne.Logintm = time.Now()
_, err := comms.DbSysHelper.GetDB().Insert(ne)
if err != nil {
logrus.Debug("nes SysUser insert err:" + err.Error())
c.ResString(hbtp.ResStatusErr, "插入用户错误!!")
return
}
info := &models.MgoUserInfo{}
info.Id = primitive.NewObjectID()
info.Uid = ne.Xid
info.Name = ne.Name
if len(m.Nick) > 0 {
info.Nick = m.Nick
} else {
info.Nick = fmt.Sprintf("U%s", utils.GenValidateCode(7))
}
_, err = service.MgoUserInfoDao.GetSession().C().InsertOne(nil, info)
if err != nil {
c.ResString(hbtp.ResStatusErr, "qmgo err:"+err.Error())
return
}
ret.Stat = 1
ret.Uid = ne.Id
ret.Xid = ne.Xid
ret.Name = ne.Name
ret.SetHash(ne.Pass)
c.ResJson(hbtp.ResStatusOk, ret)
}
func (UserRPC) Forgot(c *hbtp.Context, m *sysBean.LoginReq) {
if m.Name == "" {
c.ResString(hbtp.ResStatusErr, "param err:name!")
return
}
if m.Pass == "" {
c.ResString(hbtp.ResStatusErr, "param err:pass!")
return
}
ret := &sysBean.LoginRes{}
ne := userService.FindName(m.Name)
if ne == nil {
ret.Errs = "用户未注册过"
c.ResJson(hbtp.ResStatusOk, ret)
return
}
ne.Pass = strings.ToUpper(ruisUtil.Md5String(m.Pass))
_, err := comms.DbSysHelper.GetDB().Cols("pass").Where("id=?", ne.Id).Update(ne)
if err != nil || ne.Id <= 0 {
ret.Errs = "修改用户错误"
c.ResJson(hbtp.ResStatusOk, ret)
return
}
ret.Stat = 1
ret.Uid = ne.Id
ret.Xid = ne.Xid
ret.Name = ne.Name
ret.SetHash(ne.Pass)
c.ResJson(hbtp.ResStatusOk, ret)
}
func (UserRPC) Uppass(c *hbtp.Context, m *sysBean.UppassReq) {
if m.Xid == "" {
c.ResString(hbtp.ResStatusErr, "param err:xid!")
return
}
if m.Pass == "" || m.NPass == "" {
c.ResString(hbtp.ResStatusErr, "param err:pass!")
return
}
ret := &sysBean.LoginRes{}
ne := userService.FindXid(m.Xid)
if ne == nil {
ret.Errs = "用户未找到"
c.ResJson(hbtp.ResStatusOk, ret)
return
}
if commCloud.CheckPermission(ne.Xid, "comm:uppass") {
ret.Errs = "该用户禁止修改密码"
c.ResJson(hbtp.ResStatusOk, ret)
return
}
if ne.Pass != strings.ToUpper(ruisUtil.Md5String(m.Pass)) {
ret.Errs = "旧密码错误"
c.ResJson(hbtp.ResStatusOk, ret)
return
}
ne.Pass = strings.ToUpper(ruisUtil.Md5String(m.NPass))
_, err := comms.DbSysHelper.GetDB().Cols("pass").Where("id=?", ne.Id).Update(ne)
if err != nil || ne.Id <= 0 {
ret.Errs = "修改用户错误"
c.ResJson(hbtp.ResStatusOk, ret)
return
}
commCloud.ClearUserCache(ne.Xid)
ret.Stat = 1
ret.Uid = ne.Id
ret.Xid = ne.Xid
ret.Name = ne.Name
ret.SetHash(ne.Pass)
c.ResJson(hbtp.ResStatusOk, ret)
}