Administrator 2 лет назад
Родитель
Сommit
cb9dabf8f1
7 измененных файлов с 95 добавлено и 52 удалено
  1. 6 3
      data/dao/AuthorityDao.go
  2. 35 18
      data/dao/DetailDao.go
  3. 12 1
      data/dao/Userdao.go
  4. 11 11
      main.go
  5. 0 2
      router/DetailRouter.go
  6. 28 16
      router/UserRouter.go
  7. 3 1
      util/Email.go

+ 6 - 3
data/dao/AuthorityDao.go

@@ -7,9 +7,12 @@ import (
 )
 
 func GetListAuthorityAll() []domain.BackAuthority {
-	var authority domain.BackAuthority
-	var arr []domain.BackAuthority
-	configs.Engine.Desc("id").FindAndCount(&arr, &authority)
+	var authority = domain.BackAuthority{}
+	var arr = make([]domain.BackAuthority, 0)
+	err := configs.Engine.Table("back_authority").Desc("id").Find(&arr, &authority)
+	if err != nil {
+		panic(err)
+	}
 	return arr
 }
 func AddAllListAuthorityAll(arr []*domain.BackAuthority) {

+ 35 - 18
data/dao/DetailDao.go

@@ -228,28 +228,45 @@ func SelectSkuInfoBySkuId(skuId int64) (vo.DetailInfoVo, error) {
 		return infoVo, err
 	}
 	//查询文章下的所有
-	intrIds := make([]int64, 0)
 
-	intrIds = append(intrIds, goods.IntroductionId)
-
-	for i := range areas {
-		if areas[i].DetailIntroductionId != 0 {
-			intrIds = append(intrIds, areas[i].DetailIntroductionId)
+	var intrs = make([]domain.GoodsIntroduction, 0)
+	//{
+	//	intrIds := make([]int64, 0)
+	//	intrIds = append(intrIds, goods.IntroductionId)
+	//
+	//	for i := range areas {
+	//		if areas[i].DetailIntroductionId != 0 {
+	//			intrIds = append(intrIds, areas[i].DetailIntroductionId)
+	//		}
+	//	}
+	//
+	//	var iis = ""
+	//	for i := range intrIds {
+	//		iis += fmt.Sprint(",", intrIds[i])
+	//	}
+	//	iis = iis[1:]
+	//
+	//	err = configs.Engine.Table("goods_introduction").
+	//		Where("FIND_IN_SET(id,?)", iis).Find(&intrs)
+	//	if err != nil {
+	//		return infoVo, err
+	//	}
+	//}
+	{
+		sign := false
+		if goods.IntroductionId != 0 {
+			err = configs.Engine.Table("goods_introduction").
+				Where("id = ?", goods.IntroductionId).Find(&intrs)
+			if err != nil {
+				sign = true
+			}
+		}
+		if sign || len(intrs) == 0 {
+			err = configs.Engine.Table("goods_introduction").
+				Where("goods_id = ?", goods.Id).Find(&intrs)
 		}
 	}
 
-	var iis = ""
-	for i := range intrIds {
-		iis += fmt.Sprint(",", intrIds[i])
-	}
-	iis = iis[1:]
-
-	var intrs = make([]domain.GoodsIntroduction, 0)
-	err = configs.Engine.Table("goods_introduction").
-		Where("FIND_IN_SET(id,?)", iis).Find(&intrs)
-	if err != nil {
-		return infoVo, err
-	}
 	var skus = make([]domain.GoodsSku, 0)
 	err = configs.Engine.Table("goods_sku").Where("goods_id = ?", goods.Id).Find(&skus)
 

+ 12 - 1
data/dao/Userdao.go

@@ -47,6 +47,8 @@ func SendSms(matched bool, username string) string {
 }
 
 func SaveNewUser(user domain.User) (domain.User, error) {
+	//密码摘要
+	user.Password = Md5_KKC(user.Password)
 	insert, err := configs.Engine.Table("user").Insert(user)
 	if err != nil {
 		return user, err
@@ -67,7 +69,7 @@ func GetUserByPhoneOrEmail(phone, email string) (bool, error) {
 	return user.Id > 0, err
 }
 
-// 判断手机号数量
+// ExistsPhone 判断手机号是否注册
 func ExistsPhone(phone string) (bool, error) {
 	count, err := configs.Engine.Table("user").Where("phone = ?", phone).Count()
 	if err != nil {
@@ -76,6 +78,15 @@ func ExistsPhone(phone string) (bool, error) {
 	return count > 0, nil
 }
 
+// ExistsEmail 判断邮箱是否注册
+func ExistsEmail(phone string) (bool, error) {
+	count, err := configs.Engine.Table("user").Where("email = ?", phone).Count()
+	if err != nil {
+		return false, err
+	}
+	return count > 0, nil
+}
+
 func saveUser(username, password, code, phone string) bool {
 	return true
 

+ 11 - 11
main.go

@@ -1,9 +1,13 @@
 package main
 
-import "demo/util"
+import (
+	"demo/configs"
+	"demo/router"
+	"github.com/gin-gonic/gin"
+)
 
 func init() {
-	//configs.ConfigInit()
+	configs.ConfigInit()
 }
 func main() {
 	runGin()
@@ -12,13 +16,9 @@ func main() {
 // 接口前缀
 
 func runGin() {
-	//Router := gin.Default()
-	//apiGroup := Router.Group(router.Prefix)
-	//router.InitRouter(apiGroup)
-	//router.InitAuthority(Router)
-	//Router.Run(":8182")
-	SendEmail("1842520361@qq.com", "测试")
-}
-func SendEmail(to, content string) {
-	util.SendMail("你好", content, to)
+	Router := gin.Default()
+	apiGroup := Router.Group(router.Prefix)
+	router.InitRouter(apiGroup)
+	router.InitAuthority(Router)
+	Router.Run(":8182")
 }

+ 0 - 2
router/DetailRouter.go

@@ -27,13 +27,11 @@ func GetDetailData(c *gin.Context) {
 		c.JSON(200, CreateResultError(400, "param 参数错误"))
 		return
 	}
-
 	vo, err := dao.SelectGoodsDetailById(int64(id))
 	if err != nil || vo.DetailSkuVo.NowSkuId == 0 {
 		c.JSON(200, CreateResultError(400, "查询失败"))
 		return
 	}
-
 	c.JSON(200, CreateResultData(vo))
 }
 

+ 28 - 16
router/UserRouter.go

@@ -284,6 +284,8 @@ func login(c *gin.Context) {
 func registerCode(c *gin.Context) {
 	data := share.GetJsonAnyParam(c)
 	phone, _ := data("phone")
+
+	email, _ := data("email")
 	//判断是否为手机格式
 	if !checkPhone(c, cast.ToString(phone)) {
 		return
@@ -292,37 +294,52 @@ func registerCode(c *gin.Context) {
 	b, err := dao.ExistsPhone(cast.ToString(phone))
 	if err != nil {
 		fmt.Println(err)
-		c.JSON(http.StatusOK, CreateResultError(200, "数据库错误!!!"))
+		c.JSON(http.StatusOK, CreateResultError(200, "数据库错误!"))
 		return
 	}
 	if b {
 		c.JSON(http.StatusOK, CreateResultError(200, "手机号已注册!!!"))
 		return
 	}
+	//判断是否已经注册
+	b, err = dao.ExistsEmail(cast.ToString(email))
+	if err != nil {
+		fmt.Println(err)
+		c.JSON(http.StatusOK, CreateResultError(200, "数据库错误!"))
+		return
+	}
+	if b {
+		c.JSON(http.StatusOK, CreateResultError(200, "邮箱已注册!!!"))
+		return
+	}
+
 	//判断是否重复注册
 	get := configs.RedisDb.Exists(ctx, "PhoneVerificationCodeTime_"+cast.ToString(phone))
 	if get.Val() == 1 {
 		c.JSON(http.StatusOK, CreateResultError(200, "验证码已经发送,请等待"))
 		return
 	}
-
 	//发送短信
 	//sms := sendSms(b, cast.ToString(phone))
 	//TODO 模拟发送短信
 	//sms := testSendSms()
-	email, _ := data("email")
-	sms := testSendMail(cast.ToString(email))
+	sms := testSendSms(cast.ToString(email))
 	fmt.Println("发送成功!!!:验证码为:", sms)
+	//五分钟有效期
 	configs.RedisDb.Set(ctx, "PhoneVerificationCode_"+cast.ToString(phone), sms, 60*5*time.Second)
-	configs.RedisDb.Set(ctx, "PhoneVerificationCodeTime_"+cast.ToString(phone), time.Now(), 60*time.Second)
-	c.JSON(http.StatusOK, CreateResult())
+	//60秒有效期
+	configs.RedisDb.Set(ctx, "PhoneVerificationCodeTime_"+cast.ToString(phone), time.Now(), 5*time.Second)
 
+	c.JSON(http.StatusOK, CreateResult())
 }
 
-func testSendSms() string {
+func testSendSms(email string) string {
 	mins := big.NewInt(100000)
 	maxs := big.NewInt(999999)
 	randomNum := share.RandomInt(mins, maxs)
+	sms := randomNum.String()
+	go SendEmail(cast.ToString(email), fmt.Sprint("您的验证码为:", sms))
+
 	//if matched {
 	//	//bools, err = configs.Engine.Table("user").Where("username = ?", username).Exist(&user)
 	//
@@ -331,7 +348,8 @@ func testSendSms() string {
 	//} else {
 	//	share.SendSms(strconv.Itoa(int(randomNum.Int64())), username)
 	//}
-	return randomNum.String()
+
+	return sms
 }
 
 func testSendMail(email string) string {
@@ -349,14 +367,8 @@ func testSendMail(email string) string {
 	return randomNum.String()
 }
 
-func SendEmail(from, to, content string) {
-	mail := util.NewMail()
-	if mail == nil {
-		return
-	}
-	//字符串转 io.WriterTo
-	contentReader := strings.NewReader(content)
-	mail.Send(from, []string{to}, contentReader)
+func SendEmail(to, content string) {
+	util.SendMail("用户注册验证码", content, to)
 }
 
 func register(c *gin.Context) {

+ 3 - 1
util/Email.go

@@ -2,11 +2,13 @@ package util
 
 import (
 	"crypto/tls"
+	"fmt"
 	"gopkg.in/gomail.v2"
 )
 
 // btvdkkvrsdqhdiia
 // cgpkqmisohqreaic
+
 const (
 	host     = "smtp.qq.com"
 	port     = 465
@@ -46,6 +48,6 @@ func SendMail(title, message, to string) {
 	d.TLSConfig = &tls.Config{InsecureSkipVerify: true}
 
 	if err := d.DialAndSend(m); err != nil {
-		panic(err)
+		fmt.Println(err)
 	}
 }