jyq 4 năm trước cách đây
mục cha
commit
a7aefda521
12 tập tin đã thay đổi với 710 bổ sung4 xóa
  1. 68 1
      controller/auth.go
  2. 14 1
      controller/base.go
  3. 124 0
      docs/docs.go
  4. 124 0
      docs/swagger.json
  5. 84 0
      docs/swagger.yaml
  6. 11 2
      errors/error.go
  7. 4 0
      global/init.go
  8. 1 0
      go.mod
  9. 41 0
      model/vfCode.go
  10. 74 0
      repository/http/loallout_server.go
  11. 11 0
      repository/postgres/vf_code.go
  12. 154 0
      service/auth.go

+ 68 - 1
controller/auth.go

@@ -1,8 +1,12 @@
 package controller
 
 import (
-	"github.com/sirupsen/logrus"
 	"net/http"
+	"video_course/service"
+
+	"github.com/sirupsen/logrus"
+
+	"github.com/mojocn/base64Captcha"
 )
 
 type Auth struct {
@@ -31,3 +35,66 @@ func (a *Auth) HttpPostSignUp() (err error) {
 	a.Ctx().JSON(http.StatusOK, newResponseBase())
 	return
 }
+
+type ResponseVerifyPic struct {
+	Id  string
+	Pic string
+}
+
+// GenVerifyPic godoc
+// @Summary 获取验证图片
+// @tags Auth
+// @Description 获取验证图片和验证id
+// @Accept  x-www-form-urlencoded
+// @Param height formData string true "高"
+// @Param width formData string true "宽"
+// @Param noiseCount formData string true "噪点数量"
+// @Param length formData string true "验证码字数"
+// @Param source formData string true "验证码取值范围 比如  1234567890 或者 abcdef等"
+// @Produce json
+// @Success 200 {object} controller.ResponseVerifyPic
+// @Router /Auth/GenVerifyPic [post]
+func (a *Auth) GenVerifyPic() (err error) {
+	config := &base64Captcha.DriverString{}
+	config.Height = a.postIntNecessary("height")
+	config.Width = a.postIntNecessary("width")
+	config.NoiseCount = a.postIntNecessary("noiseCount")
+	config.Length = a.postIntNecessary("length")
+	config.Source = a.Ctx().PostForm("source")
+	if config.Source == "" {
+		config.Source = "1234567890"
+	}
+
+	id, pic := service.Auth{}.GenVerifyPic(config)
+
+	r := ResponseVerifyPic{
+		id,
+		pic,
+	}
+
+	a.Ctx().JSON(http.StatusOK, r)
+	return
+}
+
+// GetPhoneVFCode godoc
+// @Summary 手机获取验证码
+// @tags Auth
+// @Description 手机获取验证码
+// @Accept  x-www-form-urlencoded
+// @Produce  json
+// @Param phone formData string true "手机号"
+// @Param codeType formData int true "验证码类型 1:登录"
+// @Param picId formData string true "图形验证码id"
+// @Param picCode formData string true "图形验证码"
+// @Success 200 {object} controller.ResponseBase
+// @Router /Auth/GetPhoneVFCode [post]
+func (a *Auth) GetPhoneVFCode() (err error) {
+	phone := a.postString("phone", true)
+	codeType := a.postIntNecessary("codeType")
+	picId := a.Ctx().PostForm("picId")
+	picCode := a.Ctx().PostForm("picCode")
+	service.Auth{}.GenVFCode(codeType, phone, a.getIp(), picId, picCode)
+
+	a.json(newResponseBase())
+	return
+}

+ 14 - 1
controller/base.go

@@ -7,13 +7,15 @@ package controller
 
 import (
 	"fmt"
-	"gitee.com/zr233/bsf"
 	"net/http"
 	"strconv"
+	"strings"
 	"time"
 	"video_course/errors"
 	"video_course/model"
 	"video_course/utils"
+
+	"gitee.com/zr233/bsf"
 )
 
 type Controller interface {
@@ -109,3 +111,14 @@ func (b *BaseController) postFloatToInt(key string, necessary bool) int {
 func (b BaseController) json(obj interface{}) {
 	b.Ctx().JSON(http.StatusOK, obj)
 }
+
+// 获取tcp/ip
+func (b BaseController) getIp() string {
+	ip := strings.TrimSpace(b.Ctx().GetHeader("X-Real-Ip"))
+
+	//addr := b.Ctx().Request.RemoteAddr
+	//if ip, _, err := net.SplitHostPort(strings.TrimSpace(addr)); err == nil {
+	//	return ip
+	//}
+	return ip
+}

+ 124 - 0
docs/docs.go

@@ -33,6 +33,119 @@ var doc = `{
     "host": "{{.Host}}",
     "basePath": "{{.BasePath}}",
     "paths": {
+        "/Auth/GenVerifyPic": {
+            "post": {
+                "description": "获取验证图片和验证id",
+                "consumes": [
+                    "application/x-www-form-urlencoded"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Auth"
+                ],
+                "summary": "获取验证图片",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "高",
+                        "name": "height",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "宽",
+                        "name": "width",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "噪点数量",
+                        "name": "noiseCount",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "验证码字数",
+                        "name": "length",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "验证码取值范围 比如  1234567890 或者 abcdef等",
+                        "name": "source",
+                        "in": "formData",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/controller.ResponseVerifyPic"
+                        }
+                    }
+                }
+            }
+        },
+        "/Auth/GetPhoneVFCode": {
+            "post": {
+                "description": "手机获取验证码",
+                "consumes": [
+                    "application/x-www-form-urlencoded"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Auth"
+                ],
+                "summary": "手机获取验证码",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "手机号",
+                        "name": "phone",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "验证码类型 1:登录",
+                        "name": "codeType",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "图形验证码id",
+                        "name": "picId",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "图形验证码",
+                        "name": "picCode",
+                        "in": "formData",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/controller.ResponseBase"
+                        }
+                    }
+                }
+            }
+        },
         "/Auth/SignUp": {
             "post": {
                 "description": "用户添加",
@@ -1361,6 +1474,17 @@ var doc = `{
                 }
             }
         },
+        "controller.ResponseVerifyPic": {
+            "type": "object",
+            "properties": {
+                "id": {
+                    "type": "string"
+                },
+                "pic": {
+                    "type": "string"
+                }
+            }
+        },
         "controller.ShopCoachSimpleInfo": {
             "type": "object",
             "properties": {

+ 124 - 0
docs/swagger.json

@@ -17,6 +17,119 @@
     },
     "basePath": "/v1",
     "paths": {
+        "/Auth/GenVerifyPic": {
+            "post": {
+                "description": "获取验证图片和验证id",
+                "consumes": [
+                    "application/x-www-form-urlencoded"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Auth"
+                ],
+                "summary": "获取验证图片",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "高",
+                        "name": "height",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "宽",
+                        "name": "width",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "噪点数量",
+                        "name": "noiseCount",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "验证码字数",
+                        "name": "length",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "验证码取值范围 比如  1234567890 或者 abcdef等",
+                        "name": "source",
+                        "in": "formData",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/controller.ResponseVerifyPic"
+                        }
+                    }
+                }
+            }
+        },
+        "/Auth/GetPhoneVFCode": {
+            "post": {
+                "description": "手机获取验证码",
+                "consumes": [
+                    "application/x-www-form-urlencoded"
+                ],
+                "produces": [
+                    "application/json"
+                ],
+                "tags": [
+                    "Auth"
+                ],
+                "summary": "手机获取验证码",
+                "parameters": [
+                    {
+                        "type": "string",
+                        "description": "手机号",
+                        "name": "phone",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "integer",
+                        "description": "验证码类型 1:登录",
+                        "name": "codeType",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "图形验证码id",
+                        "name": "picId",
+                        "in": "formData",
+                        "required": true
+                    },
+                    {
+                        "type": "string",
+                        "description": "图形验证码",
+                        "name": "picCode",
+                        "in": "formData",
+                        "required": true
+                    }
+                ],
+                "responses": {
+                    "200": {
+                        "description": "OK",
+                        "schema": {
+                            "$ref": "#/definitions/controller.ResponseBase"
+                        }
+                    }
+                }
+            }
+        },
         "/Auth/SignUp": {
             "post": {
                 "description": "用户添加",
@@ -1345,6 +1458,17 @@
                 }
             }
         },
