Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request ThingsPanel#216 from November-12/main
Browse files Browse the repository at this point in the history
GetCaptcha
  • Loading branch information
November-12 authored Sep 25, 2023
2 parents d45545c + 780c667 commit e86a4fd
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
40 changes: 40 additions & 0 deletions controllers/tp_notification.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package controllers

import (
"ThingsPanel-Go/initialize/psql"
"ThingsPanel-Go/initialize/redis"
sendmessage "ThingsPanel-Go/initialize/send_message"
"ThingsPanel-Go/models"
"ThingsPanel-Go/services"
"ThingsPanel-Go/utils"
response "ThingsPanel-Go/utils"
valid "ThingsPanel-Go/validate"
"encoding/json"
"fmt"
"math/rand"
"strconv"
"strings"
"time"

Expand Down Expand Up @@ -523,3 +527,39 @@ func (c *TpNotification) HistoryList() {

response.SuccessWithDetailed(200, "success", ret, map[string]string{}, (*context2.Context)(c.Ctx))
}

func (c *TpNotification) GetCaptcha() {
var input struct {
PhoneNumber string `json:"phone_number" valid:"Required"`
}

err := json.Unmarshal(c.Ctx.Input.RequestBody, &input)

if err != nil {
response.SuccessWithMessage(400, "参数解析错误", (*context2.Context)(c.Ctx))
return
}

verificationCode := fmt.Sprintf("%04d", rand.Intn(10000))
err = redis.SetStr(input.PhoneNumber+"_code", verificationCode, 3*time.Minute)

if err != nil {
response.SuccessWithMessage(400, "redis err", (*context2.Context)(c.Ctx))
return
}

num, err := strconv.Atoi(input.PhoneNumber)
if err != nil {
response.SuccessWithMessage(400, "参数解析错误", (*context2.Context)(c.Ctx))
return
}

err = sendmessage.SendSMSVerificationCode(num, verificationCode, "")

if err != nil {
response.SuccessWithMessage(400, "message err", (*context2.Context)(c.Ctx))
return
}

response.SuccessWithDetailed(200, "success", nil, map[string]string{}, (*context2.Context)(c.Ctx))
}
3 changes: 2 additions & 1 deletion middleware/auth_middle.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func AuthMiddle() {
"api/auth/refresh": 0,
"api/auth/register": 1,
"api/auth/tenant/register": 0,
"api/auth/captcha": 0,
"/ws": 2,
"api/ota/download": 0,
}
Expand Down Expand Up @@ -78,7 +79,7 @@ func isAuthExceptUrl(url string, m map[string]interface{}) bool {
return ok
}

//获取token
// 获取token
func GetToken(ctx *context.Context) (string, error) {
authorization := ctx.Input.Header("Authorization")
if len(authorization) == 0 {
Expand Down
4 changes: 4 additions & 0 deletions routers/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ func init() {
web.NSRouter("/auth/logout", &controllers.AuthController{}, "*:Logout"), //ty
web.NSRouter("/auth/refresh", &controllers.AuthController{}, "*:Refresh"), //ty
web.NSRouter("/auth/me", &controllers.AuthController{}, "*:Me"), //ty

// 获取验证码
web.NSRouter("/auth/captcha", &controllers.TpNotification{}, "*:GetCaptcha"),

// 用户注册
web.NSRouter("/auth/register", &controllers.AuthController{}, "*:Register"), //ty
//租户注册TenantRegister
Expand Down

0 comments on commit e86a4fd

Please sign in to comment.