作者 唐旭辉

提交

... ... @@ -12,10 +12,10 @@ AdminPort = 8088
##数据库连接
# sqlconn ="${MYSQL_CONN||root:sutianxia2015@tcp(115.29.205.99:3306)/opportunity?charset=utf8&loc=Asia%2FShanghai}"
mysql_user = "${MYSQL_USER||root}"
mysql_password = "${MYSQL_PASSWORD||sutianxia2015}"
mysql_host = "${MYSQL_HOST||115.29.205.99}"
mysql_password = "${MYSQL_PASSWORD||sutianxia2018}"
mysql_host = "${MYSQL_HOST||101.37.68.23}"
mysql_port = "${MYSQL_PORT||3306}"
mysql_db_name = "${MYSQL_DB_NAME||opportunity}"
mysql_db_name = "${MYSQL_DB_NAME||opportunity_dev}"
##redis相关配置
redis_add = "${REDIS_HOST||127.0.0.1}"
redis_add_port = "${REDIS_PORT||6379}"
... ...
... ... @@ -102,27 +102,14 @@ func (c *AuthController) LoginSms() {
return
}
//短信校验接口调用
// logintoken, err := serveauth.LoginAuthByUCenter(param.Account, param.Password)
// if err != nil {
// msg = protocol.NewReturnResponse(nil, err)
// return
// }
// err = serveauth.ResetLoginToken(logintoken)
// if err != nil {
// log.Error("token 信息记录数据库失败")
// }
// err = serveauth.ResetLoginTokenRedis(logintoken)
// if err != nil {
// log.Error("token 信息记录redis失败")
// }
// data := map[string]interface{}{
// "access": logintoken,
// }
// logintoken
msg = protocol.NewReturnResponse(nil, nil)
return
}
//SmsCode 发送验证码短信
//@router /auth/smscode
func (c *AuthController) SmsCode() {
var msg *protocol.ResponseMessage
defer func() {
... ... @@ -141,7 +128,8 @@ func (c *AuthController) SmsCode() {
msg = protocol.BadRequestParam("1")
return
}
msg = protocol.NewReturnResponse(nil, nil)
err := serveauth.LoginSmsCodeSend(param.Phone)
msg = protocol.NewReturnResponse(nil, err)
return
}
... ...
... ... @@ -68,7 +68,6 @@ var AuthToken = func(ctx *context.Context) {
redisdata.RefreshLoginTokenExpires(mtoken.UID, mtoken.CompanyID)
ctx.Input.SetData(protocol.HeaderCompanyid, mtoken.CompanyID)
ctx.Input.SetData(protocol.HeaderUserid, mtoken.UID)
return
}
if ok := serveauth.IsJwtErrorExpired(err); ok {
... ... @@ -97,7 +96,7 @@ var AllowOption = func(ctx *context.Context) {
})
f(ctx)
ctx.Output.SetStatus(204)
ctx.Output.Body([]byte("{}"))
ctx.Output.Body(nil)
return
}
... ...
... ... @@ -72,11 +72,11 @@ func CountUserDepartByDepart(departid int64) (int64, error) {
cnt int64
err error
)
sql := `SELECT COUNT(*) FROM user_department AS a
JOIN user_company AS b ON a.user_company_id = b.id
WHERE a.enable_status = 1 AND b.delete_at = 0 AND a.department_id =?`
o := orm.NewOrm()
cnt, err = o.QueryTable(&UserDepartment{}).
Filter("department_id", departid).
Filter("enable_status", 1).
Count()
err = o.Raw(sql, departid).QueryRow(&cnt)
return cnt, err
}
... ...
... ... @@ -42,7 +42,6 @@ type ResponseDepartmentInfo struct {
Name string `json:"name"` //部门名字
ParantID int64 `json:"parentId"` //父级部门Id
Manages []DepartmentManager `json:"manages"` //部门管理员
Members []DepartmentMember `json:"members"` //部门成员
Member int64 `json:"member"` //成员数
}
... ...
... ... @@ -23,6 +23,8 @@ var errmessge ErrorMap = map[string]string{
"10025": "该账号已在其他地方登录",
// "10026": "登录凭证过期",
"10027": "无操作权限",
"10028": "验证码错误",
"10029": "获取验证码失败",
//用户相关
"10031": "无效角色",
"10032": "无效部门",
... ...
... ... @@ -182,7 +182,7 @@ func LoginAuthByUCenter(account, password string) (protocol.LoginAuthToken, erro
}
userdata, err = models.GetUserByUCenter(uclientReturn.Data.Id)
if err != nil {
log.Debug("GetUserByPhone(%s) err:%s", account, err)
log.Debug("GetUserByUCenter(%d) err:%s", uclientReturn.Data.Id, err)
return logintoken, protocol.NewErrWithMessage("10021")
}
if ok := userdata.IsDelete(); ok {
... ... @@ -231,7 +231,7 @@ type companybase struct {
}
//getUserCompanyReal 获取用户有效的公司id
func getUserCompanyReal(userid int64) ([]companybase, error) {
func getUserCompanyReal(userId int64) ([]companybase, error) {
const (
datasql0 string = `SELECT b.company_id FROM user_company AS b
WHERE b.delete_at=0 AND b.enable = 1 AND b.user_id=?`
... ... @@ -243,7 +243,7 @@ func getUserCompanyReal(userid int64) ([]companybase, error) {
ids []string
err error
)
err = utils.ExecuteQueryAll(&ids, datasql0, userid)
err = utils.ExecuteQueryAll(&ids, datasql0, userId)
if err != nil {
log.Error("EXECUTE SQL err:%s", err)
return nil, err
... ... @@ -326,3 +326,75 @@ func GetUserHasMenu(userid, companyid int64) ([]protocol.PermissionItem, error)
}
return list, nil
}
func LoginAuthBySmsCode(phone string, code string) (protocol.LoginAuthToken, error) {
var (
err error
logintoken protocol.LoginAuthToken
companys []companybase
companyid int64
userdata *models.User
)
var uclientReturn *ucenter.ResponseLoginSms
uclientReturn, err = ucenter.RequestUCenterLoginSms(phone, code)
if err != nil {
return logintoken, protocol.NewErrWithMessage("10028")
}
userdata, err = models.GetUserByUCenter(uclientReturn.Data.Cuid)
if err != nil {
log.Debug("GetUserByUCenter(%s) err:%s", uclientReturn.Data.Cuid, err)
return logintoken, protocol.NewErrWithMessage("10021")
}
if ok := userdata.IsDelete(); ok {
log.Debug("userdata.IsDelete()==true")
return logintoken, protocol.NewErrWithMessage("10021")
}
if ok := userdata.IsEnable(); !ok {
log.Debug("userdata.IsEnable()==false")
return logintoken, protocol.NewErrWithMessage("10022")
}
if companys, err = getUserCompanyReal(userdata.Id); err != nil {
log.Error("getUserCompanyReal err:%s", err)
return logintoken, protocol.NewErrWithMessage("10021")
}
if len(companys) == 0 {
log.Debug("no company")
return logintoken, protocol.NewErrWithMessage("10022")
}
//获取上一次登录的公司
uAuth, err := models.GetUserAuthByUser(userdata.Id)
if err == nil {
companyid = uAuth.CurrentCompanyId
} else {
companyid = companys[0].Id
}
//更新用户数据
userdata.Accid = uclientReturn.Data.CsAccountID
userdata.Icon = uclientReturn.Data.Image.Path
userdata.ImToken = uclientReturn.Data.Imtoken
userdata.NickName = uclientReturn.Data.Uname
userdata.LastLoginTime = time.Now()
err = models.UpdateUserById(userdata, []string{"Accid", "Icon", "ImToken", "NickName", "LastLoginTime"})
if err != nil {
log.Error("更新用户数据失败:%s", err)
}
logintoken, _ = GenerateAuthToken(userdata.Id, companyid)
return logintoken, err
}
func LoginSmsCodeSend(phone string) error {
var (
err error
)
_, err = models.GetUserByPhone(phone)
if err != nil {
log.Error("GetUserByPhone(%s) err:%s", phone, err)
return protocol.NewErrWithMessage("10027")
}
err = ucenter.RequestUCenterSmsCode(phone)
if err != nil {
return protocol.NewErrWithMessage("10029")
}
return nil
}
... ...
... ... @@ -295,12 +295,12 @@ func DepartmentDelete(param protocol.RequestDepartmentDelete) error {
cnt, err := models.CountUserDepartByDepart(subset.Id)
if err != nil {
log.Error("CountUserDepartByDepart err:%s", err)
return protocol.NewErrWithMessage("10047")
return protocol.NewErrWithMessage("1")
}
if cnt > 0 {
e := fmt.Errorf("user in department,relation:%s", subset.Relation)
log.Error(e.Error())
return protocol.NewErrWithMessage("1", e)
return protocol.NewErrWithMessage("10047", e)
}
if _, ok := toDelete[subset.Id]; ok && subset.Id != pos.Id {
delete(toDelete, subset.Id)
... ... @@ -355,7 +355,7 @@ func DepartmentListAll(companyId int64) ([]protocol.ResponseDepartmentInfo, erro
var manage []protocol.DepartmentManager
manage = v.GetManages()
depart.Manages = manage
depart.Members = v.GetMembers()
// depart.Members = v.GetMembers()
departs = append(departs, depart)
}
... ...
... ... @@ -587,7 +587,7 @@ func GetUserPosition(usercompanyid int64) []protocol.PositionBase {
WHERE a.enable_status =1 AND a.user_company_id =? `
var (
err error
data []protocol.PositionBase
data = make([]protocol.PositionBase, 0)
)
err = utils.ExecuteQueryAll(&data, datasql, usercompanyid)
if err != nil {
... ... @@ -604,7 +604,7 @@ func GetUserRole(usercompanyid int64) []protocol.RoleBase {
WHERE a.enable_status =1 AND a.user_company_id =? `
var (
err error
data []protocol.RoleBase
data = make([]protocol.RoleBase, 0)
)
err = utils.ExecuteQueryAll(&data, datasql, usercompanyid)
if err != nil {
... ...
... ... @@ -44,8 +44,8 @@ func RequestUCenterAddUser(phone string, nickname string, avatar string) (*Respo
Phone: phone,
Nickname: nickname,
Avatar: avatar,
Password: "9d1f5048afd96b39a3dae71a99f2c77a30112d29", //默认密码:fmt.Sprintf("%x", sha1.Sum([]byte("mmm1234554321")))
RegIm: 1,
//Password: "9d1f5048afd96b39a3dae71a99f2c77a30112d29", //默认密码:fmt.Sprintf("%x", sha1.Sum([]byte("mmm1234554321")))
RegIm: 1,
}
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
... ... @@ -92,6 +92,28 @@ func RequestUCenterSmsCode(phone string) error {
return nil
}
//短信验证码登录
func RequestUCenterLoginSms(phone string, captcha string) (*ResponseLoginSms, error) {
return nil, nil
param := RequestLoginSms{
Phone: phone,
Captcha: captcha,
GrantType: "signInCaptcha",
}
uclient := NewUCenterClient()
btBody, err := uclient.Call(param)
if err != nil {
log.Error("统一用户中心请求失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
var ucenterReturn *ResponseLoginSms
err = json.Unmarshal(btBody, ucenterReturn)
if err != nil {
log.Error("解析统一用户中心响应失败 err:%s", err)
return nil, protocol.NewErrWithMessage("1")
}
if !(ucenterReturn.Code == ResponseCode0 &&
ucenterReturn.Msg == ResponseMsgOk) {
return nil, protocol.NewErrWithMessage("10040")
}
return ucenterReturn, nil
}
... ...
... ... @@ -18,6 +18,10 @@ type RequesLogin struct {
Phone string `json:"phone"`
}
var (
_ IUCenterParam = RequesLogin{}
)
//Format 实现IUCenterParam接口
func (r RequesLogin) Format() []byte {
var v []byte
... ... @@ -47,10 +51,10 @@ type ResponseLogin struct {
Data struct {
Id int64 `json:"id"`
Phone string `json:"phone"`
NickName string `json:"nickname"` //昵称
Avatar string `json:"avatar"` //头像
Imtoken string `json:"imtoken"` //网易云imtoken
Accid int64 `json:"accid"`
NickName string `json:"nickname"` //昵称
Avatar string `json:"avatar"` //头像
Imtoken string `json:"imtoken"` //网易云imtoken
Accid int64 `json:"accid"` //网易云id
CustomerAccount int64 `json:"customerAccount"` //客服id
} `json:"data"`
}
... ... @@ -61,9 +65,13 @@ type RequestAddUser struct {
RegIm int8 `json:"regIm"`
Nickname string `json:"nickname"`
Avatar string `json:"avatar"`
Password string `json:"password"`
// Password string `json:"password"`
}
var (
_ IUCenterParam = RequestAddUser{}
)
//Format 实现IUCenterParam接口
func (r RequestAddUser) Format() []byte {
var v []byte
... ... @@ -73,14 +81,11 @@ func (r RequestAddUser) Format() []byte {
//Format 实现IUCenterParam接口
func (r RequestAddUser) GetPath() (string, string) {
return "/users", "POST"
return "/users/save", "POST"
}
//Format 实现IUCenterParam接口
func (r RequestAddUser) Valid() error {
if len(r.Password) == 0 {
return errors.New("len(r.Password) == 0")
}
if len(r.Phone) == 0 {
return errors.New("len(r.Phone == 0")
}
... ... @@ -100,43 +105,15 @@ type ResponseAddUser struct {
} `json:"data"`
}
// type RequestCheckCompany struct {
// CompanyId int64 `json:"company_id"`
// }
// //Format 实现IUCenterParam接口
// func (r RequestCheckCompany) Format() []byte {
// return nil
// }
// //Format 实现IUCenterParam接口
// func (r RequestCheckCompany) GetPath() (string, string) {
// return fmt.Sprintf("/company/%d", r.CompanyId), "GET"
// }
// //Format 实现IUCenterParam接口
// func (r RequestCheckCompany) Valid() error {
// if r.CompanyId == 0 {
// return errors.New("r.CompanyId == 0")
// }
// return nil
// }
// type ResponseCheckCompany struct {
// CommResponse
// Data struct {
// CompanyId int64 `json:"id"` //公司的id
// CompanyName string `json:"name"` //公司的名称
// AdminAccount string `json:"admin_account"` //主管账号
// AdminName string `json:"admin_name"` //主管名称
// } `json:"data"`
// }
//发送验证码短信
type RequestSmsCodeSend struct {
Phone string `json:"phone"`
}
var (
_ IUCenterParam = RequestSmsCodeSend{}
)
func (r RequestSmsCodeSend) Format() []byte {
var bt []byte
bt, _ = json.Marshal(r)
... ... @@ -155,6 +132,22 @@ type RequestLoginSms struct {
GrantType string `json:"grantType"` //登陆方式(signInCaptcha验证码)
}
var (
_ IUCenterParam = RequestLoginSms{}
)
//Format 实现IUCenterParam接口
func (r RequestLoginSms) Format() []byte {
var v []byte
v, _ = json.Marshal(r)
return v
}
//GetPath 实现IUCenterParam接口
func (r RequestLoginSms) GetPath() (string, string) {
return "/auth/loginAggregate", "POST"
}
//ResponseLoginSms 短信验证码登录 响应
type ResponseLoginSms struct {
CommResponse
... ... @@ -168,6 +161,6 @@ type LoginSmsData struct {
Image struct {
Path string `json:"path"`
} `json:"image"`
ImToken string `json:"imtoken"`
Imtoken string `json:"imToken"`
CsAccountID int64 `json:"csAccountID"`
}
... ...