Files
waf-platform/EdgeUser/internal/web/actions/default/settings/identity/uploadIndividual.go
2026-02-04 20:27:13 +08:00

178 lines
4.5 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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"
)
var allowedContentTypes = map[string]bool{
"image/png": true,
"image/jpeg": true,
}
type UploadIndividualAction struct {
actionutils.ParentAction
}
func (this *UploadIndividualAction) RunPost(params struct {
IdentityId int64
RealName string
Number string
BackFile *actions.File
FrontFile *actions.File
Must *actions.Must
CSRF *actionutils.CSRF
}) {
defer this.CreateLogInfo(codes.UserIdentity_LogUpdateUserIdentityIndividual)
// 检查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)
if !regexp.MustCompile(`^\d+(X)?$`).MatchString(number) {
this.FailField("number", "身份证号中只能含有数字和X字母")
}
var numberLen = len(number)
if numberLen != 15 && numberLen != 18 {
this.FailField("number", "身份证号只能是15或者18位")
}
// back
var backFile = params.BackFile
if backFile == nil {
if mustUpload {
this.Fail("请上传身份证-照片面")
}
} else {
var backContentType = strings.ToLower(backFile.ContentType)
_, ok := allowedContentTypes[backContentType]
if !ok {
this.Fail("请上传正确格式的图片")
}
// TODO 需要可以配置最大上传尺寸,包括界面上的提示
if backFile.Size > 8*sizes.M {
this.Fail("要上传的图片(身份证-照片面不能超过8MiB")
}
}
// 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 backFileId int64 = 0
var frontFileId int64 = 0
if backFile != nil {
fileId, ok, err := this.UploadFile(backFile, "user.idcard")
if err != nil {
this.ErrorPage(err)
return
}
if !ok {
this.Fail("上传失败,请刷新后重试")
}
backFileId = fileId
}
if frontFile != nil {
fileId, ok, err := this.UploadFile(frontFile, "user.idcard")
if err != nil {
this.ErrorPage(err)
return
}
if !ok {
this.Fail("上传失败,请刷新后重试")
}
frontFileId = fileId
}
if len(oldFileIds) == 2 {
if backFileId == 0 {
backFileId = oldFileIds[0]
}
if frontFileId == 0 {
frontFileId = oldFileIds[1]
}
}
var err error
if identityId > 0 {
_, err = this.RPC().UserIdentityRPC().UpdateUserIdentity(this.UserContext(), &pb.UpdateUserIdentityRequest{
UserIdentityId: identityId,
Type: userconfigs.UserIdentityTypeIDCard,
RealName: params.RealName,
Number: number,
FileIds: []int64{backFileId, frontFileId},
})
} else {
_, err = this.RPC().UserIdentityRPC().CreateUserIdentity(this.UserContext(), &pb.CreateUserIdentityRequest{
OrgType: userconfigs.UserIdentityOrgTypeIndividual,
Type: userconfigs.UserIdentityTypeIDCard,
RealName: params.RealName,
Number: number,
FileIds: []int64{backFileId, frontFileId},
})
}
if err != nil {
this.ErrorPage(err)
return
}
this.Success()
}