作者 tangxuhui

增加 :同步数据时的操作,变更用户的手机账号

... ... @@ -404,55 +404,55 @@ func (c *CompanyController) UserList() {
//InitCompany 初始化企业
//@router /ucenter/company [post]
func (c *CompanyController) InitCompany() {
var msg *protocol.ResponseMessage
defer func() {
c.ResposeJson(msg)
}()
// func (c *CompanyController) InitCompany() {
// var msg *protocol.ResponseMessage
// defer func() {
// c.ResposeJson(msg)
// }()
var param protocol.CenterCompanyInfo
if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
log.Error("json 解析失败 err:%s", err)
msg = protocol.BadRequestParam("1")
return
}
if param.CompanyId <= 0 {
log.Error("公司id为0")
msg = protocol.BadRequestParam("1")
return
}
var (
err error
)
param.CompanyName = strings.TrimSpace(param.CompanyName)
param.AdminAccount = strings.TrimSpace(param.AdminAccount)
param.AdminName = strings.TrimSpace(param.AdminName)
if ok := param.IsEnable(); ok {
if len(param.CompanyName) == 0 {
log.Error("公司名称空")
msg = protocol.BadRequestParam("1")
return
}
if len(param.AdminAccount) == 0 {
log.Error("人员账号空")
msg = protocol.BadRequestParam("1")
return
}
if len(param.AdminName) == 0 {
log.Error("人员名称空")
msg = protocol.BadRequestParam("1")
return
}
err = servecompany.InitCompanyInfo(param)
} else if ok := param.IsForbid(); ok {
err = servecompany.ForbidCompany(param.CompanyId)
} else {
err = protocol.NewErrWithMessage("1")
}
// var param protocol.CenterCompanyInfo
// if err := json.Unmarshal(c.Ctx.Input.RequestBody, &param); err != nil {
// log.Error("json 解析失败 err:%s", err)
// msg = protocol.BadRequestParam("1")
// return
// }
// if param.CompanyId <= 0 {
// log.Error("公司id为0")
// msg = protocol.BadRequestParam("1")
// return
// }
// var (
// err error
// )
// param.CompanyName = strings.TrimSpace(param.CompanyName)
// param.AdminAccount = strings.TrimSpace(param.AdminAccount)
// param.AdminName = strings.TrimSpace(param.AdminName)
// if ok := param.IsEnable(); ok {
// if len(param.CompanyName) == 0 {
// log.Error("公司名称空")
// msg = protocol.BadRequestParam("1")
// return
// }
// if len(param.AdminAccount) == 0 {
// log.Error("人员账号空")
// msg = protocol.BadRequestParam("1")
// return
// }
// if len(param.AdminName) == 0 {
// log.Error("人员名称空")
// msg = protocol.BadRequestParam("1")
// return
// }
// err = servecompany.InitCompanyInfo(param)
// } else if ok := param.IsForbid(); ok {
// err = servecompany.ForbidCompany(param.CompanyId)
// } else {
// err = protocol.NewErrWithMessage("1")
// }
msg = protocol.NewReturnResponse(nil, err)
return
}
// msg = protocol.NewReturnResponse(nil, err)
// return
// }
// EditCompanyForUCenter 总后台调用
//@router /ucenter/company/edit [post]
... ...
... ... @@ -62,8 +62,13 @@ func GetUserAuthById(id int64) (v *UserAuth, err error) {
// UpdateUserAuth updates UserAuth by Id and returns error if
// the record to be updated doesn't exist
func UpdateUserAuthById(m *UserAuth, col []string) (err error) {
o := orm.NewOrm()
func UpdateUserAuthById(m *UserAuth, col []string, om ...orm.Ormer) (err error) {
var o orm.Ormer
if len(om) > 0 {
o = om[0]
} else {
o = orm.NewOrm()
}
// ascertain id exists in the database
var num int64
if num, err = o.Update(m, col...); err == nil {
... ...
... ... @@ -195,3 +195,11 @@ func GetUserCompanyAll(companyId int64) (v []*UserCompany, err error) {
}
return nil, err
}
func GetUserCompanyByUserId(userId int64) (v []UserCompany, err error) {
o := orm.NewOrm()
_, err = o.QueryTable(&UserCompany{}).
Filter("user_id", userId).
All(&v)
return
}
... ...
... ... @@ -155,18 +155,18 @@ func init() {
beego.NSRouter("/login", &controllers.AuthController{}, "post:LoginSecretKey"),
beego.NSRouter("/startCaptcha", &controllers.AuthController{}, "post:RegisterGeetest"),
beego.NSRouter("/verifyCaptcha", &controllers.AuthController{}, "post:ValidateGeetest"),
//beego.NSRouter("/login_sms", &controllers.AuthController{}, "post:LoginSms"),
//beego.NSRouter("/sms_code", &controllers.AuthController{}, "post:SmsCode"),
//beego.NSRouter("/sms_code/check", &controllers.AuthController{}, "post:SmsCodeCheck"),
//beego.NSRouter("/my/reset_password", &controllers.MyController{}, "post:ResetPassword"),
//beego.NSRouter("/login_sms", &controllers.AuthController{}, "post:LoginSms"),
//beego.NSRouter("/sms_code", &controllers.AuthController{}, "post:SmsCode"),
//beego.NSRouter("/sms_code/check", &controllers.AuthController{}, "post:SmsCodeCheck"),
//beego.NSRouter("/my/reset_password", &controllers.MyController{}, "post:ResetPassword"),
)
nsUcenter := beego.NewNamespace("/ucenter",
beego.NSBefore(middleware.LogRequestData),
//beego.NSRouter("/company", &controllers.CompanyController{}, "post:InitCompany"),(废弃)
beego.NSRouter("/company/info", &controllers.CompanyController{}, "post:GetCompanyForUCenter"),
//beego.NSRouter("/company/edit", &controllers.CompanyController{}, "post:EditCompanyForUCenter"),(废弃)
beego.NSRouter("/user/revoke", &controllers.CompanyController{}, "post:UserRevoke"),
beego.NSRouter("/user/changePhone", &controllers.CompanyController{}, "post:UserChangePhone"),
// beego.NSRouter("/user/revoke", &controllers.CompanyController{}, "post:UserRevoke"),
// beego.NSRouter("/user/changePhone", &controllers.CompanyController{}, "post:UserChangePhone"),
beego.NSRouter("/company/allow_forbid", &controllers.CompanyController{}, "post:AllowForidCompanyForUCenter"),
)
... ...
package company
import (
"encoding/json"
"fmt"
"oppmg/common/log"
"oppmg/models"
"oppmg/protocol"
"oppmg/services/ucenter"
"oppmg/utils"
"strings"
"time"
... ... @@ -15,445 +12,445 @@ import (
)
//InitCompanyInfo 初始化公司
func InitCompanyInfo(centerCompany protocol.CenterCompanyInfo) error {
var (
newDeparment *models.Department
newRoleGroup *models.Role
newRole *models.Role
newUser *models.User
newCompany *models.Company
newUserCompany *models.UserCompany
err error
)
o := orm.NewOrm()
o.Begin()
//处理用户数据
newUser, err = initAdminUser(centerCompany, o)
if err != nil {
o.Rollback()
log.Error("处理公司主管数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
//处理公司数据
newCompany, err = initCompany(centerCompany, newUser, o)
if err != nil {
o.Rollback()
log.Error("处理公司数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
err = iniSysConfig(int(newCompany.Id), o)
if err != nil {
o.Rollback()
log.Error("处理评分配置数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加user_company
newUserCompany, err = initUserCompany(newCompany, newUser, o)
if err != nil {
o.Rollback()
log.Error("处理user_company数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加部门
newDeparment, err = initDepartment(newCompany, newUserCompany, o)
if err != nil {
o.Rollback()
log.Error("处理部门数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加用户部门数据
_, err = initUserDedaprtmet(newDeparment, newUserCompany, o)
if err != nil {
o.Rollback()
log.Error("处理user_department数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加角色组
newRoleGroup, err = initRoleGroup(newCompany, o)
if err != nil {
o.Rollback()
log.Error("处理角色组数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加角色
newRole, err = initRole(newCompany, newRoleGroup, o)
if err != nil {
o.Rollback()
log.Error("处理角色数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
//添加用户的角色
_, err = initUserRole(newUserCompany, newRole, o)
if err != nil {
o.Rollback()
log.Error("处理user_role数据失败:%s", err)
return protocol.NewErrWithMessage("1")
}
o.Commit()
return nil
}
// func InitCompanyInfo(centerCompany protocol.CenterCompanyInfo) error {
// var (
// newDeparment *models.Department
// newRoleGroup *models.Role
// newRole *models.Role
// newUser *models.User
// newCompany *models.Company
// newUserCompany *models.UserCompany
// err error
// )
// o := orm.NewOrm()
// o.Begin()
// //处理用户数据
// newUser, err = initAdminUser(centerCompany, o)
// if err != nil {
// o.Rollback()
// log.Error("处理公司主管数据失败:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// //处理公司数据
// newCompany, err = initCompany(centerCompany, newUser, o)
// if err != nil {
// o.Rollback()
// log.Error("处理公司数据失败:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// err = iniSysConfig(int(newCompany.Id), o)
// if err != nil {
// o.Rollback()
// log.Error("处理评分配置数据失败:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// //添加user_company
// newUserCompany, err = initUserCompany(newCompany, newUser, o)
// if err != nil {
// o.Rollback()
// log.Error("处理user_company数据失败:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// //添加部门
// newDeparment, err = initDepartment(newCompany, newUserCompany, o)
// if err != nil {
// o.Rollback()
// log.Error("处理部门数据失败:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// //添加用户部门数据
// _, err = initUserDedaprtmet(newDeparment, newUserCompany, o)
// if err != nil {
// o.Rollback()
// log.Error("处理user_department数据失败:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// //添加角色组
// newRoleGroup, err = initRoleGroup(newCompany, o)
// if err != nil {
// o.Rollback()
// log.Error("处理角色组数据失败:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// //添加角色
// newRole, err = initRole(newCompany, newRoleGroup, o)
// if err != nil {
// o.Rollback()
// log.Error("处理角色数据失败:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// //添加用户的角色
// _, err = initUserRole(newUserCompany, newRole, o)
// if err != nil {
// o.Rollback()
// log.Error("处理user_role数据失败:%s", err)
// return protocol.NewErrWithMessage("1")
// }
// o.Commit()
// return nil
// }
func initCompany(centerCompany protocol.CenterCompanyInfo, admininfo *models.User, o orm.Ormer) (*models.Company, error) {
var (
err error
newCompany = &models.Company{}
)
err = o.QueryTable(&models.Company{}).
Filter("user_center_id", centerCompany.CompanyId).
One(newCompany)
if err == nil {
_, err = o.QueryTable(&models.Company{}).
Filter("user_center_id", centerCompany.CompanyId).
Update(orm.Params{
"name": centerCompany.CompanyName,
"admin_id": admininfo.Id,
"delete_at": 0,
"enable": models.COMPANY_ENABLE_YES,
})
if err != nil {
log.Error("更新公司数据失败:%s", err)
return nil, err
}
} else if err == orm.ErrNoRows {
newCompany = &models.Company{
Id: centerCompany.CompanyId,
Name: centerCompany.CompanyName,
AdminId: admininfo.Id,
UserCenterId: centerCompany.CompanyId,
}
_, err = models.AddCompany(newCompany, o)
if err != nil {
log.Error("添加公司数据失败:%s", err)
return nil, err
}
// func initCompany(centerCompany protocol.CenterCompanyInfo, admininfo *models.User, o orm.Ormer) (*models.Company, error) {
// var (
// err error
// newCompany = &models.Company{}
// )
// err = o.QueryTable(&models.Company{}).
// Filter("user_center_id", centerCompany.CompanyId).
// One(newCompany)
// if err == nil {
// _, err = o.QueryTable(&models.Company{}).
// Filter("user_center_id", centerCompany.CompanyId).
// Update(orm.Params{
// "name": centerCompany.CompanyName,
// "admin_id": admininfo.Id,
// "delete_at": 0,
// "enable": models.COMPANY_ENABLE_YES,
// })
// if err != nil {
// log.Error("更新公司数据失败:%s", err)
// return nil, err
// }
// } else if err == orm.ErrNoRows {
// newCompany = &models.Company{
// Id: centerCompany.CompanyId,
// Name: centerCompany.CompanyName,
// AdminId: admininfo.Id,
// UserCenterId: centerCompany.CompanyId,
// }
// _, err = models.AddCompany(newCompany, o)
// if err != nil {
// log.Error("添加公司数据失败:%s", err)
// return nil, err
// }
} else {
log.Error("查询公司数据失败:%s", err)
return nil, err
}
return newCompany, nil
}
// } else {
// log.Error("查询公司数据失败:%s", err)
// return nil, err
// }
// return newCompany, nil
// }
func initAdminUser(centerCompany protocol.CenterCompanyInfo, o orm.Ormer) (*models.User, error) {
var (
err error
newUser = &models.User{}
)
err = o.QueryTable(&models.User{}).
Filter("phone", centerCompany.AdminAccount).
Filter("delete_at", 0).
One(newUser)
if err == nil {
// 更新数据
_, err = o.QueryTable(&models.User{}).
Filter("user_center_id", centerCompany.CompanyId).
Filter("delete_at", 0).
Update(orm.Params{
"nick_name": centerCompany.AdminName,
"phone": centerCompany.AdminAccount,
"enable_status": models.USER_ENABLE_YES,
})
if err != nil {
log.Error("数据操作失败:%s", err)
return nil, err
}
} else if err == orm.ErrNoRows {
var (
ucenterUser = &ucenter.ResponseAddUser{}
err error
)
ucenterUser, err = ucenter.RequestUCenterAddUser(centerCompany.AdminAccount, centerCompany.AdminName, "")
if err != nil {
log.Error("获取统一用户中心数据失败 :%s", err)
return nil, err
}
//添加用户
newUser = &models.User{
Phone: centerCompany.AdminAccount,
NickName: centerCompany.AdminName,
Icon: ucenterUser.Data.Avatar,
Accid: ucenterUser.Data.Accid,
UserCenterId: ucenterUser.Data.Id,
CsAccount: ucenterUser.Data.CustomerAccount,
}
_, err = models.AddUser(newUser, o)
if err != nil {
log.Error("添加用户数据失败:%s", err)
return nil, err
}
} else {
log.Error("查询用户数据失败:%s", err)
return nil, err
}
return newUser, nil
}
// func initAdminUser(centerCompany protocol.CenterCompanyInfo, o orm.Ormer) (*models.User, error) {
// var (
// err error
// newUser = &models.User{}
// )
// err = o.QueryTable(&models.User{}).
// Filter("phone", centerCompany.AdminAccount).
// Filter("delete_at", 0).
// One(newUser)
// if err == nil {
// // 更新数据
// _, err = o.QueryTable(&models.User{}).
// Filter("user_center_id", centerCompany.CompanyId).
// Filter("delete_at", 0).
// Update(orm.Params{
// "nick_name": centerCompany.AdminName,
// "phone": centerCompany.AdminAccount,
// "enable_status": models.USER_ENABLE_YES,
// })
// if err != nil {
// log.Error("数据操作失败:%s", err)
// return nil, err
// }
// } else if err == orm.ErrNoRows {
// var (
// ucenterUser = &ucenter.ResponseAddUser{}
// err error
// )
// ucenterUser, err = ucenter.RequestUCenterAddUser(centerCompany.AdminAccount, centerCompany.AdminName, "")
// if err != nil {
// log.Error("获取统一用户中心数据失败 :%s", err)
// return nil, err
// }
// //添加用户
// newUser = &models.User{
// Phone: centerCompany.AdminAccount,
// NickName: centerCompany.AdminName,
// Icon: ucenterUser.Data.Avatar,
// Accid: ucenterUser.Data.Accid,
// UserCenterId: ucenterUser.Data.Id,
// CsAccount: ucenterUser.Data.CustomerAccount,
// }
// _, err = models.AddUser(newUser, o)
// if err != nil {
// log.Error("添加用户数据失败:%s", err)
// return nil, err
// }
// } else {
// log.Error("查询用户数据失败:%s", err)
// return nil, err
// }
// return newUser, nil
// }
func initUserCompany(newcompany *models.Company, newuser *models.User, o orm.Ormer) (*models.UserCompany, error) {
var (
err error
uc = &models.UserCompany{}
)
err = o.QueryTable(&models.UserCompany{}).
Filter("user_id", newuser.Id).
Filter("company_id", newcompany.Id).
Filter("delete_at", 0).
One(uc)
if err == nil {
_, err = o.QueryTable(&models.UserCompany{}).
Filter("id", uc.Id).
Update(orm.Params{
"enable": models.USERCOMPANY_ENABLE_YES,
})
if err != nil {
log.Error("更新user_company数据失败:%s", err)
return nil, err
}
} else if err == orm.ErrNoRows {
uc = &models.UserCompany{
UserId: newuser.Id,
CompanyId: newcompany.Id,
OpenId: newuser.UserCenterId,
Enable: models.USERCOMPANY_ENABLE_YES,
ChargeStatus: models.USERCOMPANY_CHARGE_YES,
}
_, err = models.AddUserCompany(uc, o)
if err != nil {
log.Error("添加user_company数据失败:%s", err)
return nil, err
}
} else {
log.Error("获取user_company数据失败:%s", err)
return nil, err
}
return uc, nil
}
// func initUserCompany(newcompany *models.Company, newuser *models.User, o orm.Ormer) (*models.UserCompany, error) {
// var (
// err error
// uc = &models.UserCompany{}
// )
// err = o.QueryTable(&models.UserCompany{}).
// Filter("user_id", newuser.Id).
// Filter("company_id", newcompany.Id).
// Filter("delete_at", 0).
// One(uc)
// if err == nil {
// _, err = o.QueryTable(&models.UserCompany{}).
// Filter("id", uc.Id).
// Update(orm.Params{
// "enable": models.USERCOMPANY_ENABLE_YES,
// })
// if err != nil {
// log.Error("更新user_company数据失败:%s", err)
// return nil, err
// }
// } else if err == orm.ErrNoRows {
// uc = &models.UserCompany{
// UserId: newuser.Id,
// CompanyId: newcompany.Id,
// OpenId: newuser.UserCenterId,
// Enable: models.USERCOMPANY_ENABLE_YES,
// ChargeStatus: models.USERCOMPANY_CHARGE_YES,
// }
// _, err = models.AddUserCompany(uc, o)
// if err != nil {
// log.Error("添加user_company数据失败:%s", err)
// return nil, err
// }
// } else {
// log.Error("获取user_company数据失败:%s", err)
// return nil, err
// }
// return uc, nil
// }
func initDepartment(newCompany *models.Company, newusercompany *models.UserCompany, o orm.Ormer) (*models.Department, error) {
var (
newDepartment = &models.Department{}
err error
)
err = o.QueryTable(&models.Department{}).
Filter("company_id", newCompany.Id).
Filter("is_top", 1).
One(newDepartment)
if err == nil {
newDepartment.Name = newCompany.Name
err = models.UpdateDepartmentById(newDepartment, []string{"Name"}, o)
if err != nil {
log.Error("更新部门关系树数据失败:%s", err)
return nil, err
}
return newDepartment, nil
} else if err == orm.ErrNoRows {
//添加部门
newDepartment = &models.Department{
CompanyId: newCompany.Id,
Name: newCompany.Name,
IsTop: 1,
ParentId: 0,
Relation: fmt.Sprint(utils.GenerateIDBySonyflake()),
}
newDepartment.SetManages([]int64{newusercompany.Id})
_, err = models.AddDepartment(newDepartment, o)
if err != nil {
log.Error("添加部门数据失败:%s", err)
return nil, err
}
newDepartment.SetRelation(nil)
err = models.UpdateDepartmentById(newDepartment, []string{"Relation"}, o)
if err != nil {
log.Error("更新部门关系树数据失败:%s", err)
return nil, err
}
} else {
log.Error("查询部门数据失败:%s", err)
return nil, err
}
// func initDepartment(newCompany *models.Company, newusercompany *models.UserCompany, o orm.Ormer) (*models.Department, error) {
// var (
// newDepartment = &models.Department{}
// err error
// )
// err = o.QueryTable(&models.Department{}).
// Filter("company_id", newCompany.Id).
// Filter("is_top", 1).
// One(newDepartment)
// if err == nil {
// newDepartment.Name = newCompany.Name
// err = models.UpdateDepartmentById(newDepartment, []string{"Name"}, o)
// if err != nil {
// log.Error("更新部门关系树数据失败:%s", err)
// return nil, err
// }
// return newDepartment, nil
// } else if err == orm.ErrNoRows {
// //添加部门
// newDepartment = &models.Department{
// CompanyId: newCompany.Id,
// Name: newCompany.Name,
// IsTop: 1,
// ParentId: 0,
// Relation: fmt.Sprint(utils.GenerateIDBySonyflake()),
// }
// newDepartment.SetManages([]int64{newusercompany.Id})
// _, err = models.AddDepartment(newDepartment, o)
// if err != nil {
// log.Error("添加部门数据失败:%s", err)
// return nil, err
// }
// newDepartment.SetRelation(nil)
// err = models.UpdateDepartmentById(newDepartment, []string{"Relation"}, o)
// if err != nil {
// log.Error("更新部门关系树数据失败:%s", err)
// return nil, err
// }
// } else {
// log.Error("查询部门数据失败:%s", err)
// return nil, err
// }
return newDepartment, nil
}
// return newDepartment, nil
// }
func initUserDedaprtmet(newDepartment *models.Department, newUserCompany *models.UserCompany, o orm.Ormer) (*models.UserDepartment, error) {
var (
newUserDepartment = &models.UserDepartment{}
err error
)
err = o.QueryTable(&models.UserDepartment{}).
Filter("department_id", newDepartment.Id).
Filter("user_company_id", newUserCompany.Id).
Filter("enable_status", models.USER_DEPARTMENT_ENABLE_YES).
One(newUserDepartment)
if err == nil {
return newUserDepartment, nil
} else if err == orm.ErrNoRows {
//添加部门
newUserDepartment = &models.UserDepartment{
UserCompanyId: newUserCompany.Id,
//UserId: newUserCompany.UserId,
CompanyId: newUserCompany.CompanyId,
DepartmentId: newDepartment.Id,
}
_, err = models.AddUserDepartment(newUserDepartment, o)
if err != nil {
log.Error("添加部门数据失败:%s", err)
return nil, err
}
} else {
log.Error("查询部门数据失败:%s", err)
return nil, err
}
// func initUserDedaprtmet(newDepartment *models.Department, newUserCompany *models.UserCompany, o orm.Ormer) (*models.UserDepartment, error) {
// var (
// newUserDepartment = &models.UserDepartment{}
// err error
// )
// err = o.QueryTable(&models.UserDepartment{}).
// Filter("department_id", newDepartment.Id).
// Filter("user_company_id", newUserCompany.Id).
// Filter("enable_status", models.USER_DEPARTMENT_ENABLE_YES).
// One(newUserDepartment)
// if err == nil {
// return newUserDepartment, nil
// } else if err == orm.ErrNoRows {
// //添加部门
// newUserDepartment = &models.UserDepartment{
// UserCompanyId: newUserCompany.Id,
// //UserId: newUserCompany.UserId,
// CompanyId: newUserCompany.CompanyId,
// DepartmentId: newDepartment.Id,
// }
// _, err = models.AddUserDepartment(newUserDepartment, o)
// if err != nil {
// log.Error("添加部门数据失败:%s", err)
// return nil, err
// }
// } else {
// log.Error("查询部门数据失败:%s", err)
// return nil, err
// }
return newUserDepartment, nil
}
// return newUserDepartment, nil
// }
func initRoleGroup(newCompany *models.Company, o orm.Ormer) (*models.Role, error) {
var (
newRoleGroup = &models.Role{}
err error
)
err = o.QueryTable(&models.Role{}).
Filter("delete_at", 0).
Filter("is_default", models.ROLE_DEFAULR).
Filter("types", models.ROLETYPES_GROUP).
Filter("company_id", newCompany.Id).
One(newRoleGroup)
if err == nil {
return newRoleGroup, nil
} else if err == orm.ErrNoRows {
//添加部门
newRoleGroup = &models.Role{
Types: models.ROLETYPES_GROUP,
CompanyId: newCompany.Id,
IsDefault: models.ROLE_DEFAULR,
Name: "管理员",
}
_, err = models.AddRole(newRoleGroup, o)
if err != nil {
log.Error("添加角色组数据失败:%s", err)
return nil, err
}
} else {
log.Error("查询角色组数据失败:%s", err)
return nil, err
}
// func initRoleGroup(newCompany *models.Company, o orm.Ormer) (*models.Role, error) {
// var (
// newRoleGroup = &models.Role{}
// err error
// )
// err = o.QueryTable(&models.Role{}).
// Filter("delete_at", 0).
// Filter("is_default", models.ROLE_DEFAULR).
// Filter("types", models.ROLETYPES_GROUP).
// Filter("company_id", newCompany.Id).
// One(newRoleGroup)
// if err == nil {
// return newRoleGroup, nil
// } else if err == orm.ErrNoRows {
// //添加部门
// newRoleGroup = &models.Role{
// Types: models.ROLETYPES_GROUP,
// CompanyId: newCompany.Id,
// IsDefault: models.ROLE_DEFAULR,
// Name: "管理员",
// }
// _, err = models.AddRole(newRoleGroup, o)
// if err != nil {
// log.Error("添加角色组数据失败:%s", err)
// return nil, err
// }
// } else {
// log.Error("查询角色组数据失败:%s", err)
// return nil, err
// }
return newRoleGroup, nil
}
// return newRoleGroup, nil
// }
func initRole(newCompany *models.Company, newRoleGroup *models.Role, o orm.Ormer) (*models.Role, error) {
var (
newRole = &models.Role{}
err error
)
err = o.QueryTable(&models.Role{}).
Filter("delete_at", 0).
Filter("is_default", models.ROLE_DEFAULR).
Filter("types", models.ROLETYPES_ROLE).
Filter("company_id", newCompany.Id).
One(newRole)
if err == nil {
return newRole, nil
} else if err == orm.ErrNoRows {
//添加部门
newRole = &models.Role{
Types: models.ROLETYPES_ROLE,
CompanyId: newCompany.Id,
IsDefault: models.ROLE_DEFAULR,
Name: "主管理员",
Pid: newRoleGroup.Id,
}
_, err = models.AddRole(newRole, o)
if err != nil {
log.Error("添加角色数据失败:%s", err)
return nil, err
}
} else {
log.Error("查询角色数据失败:%s", err)
return nil, err
}
// func initRole(newCompany *models.Company, newRoleGroup *models.Role, o orm.Ormer) (*models.Role, error) {
// var (
// newRole = &models.Role{}
// err error
// )
// err = o.QueryTable(&models.Role{}).
// Filter("delete_at", 0).
// Filter("is_default", models.ROLE_DEFAULR).
// Filter("types", models.ROLETYPES_ROLE).
// Filter("company_id", newCompany.Id).
// One(newRole)
// if err == nil {
// return newRole, nil
// } else if err == orm.ErrNoRows {
// //添加部门
// newRole = &models.Role{
// Types: models.ROLETYPES_ROLE,
// CompanyId: newCompany.Id,
// IsDefault: models.ROLE_DEFAULR,
// Name: "主管理员",
// Pid: newRoleGroup.Id,
// }
// _, err = models.AddRole(newRole, o)
// if err != nil {
// log.Error("添加角色数据失败:%s", err)
// return nil, err
// }
// } else {
// log.Error("查询角色数据失败:%s", err)
// return nil, err
// }
return newRole, nil
}
// return newRole, nil
// }
func initUserRole(newUserCompany *models.UserCompany, newRole *models.Role, o orm.Ormer) (*models.UserRole, error) {
var (
newUserRole = &models.UserRole{}
err error
)
err = o.QueryTable(&models.UserRole{}).
Filter("enable_status", models.USER_ROLE_ENABLE_YES).
Filter("user_company_id", newUserCompany.Id).
Filter("role_id", newRole.Id).
One(newUserRole)
if err == nil {
return newUserRole, nil
} else if err == orm.ErrNoRows {
//添加UserRole
newUserRole = &models.UserRole{
RoleId: newRole.Id,
UserCompanyId: newUserCompany.Id,
CompanyId: newUserCompany.CompanyId,
}
_, err = models.AddUserRole(newUserRole, o)
if err != nil {
log.Error("添加user_role数据失败:%s", err)
return nil, err
}
} else {
log.Error("查询user_role数据失败:%s", err)
return nil, err
}
// func initUserRole(newUserCompany *models.UserCompany, newRole *models.Role, o orm.Ormer) (*models.UserRole, error) {
// var (
// newUserRole = &models.UserRole{}
// err error
// )
// err = o.QueryTable(&models.UserRole{}).
// Filter("enable_status", models.USER_ROLE_ENABLE_YES).
// Filter("user_company_id", newUserCompany.Id).
// Filter("role_id", newRole.Id).
// One(newUserRole)
// if err == nil {
// return newUserRole, nil
// } else if err == orm.ErrNoRows {
// //添加UserRole
// newUserRole = &models.UserRole{
// RoleId: newRole.Id,
// UserCompanyId: newUserCompany.Id,
// CompanyId: newUserCompany.CompanyId,
// }
// _, err = models.AddUserRole(newUserRole, o)
// if err != nil {
// log.Error("添加user_role数据失败:%s", err)
// return nil, err
// }
// } else {
// log.Error("查询user_role数据失败:%s", err)
// return nil, err
// }
return newUserRole, nil
}
// return newUserRole, nil
// }
func iniSysConfig(companyid int, o orm.Ormer) error {
var (
m *models.SysConfig
err error
)
m, err = models.GetSysConfigByCompanyId(companyid, models.KeyScore)
if err == nil {
return nil
}
v := protocol.ScoreConfig{
DiscoveryScore: &protocol.DiscoveryScore{
BasicFactor: 1,
ExtraFactor: 1,
ValueFactor: 1,
},
SumScore: &protocol.SumScore{
DiscoveryFactor: 1,
CatchFactor: 1,
},
BasicScore: &protocol.ScoreRange{
Max: 10000,
Min: 0,
Step: 1,
},
ExtraScore: &protocol.ScoreRange{
Max: 10000,
Min: 0,
Step: 1,
},
ValueScore: &protocol.ScoreRange{
Max: 10000,
Min: 0,
Step: 1,
},
}
bt, _ := json.Marshal(v)
m = &models.SysConfig{
Key: models.KeyScore,
CompanyId: companyid,
CreateAt: time.Now(),
UpdateAt: time.Now(),
Content: string(bt),
}
_, err = models.AddSysConfig(m, o)
return err
}
// func iniSysConfig(companyid int, o orm.Ormer) error {
// var (
// m *models.SysConfig
// err error
// )
// m, err = models.GetSysConfigByCompanyId(companyid, models.KeyScore)
// if err == nil {
// return nil
// }
// v := protocol.ScoreConfig{
// DiscoveryScore: &protocol.DiscoveryScore{
// BasicFactor: 1,
// ExtraFactor: 1,
// ValueFactor: 1,
// },
// SumScore: &protocol.SumScore{
// DiscoveryFactor: 1,
// CatchFactor: 1,
// },
// BasicScore: &protocol.ScoreRange{
// Max: 10000,
// Min: 0,
// Step: 1,
// },
// ExtraScore: &protocol.ScoreRange{
// Max: 10000,
// Min: 0,
// Step: 1,
// },
// ValueScore: &protocol.ScoreRange{
// Max: 10000,
// Min: 0,
// Step: 1,
// },
// }
// bt, _ := json.Marshal(v)
// m = &models.SysConfig{
// Key: models.KeyScore,
// CompanyId: companyid,
// CreateAt: time.Now(),
// UpdateAt: time.Now(),
// Content: string(bt),
// }
// _, err = models.AddSysConfig(m, o)
// return err
// }
func CompanyInfoEdit(companyid int64, logo string) error {
data := models.Company{
... ...
... ... @@ -15,7 +15,7 @@ import (
)
//同步 人员数据
//UserDepartData 用户的部门数据
type UserDepartData struct {
Id int64 `json:"id"`
CompanyId int64 `json:"company_id"`
... ... @@ -23,6 +23,7 @@ type UserDepartData struct {
UserId int64 `json:"user_id"`
}
// UserPositionData 用户的职位数据
type UserPositionData struct {
Id int64 `json:"id"`
CompanyId int64 `json:"company_id"`
... ... @@ -31,6 +32,7 @@ type UserPositionData struct {
}
//--------------------
//ModuleEmployee 同步用户数据
type ModuleEmployee struct {
Id int64 `json:"id"` //用户的id,对应本地的user_company_id
OpenId int64 `json:"open_id"` //同一用户中心的用户id
... ... @@ -55,28 +57,38 @@ type ModuleEmployee struct {
UserPositions []UserPositionData `json:"user_positions"`
}
//DeleteUserData 删除用户
type DeleteUserData struct {
CompanyId int64 `json:"companyId"`
Ids []int64 `json:"ids"`
}
//ForbidAllowUserData 启用禁用用户
type ForbidAllowUserData struct {
CompanyId int64 `json:"companyId"`
Ids []int64 `json:"ids"`
Status int8 `json:"status"`
}
//RemoveUserDepartmentData 移动通讯录用户部门
type RemoveUserDepartmentData struct {
CompanyId int64 `json:"company_id"`
UserIds []int64 `json:"user_ids"`
DepartmentIds []int64 `json:"department_ids"`
}
//ImportEmployeeData 批量导入用户数据
type ImportEmployeeData struct {
Add []ModuleEmployee `json:"add"`
Edit []ModuleEmployee `json:"edit"`
}
//EmployeeChangePhoneData 修改用户的账号
type EmployeeChangePhoneData struct {
OldPhone string `json:"oldPhone"`
NewPhone string `json:"newPhone"`
}
var _ PlatformAction = ModuleEmployee{}
//DoAction PlatformAction 的接口实现
... ... @@ -150,6 +162,18 @@ func (m ModuleEmployee) DoAction(code string, jsondata []byte) error {
UpdateEmployeeData(data.Edit[i])
}
return nil
case "changePhone":
//修改用户的手机账号
var (
data EmployeeChangePhoneData
err error
)
err = json.Unmarshal(jsondata, &data)
if err != nil {
return fmt.Errorf("数据解析失败:%s", err)
}
_ = EmployeeChangePhone(data)
return nil
default:
return errors.New("action not found")
}
... ... @@ -630,3 +654,67 @@ func RemoveUserDepartment(data RemoveUserDepartmentData) error {
o.Commit()
return nil
}
//EmployeeChangePhone 变更用户的手机账号
func EmployeeChangePhone(data EmployeeChangePhoneData) error {
var (
err error
oldUser *models.User
newUser *models.User
userCompanydatas []models.UserCompany
)
//user 表查询新旧号码用户
oldUser, err = models.GetUserByPhone(data.OldPhone)
if err != nil {
log.Error("获取user数据失败:phone = %s", data.OldPhone)
return errors.New("获取user数据失败")
}
newUser, err = models.GetUserByPhone(data.NewPhone)
if err != nil {
log.Error("获取user数据失败:phone = %s", data.NewPhone)
return errors.New("获取user数据失败")
}
userCompanydatas, err = models.GetUserCompanyByUserId(oldUser.Id)
if err != nil {
log.Error("获取user_company数据失败,user_id=%d", oldUser.Id)
return errors.New("获取user_company数据失败")
}
//user_company表 以旧用户的user_id作条件变更为新用户的对应的user_id,phone,open_id
o := orm.NewOrm()
o.Begin()
for i := range userCompanydatas {
temp := userCompanydatas[i]
temp.UserId = newUser.Id
temp.Phone = newUser.Phone
temp.OpenId = newUser.UserCenterId
temp.UpdateAt = time.Now()
err = models.UpdateUserCompanyById(&temp, []string{"UserId", "Phone", "OpenId", "UpdateAt"}, o)
if err != nil {
log.Error("更新user_company数据失败,id=%d", temp.Id)
o.Rollback()
return errors.New("变更用户手机号操作失败")
}
}
//user.user_center_id 同 user_company表.open_id
//UserAuth注销手机登录
var oldUserAuthData *models.UserAuth
oldUserAuthData, err = models.GetUserAuthByUser(oldUser.Id)
if err != nil {
log.Warn("为找到userAuth数据,user_id=%d", oldUser.Id)
} else {
oldUserAuthData.AccessToken = ""
oldUserAuthData.RefreshToken = ""
oldUserAuthData.AuthCode = ""
oldUserAuthData.UpdateAt = time.Now()
//更新userAuth数据
err = models.UpdateUserAuthById(oldUserAuthData,
[]string{"AccessToken", "RefreshToken", "AuthCode", "UpdateAt"},
o,
)
if err != nil {
log.Warn("更新user_auth数据失败,id=%d", oldUserAuthData.Id)
}
}
o.Commit()
return nil
}
... ...