创建验证码
This commit is contained in:
		
							
								
								
									
										46
									
								
								routers/codes.go
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								routers/codes.go
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,46 @@
 | 
			
		||||
package routers
 | 
			
		||||
 | 
			
		||||
import (
 | 
			
		||||
	"encoding/json"
 | 
			
		||||
	"io"
 | 
			
		||||
	"main/configs"
 | 
			
		||||
	"main/models"
 | 
			
		||||
	"main/utils"
 | 
			
		||||
	"net/http"
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
// 获取验证码列表(仅限管理员)
 | 
			
		||||
func CodesGet(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	var listview models.ListView
 | 
			
		||||
	listview.Page = utils.ParamInt(r.URL.Query().Get("page"), 1)
 | 
			
		||||
	listview.PageSize = utils.ParamInt(r.URL.Query().Get("pageSize"), 20)
 | 
			
		||||
	var codes []models.Code
 | 
			
		||||
	configs.ORMDB().Offset((listview.Page - 1) * listview.PageSize).Limit(listview.PageSize).Find(&codes).Count(&listview.Total)
 | 
			
		||||
	listview.List = codes
 | 
			
		||||
	listview.Next = listview.Page*listview.PageSize < int(listview.Total)
 | 
			
		||||
	listview.WriteJSON(w)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// 创建验证码
 | 
			
		||||
func CodesPost(w http.ResponseWriter, r *http.Request) {
 | 
			
		||||
	// 从body取得参数 email mobile 和 code
 | 
			
		||||
	var code models.Code
 | 
			
		||||
	body, err := io.ReadAll(r.Body)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	defer r.Body.Close()
 | 
			
		||||
	if err := json.Unmarshal(body, &code); err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// 创建验证码
 | 
			
		||||
	if err := models.CodeCreate(code.Email, code.Mobile); err != nil {
 | 
			
		||||
		http.Error(w, err.Error(), http.StatusInternalServerError)
 | 
			
		||||
		return
 | 
			
		||||
	}
 | 
			
		||||
	// 返回成功
 | 
			
		||||
	w.WriteHeader(http.StatusCreated)
 | 
			
		||||
	w.Write([]byte("验证码已发送"))
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user