Files
2026-02-04 20:27:13 +08:00

133 lines
3.3 KiB
Go
Raw Permalink 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 mobileverify
import (
"errors"
"github.com/TeaOSLab/EdgeCommon/pkg/rpc/pb"
"github.com/TeaOSLab/EdgeCommon/pkg/userconfigs"
"github.com/TeaOSLab/EdgeUser/internal/configloaders"
"github.com/TeaOSLab/EdgeUser/internal/utils"
"github.com/TeaOSLab/EdgeUser/internal/web/actions/actionutils"
"github.com/iwind/TeaGo/actions"
"github.com/iwind/TeaGo/types"
)
type IndexAction struct {
actionutils.ParentAction
}
func (this *IndexAction) Init() {
this.Nav("", "", "")
}
func (this *IndexAction) RunGet(params struct {
ViewOnly bool
}) {
this.Data["isModifying"] = !params.ViewOnly
this.Data["verifiedMobile"] = ""
// 是否可以激活手机号码
registerConfig, _ := configloaders.LoadRegisterConfig()
this.Data["canVerify"] = false
if registerConfig == nil {
this.Show()
return
}
var canVerify = registerConfig.MobileVerification.IsOn
if !canVerify {
this.Show()
return
}
this.Data["canVerify"] = true
this.Data["forceVerify"] = registerConfig.MobileVerification.Force
// 当前已激活手机号码
userResp, err := this.RPC().UserRPC().FindEnabledUser(this.UserContext(), &pb.FindEnabledUserRequest{
UserId: this.UserId(),
})
if err != nil {
this.ErrorPage(err)
return
}
if userResp.User == nil {
this.ErrorPage(errors.New("can not find user info"))
return
}
this.Data["verifiedMobile"] = userResp.User.VerifiedMobile
// 有效期
var smsLife = userconfigs.MobileVerificationDefaultLife
if smsLife%60 == 0 {
this.Data["smsLife"] = types.String(smsLife/60) + "分钟"
} else {
this.Data["smsLife"] = types.String(smsLife) + "秒钟"
}
this.Data["smsCodeLength"] = userconfigs.MobileVerificationCodeLength
this.Show()
}
func (this *IndexAction) RunPost(params struct {
Mobile string
Must *actions.Must
CSRF *actionutils.CSRF
}) {
if len(params.Mobile) == 0 {
this.FailField("mobile", "请输入要激活的手机号码")
return
}
if !utils.IsValidMobile(params.Mobile) {
this.FailField("mobile", "手机号码格式不正确")
return
}
// 检查手机号码是否已被使用
checkResp, err := this.RPC().UserRPC().CheckUserMobile(this.UserContext(), &pb.CheckUserMobileRequest{Mobile: params.Mobile})
if err != nil {
this.ErrorPage(err)
return
}
if checkResp.Exists {
this.FailField("mobile", "当前手机号码已被别的用户使用,请换一个")
return
}
// 如果同自己已认证手机号码一致,则不重复发送
userResp, err := this.RPC().UserRPC().FindEnabledUser(this.UserContext(), &pb.FindEnabledUserRequest{UserId: this.UserId()})
if err != nil {
this.ErrorPage(err)
return
}
if userResp.User == nil {
this.Fail("can not find current user")
return
}
if userResp.User.VerifiedMobile == params.Mobile {
this.FailField("mobile", "此手机号码已激活,无需再次激活")
return
}
// 发送激活短信
resp, err := this.RPC().UserMobileVerificationRPC().SendUserMobileVerification(this.UserContext(), &pb.SendUserMobileVerificationRequest{Mobile: params.Mobile})
if err != nil {
this.ErrorPage(err)
return
}
if !resp.IsOk {
if len(resp.ErrorCode) > 0 {
this.Fail("短信发送失败,请稍后重试(错误代号:" + resp.ErrorCode + "")
} else {
this.Fail("短信发送失败,请稍后重试")
}
return
}
this.Success()
}