批量修复,增加组织架构
This commit is contained in:
+59
-12
@@ -62,8 +62,8 @@ func (c *AuthController) Login() {
|
||||
return
|
||||
}
|
||||
|
||||
// 验证用户(先验证租户,再验证租户下的用户)
|
||||
user, err := models.ValidateUser(username, password, tenantName)
|
||||
// 验证用户(先检查用户表,找不到再检查员工表)
|
||||
user, employee, err := models.ValidateUser(username, password, tenantName)
|
||||
|
||||
if err != nil {
|
||||
// 登录失败
|
||||
@@ -72,8 +72,54 @@ func (c *AuthController) Login() {
|
||||
"message": err.Error(),
|
||||
}
|
||||
} else {
|
||||
var tokenString string
|
||||
var userId int
|
||||
var usernameForToken string
|
||||
var tenantId int
|
||||
var userInfo map[string]interface{}
|
||||
|
||||
// 判断是用户登录还是员工登录
|
||||
if user != nil {
|
||||
// 用户登录
|
||||
userId = user.Id
|
||||
usernameForToken = user.Username
|
||||
tenantId = user.TenantId
|
||||
userInfo = map[string]interface{}{
|
||||
"id": user.Id,
|
||||
"username": user.Username,
|
||||
"email": user.Email,
|
||||
"avatar": user.Avatar,
|
||||
"nickname": user.Nickname,
|
||||
"tenant_id": user.TenantId,
|
||||
"type": "user", // 标识是用户登录
|
||||
}
|
||||
} else if employee != nil {
|
||||
// 员工登录
|
||||
userId = employee.Id
|
||||
usernameForToken = employee.EmployeeNo
|
||||
tenantId = employee.TenantId
|
||||
userInfo = map[string]interface{}{
|
||||
"id": employee.Id,
|
||||
"username": employee.EmployeeNo,
|
||||
"name": employee.Name,
|
||||
"email": employee.Email,
|
||||
"phone": employee.Phone,
|
||||
"tenant_id": employee.TenantId,
|
||||
"department_id": employee.DepartmentId,
|
||||
"position_id": employee.PositionId,
|
||||
"type": "employee", // 标识是员工登录
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "登录验证失败",
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 使用models包中的GenerateToken函数生成token
|
||||
tokenString, err := models.GenerateToken(user.Id, user.Username, user.TenantId)
|
||||
tokenString, err = models.GenerateToken(userId, usernameForToken, tenantId)
|
||||
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
@@ -125,7 +171,15 @@ func (c *AuthController) Login() {
|
||||
}
|
||||
|
||||
o := orm.NewOrm()
|
||||
_, _ = o.Raw("UPDATE yz_users SET last_login_time = ?, last_login_ip = ?, login_count = IFNULL(login_count,0)+1 WHERE id = ?", loginTime, clientIP, user.Id).Exec()
|
||||
// 更新登录信息
|
||||
if user != nil {
|
||||
// 更新用户表
|
||||
_, _ = o.Raw("UPDATE yz_users SET last_login_time = ?, last_login_ip = ?, login_count = IFNULL(login_count,0)+1 WHERE id = ?", loginTime, clientIP, user.Id).Exec()
|
||||
} else if employee != nil {
|
||||
// 更新员工表(如果员工表有last_login_time和last_login_ip字段)
|
||||
// 注意:如果员工表没有这些字段,需要先添加字段
|
||||
_, _ = o.Raw("UPDATE yz_tenant_employees SET last_login_time = ?, last_login_ip = ? WHERE id = ?", loginTime, clientIP, employee.Id).Exec()
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
@@ -133,14 +187,7 @@ func (c *AuthController) Login() {
|
||||
"data": map[string]interface{}{
|
||||
"accessToken": tokenString,
|
||||
"token": tokenString, // 兼容性
|
||||
"user": map[string]interface{}{
|
||||
"id": user.Id,
|
||||
"username": user.Username,
|
||||
"email": user.Email,
|
||||
"avatar": user.Avatar,
|
||||
"nickname": user.Nickname,
|
||||
"tenant_id": user.TenantId,
|
||||
},
|
||||
"user": userInfo,
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,270 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"server/models"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// DepartmentController 部门控制器
|
||||
type DepartmentController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// GetTenantDepartments 获取租户下的所有部门
|
||||
// @router /departments/tenant/:tenantId [get]
|
||||
func (c *DepartmentController) GetTenantDepartments() {
|
||||
tenantId, err := c.GetInt(":tenantId")
|
||||
if err != nil || tenantId <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "租户ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
departments, err := models.GetTenantDepartments(tenantId)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "获取部门列表失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 格式化返回数据
|
||||
deptList := make([]map[string]interface{}, 0)
|
||||
for _, dept := range departments {
|
||||
deptList = append(deptList, map[string]interface{}{
|
||||
"id": dept.Id,
|
||||
"tenant_id": dept.TenantId,
|
||||
"name": dept.Name,
|
||||
"code": dept.Code,
|
||||
"parent_id": dept.ParentId,
|
||||
"description": dept.Description,
|
||||
"manager_id": dept.ManagerId,
|
||||
"sort_order": dept.SortOrder,
|
||||
"status": dept.Status,
|
||||
})
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "获取部门列表成功",
|
||||
"data": deptList,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetDepartmentInfo 获取部门详情
|
||||
// @router /departments/:id [get]
|
||||
func (c *DepartmentController) GetDepartmentInfo() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "部门ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
department, err := models.GetDepartmentById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "部门不存在",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "获取部门信息成功",
|
||||
"data": map[string]interface{}{
|
||||
"id": department.Id,
|
||||
"tenant_id": department.TenantId,
|
||||
"name": department.Name,
|
||||
"code": department.Code,
|
||||
"parent_id": department.ParentId,
|
||||
"description": department.Description,
|
||||
"manager_id": department.ManagerId,
|
||||
"sort_order": department.SortOrder,
|
||||
"status": department.Status,
|
||||
},
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// AddDepartment 添加部门
|
||||
// @router /departments [post]
|
||||
func (c *DepartmentController) AddDepartment() {
|
||||
var deptData struct {
|
||||
TenantId int `json:"tenant_id"`
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
ParentId int `json:"parent_id"`
|
||||
Description string `json:"description"`
|
||||
ManagerId int `json:"manager_id"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
Status int8 `json:"status"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &deptData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数错误: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
department := &models.Department{
|
||||
TenantId: deptData.TenantId,
|
||||
Name: deptData.Name,
|
||||
Code: deptData.Code,
|
||||
ParentId: deptData.ParentId,
|
||||
Description: deptData.Description,
|
||||
ManagerId: deptData.ManagerId,
|
||||
SortOrder: deptData.SortOrder,
|
||||
Status: deptData.Status,
|
||||
}
|
||||
|
||||
if department.TenantId == 0 {
|
||||
department.TenantId = 1
|
||||
}
|
||||
|
||||
id, err := models.AddDepartment(department)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "添加部门失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
department.Id = int(id)
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "添加部门成功",
|
||||
"data": department,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// UpdateDepartment 更新部门信息
|
||||
// @router /departments/:id [put]
|
||||
func (c *DepartmentController) UpdateDepartment() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "部门ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var updateData struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
ParentId int `json:"parent_id"`
|
||||
Description string `json:"description"`
|
||||
ManagerId int `json:"manager_id"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
Status int8 `json:"status"`
|
||||
}
|
||||
|
||||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &updateData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数错误: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
department, err := models.GetDepartmentById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "部门不存在",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
department.Name = updateData.Name
|
||||
department.Code = updateData.Code
|
||||
department.ParentId = updateData.ParentId
|
||||
department.Description = updateData.Description
|
||||
department.ManagerId = updateData.ManagerId
|
||||
department.SortOrder = updateData.SortOrder
|
||||
department.Status = updateData.Status
|
||||
|
||||
if err := models.UpdateDepartment(department); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "更新部门信息失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "更新部门信息成功",
|
||||
"data": department,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteDepartment 删除部门
|
||||
// @router /departments/:id [delete]
|
||||
func (c *DepartmentController) DeleteDepartment() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "部门ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.DeleteDepartment(id); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "删除部门失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "删除部门成功",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,403 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"server/models"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// EmployeeController 员工控制器
|
||||
type EmployeeController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// GetAllEmployees 获取所有员工(可选,用于管理员查看所有员工)
|
||||
// @router /employees [get]
|
||||
func (c *EmployeeController) GetAllEmployees() {
|
||||
employees, err := models.GetAllEmployees()
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "获取员工列表失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
} else {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "获取员工列表成功",
|
||||
"data": employees,
|
||||
}
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetTenantEmployees 获取租户下的所有员工
|
||||
// @router /employees/tenant/:tenantId [get]
|
||||
func (c *EmployeeController) GetTenantEmployees() {
|
||||
tenantId, err := c.GetInt(":tenantId")
|
||||
if err != nil || tenantId <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "租户ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
employees, err := models.GetTenantEmployees(tenantId)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "获取员工列表失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 格式化返回数据
|
||||
employeeList := make([]map[string]interface{}, 0)
|
||||
for _, emp := range employees {
|
||||
employeeList = append(employeeList, map[string]interface{}{
|
||||
"id": emp.Id,
|
||||
"tenant_id": emp.TenantId,
|
||||
"employee_no": emp.EmployeeNo,
|
||||
"name": emp.Name,
|
||||
"phone": emp.Phone,
|
||||
"email": emp.Email,
|
||||
"department_id": emp.DepartmentId,
|
||||
"position_id": emp.PositionId,
|
||||
"bank_name": emp.BankName,
|
||||
"bank_account": emp.BankAccount,
|
||||
"status": emp.Status,
|
||||
"create_time": emp.CreateTime,
|
||||
})
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "获取员工列表成功",
|
||||
"data": employeeList,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetEmployeeInfo 获取员工详情
|
||||
// @router /employees/:id [get]
|
||||
func (c *EmployeeController) GetEmployeeInfo() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "员工ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
employee, err := models.GetEmployeeById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "员工不存在",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "获取员工信息成功",
|
||||
"data": map[string]interface{}{
|
||||
"id": employee.Id,
|
||||
"tenant_id": employee.TenantId,
|
||||
"employee_no": employee.EmployeeNo,
|
||||
"name": employee.Name,
|
||||
"phone": employee.Phone,
|
||||
"email": employee.Email,
|
||||
"department_id": employee.DepartmentId,
|
||||
"position_id": employee.PositionId,
|
||||
"bank_name": employee.BankName,
|
||||
"bank_account": employee.BankAccount,
|
||||
"status": employee.Status,
|
||||
"create_time": employee.CreateTime,
|
||||
},
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// AddEmployee 添加员工
|
||||
// @router /employees [post]
|
||||
func (c *EmployeeController) AddEmployee() {
|
||||
var employeeData struct {
|
||||
TenantId int `json:"tenant_id"`
|
||||
EmployeeNo string `json:"employee_no"`
|
||||
Name string `json:"name"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
DepartmentId int `json:"department_id"`
|
||||
PositionId int `json:"position_id"`
|
||||
BankName string `json:"bank_name"`
|
||||
BankAccount string `json:"bank_account"`
|
||||
Status int8 `json:"status"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &employeeData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数错误: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
employee := &models.Employee{
|
||||
TenantId: employeeData.TenantId,
|
||||
EmployeeNo: employeeData.EmployeeNo,
|
||||
Name: employeeData.Name,
|
||||
Phone: employeeData.Phone,
|
||||
Email: employeeData.Email,
|
||||
DepartmentId: employeeData.DepartmentId,
|
||||
PositionId: employeeData.PositionId,
|
||||
BankName: employeeData.BankName,
|
||||
BankAccount: employeeData.BankAccount,
|
||||
Status: employeeData.Status,
|
||||
}
|
||||
|
||||
// 如果没有指定租户ID,从当前登录用户获取(需要JWT中间件支持)
|
||||
// 这里暂时使用传入的tenant_id,如果为0则使用默认值
|
||||
if employee.TenantId == 0 {
|
||||
// 可以从JWT token中获取租户ID,这里暂时设为1作为默认值
|
||||
employee.TenantId = 1
|
||||
}
|
||||
|
||||
// 默认密码
|
||||
defaultPassword := "yunzer123"
|
||||
id, err := models.AddEmployee(employee, defaultPassword)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "添加员工失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
employee.Id = int(id)
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "添加员工成功",
|
||||
"data": employee,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// UpdateEmployee 更新员工信息
|
||||
// @router /employees/:id [put]
|
||||
func (c *EmployeeController) UpdateEmployee() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "员工ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var updateData struct {
|
||||
EmployeeNo string `json:"employee_no"`
|
||||
Name string `json:"name"`
|
||||
Phone string `json:"phone"`
|
||||
Email string `json:"email"`
|
||||
DepartmentId int `json:"department_id"`
|
||||
PositionId int `json:"position_id"`
|
||||
BankName string `json:"bank_name"`
|
||||
BankAccount string `json:"bank_account"`
|
||||
Status int8 `json:"status"`
|
||||
}
|
||||
|
||||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &updateData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数错误: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
employee, err := models.GetEmployeeById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "员工不存在",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 更新字段
|
||||
employee.EmployeeNo = updateData.EmployeeNo
|
||||
employee.Name = updateData.Name
|
||||
employee.Phone = updateData.Phone
|
||||
employee.Email = updateData.Email
|
||||
employee.DepartmentId = updateData.DepartmentId
|
||||
employee.PositionId = updateData.PositionId
|
||||
employee.BankName = updateData.BankName
|
||||
employee.BankAccount = updateData.BankAccount
|
||||
employee.Status = updateData.Status
|
||||
|
||||
if err := models.UpdateEmployee(employee); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "更新员工信息失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "更新员工信息成功",
|
||||
"data": employee,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeleteEmployee 删除员工
|
||||
// @router /employees/:id [delete]
|
||||
func (c *EmployeeController) DeleteEmployee() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "员工ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.DeleteEmployee(id); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "删除员工失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "删除员工成功",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// ResetEmployeePassword 重置员工密码
|
||||
// @router /employees/:id/reset-password [post]
|
||||
func (c *EmployeeController) ResetEmployeePassword() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "员工ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 默认密码
|
||||
defaultPassword := "yunzer123"
|
||||
|
||||
if err := models.ResetEmployeePassword(id, defaultPassword); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "重置密码失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "密码重置成功,新密码为: " + defaultPassword,
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// ChangeEmployeePassword 修改员工密码
|
||||
// @router /employees/:id/change-password [post]
|
||||
func (c *EmployeeController) ChangeEmployeePassword() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "员工ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var passwordData struct {
|
||||
OldPassword string `json:"old_password"`
|
||||
NewPassword string `json:"new_password"`
|
||||
}
|
||||
|
||||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &passwordData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数错误: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
if passwordData.NewPassword == "" {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "新密码不能为空",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.ChangeEmployeePassword(id, passwordData.OldPassword, passwordData.NewPassword); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "修改密码失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "密码修改成功",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
@@ -0,0 +1,319 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"server/models"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// PositionController 职位控制器
|
||||
type PositionController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
// GetTenantPositions 获取租户下的所有职位
|
||||
// @router /positions/tenant/:tenantId [get]
|
||||
func (c *PositionController) GetTenantPositions() {
|
||||
tenantId, err := c.GetInt(":tenantId")
|
||||
if err != nil || tenantId <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "租户ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
positions, err := models.GetTenantPositions(tenantId)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "获取职位列表失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 格式化返回数据
|
||||
posList := make([]map[string]interface{}, 0)
|
||||
for _, pos := range positions {
|
||||
posList = append(posList, map[string]interface{}{
|
||||
"id": pos.Id,
|
||||
"tenant_id": pos.TenantId,
|
||||
"name": pos.Name,
|
||||
"code": pos.Code,
|
||||
"department_id": pos.DepartmentId,
|
||||
"level": pos.Level,
|
||||
"description": pos.Description,
|
||||
"sort_order": pos.SortOrder,
|
||||
"status": pos.Status,
|
||||
})
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "获取职位列表成功",
|
||||
"data": posList,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetPositionsByDepartment 根据部门ID获取职位列表
|
||||
// @router /positions/department/:departmentId [get]
|
||||
func (c *PositionController) GetPositionsByDepartment() {
|
||||
departmentId, err := c.GetInt(":departmentId")
|
||||
if err != nil || departmentId <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "部门ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
positions, err := models.GetPositionsByDepartment(departmentId)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "获取职位列表失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
// 格式化返回数据
|
||||
posList := make([]map[string]interface{}, 0)
|
||||
for _, pos := range positions {
|
||||
posList = append(posList, map[string]interface{}{
|
||||
"id": pos.Id,
|
||||
"tenant_id": pos.TenantId,
|
||||
"name": pos.Name,
|
||||
"code": pos.Code,
|
||||
"department_id": pos.DepartmentId,
|
||||
"level": pos.Level,
|
||||
"description": pos.Description,
|
||||
"sort_order": pos.SortOrder,
|
||||
"status": pos.Status,
|
||||
})
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "获取职位列表成功",
|
||||
"data": posList,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// GetPositionInfo 获取职位详情
|
||||
// @router /positions/:id [get]
|
||||
func (c *PositionController) GetPositionInfo() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "职位ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
position, err := models.GetPositionById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "职位不存在",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "获取职位信息成功",
|
||||
"data": map[string]interface{}{
|
||||
"id": position.Id,
|
||||
"tenant_id": position.TenantId,
|
||||
"name": position.Name,
|
||||
"code": position.Code,
|
||||
"department_id": position.DepartmentId,
|
||||
"level": position.Level,
|
||||
"description": position.Description,
|
||||
"sort_order": position.SortOrder,
|
||||
"status": position.Status,
|
||||
},
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// AddPosition 添加职位
|
||||
// @router /positions [post]
|
||||
func (c *PositionController) AddPosition() {
|
||||
var posData struct {
|
||||
TenantId int `json:"tenant_id"`
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
DepartmentId int `json:"department_id"`
|
||||
Level int `json:"level"`
|
||||
Description string `json:"description"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
Status int8 `json:"status"`
|
||||
}
|
||||
|
||||
err := json.Unmarshal(c.Ctx.Input.RequestBody, &posData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数错误: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
position := &models.Position{
|
||||
TenantId: posData.TenantId,
|
||||
Name: posData.Name,
|
||||
Code: posData.Code,
|
||||
DepartmentId: posData.DepartmentId,
|
||||
Level: posData.Level,
|
||||
Description: posData.Description,
|
||||
SortOrder: posData.SortOrder,
|
||||
Status: posData.Status,
|
||||
}
|
||||
|
||||
if position.TenantId == 0 {
|
||||
position.TenantId = 1
|
||||
}
|
||||
|
||||
id, err := models.AddPosition(position)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "添加职位失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
position.Id = int(id)
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "添加职位成功",
|
||||
"data": position,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// UpdatePosition 更新职位信息
|
||||
// @router /positions/:id [put]
|
||||
func (c *PositionController) UpdatePosition() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "职位ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
var updateData struct {
|
||||
Name string `json:"name"`
|
||||
Code string `json:"code"`
|
||||
DepartmentId int `json:"department_id"`
|
||||
Level int `json:"level"`
|
||||
Description string `json:"description"`
|
||||
SortOrder int `json:"sort_order"`
|
||||
Status int8 `json:"status"`
|
||||
}
|
||||
|
||||
err = json.Unmarshal(c.Ctx.Input.RequestBody, &updateData)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "请求参数错误: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
position, err := models.GetPositionById(id)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "职位不存在",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
position.Name = updateData.Name
|
||||
position.Code = updateData.Code
|
||||
position.DepartmentId = updateData.DepartmentId
|
||||
position.Level = updateData.Level
|
||||
position.Description = updateData.Description
|
||||
position.SortOrder = updateData.SortOrder
|
||||
position.Status = updateData.Status
|
||||
|
||||
if err := models.UpdatePosition(position); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "更新职位信息失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "更新职位信息成功",
|
||||
"data": position,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
// DeletePosition 删除职位
|
||||
// @router /positions/:id [delete]
|
||||
func (c *PositionController) DeletePosition() {
|
||||
id, err := c.GetInt(":id")
|
||||
if err != nil || id <= 0 {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "职位ID无效",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
if err := models.DeletePosition(id); err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 1,
|
||||
"message": "删除职位失败: " + err.Error(),
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
return
|
||||
}
|
||||
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
"code": 0,
|
||||
"message": "删除职位成功",
|
||||
"data": nil,
|
||||
}
|
||||
c.ServeJSON()
|
||||
}
|
||||
|
||||
+24
-16
@@ -208,13 +208,15 @@ func (c *UserController) GetUserInfo() {
|
||||
func (c *UserController) AddUser() {
|
||||
// 定义接收用户数据的结构体
|
||||
var userData struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
Nickname string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
TenantId int `json:"tenant_id"`
|
||||
Role int `json:"role"` // 角色ID
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
Email string `json:"email"`
|
||||
Nickname string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
TenantId int `json:"tenant_id"`
|
||||
Role int `json:"role"` // 角色ID
|
||||
DepartmentId int `json:"department_id"`
|
||||
PositionId int `json:"position_id"`
|
||||
}
|
||||
|
||||
// 解析请求体JSON数据
|
||||
@@ -266,7 +268,9 @@ func (c *UserController) AddUser() {
|
||||
userData.Nickname,
|
||||
userData.Avatar,
|
||||
userData.TenantId,
|
||||
userData.Role, // 添加 role 参数
|
||||
userData.Role,
|
||||
userData.DepartmentId,
|
||||
userData.PositionId,
|
||||
)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
@@ -295,13 +299,15 @@ func (c *UserController) AddUser() {
|
||||
func (c *UserController) EditUser() {
|
||||
// 定义接收更新数据的结构体
|
||||
var updateData struct {
|
||||
Id int `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Nickname string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
Status string `json:"status"`
|
||||
Role int `json:"role"` // 改为 role,存储角色ID
|
||||
Id int `json:"id"`
|
||||
Username string `json:"username"`
|
||||
Email string `json:"email"`
|
||||
Nickname string `json:"nickname"`
|
||||
Avatar string `json:"avatar"`
|
||||
Status string `json:"status"`
|
||||
Role int `json:"role"` // 角色ID
|
||||
DepartmentId int `json:"department_id"`
|
||||
PositionId int `json:"position_id"`
|
||||
}
|
||||
|
||||
// 解析请求体JSON
|
||||
@@ -335,7 +341,9 @@ func (c *UserController) EditUser() {
|
||||
updateData.Nickname,
|
||||
updateData.Avatar,
|
||||
updateData.Status,
|
||||
updateData.Role, // 改为 Role
|
||||
updateData.Role,
|
||||
updateData.DepartmentId,
|
||||
updateData.PositionId,
|
||||
)
|
||||
if err != nil {
|
||||
c.Data["json"] = map[string]interface{}{
|
||||
|
||||
Reference in New Issue
Block a user