+        "controller.ResponseVerifyPic": {
+            "type": "object",
+            "properties": {
+                "id": {
+                    "type": "string"
+                },
+                "pic": {
+                    "type": "string"
+                }
+            }
+        },
         "controller.ShopCoachSimpleInfo": {
             "type": "object",
             "properties": {

+ 84 - 0
docs/swagger.yaml

@@ -7,6 +7,13 @@ definitions:
       memo:
         type: string
     type: object
+  controller.ResponseVerifyPic:
+    properties:
+      id:
+        type: string
+      pic:
+        type: string
+    type: object
   controller.ShopCoachSimpleInfo:
     properties:
       code:
@@ -60,6 +67,83 @@ info:
   title: Video Course框架
   version: "1.0"
 paths:
+  /Auth/GenVerifyPic:
+    post:
+      consumes:
+      - application/x-www-form-urlencoded
+      description: 获取验证图片和验证id
+      parameters:
+      - description: 高
+        in: formData
+        name: height
+        required: true
+        type: string
+      - description: 宽
+        in: formData
+        name: width
+        required: true
+        type: string
+      - description: 噪点数量
+        in: formData
+        name: noiseCount
+        required: true
+        type: string
+      - description: 验证码字数
+        in: formData
+        name: length
+        required: true
+        type: string
+      - description: 验证码取值范围 比如  1234567890 或者 abcdef等
+        in: formData
+        name: source
+        required: true
+        type: string
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            $ref: '#/definitions/controller.ResponseVerifyPic'
+      summary: 获取验证图片
+      tags:
+      - Auth
+  /Auth/GetPhoneVFCode:
+    post:
+      consumes:
+      - application/x-www-form-urlencoded
+      description: 手机获取验证码
+      parameters:
+      - description: 手机号
+        in: formData
+        name: phone
+        required: true
+        type: string
+      - description: 验证码类型 1:登录
+        in: formData
+        name: codeType
+        required: true
+        type: integer
+      - description: 图形验证码id
+        in: formData
+        name: picId
+        required: true
+        type: string
+      - description: 图形验证码
+        in: formData
+        name: picCode
+        required: true
+        type: string
+      produces:
+      - application/json
+      responses:
+        "200":
+          description: OK
+          schema:
+            $ref: '#/definitions/controller.ResponseBase'
+      summary: 手机获取验证码
+      tags:
+      - Auth
   /Auth/SignUp:
     post:
       consumes:

+ 11 - 2
errors/error.go

@@ -8,6 +8,7 @@ package errors
 
 import (
 	"fmt"
+
 	"gitee.com/zr233/bsf/errors"
 )
 
@@ -19,21 +20,29 @@ const (
 	CodeBusy                ErrorCode = 1
 	CodeServiceNotAvailable ErrorCode = 2
 	CodeKeyNotExist         ErrorCode = 3
+	PicVerifyCodeErr        ErrorCode = 1003
 	CodePermissionDenied    ErrorCode = 1004
 	CodePasswordErr         ErrorCode = 1006
+	UserBan                 ErrorCode = 1007
 	CodeTokenErr            ErrorCode = 1010
+	UserNotExists           ErrorCode = 1012
 	CodeNoRecord            ErrorCode = 1014
 	BirthdayErr             ErrorCode = 1036
 	StaticHrErr             ErrorCode = 1037
 	CodeParamErr            ErrorCode = 5000
 	CodeDATABASE            ErrorCode = 9000
 	CodeREDIS               ErrorCode = 10000
+	HrSensorsTimeOutErr     ErrorCode = 30012
 	CodeParam                         = ErrorCode(errors.CodeParam)
 )
 
 var (
-	ErrStaticHr = NewServiceErr(StaticHrErr, "会员年龄太小,请重新输入用户生日。")
-	ErrBirthday = NewServiceErr(BirthdayErr, "用户生日不得大于当前年份,请重新输入")
+	ErrUserBan          = NewServiceErr(UserBan, "用户被禁用")
+	ErrUserNotExists    = NewServiceErr(UserNotExists, "用户不存在")
+	ErrPicVerifyCode    = NewServiceErr(PicVerifyCodeErr, "图形验证码错误")
+	ErrStaticHr         = NewServiceErr(StaticHrErr, "会员年龄太小,请重新输入用户生日。")
+	ErrBirthday         = NewServiceErr(BirthdayErr, "用户生日不得大于当前年份,请重新输入")
+	ErrHrSensorsTimeOut = NewServiceErr(HrSensorsTimeOutErr, "心率系统连接超时")
 )
 
 func (e ErrorCode) ShowMsg() string {

+ 4 - 0
global/init.go

@@ -13,6 +13,10 @@ var (
 	Config     *gconfig.Config
 
 	Project struct {
+		UserServer struct {
+			Host string
+		}
+
 		DBName   string
 		LogLevel string
 		Host     string

+ 1 - 0
go.mod

@@ -15,6 +15,7 @@ require (
 	github.com/hashicorp/consul/api v1.8.1
 	github.com/jackc/pgconn v1.8.0
 	github.com/jackc/pgx/v4 v4.10.1
+	github.com/mojocn/base64Captcha v1.3.1
 	github.com/sirupsen/logrus v1.7.0
 	github.com/spf13/viper v1.7.1
 	github.com/swaggo/gin-swagger v1.3.0

+ 41 - 0
model/vfCode.go

@@ -0,0 +1,41 @@
+/**
+ * @ File:
+ * @ Date: 2020/8/28 18:29
+ * @ Author: JYQ
+ * @ Description:
+ */
+package model
+
+import (
+	"fmt"
+	"time"
+	"video_course/errors"
+)
+
+type VFCodeCodeTypeEnum int
+
+const (
+	_ VFCodeCodeTypeEnum = iota
+	VFCodeCodeTypeLogin
+)
+
+func VFCodeCodeTypeFromInt(VFCodeCodeTypeInt int) VFCodeCodeTypeEnum {
+	VFCodeCodeType := VFCodeCodeTypeEnum(VFCodeCodeTypeInt)
+	switch VFCodeCodeType {
+	case VFCodeCodeTypeLogin:
+	default:
+		panic(errors.NewParamErr(fmt.Errorf("vfCode")))
+	}
+	return VFCodeCodeType
+}
+
+type VfCode struct {
+	Id         int
+	Name       string
+	SendType   int
+	CodeType   VFCodeCodeTypeEnum
+	Code       string
+	Ip         string
+	CreateTime time.Time
+	ExpireAt   time.Time
+}

+ 74 - 0
repository/http/loallout_server.go

@@ -0,0 +1,74 @@
+package http
+
+import (
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"net/http"
+	"net/url"
+	"strings"
+	"video_course/errors"
+	"video_course/global"
+)
+
+type LoalloutServer struct {
+}
+
+func (l LoalloutServer) CheckPhone(phone string) (userMd5 string) {
+	api := strings.Join([]string{global.Project.UserServer.Host, "v1/Auth/CheckPhone"}, "/")
+
+	resp, err := http.PostForm(api, url.Values{
+		"phone": {phone},
+	})
+	if err != nil {
+		panic(err)
+	}
+
+	defer resp.Body.Close()
+	body, err := ioutil.ReadAll(resp.Body)
+	if err != nil {
+		err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
+		panic(err)
+	}
+
+	var responseBase struct {
+		Code int
+		Memo string
+	}
+	err = json.Unmarshal(body, &responseBase)
+	if err != nil {
+		err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
+		panic(err)
+	}
+	const (
+		success     = 0
+		notRegister = 1012
+		ban         = 1007
+	)
+	switch responseBase.Code {
+	case success:
+		{
+			var responseAll struct {
+				Code int
+				Memo string
+				Md5  string
+			}
+			err = json.Unmarshal(body, &responseAll)
+			if err != nil {
+				err = fmt.Errorf("response:[%s], err:\n%w", string(body), err)
+				panic(err)
+			}
+			userMd5 = responseAll.Md5
+		}
+
+	case notRegister:
+		panic(errors.ErrUserNotExists)
+	case ban:
+		panic(errors.ErrUserBan)
+
+	default:
+		panic(errors.ErrHrSensorsTimeOut)
+	}
+
+	return
+}

+ 11 - 0
repository/postgres/vf_code.go

@@ -0,0 +1,11 @@
+/**
+ * @ File:
+ * @ Date: 2021/2/2 12:54
+ * @ Author: JYQ
+ * @ Description:
+ */
+package postgres
+
+type VFCode struct {
+	base
+}

+ 154 - 0
service/auth.go

@@ -0,0 +1,154 @@
+package service
+
+import (
+	//"fmt"
+	//"server/dao/gorm"
+	//"server/dao/short_msg"
+	//"server/dao/wx"
+	//"server/errors"
+	//"server/function"
+	//"server/model"
+	//"server/utils"
+	//"strconv"
+	//"time"
+
+	"github.com/mojocn/base64Captcha"
+)
+
+var store = base64Captcha.DefaultMemStore
+
+type Auth struct {
+	base
+}
+
+//
+//func (a Auth) SignOut(session *model.Session) (logInfo string) {
+//	user := a.getUser(session)
+//	function.Session{}.Delete(session)
+//	logInfo = fmt.Sprintf("用户[%s]退出", user.GetShowName())
+//	return
+//}
+//func (a Auth) SignIn(
+//	verifyName string,
+//	password string,
+//	channel model.LoginChannel,
+//	picId string,
+//	picCode string,
+//	ip string) (sess *model.Session, logInfo string) {
+//	var user *model.User
+//
+//	defer func() {
+//		if p := recover(); p == nil {
+//			logInfo = fmt.Sprintf("用户[%s]在%s登录成功", user.GetShowName(), channel)
+//		} else {
+//			panic(p)
+//		}
+//	}()
+//
+//	if !store.Verify(picId, picCode, true) {
+//		panic(errors.ErrPicVerifyCode)
+//	}
+//
+//	dao := gorm.UserDAO{}
+//	shopDao := gorm.ShopDAO{}
+//	user = dao.GetUserByVerifyName(verifyName)
+//	user.CheckStatus()
+//	shop, _ := shopDao.ShopDetailOne(user.ShopId)
+//	shop.CheckShopStatus()
+//
+//	if !dao.PasswordCorrect(user.Id, password) {
+//		panic(errors.ErrPassword)
+//	}
+//	sess = function.Auth{}.LoginUser(user, channel, "", ip)
+//
+//	return
+//}
+
+func (Auth) GenVerifyPic(config *base64Captcha.DriverString) (id string, pic string) {
+	var (
+		err error
+	)
+
+	driver := config.ConvertFonts()
+
+	c := base64Captcha.NewCaptcha(driver, store)
+	id, pic, err = c.Generate()
+	if err != nil {
+		panic(err)
+	}
+
+	return id, pic
+}
+
+// 手机号发送短信验证码
+func (a Auth) GenVFCode(codeTypeInt int, name string, ip string,
+	picId string,
+	picCode string) {
+	//if !store.Verify(picId, picCode, true) {
+	//	panic(errors.ErrPicVerifyCode)
+	//}
+	//_ = dao.User{}.GetUserByPhone(name)
+	//codeType := model.VFCodeCodeTypeFromInt(codeTypeInt)
+	//vfcodeDao := dao.VFCode{}
+	//nameLastSend := vfcodeDao.NameLastSendTime(codeType, name)
+	//ipLastSend := vfcodeDao.IpLastSendTime(ip)
+	//
+	//// 手机号发送间隔
+	//const NameSendInterval = time.Second * 60
+	//// ip 发送间隔
+	//const IpSendInterval = time.Second * 1
+	//
+	//if time.Now().Sub(nameLastSend) < NameSendInterval {
+	//	panic(errors.ErrSendTooFast)
+	//}
+	//if time.Now().Sub(ipLastSend) < IpSendInterval {
+	//	panic(errors.ErrSendTooFast)
+	//}
+	//
+	//code := strconv.Itoa(utils.RandomInt(6))
+	//
+	//smDAO := short_msg.NewShortMsg()
+	//smDAO.SendVFCode(name, code, codeType)
+	//
+	//vfcodeDao.Save(codeType, name, code, time.Now().Add(time.Minute*3), ip)
+
+}
+
+//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) PassEdit(sess *model.Session, oldpass string, newpass string) (
+//	logInfo string, err error) {
+//	user := a.getUser(sess)
+//	userDAO := gorm.UserDAO{}
+//	if !userDAO.PasswordCorrect(user.Id, oldpass) {
+//		err = errors.ErrNamePwdIncorrect
+//		return
+//	}
+//	err = userDAO.ShopManagerPassEdit(user, newpass)
+//	if err != nil {
+//		return
+//	}
+//	logInfo = fmt.Sprintf("用户[%s]修改密码", user.Name)
+//	return
+//}
+//
+//func (a Auth) CheckPhone(phone string) (
+//	md5 string) {
+//
+//	user := gorm.UserDAO{}.CheckPhone(phone)
+//	user.CheckStatus()
+//
+//	md5 = user.UserMd5
+//	return
+//}