增加api错误码并删除无用的接口
This commit is contained in:
@@ -5,8 +5,6 @@ type ApiSystem struct {
|
||||
LoginApi LoginApi
|
||||
UserApi UserApi
|
||||
FileApi FileApi
|
||||
CaptchaApi CaptchaApi
|
||||
RegisterApi RegisterApi
|
||||
NoticeApi NoticeApi
|
||||
ModuleConfigApi ModuleConfigApi
|
||||
MonitorApi MonitorApi
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"strconv"
|
||||
"sun-panel/lib/captcha"
|
||||
"sun-panel/lib/cmn"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
type CaptchaApi struct {
|
||||
ErrMsg string // 错误信息
|
||||
}
|
||||
|
||||
// 获取图像
|
||||
func (c *CaptchaApi) GetImage(ctx *gin.Context) {
|
||||
key := cmn.BuildRandCode(16, cmn.RAND_CODE_MODE2)
|
||||
width, _ := strconv.Atoi(ctx.Param("width"))
|
||||
height, _ := strconv.Atoi(ctx.Param("height"))
|
||||
if width == 0 || width > 500 {
|
||||
width = 120
|
||||
}
|
||||
if height == 0 || height > 500 {
|
||||
height = 44
|
||||
}
|
||||
// 设置网页验证码的cookie
|
||||
ctx.SetCookie("CaptchaId", key, 3600, "/", "", false, false)
|
||||
base64Str := captcha.GenerateCaptchaHandler(key, width, height)
|
||||
_ = base64Str
|
||||
// base64 字符串一般会包含头部 data:image/xxx;base64, 需要去除
|
||||
baseImg, _ := base64.StdEncoding.DecodeString(base64Str[22:])
|
||||
_, _ = ctx.Writer.WriteString(string(baseImg))
|
||||
}
|
||||
|
||||
// 获取图像根据验证器id,id从地址栏获取
|
||||
func (c *CaptchaApi) GetImageByCaptchaId(ctx *gin.Context) {
|
||||
// key := cmn.BuildRandCode(16, cmn.RAND_CODE_MODE2)
|
||||
width, _ := strconv.Atoi(ctx.Param("width"))
|
||||
height, _ := strconv.Atoi(ctx.Param("height"))
|
||||
captchaId := ctx.Param("captchaId")
|
||||
if width == 0 || width > 500 {
|
||||
width = 120
|
||||
}
|
||||
if height == 0 || height > 500 {
|
||||
height = 44
|
||||
}
|
||||
// 设置网页验证码的cookie
|
||||
base64Str := captcha.GenerateCaptchaHandler(captchaId, width, height)
|
||||
_ = base64Str
|
||||
// base64 字符串一般会包含头部 data:image/xxx;base64, 需要去除
|
||||
baseImg, _ := base64.StdEncoding.DecodeString(base64Str[22:])
|
||||
_, _ = ctx.Writer.WriteString(string(baseImg))
|
||||
}
|
||||
|
||||
func (c *CaptchaApi) CheckVCode(id, vcode string) {
|
||||
// Captcha.Store = base64Captcha.DefaultMemStore
|
||||
// if store.Verify(id, vcode, true) {
|
||||
// body = map[string]interface{}{"code": 1001, "msg": "ok"}
|
||||
// }
|
||||
// w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
||||
// json.NewEncoder(w).Encode(body)
|
||||
}
|
||||
@@ -25,7 +25,7 @@ func (a *FileApi) UploadImg(c *gin.Context) {
|
||||
configUpload := global.Config.GetValueString("base", "source_path")
|
||||
f, err := c.FormFile("imgfile")
|
||||
if err != nil {
|
||||
apiReturn.Error(c, "上传失败")
|
||||
apiReturn.ErrorByCode(c, 1300)
|
||||
return
|
||||
} else {
|
||||
fileExt := strings.ToLower(path.Ext(f.Filename))
|
||||
@@ -40,7 +40,7 @@ func (a *FileApi) UploadImg(c *gin.Context) {
|
||||
}
|
||||
|
||||
if !cmn.InArray(agreeExts, fileExt) {
|
||||
apiReturn.Error(c, "上传失败!只允许png,jpg,gif,jpeg,svg,ico文件")
|
||||
apiReturn.ErrorByCode(c, 1301)
|
||||
return
|
||||
}
|
||||
fileName := cmn.Md5(fmt.Sprintf("%s%s", f.Filename, time.Now().String()))
|
||||
@@ -67,7 +67,7 @@ func (a *FileApi) UploadFiles(c *gin.Context) {
|
||||
|
||||
form, err := c.MultipartForm()
|
||||
if err != nil {
|
||||
apiReturn.Error(c, "上传失败")
|
||||
apiReturn.ErrorByCode(c, 1300)
|
||||
return
|
||||
}
|
||||
files := form.File["files[]"]
|
||||
|
||||
@@ -3,15 +3,12 @@ package system
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
"sun-panel/api/api_v1/common/apiData/commonApiStructs"
|
||||
"sun-panel/api/api_v1/common/apiReturn"
|
||||
"sun-panel/api/api_v1/common/base"
|
||||
"sun-panel/global"
|
||||
"sun-panel/lib/cmn"
|
||||
"sun-panel/lib/cmn/systemSetting"
|
||||
"sun-panel/lib/mail"
|
||||
"sun-panel/models"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
@@ -114,87 +111,3 @@ func (l *LoginApi) Logout(c *gin.Context) {
|
||||
global.CUserToken.Delete(cToken)
|
||||
apiReturn.Success(c)
|
||||
}
|
||||
|
||||
// 获取重置密码的验证码
|
||||
func (l *LoginApi) SendResetPasswordVCode(c *gin.Context) {
|
||||
type ResstRequest struct {
|
||||
LoginLoginVerify
|
||||
Verification commonApiStructs.VerificationRequest `json:"verification"`
|
||||
}
|
||||
req := ResstRequest{}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
apiReturn.Error(c, global.Lang.Get("common.api_error_param_format"))
|
||||
return
|
||||
}
|
||||
|
||||
// 验证码验证
|
||||
{
|
||||
errCode, verifcationId := base.VerificationCheck(req.Verification.CodeID, req.Verification.VCode)
|
||||
if errCode != apiReturn.ERROR_CODE_SUCCESS {
|
||||
apiReturn.ErrorVerification(c, errCode, verifcationId)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
emailVCode := cmn.BuildRandCode(6, cmn.RAND_CODE_MODE2)
|
||||
global.VerifyCodeCachePool.Set(req.Email, emailVCode, 10*time.Minute)
|
||||
|
||||
userCheck := &models.User{Mail: req.Email}
|
||||
userInfo := userCheck.GetUserInfoByMail()
|
||||
if userInfo == nil {
|
||||
apiReturn.Error(c, "账号不存在")
|
||||
return
|
||||
}
|
||||
emailInfoConfig := systemSetting.Email{}
|
||||
global.SystemSetting.GetValueByInterface("system_email", &emailInfoConfig)
|
||||
emailInfo := mail.EmailInfo{
|
||||
Username: emailInfoConfig.Mail,
|
||||
Password: emailInfoConfig.Password,
|
||||
Host: emailInfoConfig.Host,
|
||||
Port: emailInfoConfig.Port,
|
||||
}
|
||||
if err := mail.SendResetPasswordVCode(mail.NewEmailer(emailInfo), req.Email, emailVCode); err != nil {
|
||||
apiReturn.Error(c, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
apiReturn.Success(c)
|
||||
|
||||
}
|
||||
|
||||
// 使用邮箱验证码重置密码
|
||||
func (l *LoginApi) ResetPasswordByVCode(c *gin.Context) {
|
||||
req := registerInfo{}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
apiReturn.Error(c, global.Lang.Get("common.api_error_param_format"))
|
||||
return
|
||||
}
|
||||
|
||||
userCheck := &models.User{Mail: req.Email}
|
||||
userInfo := userCheck.GetUserInfoByMail()
|
||||
if userInfo == nil {
|
||||
apiReturn.Error(c, "账号不存在")
|
||||
return
|
||||
}
|
||||
|
||||
// 校验验证码
|
||||
{
|
||||
if emailVCode, ok := global.VerifyCodeCachePool.Get(req.Email); !ok || req.EmailVCode != emailVCode {
|
||||
apiReturn.Error(c, global.Lang.Get("common.captcha_code_error"))
|
||||
return
|
||||
}
|
||||
global.VerifyCodeCachePool.Delete(req.Email)
|
||||
}
|
||||
|
||||
updateData := map[string]interface{}{
|
||||
"password": cmn.PasswordEncryption(req.Password),
|
||||
"token": "",
|
||||
}
|
||||
global.UserToken.Delete(userInfo.Token) // 更新用户信息
|
||||
if err := userInfo.UpdateUserInfoByUserId(userInfo.ID, updateData); err != nil {
|
||||
apiReturn.ErrorDatabase(c, err.Error())
|
||||
return
|
||||
}
|
||||
apiReturn.Success(c)
|
||||
|
||||
}
|
||||
|
||||
@@ -1,183 +0,0 @@
|
||||
package system
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"math/rand"
|
||||
"sun-panel/api/api_v1/common/apiData/commonApiStructs"
|
||||
"sun-panel/api/api_v1/common/apiReturn"
|
||||
"sun-panel/api/api_v1/common/base"
|
||||
"sun-panel/global"
|
||||
"sun-panel/lib/cmn"
|
||||
"sun-panel/lib/cmn/systemSetting"
|
||||
"sun-panel/lib/mail"
|
||||
"sun-panel/models"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type registerInfo struct {
|
||||
Email string `json:"email"`
|
||||
UserName string `json:"userName"`
|
||||
Password string `json:"password"`
|
||||
Vcode string `json:"vcode"`
|
||||
EmailVCode string `json:"emailVCode"`
|
||||
VCode string `json:"vCode"`
|
||||
Verification commonApiStructs.VerificationRequest `json:"verification"`
|
||||
ReferralCode string `json:"referralCode"`
|
||||
}
|
||||
|
||||
const EmailCodeCapacity = 1000
|
||||
|
||||
type RegisterApi struct{}
|
||||
|
||||
// 获取注册验证码
|
||||
func (l RegisterApi) SendRegisterVcode(c *gin.Context) {
|
||||
req := registerInfo{}
|
||||
err := c.ShouldBindJSON(&req)
|
||||
req.Email = req.UserName
|
||||
if err != nil {
|
||||
apiReturn.ErrorParamFomat(c, err.Error())
|
||||
return
|
||||
}
|
||||
errMsg, err := base.ValidateInputStruct(req)
|
||||
if err != nil {
|
||||
apiReturn.Error(c, errMsg)
|
||||
return
|
||||
}
|
||||
|
||||
// 验证码验证
|
||||
{
|
||||
errCode, verifcationId := base.VerificationCheck(req.Verification.CodeID, req.Verification.VCode)
|
||||
if errCode != apiReturn.ERROR_CODE_SUCCESS {
|
||||
apiReturn.ErrorVerification(c, errCode, verifcationId)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 验证是否开启注册和后缀格式是否正确
|
||||
{
|
||||
systemSettingInfo := systemSetting.ApplicationSetting{}
|
||||
if err := global.SystemSetting.GetValueByInterface("system_application", &systemSettingInfo); err != nil || !systemSettingInfo.Register.OpenRegister {
|
||||
apiReturn.Error(c, global.Lang.Get("register.unopened_register"))
|
||||
return
|
||||
}
|
||||
|
||||
if systemSettingInfo.Register.EmailSuffix != "" && !cmn.VerifyFormat("^.*"+systemSettingInfo.Register.EmailSuffix+"$", req.Email) {
|
||||
apiReturn.Error(c, global.Lang.GetWithFields("register.emailSuffix_error", map[string]string{"EmailSuffix": systemSettingInfo.Register.EmailSuffix}))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 验证邮箱是否被注册
|
||||
{
|
||||
userCheck := &models.User{Mail: req.UserName}
|
||||
if _, err := userCheck.GetUserInfoByUsername(req.UserName); err == nil && err != gorm.ErrRecordNotFound {
|
||||
apiReturn.Error(c, global.Lang.Get("register.mail_exist"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
emailCode := generateEmailCode()
|
||||
count, err := global.VerifyCodeCachePool.ItemCount()
|
||||
if err != nil || count >= EmailCodeCapacity {
|
||||
global.VerifyCodeCachePool.Flush()
|
||||
}
|
||||
global.VerifyCodeCachePool.Set(req.Email, emailCode, 0)
|
||||
emailInfoConfig := systemSetting.Email{}
|
||||
global.SystemSetting.GetValueByInterface("system_email", &emailInfoConfig)
|
||||
emailInfo := mail.EmailInfo{
|
||||
Username: emailInfoConfig.Mail,
|
||||
Password: emailInfoConfig.Password,
|
||||
Host: emailInfoConfig.Host,
|
||||
Port: emailInfoConfig.Port,
|
||||
}
|
||||
err = mail.SendRegisterEmail(mail.NewEmailer(emailInfo), req.Email, emailCode)
|
||||
if err != nil {
|
||||
apiReturn.Error(c, global.Lang.Get("mail.send_mail_fail"))
|
||||
global.Logger.Errorf("[register] fail to send email to%s", req.UserName)
|
||||
return
|
||||
}
|
||||
apiReturn.Success(c)
|
||||
}
|
||||
|
||||
// 注册提交(开始注册)
|
||||
func (l *RegisterApi) Commit(c *gin.Context) {
|
||||
req := registerInfo{}
|
||||
err := c.ShouldBindJSON(&req)
|
||||
req.Email = req.UserName
|
||||
if err != nil {
|
||||
apiReturn.ErrorParamFomat(c, err.Error())
|
||||
return
|
||||
}
|
||||
errMsg, err := base.ValidateInputStruct(req)
|
||||
if err != nil {
|
||||
apiReturn.Error(c, errMsg)
|
||||
return
|
||||
}
|
||||
|
||||
// 验证是否开启注册和后缀格式是否正确
|
||||
{
|
||||
systemSettingInfo := systemSetting.ApplicationSetting{}
|
||||
if err := global.SystemSetting.GetValueByInterface("system_application", &systemSettingInfo); err != nil || !systemSettingInfo.Register.OpenRegister {
|
||||
apiReturn.Error(c, global.Lang.Get("register.unopened_register"))
|
||||
return
|
||||
}
|
||||
|
||||
if systemSettingInfo.Register.EmailSuffix != "" && !cmn.VerifyFormat("^.*"+systemSettingInfo.Register.EmailSuffix+"$", req.Email) {
|
||||
apiReturn.Error(c, global.Lang.GetWithFields("register.emailSuffix_error", map[string]string{"EmailSuffix": systemSettingInfo.Register.EmailSuffix}))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 验证邮箱是否被注册
|
||||
{
|
||||
userCheck := &models.User{Mail: req.UserName}
|
||||
if _, err := userCheck.GetUserInfoByUsername(req.UserName); err == nil && err != gorm.ErrRecordNotFound {
|
||||
apiReturn.Error(c, global.Lang.Get("register.mail_exist"))
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 验证码验证
|
||||
{
|
||||
vCode, ok := global.VerifyCodeCachePool.Get(req.Email)
|
||||
if !ok {
|
||||
apiReturn.Error(c, global.Lang.Get("common.captcha_code_error"))
|
||||
//验证码不存在
|
||||
return
|
||||
}
|
||||
if vCode != req.EmailVCode {
|
||||
apiReturn.Error(c, global.Lang.Get("common.captcha_code_error"))
|
||||
return
|
||||
//验证码有误
|
||||
}
|
||||
}
|
||||
|
||||
// 自动生成用户昵称
|
||||
name := "用户" + cmn.BuildRandCode(4, cmn.RAND_CODE_MODE3)
|
||||
|
||||
//验证通过,注册
|
||||
user := &models.User{
|
||||
Mail: req.UserName,
|
||||
Name: name,
|
||||
Username: req.UserName,
|
||||
Password: cmn.PasswordEncryption(req.Password),
|
||||
Status: 1,
|
||||
Role: 2,
|
||||
}
|
||||
_, err = user.CreateOne()
|
||||
if err != nil {
|
||||
apiReturn.ErrorDatabase(c, err.Error())
|
||||
return
|
||||
}
|
||||
//删除旧的验证码
|
||||
global.VerifyCodeCachePool.Delete(req.Email)
|
||||
|
||||
apiReturn.Success(c)
|
||||
}
|
||||
|
||||
func generateEmailCode() string {
|
||||
return fmt.Sprintf("%06v", rand.New(rand.NewSource(time.Now().UnixNano())).Int31n(1000000))
|
||||
}
|
||||
Reference in New Issue
Block a user