Browse Source

fix(联系人绑定):新增缓存机制,避免手机号多次提交

Roc 3 years ago
parent
commit
2d37044561
2 changed files with 30 additions and 5 deletions
  1. 10 1
      services/user.go
  2. 20 4
      utils/config.go

+ 10 - 1
services/user.go

@@ -196,6 +196,13 @@ func BindWxUser(openid, mobile, email string, registerPlatform int) (wxUser *mod
 	var userId int
 	//如果查询出来的用户是nil,那么需要新增用户
 	if wxUser == nil {
+		key := "bind_wx_user:mobile:" + mobile + ":email:" + email
+		isHas := utils.Rc.IsExist(key)
+		if isHas {
+			err = errors.New("多次提交,请关闭页面重新进入")
+			return
+		}
+		utils.Rc.SetNX(key, "ok", time.Second*300)
 		user := &models.WxUser{
 			CompanyId:        1,
 			CreatedTime:      time.Now(),
@@ -209,7 +216,9 @@ func BindWxUser(openid, mobile, email string, registerPlatform int) (wxUser *mod
 			Source:           source,
 		}
 		tmpUserId, addUserErr := models.AddWxUser(user)
-		if err != nil {
+		//添加完成,清除缓存
+		_ = utils.Rc.Delete(key)
+		if addUserErr != nil {
 			err = addUserErr
 			return
 		}

+ 20 - 4
utils/config.go

@@ -1,7 +1,9 @@
 package utils
 
 import (
+	"fmt"
 	"github.com/astaxie/beego"
+	"rdluck_tools/cache"
 )
 
 var (
@@ -9,6 +11,10 @@ var (
 	MYSQL_URL      string //数据库连接
 	MYSQL_URL_RDDP string //数据库连接
 	MYSQL_URL_EDB  string
+
+	REDIS_CACHE string       //缓存地址
+	Rc          *cache.Cache //redis缓存
+	Re          error        //redis错误
 )
 
 var (
@@ -22,7 +28,7 @@ var (
 	WxAppSecret         string
 	TemplateIdByProduct string //产品运行报告通知-模板ID
 	TemplateRedirectUrl string //模板消息跳转地址
-	WxPlatform int	//用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
+	WxPlatform          int    //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:1
 )
 
 //pc端微信配置信息
@@ -32,7 +38,7 @@ var (
 	PcWxAppSecret         string
 	PcTemplateIdByProduct string //产品运行报告通知-模板ID
 	PcTemplateRedirectUrl string //模板消息跳转地址
-	WxPcPlatform int		//用户来源,需要入库,用来保存该用户来自哪个平台,默认是:3
+	WxPcPlatform          int    //用户来源,需要入库,用来保存该用户来自哪个平台,默认是:3
 )
 
 func init() {
@@ -46,6 +52,16 @@ func init() {
 	MYSQL_URL_RDDP = config["mysql_url_rddp"]
 	MYSQL_URL_EDB = config["mysql_url_edb"]
 
+	REDIS_CACHE = config["beego_cache"]
+	if len(REDIS_CACHE) <= 0 {
+		panic("redis链接参数没有配置")
+	}
+	Rc, Re = cache.NewCache(REDIS_CACHE) //初始化缓存
+	if Re != nil {
+		fmt.Println(Re)
+		panic(Re)
+	}
+
 	if RunMode == "release" {
 		WxAppId = "wx4a844c734d8c8e56"
 		WxAppSecret = "26c586e7ccb3c575433f0f37797b3eeb"
@@ -56,7 +72,7 @@ func init() {
 		WxPcPlatform = 3
 
 		PcWxAppId = "wx615472d6874eeb7f"
-		PcWxAppSecret="97fe374fb0cc90ef58c4b49d431366f1"
+		PcWxAppSecret = "97fe374fb0cc90ef58c4b49d431366f1"
 		STATIC_DIR = "/home/static/imgs/"
 	} else {
 		WxAppId = "wx9b5d7291e581233a"
@@ -68,7 +84,7 @@ func init() {
 		WxPcPlatform = 3
 
 		PcWxAppId = "wx7c8084f6e5b1d85a"
-		PcWxAppSecret="9e4210cd5a363aa1f316b7c4b8898418"
+		PcWxAppSecret = "9e4210cd5a363aa1f316b7c4b8898418"
 
 		STATIC_DIR = "/home/static/imgs/"
 	}