lhs 4 年之前
父節點
當前提交
0eee6e5097
共有 4 個文件被更改,包括 104 次插入32 次删除
  1. 38 2
      controller/auth.go
  2. 2 0
      main.go
  3. 27 0
      repository/redis/init.go
  4. 37 30
      service/auth.go

+ 38 - 2
controller/auth.go

@@ -93,8 +93,44 @@ func (a *Auth) GetPhoneVFCode() (err error) {
 	codeType := a.postIntNecessary("codeType")
 	picId := a.Ctx().PostForm("picId")
 	picCode := a.Ctx().PostForm("picCode")
-	service.Auth{}.GenVFCode(codeType, phone, a.getIp(), picId, picCode)
+	err = service.Auth{}.GenVFCode(codeType, phone, a.getIp(), picId, picCode)
+	if err != nil {
+		return
+	}
+	a.Ctx().JSON(http.StatusOK, newResponseBase())
+	return
+}
 
-	a.json(newResponseBase())
+type LoginResponse struct {
+	ResponseBase
+	Rs map[string]string
+}
+
+// PhoneSignIn godoc
+// @Summary 手机验证码登录
+// @tags Auth
+// @Description 手机验证码登录
+// @Accept  x-www-form-urlencoded
+// @Produce  json
+// @Param phone formData string true "手机号"
+// @Param smsCode formData int true "验证码"
+// @Success 200 {object} controller.ResponseBase
+// @Router /Auth/PhoneSignIn [post]
+func (a *Auth) PhoneSignIn() (err error) {
+	phone := a.postString("phone", true)
+	smsCode := a.postString("smsCode", true)
+
+	session, err := service.Auth{}.PhoneSignIn(phone, smsCode, a.getIp())
+	if err != nil {
+		return
+	}
+	r := LoginResponse{
+		ResponseBase: newResponseBase(),
+		Rs: map[string]string{
+			"token": session.Token,
+		},
+	}
+
+	a.Ctx().JSON(http.StatusOK, r)
 	return
 }

+ 2 - 0
main.go

@@ -15,6 +15,7 @@ import (
 	"video_course/logger"
 	"video_course/middleware"
 	"video_course/repository/postgres"
+	"video_course/repository/redis"
 )
 
 var _VERSION_ = "unknown"
@@ -78,6 +79,7 @@ func main() {
 	logger.SetOnlineWriters()
 	flagInit()
 	postgres.Init()
+	redis.Init()
 
 	r := bsf.NewDefaultEngine()
 	gin.SetMode(gin.ReleaseMode)

+ 27 - 0
repository/redis/init.go

@@ -0,0 +1,27 @@
+package redis
+
+import (
+	"github.com/go-redis/redis/v7"
+	"video_course/global"
+)
+
+var client redis.UniversalClient
+
+func GetRedis() redis.UniversalClient {
+	return client
+}
+func Init() (err error) {
+	cfg, err := global.Config.GetRedis()
+	if err != nil {
+		panic(err)
+	}
+
+	opt := &redis.UniversalOptions{
+		Addrs:      cfg.Addrs,
+		Password:   cfg.Password,
+		MasterName: cfg.Mastername,
+	}
+	client = redis.NewUniversalClient(opt)
+	_, err = client.Ping().Result()
+	return
+}

+ 37 - 30
service/auth.go

@@ -1,19 +1,13 @@
 package service
 
 import (
-	//"fmt"
-	//"server/dao/gorm"
-	//"server/dao/short_msg"
-	//"server/dao/wx"
-	//"server/errors"
-	//"server/function"
-	//"server/model"
-	//"server/utils"
-	//"strconv"
-	//"time"
-	h "video_course/repository/http"
-
+	"fmt"
 	"github.com/mojocn/base64Captcha"
+	"time"
+	"video_course/errors"
+	"video_course/global"
+	"video_course/model"
+	rdb "video_course/repository/redis"
 )
 
 var store = base64Captcha.DefaultMemStore
@@ -82,14 +76,25 @@ func (Auth) GenVerifyPic(config *base64Captcha.DriverString) (id string, pic str
 }
 
 // 手机号发送短信验证码
-func (a Auth) GenVFCode(codeTypeInt int, name string, ip string,
+func (a Auth) GenVFCode(codeTypeInt int, phone string, ip string,
 	picId string,
-	picCode string) {
-	//if !store.Verify(picId, picCode, true) {
-	//	panic(errors.ErrPicVerifyCode)
-	//}
+	picCode string) (err error) {
+	//图形验证码验证
+	if !store.Verify(picId, picCode, true) {
+		return errors.ErrPicVerifyCode
+	}
+	//生成短信验证码
+	smsCode := "123456"
+	//发送短信
+	fmt.Println(smsCode)
+	//存储短信验证码
+	redisdb := rdb.GetRedis()
+	redisdb.Set(global.AppName+":"+"smsCode:"+phone, []byte(smsCode), 3*time.Minute)
+
+	return
+	//dao := a.getUserDao()
 	//_ = dao.User{}.GetUserByPhone(name)
-	h.LoalloutServer{}.CheckPhone(name)
+	//h.LoalloutServer{}.CheckPhone(phone)
 	//codeType := model.VFCodeCodeTypeFromInt(codeTypeInt)
 	//vfcodeDao := dao.VFCode{}
 	//nameLastSend := vfcodeDao.NameLastSendTime(codeType, name)
@@ -116,18 +121,20 @@ func (a Auth) GenVFCode(codeTypeInt int, name string, ip string,
 
 }
 
-//func (a Auth) PhoneSignIn(phone string, code string, channel model.LoginChannel, ip string) (
-//	sess *model.Session, logInfo string) {
-//
-//	gorm.VFCodeDAO{}.CheckVFCode(model.VFCodeCodeTypeLogin, phone, code)
-//
-//	user := gorm.UserDAO{}.GetUserByPhone(phone)
-//	user.CheckStatus()
-//
-//	sess = function.Auth{}.WxLoginUser(user, channel, "", ip, phone, user.Phone)
-//	logInfo = fmt.Sprintf("用户[%s]通过手机验证码登录", user.Name)
-//	return
-//}
+func (a Auth) PhoneSignIn(phone string, smsCode string, ip string) (
+	sess *model.Session, err error) {
+
+	//sess = NewSession
+	//
+	//gorm.VFCodeDAO{}.CheckVFCode(model.VFCodeCodeTypeLogin, phone, code)
+	//
+	//user := gorm.UserDAO{}.GetUserByPhone(phone)
+	//user.CheckStatus()
+	//
+	//sess = function.Auth{}.WxLoginUser(user, channel, "", ip, phone, user.Phone)
+	//logInfo = fmt.Sprintf("用户[%s]通过手机验证码登录", user.Name)
+	return
+}
 
 //func (a Auth) PassEdit(sess *model.Session, oldpass string, newpass string) (
 //	logInfo string, err error) {