更新若干
This commit is contained in:
1 parent
54c90072fb
commit
5682069553
59 files changed
+3813
-1617
No files matched your search
@@ -0,0 +1,366 @@
|
||||
package controllers
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"server/models"
|
||||
"server/pkg/jwtutil"
|
||||
"server/services"
|
||||
|
||||
"github.com/beego/beego/v2/client/orm"
|
||||
)
|
||||
|
||||
// AppCrmProjectController 提供 App 端(uniapp)CRM 项目管理接口。
|
||||
// 与 Web 后台的 /backend/crm/project/* 共用同一套模型、权限与核心逻辑,
|
||||
// 差异仅在于鉴权入口(App/Backend 双端令牌)与响应包装。
|
||||
type AppCrmProjectController struct {
|
||||
AppErpBaseController
|
||||
}
|
||||
|
||||
type crmProjectSavePayload struct {
|
||||
ID uint64 `json:"id"`
|
||||
projectPayload
|
||||
}
|
||||
|
||||
// List GET /api/crm/project/list
|
||||
func (c *AppCrmProjectController) List() {
|
||||
cl, err := c.erpClaims()
|
||||
if c.erpDenied(err) {
|
||||
return
|
||||
}
|
||||
page, pageSize := c.erpPage()
|
||||
tenantID := pipelineTenantID(cl)
|
||||
|
||||
cond := orm.NewCondition().
|
||||
And("tenant_id", tenantID).
|
||||
And("delete_time__isnull", true)
|
||||
cond = crmApplyOwnerScope(cond, cl)
|
||||
|
||||
if kw := c.erpQuery("keyword"); kw != "" {
|
||||
cond = cond.AndCond(orm.NewCondition().
|
||||
Or("project_name__contains", kw).
|
||||
Or("project_no__contains", kw).
|
||||
Or("customer_name__contains", kw).
|
||||
Or("supplier_name__contains", kw))
|
||||
}
|
||||
if st := c.erpQuery("status"); st != "" {
|
||||
cond = cond.And("status", st)
|
||||
}
|
||||
if pt := c.erpQuery("project_type"); pt != "" {
|
||||
if _, err := strconv.Atoi(pt); err == nil {
|
||||
cond = cond.And("project_type", pt)
|
||||
}
|
||||
}
|
||||
if y := c.erpQuery("year"); y != "" {
|
||||
if year, err := strconv.Atoi(y); err == nil && year > 0 {
|
||||
from := time.Date(year, time.January, 1, 0, 0, 0, 0, time.Local)
|
||||
cond = cond.AndCond(orm.NewCondition().
|
||||
And("start_date__gte", from).
|
||||
And("start_date__lt", from.AddDate(1, 0, 0)))
|
||||
}
|
||||
}
|
||||
|
||||
qs := models.Orm.QueryTable(new(models.TenantCrmProject)).SetCond(cond)
|
||||
total, _ := qs.Count()
|
||||
list := make([]models.TenantCrmProject, 0)
|
||||
if total > 0 {
|
||||
if _, err := qs.OrderBy("-start_date", "-id").
|
||||
Limit(pageSize, (page-1)*pageSize).All(&list); err != nil {
|
||||
c.erpFail(500, 500, "查询失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
attachBusinessName(tenantID, list)
|
||||
}
|
||||
c.erpListOK(list, total, page, pageSize, nil)
|
||||
}
|
||||
|
||||
// Detail GET /api/crm/project/detail/:id
|
||||
func (c *AppCrmProjectController) Detail() {
|
||||
cl, err := c.erpClaims()
|
||||
if c.erpDenied(err) {
|
||||
return
|
||||
}
|
||||
id := c.erpPathID()
|
||||
if id == 0 {
|
||||
c.erpFail(400, 400, "无效的ID")
|
||||
return
|
||||
}
|
||||
tenantID := pipelineTenantID(cl)
|
||||
var proj models.TenantCrmProject
|
||||
if err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
||||
Filter("id", id).Filter("tenant_id", tenantID).
|
||||
Filter("delete_time__isnull", true).One(&proj); err != nil {
|
||||
c.erpFail(404, 404, "项目未找到")
|
||||
return
|
||||
}
|
||||
if !canAccessCrmRecord(cl, proj.OwnerUserID, proj.CreateUserID) {
|
||||
c.erpFail(403, 403, "无权查看该项目数据")
|
||||
return
|
||||
}
|
||||
list := []models.TenantCrmProject{proj}
|
||||
attachBusinessName(tenantID, list)
|
||||
c.erpOk(list[0])
|
||||
}
|
||||
|
||||
// Save POST /api/crm/project/save
|
||||
// id 为 0 时新建,否则更新。
|
||||
func (c *AppCrmProjectController) Save() {
|
||||
cl, err := c.erpClaims()
|
||||
if c.erpDenied(err) {
|
||||
return
|
||||
}
|
||||
var p crmProjectSavePayload
|
||||
if !c.erpBody(&p, false) {
|
||||
return
|
||||
}
|
||||
if strings.TrimSpace(p.ProjectName) == "" {
|
||||
c.erpFail(400, 400, "项目名称不能为空")
|
||||
return
|
||||
}
|
||||
tenantID := pipelineTenantID(cl)
|
||||
if p.ID == 0 {
|
||||
c.createProject(cl, tenantID, &p)
|
||||
return
|
||||
}
|
||||
c.updateProject(cl, tenantID, &p)
|
||||
}
|
||||
|
||||
func (c *AppCrmProjectController) createProject(cl *jwtutil.Claims, tenantID string, p *crmProjectSavePayload) {
|
||||
custIDVal, custRefType, customerName := resolveProjectCustomer(tenantID, p.CustomerRefType, p.CustomerID, p.CustomerName)
|
||||
status := p.Status
|
||||
if status == 0 {
|
||||
status = 1
|
||||
}
|
||||
pt, upstream, downstream, upContact, upPhone, downContact, downPhone := normalizeProjectFields(&p.projectPayload)
|
||||
supplierID, supplierRefType, supplierName := resolveProjectSupplier(tenantID, p.SupplierRefType, p.SupplierID, p.SupplierName)
|
||||
if pt == 1 {
|
||||
supplierID, supplierRefType, supplierName = nil, 0, ""
|
||||
}
|
||||
now := time.Now()
|
||||
proj := models.TenantCrmProject{
|
||||
TenantID: tenantID,
|
||||
ProjectName: strings.TrimSpace(p.ProjectName),
|
||||
ProjectNo: strings.TrimSpace(p.ProjectNo),
|
||||
ProjectType: pt,
|
||||
CustomerID: custIDVal,
|
||||
CustomerRefType: custRefType,
|
||||
CustomerName: customerName,
|
||||
SupplierID: supplierID,
|
||||
SupplierRefType: supplierRefType,
|
||||
SupplierName: supplierName,
|
||||
SupplierIndustry: strings.TrimSpace(p.SupplierIndustry),
|
||||
OwnerUserID: firstNonEmpty(p.OwnerUserID, pipelineUID(cl)),
|
||||
OwnerUserName: firstNonEmpty(p.OwnerUserName, resolveUserName(cl)),
|
||||
Industry: strings.TrimSpace(p.Industry),
|
||||
UpstreamAmount: upstream,
|
||||
DownstreamAmount: downstream,
|
||||
Amount: upstream,
|
||||
Status: status,
|
||||
StartDate: parsePipelineDate(p.StartDate),
|
||||
EndDate: parsePipelineDate(p.EndDate),
|
||||
UpstreamContact: upContact,
|
||||
UpstreamPhone: upPhone,
|
||||
DownstreamContact: downContact,
|
||||
DownstreamPhone: downPhone,
|
||||
ContactPerson: upContact,
|
||||
ContactPhone: upPhone,
|
||||
Address: strings.TrimSpace(p.Address),
|
||||
Remark: p.Remark,
|
||||
CreateUserID: pipelineUID(cl),
|
||||
CreateTime: now,
|
||||
UpdateTime: now,
|
||||
}
|
||||
if crmProjectNameDuplicated(tenantID, proj.ProjectName, proj.CustomerID, proj.CustomerRefType) {
|
||||
c.erpFail(409, 409, "该客户下已存在同名项目(可能由其他同事创建)")
|
||||
return
|
||||
}
|
||||
id, err := models.Orm.Insert(&proj)
|
||||
if err != nil {
|
||||
c.erpFail(500, 500, "创建失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if cid, err := services.EnsureCrmProjectDocCategory(cl.TenantId, uint64(id), proj.ProjectName, 0); err == nil {
|
||||
proj.DocCategoryID = cid
|
||||
}
|
||||
crmWriteLog(tenantID, 3, uint64(id), "create", "创建项目:"+proj.ProjectName, cl)
|
||||
c.erpOk(map[string]interface{}{"id": id, "doc_category_id": proj.DocCategoryID})
|
||||
}
|
||||
|
||||
func (c *AppCrmProjectController) updateProject(cl *jwtutil.Claims, tenantID string, p *crmProjectSavePayload) {
|
||||
var proj models.TenantCrmProject
|
||||
if err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
||||
Filter("id", p.ID).Filter("tenant_id", tenantID).
|
||||
Filter("delete_time__isnull", true).One(&proj); err != nil {
|
||||
c.erpFail(404, 404, "项目未找到")
|
||||
return
|
||||
}
|
||||
if !canAccessCrmRecord(cl, proj.OwnerUserID, proj.CreateUserID) {
|
||||
c.erpFail(403, 403, "无权操作该项目数据")
|
||||
return
|
||||
}
|
||||
|
||||
proj.ProjectName = strings.TrimSpace(p.ProjectName)
|
||||
proj.ProjectNo = strings.TrimSpace(p.ProjectNo)
|
||||
if p.CustomerID > 0 {
|
||||
cid, cref, cname := resolveProjectCustomer(tenantID, p.CustomerRefType, p.CustomerID, p.CustomerName)
|
||||
proj.CustomerID = cid
|
||||
proj.CustomerRefType = cref
|
||||
proj.CustomerName = cname
|
||||
} else if strings.TrimSpace(p.CustomerName) != "" {
|
||||
proj.CustomerName = strings.TrimSpace(p.CustomerName)
|
||||
}
|
||||
if strings.TrimSpace(p.OwnerUserID) != "" {
|
||||
proj.OwnerUserID = strings.TrimSpace(p.OwnerUserID)
|
||||
}
|
||||
if strings.TrimSpace(p.OwnerUserName) != "" {
|
||||
proj.OwnerUserName = strings.TrimSpace(p.OwnerUserName)
|
||||
}
|
||||
proj.Industry = strings.TrimSpace(p.Industry)
|
||||
pt, upstream, downstream, upContact, upPhone, downContact, downPhone := normalizeProjectFields(&p.projectPayload)
|
||||
supplierID, supplierRefType, supplierName := resolveProjectSupplier(tenantID, p.SupplierRefType, p.SupplierID, p.SupplierName)
|
||||
if pt == 1 {
|
||||
supplierID, supplierRefType, supplierName = nil, 0, ""
|
||||
}
|
||||
proj.ProjectType = pt
|
||||
proj.SupplierID = supplierID
|
||||
proj.SupplierRefType = supplierRefType
|
||||
proj.SupplierName = supplierName
|
||||
proj.SupplierIndustry = strings.TrimSpace(p.SupplierIndustry)
|
||||
proj.UpstreamAmount = upstream
|
||||
proj.DownstreamAmount = downstream
|
||||
proj.Amount = upstream
|
||||
proj.UpstreamContact = upContact
|
||||
proj.UpstreamPhone = upPhone
|
||||
proj.DownstreamContact = downContact
|
||||
proj.DownstreamPhone = downPhone
|
||||
proj.ContactPerson = upContact
|
||||
proj.ContactPhone = upPhone
|
||||
|
||||
prevStatus := proj.Status
|
||||
if p.Status != 0 {
|
||||
proj.Status = p.Status
|
||||
}
|
||||
proj.StartDate = parsePipelineDate(p.StartDate)
|
||||
proj.EndDate = parsePipelineDate(p.EndDate)
|
||||
proj.Address = strings.TrimSpace(p.Address)
|
||||
proj.Remark = p.Remark
|
||||
proj.UpdateTime = time.Now()
|
||||
|
||||
statusChanged := p.Status != 0 && p.Status != prevStatus
|
||||
reason := strings.TrimSpace(p.Reason)
|
||||
if p.Status == 5 && prevStatus != 5 && reason == "" {
|
||||
c.erpFail(400, 400, "请填写项目异常原因")
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := models.Orm.Update(&proj); err != nil {
|
||||
c.erpFail(500, 500, "更新失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
if proj.DocCategoryID > 0 {
|
||||
_ = services.RenameCrmProjectDocCategory(cl.TenantId, proj.DocCategoryID, proj.ProjectName)
|
||||
}
|
||||
if statusChanged {
|
||||
logText := "项目状态:" + projectStatusName(prevStatus) + " → " + projectStatusName(proj.Status)
|
||||
if proj.Status == 5 && reason != "" {
|
||||
logText += ";异常原因:" + reason
|
||||
}
|
||||
crmWriteLog(tenantID, 3, proj.ID, "status", logText, cl)
|
||||
}
|
||||
crmWriteLog(tenantID, 3, proj.ID, "update", "更新项目:"+proj.ProjectName, cl)
|
||||
c.erpOk(map[string]interface{}{"id": proj.ID})
|
||||
}
|
||||
|
||||
// ChangeStatus POST /api/crm/project/status/:id
|
||||
func (c *AppCrmProjectController) ChangeStatus() {
|
||||
cl, err := c.erpClaims()
|
||||
if c.erpDenied(err) {
|
||||
return
|
||||
}
|
||||
id := c.erpPathID()
|
||||
if id == 0 {
|
||||
c.erpFail(400, 400, "无效的ID")
|
||||
return
|
||||
}
|
||||
var body struct {
|
||||
Status int8 `json:"status"`
|
||||
Reason string `json:"reason"`
|
||||
}
|
||||
if !c.erpBody(&body, false) {
|
||||
return
|
||||
}
|
||||
if body.Status < 1 || body.Status > 5 {
|
||||
c.erpFail(400, 400, "项目状态不合法")
|
||||
return
|
||||
}
|
||||
reason := strings.TrimSpace(body.Reason)
|
||||
if body.Status == 5 && reason == "" {
|
||||
c.erpFail(400, 400, "请填写项目异常原因")
|
||||
return
|
||||
}
|
||||
tenantID := pipelineTenantID(cl)
|
||||
var proj models.TenantCrmProject
|
||||
if err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
||||
Filter("id", id).Filter("tenant_id", tenantID).
|
||||
Filter("delete_time__isnull", true).One(&proj); err != nil {
|
||||
c.erpFail(404, 404, "项目未找到")
|
||||
return
|
||||
}
|
||||
if !canAccessCrmRecord(cl, proj.OwnerUserID, proj.CreateUserID) {
|
||||
c.erpFail(403, 403, "无权操作该项目数据")
|
||||
return
|
||||
}
|
||||
prevStatus := proj.Status
|
||||
proj.Status = body.Status
|
||||
proj.UpdateTime = time.Now()
|
||||
if _, err := models.Orm.Update(&proj); err != nil {
|
||||
c.erpFail(500, 500, "状态更新失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
logText := "项目状态:" + projectStatusName(prevStatus) + " → " + projectStatusName(proj.Status)
|
||||
if proj.Status == 5 && reason != "" {
|
||||
logText += ";异常原因:" + reason
|
||||
}
|
||||
crmWriteLog(tenantID, 3, proj.ID, "status", logText, cl)
|
||||
c.erpOk(map[string]interface{}{"id": proj.ID, "status": proj.Status})
|
||||
}
|
||||
|
||||
// Delete DELETE /api/crm/project/delete/:id
|
||||
func (c *AppCrmProjectController) Delete() {
|
||||
cl, err := c.erpClaims()
|
||||
if c.erpDenied(err) {
|
||||
return
|
||||
}
|
||||
id := c.erpPathID()
|
||||
if id == 0 {
|
||||
c.erpFail(400, 400, "无效的ID")
|
||||
return
|
||||
}
|
||||
tenantID := pipelineTenantID(cl)
|
||||
var proj models.TenantCrmProject
|
||||
if err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
||||
Filter("id", id).Filter("tenant_id", tenantID).
|
||||
Filter("delete_time__isnull", true).One(&proj); err != nil {
|
||||
c.erpFail(404, 404, "项目未找到")
|
||||
return
|
||||
}
|
||||
if !canAccessCrmRecord(cl, proj.OwnerUserID, proj.CreateUserID) {
|
||||
c.erpFail(403, 403, "无权操作该项目数据")
|
||||
return
|
||||
}
|
||||
if !canDeleteCrmRecord(cl, proj.CreateUserID) {
|
||||
c.erpFail(403, 403, "只有创建人、租户管理员或平台管理员可以删除该项目")
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
if _, err := models.Orm.QueryTable(new(models.TenantCrmProject)).
|
||||
Filter("id", id).Filter("tenant_id", tenantID).
|
||||
Update(map[string]interface{}{"delete_time": now, "update_time": now}); err != nil {
|
||||
c.erpFail(500, 500, "删除失败: "+err.Error())
|
||||
return
|
||||
}
|
||||
crmWriteLog(tenantID, 3, proj.ID, "delete", "删除项目:"+proj.ProjectName, cl)
|
||||
c.erpOk(map[string]interface{}{"id": proj.ID})
|
||||
}
|
||||
@@ -0,0 +1,161 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"strings"
|
||||
|
||||
"server/services"
|
||||
authsvc "server/services/auth"
|
||||
|
||||
"github.com/beego/beego/v2/core/logs"
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
// AuthCodeLoginController 手机号/邮箱验证码登录(统一认证体系)。
|
||||
type AuthCodeLoginController struct {
|
||||
beego.Controller
|
||||
}
|
||||
|
||||
func (c *AuthCodeLoginController) serveJSON(data map[string]interface{}) {
|
||||
c.Data["json"] = data
|
||||
_ = c.ServeJSON()
|
||||
}
|
||||
|
||||
func (c *AuthCodeLoginController) fail(bizCode int, msg string) {
|
||||
c.Ctx.Output.SetStatus(400)
|
||||
c.serveJSON(map[string]interface{}{"code": bizCode, "msg": msg})
|
||||
}
|
||||
|
||||
func (c *AuthCodeLoginController) readBody() map[string]interface{} {
|
||||
body := c.Ctx.Input.RequestBody
|
||||
if len(body) == 0 {
|
||||
body, _ = io.ReadAll(c.Ctx.Request.Body)
|
||||
}
|
||||
var req map[string]interface{}
|
||||
if err := json.Unmarshal(body, &req); err != nil {
|
||||
return map[string]interface{}{}
|
||||
}
|
||||
return req
|
||||
}
|
||||
|
||||
func codeLoginString(req map[string]interface{}, key string) string {
|
||||
if req == nil {
|
||||
return ""
|
||||
}
|
||||
if v, ok := req[key].(string); ok {
|
||||
return strings.TrimSpace(v)
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// SendCode 发送登录验证码。account 为手机号或邮箱,channel 为 sms/email。
|
||||
func (c *AuthCodeLoginController) SendCode() {
|
||||
req := c.readBody()
|
||||
account := codeLoginString(req, "account")
|
||||
channel := codeLoginString(req, "channel")
|
||||
if account == "" {
|
||||
c.fail(400, "请输入手机号或邮箱")
|
||||
return
|
||||
}
|
||||
if channel == "" {
|
||||
if services.IsEmail(account) {
|
||||
channel = "email"
|
||||
} else if services.IsMobile(account) {
|
||||
channel = "sms"
|
||||
}
|
||||
}
|
||||
if channel == "sms" && !services.IsMobile(account) {
|
||||
c.fail(400, "请输入正确的手机号")
|
||||
return
|
||||
}
|
||||
if channel == "email" && !services.IsEmail(account) {
|
||||
c.fail(400, "请输入正确的邮箱")
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
if channel == "email" {
|
||||
err = services.SendIdentityEmailCode("login", account)
|
||||
} else {
|
||||
err = services.SendIdentityCode("login", account)
|
||||
}
|
||||
if err != nil {
|
||||
c.fail(400, err.Error())
|
||||
return
|
||||
}
|
||||
c.serveJSON(map[string]interface{}{"code": 200, "msg": "验证码已发送"})
|
||||
}
|
||||
|
||||
// Login 验证码登录。校验验证码后签发统一认证登录态。
|
||||
func (c *AuthCodeLoginController) Login() {
|
||||
req := c.readBody()
|
||||
account := codeLoginString(req, "account")
|
||||
code := codeLoginString(req, "code")
|
||||
channel := codeLoginString(req, "channel")
|
||||
clientID := codeLoginString(req, "client_id")
|
||||
if clientID == "" {
|
||||
clientID = "yz-miniapp"
|
||||
}
|
||||
if account == "" || code == "" {
|
||||
c.fail(400, "请输入手机号/邮箱和验证码")
|
||||
return
|
||||
}
|
||||
if channel == "" {
|
||||
if services.IsEmail(account) {
|
||||
channel = "email"
|
||||
} else {
|
||||
channel = "sms"
|
||||
}
|
||||
}
|
||||
|
||||
var verifyErr error
|
||||
if channel == "email" {
|
||||
verifyErr = services.VerifyIdentityEmailCode("login", account, code)
|
||||
} else {
|
||||
verifyErr = services.VerifyIdentityCode("login", account, code)
|
||||
}
|
||||
if verifyErr != nil {
|
||||
c.fail(400, verifyErr.Error())
|
||||
return
|
||||
}
|
||||
|
||||
identity, err := authsvc.FindIdentityByAccount(account)
|
||||
if err != nil || identity == nil {
|
||||
c.fail(404, "账号不存在")
|
||||
return
|
||||
}
|
||||
|
||||
amr := authsvc.AmrSms
|
||||
loginType := "sms"
|
||||
if channel == "email" {
|
||||
amr = "email"
|
||||
loginType = "email"
|
||||
}
|
||||
|
||||
clientIP := c.Ctx.Input.IP()
|
||||
userAgent := c.Ctx.Request.UserAgent()
|
||||
result, err := authsvc.CodeLogin(identity, clientID, clientIP, userAgent, amr, loginType)
|
||||
if err != nil {
|
||||
if err == authsvc.ErrNoTenantBound {
|
||||
c.fail(403, err.Error())
|
||||
return
|
||||
}
|
||||
logs.Warn("[auth-code-login] 登录失败: %v", err)
|
||||
c.fail(400, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
resp := map[string]interface{}{
|
||||
"code": 200,
|
||||
"msg": "登录成功",
|
||||
"tokens": result.Tokens,
|
||||
"tenants": result.Tenants,
|
||||
"identity": result.Identity,
|
||||
}
|
||||
if result.NeedChooseTenant {
|
||||
resp["need_choose_tenant"] = true
|
||||
resp["sid"] = result.Tokens.Sid
|
||||
}
|
||||
c.serveJSON(resp)
|
||||
}
|
||||
@@ -305,6 +305,7 @@ func (c *BackendProductPurchaseController) GetPackageList() {
|
||||
if _, err := models.Orm.QueryTable(new(models.SystemTenantPackage)).
|
||||
Filter("delete_time__isnull", true).
|
||||
Filter("status", 1).
|
||||
Filter("can_purchase", 1).
|
||||
OrderBy("sort", "id").
|
||||
All(&rows); err != nil {
|
||||
c.jsonErr(500, 500, "获取失败:"+err.Error())
|
||||
@@ -349,6 +350,7 @@ func (c *BackendProductPurchaseController) GetPackageList() {
|
||||
"extra_user_price": r.ExtraUserPrice,
|
||||
"is_default": r.IsDefault,
|
||||
"allow_repurchase": r.AllowRepurchase,
|
||||
"can_purchase": r.CanPurchase,
|
||||
"purchased": purchased[r.ID],
|
||||
"sort": r.Sort,
|
||||
"status": r.Status,
|
||||
@@ -378,6 +380,7 @@ func (c *BackendProductPurchaseController) GetPackageDetailByCode() {
|
||||
Filter("code", code).
|
||||
Filter("delete_time__isnull", true).
|
||||
Filter("status", 1).
|
||||
Filter("can_purchase", 1).
|
||||
One(&row); err != nil {
|
||||
c.jsonErr(404, 404, "套餐不存在或已下架")
|
||||
return
|
||||
@@ -448,6 +451,10 @@ func (c *BackendProductPurchaseController) CreatePackageOrder() {
|
||||
c.jsonErr(404, 404, "套餐不存在或已下架")
|
||||
return
|
||||
}
|
||||
if pkg.CanPurchase == 0 {
|
||||
c.jsonErr(403, 403, "该套餐暂不支持购买,请联系平台管理员")
|
||||
return
|
||||
}
|
||||
if pkg.Price <= 0 {
|
||||
c.jsonErr(400, 400, "该套餐未配置价格,请联系平台")
|
||||
return
|
||||
|
||||
@@ -38,6 +38,8 @@ type tenantDTO struct {
|
||||
UserQuota int `json:"user_quota"`
|
||||
UserUsed int `json:"user_used"`
|
||||
StorageQuotaMB int `json:"storage_quota_mb"`
|
||||
StorageUsedMB float64 `json:"storage_used_mb"`
|
||||
StoragePercent float64 `json:"storage_percent"`
|
||||
Remark string `json:"remark"`
|
||||
CreateTime *time.Time `json:"create_time,omitempty"`
|
||||
UpdateTime *time.Time `json:"update_time,omitempty"`
|
||||
@@ -164,6 +166,11 @@ func (c *PlatformTenantController) GetTenant() {
|
||||
dto := toTenantDTO(t)
|
||||
dto.PackageName = packageNames[t.PackageID]
|
||||
dto.UserUsed = userCounts[t.ID]
|
||||
if info, ierr := services.GetTenantStorageInfo(t.ID); ierr == nil && info != nil {
|
||||
dto.StorageQuotaMB = info.QuotaMB
|
||||
dto.StorageUsedMB = math.Round(float64(info.UsedBytes)/float64(1024*1024)*10) / 10
|
||||
dto.StoragePercent = info.UsedPercent
|
||||
}
|
||||
list = append(list, dto)
|
||||
}
|
||||
|
||||
|
||||
@@ -92,6 +92,7 @@ type tenantPackagePayload struct {
|
||||
ExtraUserPrice *float64 `json:"extra_user_price"`
|
||||
IsDefault *int8 `json:"is_default"`
|
||||
AllowRepurchase *int8 `json:"allow_repurchase"`
|
||||
CanPurchase *int8 `json:"can_purchase"`
|
||||
Sort *int `json:"sort"`
|
||||
Status *int8 `json:"status"`
|
||||
Remark string `json:"remark"`
|
||||
@@ -182,6 +183,7 @@ func (c *PlatformTenantPackageController) GetPackageList() {
|
||||
"extra_user_price": r.ExtraUserPrice,
|
||||
"is_default": r.IsDefault,
|
||||
"allow_repurchase": r.AllowRepurchase,
|
||||
"can_purchase": r.CanPurchase,
|
||||
"sort": r.Sort,
|
||||
"status": r.Status,
|
||||
"remark": r.Remark,
|
||||
@@ -241,6 +243,7 @@ func (c *PlatformTenantPackageController) GetPackageSelectList() {
|
||||
"extra_user_price": r.ExtraUserPrice,
|
||||
"is_default": r.IsDefault,
|
||||
"allow_repurchase": r.AllowRepurchase,
|
||||
"can_purchase": r.CanPurchase,
|
||||
"modules": modules,
|
||||
"module_codes": moduleCodes(modules),
|
||||
})
|
||||
@@ -401,6 +404,7 @@ func (c *PlatformTenantPackageController) CreatePackage() {
|
||||
ExtraUserPrice: valueFloat(p.ExtraUserPrice, services.DefaultTenantExtraUserPrice),
|
||||
IsDefault: valueInt8(p.IsDefault, 0),
|
||||
AllowRepurchase: valueInt8(p.AllowRepurchase, 1),
|
||||
CanPurchase: valueInt8(p.CanPurchase, 1),
|
||||
Sort: valueInt(p.Sort, 0),
|
||||
Status: valueInt8(p.Status, 1),
|
||||
Remark: strings.TrimSpace(p.Remark),
|
||||
@@ -491,6 +495,9 @@ func (c *PlatformTenantPackageController) EditPackage() {
|
||||
if p.AllowRepurchase != nil {
|
||||
update["allow_repurchase"] = *p.AllowRepurchase
|
||||
}
|
||||
if p.CanPurchase != nil {
|
||||
update["can_purchase"] = *p.CanPurchase
|
||||
}
|
||||
|
||||
allowRepurchase := int8(1)
|
||||
if p.AllowRepurchase != nil {
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
SET @sql := CONCAT(
|
||||
'ALTER TABLE `yz_system_tenant_package` ',
|
||||
IF(
|
||||
(SELECT COUNT(*) FROM information_schema.COLUMNS
|
||||
WHERE TABLE_SCHEMA = DATABASE()
|
||||
AND TABLE_NAME = 'yz_system_tenant_package'
|
||||
AND COLUMN_NAME = 'can_purchase') = 0,
|
||||
'ADD COLUMN `can_purchase` TINYINT NOT NULL DEFAULT 1 COMMENT ''1=允许租户购买 0=仅后台指派'' AFTER `allow_repurchase`',
|
||||
''
|
||||
)
|
||||
);
|
||||
|
||||
SET @sql := IF(RIGHT(TRIM(@sql), 1) = '`', 'SELECT 1 AS column_already_exists', @sql);
|
||||
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
@@ -13,6 +13,7 @@ type SystemTenantPackage struct {
|
||||
ExtraUserPrice float64 `orm:"column(extra_user_price);digits(10);decimals(2)" json:"extra_user_price"`
|
||||
IsDefault int8 `orm:"column(is_default);default(0)" json:"is_default"`
|
||||
AllowRepurchase int8 `orm:"column(allow_repurchase);default(1)" json:"allow_repurchase"`
|
||||
CanPurchase int8 `orm:"column(can_purchase);default(1)" json:"can_purchase"`
|
||||
Sort int `orm:"column(sort);default(0)" json:"sort"`
|
||||
Status int8 `orm:"column(status);default(1)" json:"status"`
|
||||
Remark string `orm:"column(remark);size(255)" json:"remark"`
|
||||
|
||||
@@ -8,6 +8,7 @@ import (
|
||||
|
||||
func Register() {
|
||||
RegisterErpRoutes()
|
||||
RegisterCrmRoutes()
|
||||
|
||||
beego.Router("/api/softwareupgrade/check", &controllers.ApiSoftwareUpgradeController{}, "get:Check")
|
||||
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"server/controllers"
|
||||
|
||||
beego "github.com/beego/beego/v2/server/web"
|
||||
)
|
||||
|
||||
func RegisterCrmRoutes() {
|
||||
beego.Router("/api/crm/project/list", &controllers.AppCrmProjectController{}, "get:List")
|
||||
beego.Router("/api/crm/project/detail/:id", &controllers.AppCrmProjectController{}, "get:Detail")
|
||||
beego.Router("/api/crm/project/save", &controllers.AppCrmProjectController{}, "post:Save")
|
||||
beego.Router("/api/crm/project/status/:id", &controllers.AppCrmProjectController{}, "post:ChangeStatus")
|
||||
beego.Router("/api/crm/project/delete/:id", &controllers.AppCrmProjectController{}, "delete:Delete")
|
||||
}
|
||||
@@ -35,6 +35,9 @@ func Register() {
|
||||
beego.Router("/auth/sessions/kick", &authctl.AuthLoginController{}, "post:KickSession")
|
||||
beego.Router("/auth/verify-config", &authctl.AuthLoginController{}, "get:VerifyConfig")
|
||||
|
||||
beego.Router("/auth/login/code/send", &authctl.AuthCodeLoginController{}, "post:SendCode")
|
||||
beego.Router("/auth/login/code/verify", &authctl.AuthCodeLoginController{}, "post:Login")
|
||||
|
||||
beego.Router("/auth/register", &authctl.AuthAccountController{}, "get:RegisterPage")
|
||||
beego.Router("/auth/register/send-code", &authctl.AuthAccountController{}, "post:SendRegisterCode")
|
||||
beego.Router("/auth/register/verify", &authctl.AuthAccountController{}, "post:RegisterVerify")
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"server/models"
|
||||
)
|
||||
|
||||
// CodeLogin 通过已校验的验证码完成登录,登录态结构与 PasswordLogin 保持一致。
|
||||
// identity 由调用方按手机号/邮箱查得并已完成验证码校验。
|
||||
func CodeLogin(identity *models.AuthIdentity, clientID, ip, userAgent, amr, loginType string) (*LoginResult, error) {
|
||||
if identity == nil {
|
||||
return nil, ErrPasswordWrong
|
||||
}
|
||||
if err := checkIdentityStatus(identity); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tenants, err := ListTenantOptions(identity.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(tenants) == 0 {
|
||||
_ = writeLoginLog(0, identity.ID, accountOf(identity), "", clientID, loginType, amr, 0, ErrNoTenantBound.Error(), ip, userAgent)
|
||||
return nil, ErrNoTenantBound
|
||||
}
|
||||
|
||||
profile, err := BuildProfile(identity)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
result := &LoginResult{Tenants: tenants, Identity: profile}
|
||||
|
||||
targetTid := uint64(PendingTenantID)
|
||||
if len(tenants) == 1 {
|
||||
targetTid = tenants[0].Tid
|
||||
}
|
||||
sess, err := CreateSession(SessionInfo{
|
||||
IdentityID: identity.ID,
|
||||
Tid: targetTid,
|
||||
ClientID: clientID,
|
||||
IP: ip,
|
||||
UserAgent: userAgent,
|
||||
LoginType: loginType,
|
||||
Amr: amr,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
accessTTL := 0
|
||||
if targetTid == PendingTenantID {
|
||||
accessTTL = 600
|
||||
result.NeedChooseTenant = true
|
||||
}
|
||||
|
||||
tokens, err := IssueTokens(TokenIssue{
|
||||
IdentityID: identity.ID,
|
||||
Tid: targetTid,
|
||||
ClientID: clientID,
|
||||
Sid: sess.Sid,
|
||||
Username: profile.Nickname,
|
||||
Amr: amr,
|
||||
AccessTTL: accessTTL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
result.Tokens = tokens
|
||||
|
||||
_ = writeLoginLog(targetTid, identity.ID, accountOf(identity), profile.Nickname, clientID, loginType, amr, 1, "登录成功", ip, userAgent)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
func accountOf(identity *models.AuthIdentity) string {
|
||||
if identity != nil && identity.Account != nil {
|
||||
return *identity.Account
|
||||
}
|
||||
if identity != nil && identity.Mobile != nil {
|
||||
return *identity.Mobile
|
||||
}
|
||||
if identity != nil && identity.Email != nil {
|
||||
return *identity.Email
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -34,23 +34,31 @@ func FindIdentityByAccount(account string) (*models.AuthIdentity, error) {
|
||||
if account == "" {
|
||||
return nil, orm.ErrNoRows
|
||||
}
|
||||
base := models.Orm.QueryTable(new(models.AuthIdentity)).Filter("delete_time__isnull", true)
|
||||
|
||||
for _, field := range []string{"account", "mobile", "email"} {
|
||||
m := &models.AuthIdentity{}
|
||||
if err := base.Filter(field, account).One(m); err == nil {
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 优先走企业绑定关系:企业用户名通过 AuthTenantUser 明确关联到唯一 identity。
|
||||
// 若先按 identity 表的 account 直接匹配,可能命中历史遗留的同名账号(账号相同但密码/手机号不同),
|
||||
// 导致用错误的密码哈希校验而登录失败。
|
||||
var bind models.AuthTenantUser
|
||||
if err := models.Orm.QueryTable(new(models.AuthTenantUser)).
|
||||
Filter("account", account).
|
||||
Filter("delete_time__isnull", true).
|
||||
OrderBy("-is_default", "id").
|
||||
One(&bind); err == nil {
|
||||
One(&bind); err == nil && bind.IdentityID > 0 {
|
||||
m := &models.AuthIdentity{}
|
||||
if err := models.Orm.QueryTable(new(models.AuthIdentity)).Filter("id", bind.IdentityID).One(m); err == nil {
|
||||
if err := models.Orm.QueryTable(new(models.AuthIdentity)).
|
||||
Filter("id", bind.IdentityID).
|
||||
Filter("delete_time__isnull", true).
|
||||
One(m); err == nil {
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
// 回退:身份只认手机号/邮箱作为凭证,不用 account 字段直接匹配身份。
|
||||
// 企业账号必须经上面的 AuthTenantUser 绑定关系定位身份,避免账号撞名命中错误身份。
|
||||
base := models.Orm.QueryTable(new(models.AuthIdentity)).Filter("delete_time__isnull", true)
|
||||
for _, field := range []string{"mobile", "email"} {
|
||||
m := &models.AuthIdentity{}
|
||||
if err := base.Filter(field, account).One(m); err == nil {
|
||||
return m, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ func SendIdentityCode(purpose, phone string) error {
|
||||
if !IsMobile(phone) {
|
||||
return errors.New("请输入正确的手机号")
|
||||
}
|
||||
if purpose != "register" && purpose != "reset" {
|
||||
if purpose != "register" && purpose != "reset" && purpose != "login" {
|
||||
return errors.New("参数错误")
|
||||
}
|
||||
|
||||
@@ -61,7 +61,10 @@ func SendIdentityCode(purpose, phone string) error {
|
||||
if purpose == "register" && existed != nil {
|
||||
return errors.New("该手机号已注册,请直接登录或找回密码")
|
||||
}
|
||||
if purpose == "reset" && existed == nil {
|
||||
if (purpose == "reset" || purpose == "login") && existed == nil {
|
||||
if purpose == "login" {
|
||||
return errors.New("该手机号尚未注册,请先注册")
|
||||
}
|
||||
return errors.New("该手机号尚未注册")
|
||||
}
|
||||
|
||||
|
||||
@@ -77,10 +77,13 @@ func SendIdentityEmailCode(purpose, email string) error {
|
||||
if !IsEmail(email) {
|
||||
return errors.New("请输入正确的邮箱地址")
|
||||
}
|
||||
if purpose != "reset" {
|
||||
if purpose != "reset" && purpose != "login" {
|
||||
return errors.New("参数错误")
|
||||
}
|
||||
if findIdentityByEmail(email) == nil {
|
||||
if purpose == "login" {
|
||||
return errors.New("该邮箱尚未注册,请先注册")
|
||||
}
|
||||
return errors.New("该邮箱尚未绑定账号")
|
||||
}
|
||||
|
||||
@@ -97,15 +100,21 @@ func SendIdentityEmailCode(purpose, email string) error {
|
||||
}
|
||||
|
||||
code := fmt.Sprintf("%06d", rand.Intn(1000000)) //nolint:gosec // 验证码场景可接受
|
||||
mailTitle := "找回密码验证码"
|
||||
mailHeading := "找回密码验证码"
|
||||
if purpose == "login" {
|
||||
mailTitle = "登录验证码"
|
||||
mailHeading = "登录验证码"
|
||||
}
|
||||
html := fmt.Sprintf(`
|
||||
<div style="font-family: Arial, sans-serif; padding: 22px; border: 1px solid #eee; border-radius: 6px; max-width: 520px;">
|
||||
<h3 style="color: #2563eb; margin: 0 0 16px;">找回密码验证码</h3>
|
||||
<h3 style="color: #2563eb; margin: 0 0 16px;">%s</h3>
|
||||
<p style="font-size: 15px; color: #333; margin: 0 0 6px;">您的验证码是:</p>
|
||||
<p style="font-size: 30px; font-weight: 700; letter-spacing: 6px; color: #2563eb; margin: 0 0 18px;">%s</p>
|
||||
<p style="font-size: 13px; color: #666; margin: 0;">验证码 5 分钟内有效。为保护账号安全,请勿转发给他人。</p>
|
||||
</div>
|
||||
`, code)
|
||||
if err := SendHTMLEmailSMTP(cfg, email, "找回密码验证码", html); err != nil {
|
||||
`, mailHeading, code)
|
||||
if err := SendHTMLEmailSMTP(cfg, email, mailTitle, html); err != nil {
|
||||
return errors.New("邮件发送失败,请稍后重试")
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user