🎨 新增讲师相关接口(待完善)

This commit is contained in:
2025-07-20 03:07:42 +08:00
parent 48ddb72cf2
commit 70f65c96bd
10 changed files with 186 additions and 6 deletions

View File

@@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"git.echol.cn/loser/lckt/global" "git.echol.cn/loser/lckt/global"
"git.echol.cn/loser/lckt/model/app"
r "git.echol.cn/loser/lckt/model/common/response" r "git.echol.cn/loser/lckt/model/common/response"
"git.echol.cn/loser/lckt/model/user/request" "git.echol.cn/loser/lckt/model/user/request"
"git.echol.cn/loser/lckt/utils" "git.echol.cn/loser/lckt/utils"
@@ -194,6 +195,7 @@ func (*AppUserApi) PwdLogin(ctx *gin.Context) {
}, "登录成功", ctx) }, "登录成功", ctx)
} }
// Register 用户手机注册
func (a *AppUserApi) Register(context *gin.Context) { func (a *AppUserApi) Register(context *gin.Context) {
var p request.RegisterReq var p request.RegisterReq
if err := context.ShouldBind(&p); err != nil { if err := context.ShouldBind(&p); err != nil {
@@ -241,3 +243,30 @@ func (a *AppUserApi) Register(context *gin.Context) {
"ExpiresAt": claims.RegisteredClaims.ExpiresAt.Unix() * 1000, "ExpiresAt": claims.RegisteredClaims.ExpiresAt.Unix() * 1000,
}, "注册成功", context) }, "注册成功", context)
} }
// ApplyTeacher 申请成为教师
func (a *AppUserApi) ApplyTeacher(context *gin.Context) {
var p app.TeacherApply
if err := context.ShouldBind(&p); err != nil {
r.FailWithMessage(err.Error(), context)
global.GVA_LOG.Error("参数错误,申请失败", zap.Error(err))
return
}
id := user_jwt.GetUserID(context)
if id == 0 {
global.GVA_LOG.Error("获取用户ID失败")
r.FailWithMessage("获取用户ID失败", context)
return
}
p.UserId = id
if err := appUserService.ApplyTeacher(p); err != nil {
global.GVA_LOG.Error("申请失败", zap.Error(err))
r.FailWithMessage("申请失败", context)
return
}
r.OkWithMessage("申请成功", context)
}

View File

@@ -114,3 +114,27 @@ func (a *UserApi) GetTeachers(context *gin.Context) {
PageSize: p.PageSize, PageSize: p.PageSize,
}, "讲师列表获取成功", context) }, "讲师列表获取成功", context)
} }
// GetTeacherApplyList 获取教师申请列表
func (a *UserApi) GetTeacherApplyList(context *gin.Context) {
var p request.GetTeacherApplyListReq
if err := context.ShouldBind(&p); err != nil {
r.FailWithMessage(err.Error(), context)
global.GVA_LOG.Error("参数错误,获取教师申请列表失败", zap.Error(err))
return
}
list, total, err := userService.GetTeacherApplyList(p)
if err != nil {
global.GVA_LOG.Error("获取教师申请列表失败", zap.Error(err))
r.FailWithMessage("获取教师申请列表失败", context)
return
}
r.OkWithDetailed(
r.PageResult{
List: list,
Total: total,
Page: p.Page,
PageSize: p.PageSize,
}, "获取教师申请列表成功", context)
}

View File

@@ -0,0 +1,17 @@
package app
import "git.echol.cn/loser/lckt/global"
type TeacherApply struct {
global.GVA_MODEL
UserId uint `json:"userId"`
Reason string `json:"reason" gorm:"type:varchar(255);comment:申请理由"`
Status int8 `json:"status" gorm:"default:0;comment:申请状态 0 待审核 1 通过 2 拒绝"`
ExpectRate string `json:"expectRate" gorm:"type:varchar(255);comment:期望分成比例"`
Phone string `json:"phone" gorm:"type:varchar(20);comment:联系电话"`
Nickname string `json:"nickname" gorm:"type:varchar(255);comment:讲师名称"`
}
func (TeacherApply) TableName() string {
return "teacher_apply"
}

