package controller import ( "errors" "github.com/labstack/echo/v4" "gorm.io/gorm" "sorbet/internal/entities" "sorbet/internal/errs" "sorbet/internal/repositories" "sorbet/internal/services/company/request" "sorbet/pkg/crud" "sorbet/pkg/db" "sorbet/pkg/rsp" "sorbet/pkg/wx" ) type CompanyEmployeeController struct { crud.Controller[entities.CompanyEmployee, request.CompanyEmployeeUpsertRequest] } func (ctr *CompanyEmployeeController) InitRoutes(r *echo.Group) { ctr.RegisterRoutes("/company/employees", r) } func (ctr *CompanyEmployeeController) RegisterRoutes(path string, r *echo.Group) { ctr.Controller.RegisterRoutes(path, r) r.POST(path+"/auth/login", ctr.Login) } // Login 员工登录 func (ctr *CompanyEmployeeController) Login(c echo.Context) error { var req struct { Code string `json:"code" xml:"code" form:"code"` } if err := c.Bind(&req); err != nil { return err } login, err := wx.Login(c.Request().Context(), req.Code) if err != nil { return err } if err = login.Err(); err != nil { return err } employee, ticketString, err := repositories. NewCompanyAuthTicketRepository(ctr.MustORM(c)). GenerateForWechat(c.Request().Context(), login.Openid) if err != nil { if !errors.Is(err, gorm.ErrRecordNotFound) { return err } // 微信尚未绑定 return errs.ErrWechatNotBound.WithData(echo.Map{ "openid": login.Openid, }) } var companies []*entities.Company err = ctr.MustORM(c). Model(&entities.Company{}). Where( "EXISTS(?)", db.Model(&entities.CompanyEmployee{}).Where("wechat_openid=?", employee.WechatOpenid), ). Find(&companies). Error if err != nil { return err } return rsp.Ok(c, echo.Map{ "ticket": ticketString, "openid": login.Openid, "companies": companies, "employee": employee, }) }