🎨 一堆杂七杂八的优化

This commit is contained in:
2025-09-01 21:30:10 +08:00
parent 3beb54c12c
commit 80decc222f
7 changed files with 71 additions and 17 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
common "git.echol.cn/loser/lckt/model/common/request" common "git.echol.cn/loser/lckt/model/common/request"
"gorm.io/gorm"
"strconv" "strconv"
"time" "time"
@@ -283,6 +284,11 @@ func (a *AppUserApi) GetTeacherApply(context *gin.Context) {
} }
status, err := appUserService.GetTeacherApplyStatus(id) status, err := appUserService.GetTeacherApplyStatus(id)
// 判断是否是记录未找到的错误
if errors.Is(err, gorm.ErrRecordNotFound) {
r.OkWithMessage("暂无申请记录", context)
return
}
if err != nil { if err != nil {
global.GVA_LOG.Error("获取教师申请状态失败", zap.Error(err)) global.GVA_LOG.Error("获取教师申请状态失败", zap.Error(err))
r.FailWithMessage("获取教师申请状态失败", context) r.FailWithMessage("获取教师申请状态失败", context)

View File

@@ -119,7 +119,7 @@ func Routers() *gin.Engine {
{ {
appRouter.InitAppUserRouter(AppAuthGroup, PublicGroup) appRouter.InitAppUserRouter(AppAuthGroup, PublicGroup)
appRouter.InitBannerRouter(PrivateGroup, PublicGroup) // Banner相关路由 appRouter.InitBannerRouter(PrivateGroup, PublicGroup) // Banner相关路由
appRouter.InitOrderRouter(AppAuthGroup) // 订单相关路由 appRouter.InitOrderRouter(AppAuthGroup, PublicGroup) // 订单相关路由
} }
//插件路由安装 //插件路由安装

View File

@@ -13,7 +13,7 @@ type Response struct {
} }
const ( const (
ERROR = 7 ERROR = 5
SUCCESS = 0 SUCCESS = 0
) )

View File

@@ -5,14 +5,17 @@ import "github.com/gin-gonic/gin"
type OrderRouter struct{} type OrderRouter struct{}
// InitOrderRouter 初始化订单路由 // InitOrderRouter 初始化订单路由
func (r *OrderRouter) InitOrderRouter(AppRouter *gin.RouterGroup) { func (r *OrderRouter) InitOrderRouter(AppRouter, PublicRouter *gin.RouterGroup) {
appRouter := AppRouter.Group("app_order") appRouter := AppRouter.Group("app_order")
publicRouter := PublicRouter.Group("app_order")
{ {
appRouter.POST("", orderApi.CreateOrder) // 创建订单 appRouter.POST("", orderApi.CreateOrder) // 创建订单
appRouter.POST("/wechat/pay", orderApi.PayOrder) // 微信支付订单 appRouter.POST("/wechat/pay", orderApi.PayOrder) // 微信支付订单
appRouter.GET("/list", orderApi.GetOrderList) // 获取订单列表 appRouter.GET("/list", orderApi.GetOrderList) // 获取订单列表
appRouter.GET(":id", orderApi.GetOrderDetail) // 获取订单详情 appRouter.GET(":id", orderApi.GetOrderDetail) // 获取订单详情
appRouter.POST("/balance/pay", orderApi.BalancePay) // 余额支付 appRouter.POST("/balance/pay", orderApi.BalancePay) // 余额支付
appRouter.POST("/notify", orderApi.NotifyOrder) // 微信支付回调通知 }
{
publicRouter.POST("/notify", orderApi.NotifyOrder) // 微信支付回调通知
} }
} }

View File

@@ -1,6 +1,7 @@
package app package app
import ( import (
"errors"
"fmt" "fmt"
"git.echol.cn/loser/lckt/global" "git.echol.cn/loser/lckt/global"
"git.echol.cn/loser/lckt/model/app" "git.echol.cn/loser/lckt/model/app"
@@ -166,12 +167,13 @@ func (u *AppUserService) ApplyTeacher(p app.TeacherApply) (err error) {
return nil return nil
} }
func (u *AppUserService) GetTeacherApplyStatus(id uint) (teacherApply app.TeacherApply, err error) { func (u *AppUserService) GetTeacherApplyStatus(id uint) (teacherApply *app.TeacherApply, err error) {
// 获取最后一条申请记录 // 获取最后一条申请记录
err = global.GVA_DB.Model(&app.TeacherApply{}).Where("user_id = ?", id).Last(&teacherApply).Error err = global.GVA_DB.Model(&app.TeacherApply{}).Where("user_id = ?", id).Last(&teacherApply).Error
if err != nil { // 判断是否是记录未找到的错误
global.GVA_LOG.Error("查询申请记录失败", zap.Error(err)) if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
return teacherApply, err global.GVA_LOG.Error("查询申请信息失败", zap.Error(err))
return
} }
return return

View File

@@ -35,6 +35,17 @@ func (ArticleService) GetArticleList(pageInfo request.GetList) (list []article.A
offset := pageInfo.PageSize * (pageInfo.Page - 1) offset := pageInfo.PageSize * (pageInfo.Page - 1)
// 创建db // 创建db
db := global.GVA_DB.Model(&article.Article{}) db := global.GVA_DB.Model(&article.Article{})
if pageInfo.Title != "" {
db = db.Where("title LIKE ?", "%"+pageInfo.Title+"%")
}
if pageInfo.CategoryId != 0 {
db = db.Where("category_id = ?", pageInfo.CategoryId)
}
if pageInfo.TeacherId != 0 {
db = db.Where("teacher_id = ?", pageInfo.TeacherId)
}
err = db.Count(&total).Error err = db.Count(&total).Error
if err != nil { if err != nil {
return return

View File

@@ -1,18 +1,34 @@
package test package test
import ( import (
"crypto/md5"
"fmt" "fmt"
"git.echol.cn/loser/lckt/utils/sms"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"math/rand" "math/rand"
"net/http"
"net/url"
"testing" "testing"
"time" "time"
) )
const charset = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
func TestRain(t *testing.T) { func TestRain(t *testing.T) {
rand.New(rand.NewSource(time.Now().UnixNano())) num := GenerateOrderNum()
verifyCode := fmt.Sprintf("%06v", rand.Int31n(1000000)) fmt.Println("生成的订单号:", num)
fmt.Println(verifyCode) }
func GenerateOrderNum() string {
rand.Seed(time.Now().UnixNano())
// 拼接用户ID和随机数
data := fmt.Sprintf("%d%d", 6, rand.Intn(1000000))
hash := md5.Sum([]byte(data))
code := ""
for i := 0; i < 12; i++ {
// 取哈希的前6位每位映射到字符集
code += string(charset[int(hash[i])%len(charset)])
}
return code
} }
func TestPwd(t *testing.T) { func TestPwd(t *testing.T) {
@@ -28,14 +44,30 @@ func TestPwd(t *testing.T) {
} }
func TestCode(t *testing.T) { func TestCode(t *testing.T) {
// 测试验证码生成
rand.New(rand.NewSource(time.Now().UnixNano())) rand.New(rand.NewSource(time.Now().UnixNano()))
verifyCode := fmt.Sprintf("%06v", rand.Int31n(1000000)) verifyCode := fmt.Sprintf("%06v", rand.Int31n(1000000))
// 测试验证码生成
sendCode(verifyCode)
}
test := sms.SendSMSTest("17754945397", verifyCode) func sendCode(code string) {
if test { // 内容 通过urlEncode编码
fmt.Println("短信发送成功") content := "【海口龙华铁坚成电子商务商行】您的验证码是" + code + "。如非本人操作,请忽略本短信"
// urlencode编码内容
content = url.QueryEscape(content)
api := "https://api.smsbao.com/sms?u=lchz5599&p=7ea114c87a224cd38a0d616b9be3faed&g=海口龙华铁坚成电子商务商行&m=17754945397&c=" + content
// 发送GET请求
resp, err := http.Get(api)
if err != nil {
fmt.Println("请求失败:", err)
return
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
fmt.Println("请求成功,短信发送成功")
} else { } else {
fmt.Println("短信发送失败") fmt.Println("请求失败,状态码:", resp.StatusCode)
} }
} }