View File

@@ -66,3 +66,17 @@ type RegisterReq struct {
UserType int8 `json:"user_type" form:"user_type"` UserType int8 `json:"user_type" form:"user_type"`
UserLabel int64 `json:"user_label" form:"user_label"` UserLabel int64 `json:"user_label" form:"user_label"`
} }
type ApplyTeacherReq struct {
UserId int `json:"user_id" form:"user_id" vd:"@:len($)>0; msg:'用户ID不能为空'"`
Phone string `json:"phone" form:"phone" vd:"@:len($)>0; msg:'手机号码不能为空'"`
NickName string `json:"nick_name" form:"nick_name" vd:"@:len($)>0; msg:'昵称不能为空'"`
Desc string `json:"desc" form:"desc" vd:"@:len($)>0; msg:'申请理由不能为空'"`
ExpectRate string `json:"expect_rate" form:"expect_rate" vd:"@:len($)>0; msg:'期望分成比例不能为空'"`
}
type GetTeacherApplyListReq struct {
request.PageInfo
Phone string `json:"phone" form:"phone"`
Nickname string `json:"nickname" form:"nickname"`
}

View File

@@ -11,6 +11,8 @@ func (s *UserRouter) InitAppUserRouter(AppAuthGroup, PublicRouter *gin.RouterGro
publicRouter := PublicRouter.Group("h5_user") publicRouter := PublicRouter.Group("h5_user")
{ {
appUserRouter.GET("/info", userApi.GetUserInfo) // 获取用户信息 appUserRouter.GET("/info", userApi.GetUserInfo) // 获取用户信息
//申请成为讲师
appUserRouter.POST("/applyTeacher", userApi.ApplyTeacher) // 申请成为讲师
} }
{ {
publicRouter.POST("wxLogin", userApi.WechatLogin) // 微信登录 publicRouter.POST("wxLogin", userApi.WechatLogin) // 微信登录

View File

@@ -17,5 +17,6 @@ func (s *UserRouter) InitUserRouter(Router *gin.RouterGroup, PublicRouter *gin.R
userRouter.PUT("status/:id", userApi.SetUserStatus) // 更新用户状态 userRouter.PUT("status/:id", userApi.SetUserStatus) // 更新用户状态
userRouter.GET(":id", userApi.GetUserById) // 获取用户信息 userRouter.GET(":id", userApi.GetUserById) // 获取用户信息
userRouter.GET("/teachers", userApi.GetTeachers) // 获取教师列表 userRouter.GET("/teachers", userApi.GetTeachers) // 获取教师列表
userRouter.GET("/teacherApplyList", userApi.GetTeacherApplyList) // 获取教师信息
} }
} }

View File

