131 lines
3.4 KiB
Go
131 lines
3.4 KiB
Go
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved. Official site: https://goedge.cn .
|
||
|
||
package identity
|
||
|
||
import (
|
||
"github.com/TeaOSLab/EdgeCommon/pkg/langs/codes"
|
||
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
|
||
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
|
||
"github.com/TeaOSLab/EdgeUser/internal/utils/sizes"
|
||
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
|
||
"github.com/iwind/TeaGo/actions"
|
||
"regexp"
|
||
"strings"
|
||
)
|
||
|
||
type UploadEnterpriseAction struct {
|
||
actionutils.ParentAction
|
||
}
|
||
|
||
func (this *UploadEnterpriseAction) RunPost(params struct {
|
||
IdentityId int64
|
||
|
||
RealName string
|
||
Number string
|
||
FrontFile *actions.File
|
||
|
||
Must *actions.Must
|
||
CSRF *actionutils.CSRF
|
||
}) {
|
||
defer this.CreateLogInfo(codes.UserIdentity_LogUpdateUserIdentityEnterprise)
|
||
|
||
// 检查IdentityId
|
||
var identityId = params.IdentityId
|
||
var oldFileIds = []int64{0, 0}
|
||
if identityId > 0 {
|
||
identityResp, err := this.RPC().UserIdentityRPC().FindEnabledUserIdentity(this.UserContext(), &pb.FindEnabledUserIdentityRequest{
|
||
UserIdentityId: identityId,
|
||
})
|
||
if err != nil {
|
||
this.ErrorPage(err)
|
||
return
|
||
}
|
||
if identityResp.UserIdentity == nil {
|
||
this.Fail("错误的表单数据,请刷新后重试")
|
||
}
|
||
|
||
var status = identityResp.UserIdentity.Status
|
||
if status != userconfigs.UserIdentityStatusNone && status != userconfigs.UserIdentityStatusRejected {
|
||
this.Fail("当前状态下无法修改")
|
||
}
|
||
|
||
oldFileIds = identityResp.UserIdentity.FileIds
|
||
}
|
||
var mustUpload = identityId <= 0
|
||
|
||
params.Must.
|
||
Field("realName", params.RealName).
|
||
Require("请输入企业全称").
|
||
Field("number", params.Number).
|
||
Require("请输入营业执照号码")
|
||
|
||
var number = regexp.MustCompile(`\s+`).ReplaceAllString(params.Number, "")
|
||
number = strings.ToUpper(number)
|
||
|
||
// front
|
||
var frontFile = params.FrontFile
|
||
if frontFile == nil {
|
||
if mustUpload {
|
||
this.Fail("请上传营业执照照片")
|
||
}
|
||
} else {
|
||
var frontContentType = strings.ToLower(frontFile.ContentType)
|
||
_, ok := allowedContentTypes[frontContentType]
|
||
if !ok {
|
||
this.Fail("请上传正确格式的图片")
|
||
}
|
||
|
||
// TODO 需要可以配置最大上传尺寸,包括界面上的提示
|
||
if frontFile.Size > 8*sizes.M {
|
||
this.Fail("要上传的图片(营业执照照片)不能超过8MiB")
|
||
}
|
||
}
|
||
|
||
// 上传到服务器
|
||
|
||
var frontFileId int64 = 0
|
||
|
||
if frontFile != nil {
|
||
fileId, ok, err := this.UploadFile(frontFile, "enterprise.license")
|
||
if err != nil {
|
||
this.ErrorPage(err)
|
||
return
|
||
}
|
||
if !ok {
|
||
this.Fail("上传失败,请刷新后重试")
|
||
}
|
||
frontFileId = fileId
|
||
}
|
||
|
||
if len(oldFileIds) == 1 {
|
||
if frontFileId == 0 {
|
||
frontFileId = oldFileIds[0]
|
||
}
|
||
}
|
||
|
||
var err error
|
||
if identityId > 0 {
|
||
_, err = this.RPC().UserIdentityRPC().UpdateUserIdentity(this.UserContext(), &pb.UpdateUserIdentityRequest{
|
||
UserIdentityId: identityId,
|
||
Type: userconfigs.UserIdentityTypeEnterpriseLicense,
|
||
RealName: params.RealName,
|
||
Number: number,
|
||
FileIds: []int64{frontFileId},
|
||
})
|
||
} else {
|
||
_, err = this.RPC().UserIdentityRPC().CreateUserIdentity(this.UserContext(), &pb.CreateUserIdentityRequest{
|
||
OrgType: userconfigs.UserIdentityOrgTypeEnterprise,
|
||
Type: userconfigs.UserIdentityTypeEnterpriseLicense,
|
||
RealName: params.RealName,
|
||
Number: number,
|
||
FileIds: []int64{frontFileId},
|
||
})
|
||
}
|
||
if err != nil {
|
||
this.ErrorPage(err)
|
||
return
|
||
}
|
||
|
||
this.Success()
|
||
}
|