@@ -3,6 +3,7 @@ package app
import ( import (
"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/vo" "git.echol.cn/loser/lckt/model/app/vo"
"git.echol.cn/loser/lckt/model/user" "git.echol.cn/loser/lckt/model/user"
"git.echol.cn/loser/lckt/model/user/request" "git.echol.cn/loser/lckt/model/user/request"
@@ -155,3 +156,11 @@ func (u *AppUserService) Register(p request.RegisterReq) (userInfo user.User, er
}) })
return return
} }
func (u *AppUserService) ApplyTeacher(p app.TeacherApply) (err error) {
if err = global.GVA_DB.Save(&p).Error; err != nil {
global.GVA_LOG.Error("保存申请信息失败", zap.Error(err))
return err
}
return nil
}

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"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/vo" "git.echol.cn/loser/lckt/model/app/vo"
common "git.echol.cn/loser/lckt/model/common/request" common "git.echol.cn/loser/lckt/model/common/request"
"git.echol.cn/loser/lckt/model/user" "git.echol.cn/loser/lckt/model/user"
@@ -177,3 +178,27 @@ func (u *UserService) GetTeachers(p common.PageInfo) (list []vo.UserInfo, total
err = db.Limit(limit).Offset(offset).Find(&list).Error err = db.Limit(limit).Offset(offset).Find(&list).Error
return return
} }
func (u *UserService) GetTeacherApplyList(p request.GetTeacherApplyListReq) (list []app.TeacherApply, total int64, err error) {
query := global.GVA_DB.Model(&app.TeacherApply{})
if p.Phone != "" {
query = query.Where("phone LIKE ?", "%"+p.Phone+"%")
}
if p.Nickname != "" {
query = query.Where("nick_name LIKE ?", "%"+p.Nickname+"%")
}
err = query.Count(&total).Error
if err != nil {
global.GVA_LOG.Error("查询申请列表总数失败", zap.Error(err))
return
}
err = query.Offset((p.Page - 1) * p.PageSize).Limit(p.PageSize).Order("create_at desc").Find(&list).Error
if err != nil {
global.GVA_LOG.Error("查询申请列表失败", zap.Error(err))
return
}
return
}

View File

@@ -2,6 +2,7 @@ package test
import ( import (
"fmt" "fmt"
"git.echol.cn/loser/lckt/utils/sms"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"math/rand" "math/rand"
"testing" "testing"
@@ -25,3 +26,16 @@ func TestPwd(t *testing.T) {
fmt.Println("密码正确") fmt.Println("密码正确")
} }
} }
func TestCode(t *testing.T) {
// 测试验证码生成
rand.New(rand.NewSource(time.Now().UnixNano()))
verifyCode := fmt.Sprintf("%06v", rand.Int31n(1000000))
test := sms.SendSMSTest("17754945397", verifyCode)
if test {
fmt.Println("短信发送成功")
} else {
fmt.Println("短信发送失败")
}
}

View File

@@ -2,10 +2,15 @@ package sms
// SMS短信服务 // SMS短信服务
import ( import (
"crypto/tls"
"encoding/json" "encoding/json"
"fmt"
"git.echol.cn/loser/lckt/global" "git.echol.cn/loser/lckt/global"
"github.com/alibabacloud-go/tea/tea" "github.com/alibabacloud-go/tea/tea"
"go.uber.org/zap" "go.uber.org/zap"
"net/http"
"net/url"
"strings"
util "github.com/alibabacloud-go/tea-utils/v2/service" util "github.com/alibabacloud-go/tea-utils/v2/service"
@@ -71,3 +76,43 @@ func SendSMS(phone string, code string) bool {
global.GVA_LOG.Info("短信[阿里云]", zap.String("发送成功", "手机号: "+phone)) global.GVA_LOG.Info("短信[阿里云]", zap.String("发送成功", "手机号: "+phone))
return true return true
} }
func SendSMSTest(phone, code string) bool {
endpoint := "https://dfsns.market.alicloudapi.com/data/send_sms"
templateID := "CST_ptdie100"
// 构造 POST 表单数据
form := url.Values{}
form.Set("content", fmt.Sprintf("code:%s", code))
form.Set("template_id", templateID)
form.Set("phone_number", phone)
// 创建 HTTP 请求
req, err := http.NewRequest("POST", endpoint, strings.NewReader(form.Encode()))
if err != nil {
return false
}
// 添加请求头
req.Header.Set("Authorization", "APPCODE "+"b8f46ced154b44c5a40a0a49a91e1634")
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
// 创建 HTTP 客户端(跳过证书校验,模拟 curl -k
client := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
}
// 发送请求
resp, err := client.Do(req)
if err != nil {
return false
}
defer resp.Body.Close()
if resp.Status != "ok" {
return false
}
return true
}