Browse Source

易董接口文档

xingzai 2 years ago
parent
commit
5bf2af744e

+ 112 - 9
controllers/yidong.go

@@ -2,15 +2,21 @@ package controllers
 
 import (
 	"encoding/json"
+	"fmt"
 	"hongze/hongze_open_api/models/request/yidong"
 	cygxActivity "hongze/hongze_open_api/models/tables/cygx_activity"
 	"hongze/hongze_open_api/services/alarm_msg"
+	servicesYidong "hongze/hongze_open_api/services/yidong"
 	"hongze/hongze_open_api/utils"
+	"math"
+	"strconv"
+	"strings"
+	"time"
 )
 
 // 易董
 type YiDongController struct {
-	BaseAuth
+	BaseCommon
 }
 
 // YiDongController
@@ -18,25 +24,109 @@ type YiDongController struct {
 // @Description 易董提交报名用户审核接口
 // @Param	request	body yidong.ActivityExamineReq true "type json string"
 // @Success 200 创建成功
-// @router /activity/examine [post]
+// @router /activity/examine/signup [post]
 func (c *YiDongController) ActivityExamine() {
-
 	var req yidong.ActivityExamineReq
 	err := json.Unmarshal(c.Ctx.Input.RequestBody, &req)
 	if err != nil {
 		c.FailWithMessage("参数解析异常")
 		return
 	}
-	//appid权限校验
 	activityIdYiDong := req.ActivityId
+	timeInt := req.Time
+	list := req.List
+	appid := req.Appid
+	timeUnix := time.Now().Unix() //当前格林威治时间,int64类型
+	timestamp := int64(timeInt)
+	if math.Abs(float64(timeUnix-timestamp)) > 600 {
+		c.FailWithMessage("当前时间异常,请调整设备时间与北京时间一致:" + strconv.Itoa(timeInt))
+		return
+	}
+	//校验 APPID 与ip白名单
+	ip := c.Ctx.Input.IP()
+	err = servicesYidong.CheckAppidAndIp(appid, ip)
+	if err != nil {
+		c.FailWithMessage(err.Error())
+		return
+	}
+	//校验 签名
+	paramStr := fmt.Sprintf(`activity_id=%stime=%s`, activityIdYiDong, strconv.Itoa(timeInt))
+	signStr := servicesYidong.GetSign(paramStr)
+	fmt.Println("____")
+	fmt.Println(signStr)
+	ownSign := req.Sign
+	if ownSign != signStr {
+		c.FailWithMessage("签名错误!")
+		return
+	}
 	activityInfo, err := cygxActivity.GetAddActivityInfoById(activityIdYiDong)
 	if err != nil {
 		c.FailWithMessage("会议id异常:" + activityIdYiDong)
 		return
 	}
 	activityId := activityInfo.ActivityId
+	signupList, err := cygxActivity.GetActivitySignupListByActivity(activityId)
+	if err != nil {
+		c.FailWithMessage("会议id异常:" + activityIdYiDong)
+		return
+	}
+	mapSignUp := make(map[string]string)
+	for _, v := range signupList {
+		mapSignUp[v.Mobile] = v.Mobile
+	}
+	var examineMobile string
+	for _, v := range list {
+		var dateTxt = []byte(v.Mobile)
+		resultDe := utils.DesBase64Decrypt(dateTxt)
+		deMobile := string(resultDe)
+		if deMobile == "" {
+			go alarm_msg.SendAlarmMsg("加密手机号解密失败:"+v.Mobile, 3)
+			c.FailWithMessage("加密手机号解密失败:" + v.Mobile)
+			return
+		}
+		examineMobile += "'" + deMobile + "'" + ","
+	}
+	examineMobile = strings.TrimRight(examineMobile, ",")
+	userList, err := cygxActivity.GetUserListByMobile(examineMobile)
+	if err != nil {
+		c.FailWithMessage("操作失败,用户信息不存在")
+		return
+	}
+	mapUserinfo := make(map[string]*cygxActivity.CygxActivitySignup)
+	for _, v := range userList {
+		item := new(cygxActivity.CygxActivitySignup)
+		item.UserId = v.UserId
+		item.RealName = v.RealName
+		item.SellerName = v.SellerName
+		item.ActivityId = activityId
+		item.CreateTime = time.Now()
+		item.Mobile = v.Mobile
+		item.Email = v.Email
+		item.CompanyId = v.CompanyId
+		item.CompanyName = v.CompanyName
+		item.Source = 3
+		//优先绑定用户修改过的外呼手机号
+		if v.OutboundMobile != "" {
+			item.OutboundMobile = v.OutboundMobile
+			if v.OutboundCountryCode == "" {
+				item.CountryCode = "86"
+			} else {
+				item.CountryCode = v.OutboundCountryCode
+			}
+		} else {
+			item.OutboundMobile = v.Mobile
+			if v.CountryCode == "" {
+				item.CountryCode = "86"
+			} else {
+				item.CountryCode = v.CountryCode
+			}
+		}
+		item.SignupType = 1
+		mapUserinfo[v.Mobile] = item
+	}
 	var items []*cygxActivity.ExamineStatusReq
-	list := req.List
+	var itemsAdd []*cygxActivity.CygxActivitySignup
+
 	for _, v := range list {
 		item := new(cygxActivity.ExamineStatusReq)
 		//encryptMobile := string(utils.DesBase64Encrypt([]byte(v.Mobile)))
@@ -51,10 +141,17 @@ func (c *YiDongController) ActivityExamine() {
 			go alarm_msg.SendAlarmMsg("加密手机号解密失败:"+v.Mobile, 3)
 			return
 		}
-		item.ActivityId = activityId
-		item.Mobile = deMobile
-		item.ExamineStatus = v.ExamineStatus
-		items = append(items, item)
+		if mapSignUp[deMobile] != "" {
+			item.ActivityId = activityId
+			item.Mobile = deMobile
+			item.ExamineStatus = v.ExamineStatus
+			items = append(items, item)
+		} else {
+			if mapUserinfo[deMobile] != nil {
+				mapUserinfo[deMobile].YidongExamineStatus = v.ExamineStatus
+				itemsAdd = append(itemsAdd, mapUserinfo[deMobile])
+			}
+		}
 	}
 	err = cygxActivity.UpdateActivitySignupNumMulti(items)
 	if err != nil {
@@ -62,5 +159,11 @@ func (c *YiDongController) ActivityExamine() {
 		go alarm_msg.SendAlarmMsg("修改活动审核状态失败:"+err.Error()+"活动ID"+activityIdYiDong, 3)
 		return
 	}
+	err = cygxActivity.AddActivitySignupNumMulti(itemsAdd)
+	if err != nil {
+		c.FailWithMessage("修改活动审核状态失败:" + activityIdYiDong)
+		go alarm_msg.SendAlarmMsg("修改活动审核状态失败:"+err.Error()+"活动ID"+activityIdYiDong, 3)
+		return
+	}
 	c.OkWithMessage("操作成功")
 }

+ 5 - 3
models/db_init.go

@@ -7,6 +7,7 @@ import (
 	"hongze/hongze_open_api/models/tables/article"
 	"hongze/hongze_open_api/models/tables/company"
 	"hongze/hongze_open_api/models/tables/company_product"
+	"hongze/hongze_open_api/models/tables/cygx_activity"
 	"hongze/hongze_open_api/models/tables/open_api_user"
 	"hongze/hongze_open_api/models/tables/qs_event"
 	"hongze/hongze_open_api/models/tables/qs_event_user"
@@ -34,9 +35,10 @@ func init() {
 
 	//注册对象
 	orm.RegisterModel(
-		new(open_api_user.OpenApiUser),    //开放API用户表
-		new(admin.Admin),                  //系统用户表
-		new(article.CygxArticleCeluePush), //策略平台推送过来更新的文章
+		new(open_api_user.OpenApiUser),        //开放API用户表
+		new(admin.Admin),                      //系统用户表
+		new(article.CygxArticleCeluePush),     //策略平台推送过来更新的文章
+		new(cygx_activity.CygxActivitySignup), //易董报名审核
 	)
 	orm.Debug = true
 	orm.DebugLog = orm.NewLog(utils.Binlog)

+ 3 - 0
models/request/yidong/yidong.go

@@ -3,6 +3,9 @@ package yidong
 // ActivityExamineReq 用户报名审核结构体
 type ActivityExamineReq struct {
 	ActivityId string             `description:"文章ID" json:"activity_id"`
+	Time       int                `description:"时间戳" json:"time"`
+	Appid      string             `description:"开放平台给分配的appid" json:"appid"`
+	Sign       string             `description:"签名" json:"sign"`
 	List       []ExamineStatusReq `description:"审核结果" json:"list"`
 }
 

+ 76 - 4
models/tables/cygx_activity/cygx_activity.go

@@ -1,6 +1,7 @@
 package cygx_activity
 
 import (
+	"fmt"
 	"github.com/beego/beego/v2/client/orm"
 	"time"
 )
@@ -55,6 +56,24 @@ type CygxActivity struct {
 	AdminName            string    `description:"销售/管理员姓名"`
 }
 
+type CygxActivitySignup struct {
+	Id                  int       `orm:"column(id)" description:"自增Id"`
+	UserId              int       `description:"用户id,多个用,隔开"`
+	ActivityId          int       `description:"活动ID"`
+	CreateTime          time.Time `description:"创建时间"`
+	Mobile              string    `description:"手机号"`
+	Email               string    `description:"邮箱号"`
+	CompanyId           int       `description:"公司ID"`
+	CompanyName         string    `description:"公司名称"`
+	OutboundMobile      string    `description:"外呼手机号"`
+	CountryCode         string    `description:"手机国家区号"`
+	Source              int       `description:"来源,1小程序,2后台添加"`
+	SignupType          int       `description:"报名方式,1预约外呼,2自主拨入,3我要报名"`
+	RealName            string    `description:"用户实际名称"`
+	SellerName          string    `description:"所属销售"`
+	YidongExamineStatus int       `description:"易董活动报名审核状态0审核中,1:审核通过、2审核不通过"`
+}
+
 //通过纪要ID获取活动详情
 func GetAddActivityInfoById(activityIdYiDong string) (item *CygxActivity, err error) {
 	o := orm.NewOrm()
@@ -84,11 +103,64 @@ func UpdateActivitySignupNumMulti(items []*ExamineStatusReq) (err error) {
 	defer func() {
 		_ = p.Close() // 别忘记关闭 statement
 	}()
-	for _, v := range items {
-		_, err = p.Exec(v.ExamineStatus, v.Mobile, v.ActivityId)
-		if err != nil {
-			return
+	if len(items) > 0 {
+		for _, v := range items {
+			_, err = p.Exec(v.ExamineStatus, v.Mobile, v.ActivityId)
+			if err != nil {
+				return
+			}
+		}
+	}
+
+	return
+}
+
+// AddActivitySignupNumMulti 批量新增易董报名用户审核状态
+func AddActivitySignupNumMulti(itemsAdd []*CygxActivitySignup) (err error) {
+	o := orm.NewOrm()
+	//to, err := o.Begin()
+	//if err != nil {
+	//	fmt.Print(err)
+	//	return
+	//}
+	fmt.Print()
+	if len(itemsAdd) > 0 {
+		for _, v := range itemsAdd {
+			_, err = o.Insert(v)
+			//_, err = to.Insert(v)
+			if err != nil {
+				return
+			}
 		}
 	}
 	return
 }
+
+func GetActivitySignupListByActivity(activityId int) (items []*CygxActivitySignup, err error) {
+	o := orm.NewOrm()
+	sql := `SELECT * FROM cygx_activity_signup WHERE activity_id = ?`
+	_, err = o.Raw(sql, activityId).QueryRows(&items)
+	return
+}
+
+type UserAndCompanyName struct {
+	UserId              int `orm:"column(user_id);pk"`
+	Mobile              string
+	Email               string
+	CompanyId           int
+	CompanyName         string `description:"公司名称"`
+	CountryCode         string `description:"手机国家区号"`
+	OutboundMobile      string `description:"外呼手机号"`
+	OutboundCountryCode string `description:"外呼手机号区号"`
+	SellerName          string `description:"所属销售"`
+	RealName            string `description:"真实姓名"`
+}
+
+func GetUserListByMobile(mobile string) (items []*UserAndCompanyName, err error) {
+	o := orm.NewOrm()
+	sql := ` SELECT u.*, c.company_name FROM wx_user AS u
+	         INNER JOIN company AS c ON c.company_id = u.company_id 
+			WHERE u.mobile IN (` + mobile + `)`
+	_, err = o.Raw(sql).QueryRows(&items)
+	return
+}

+ 1 - 1
routers/commentsRouter.go

@@ -127,7 +127,7 @@ func init() {
     beego.GlobalControllerRouter["hongze/hongze_open_api/controllers:YiDongController"] = append(beego.GlobalControllerRouter["hongze/hongze_open_api/controllers:YiDongController"],
         beego.ControllerComments{
             Method: "ActivityExamine",
-            Router: `/activity/examine`,
+            Router: `/activity/examine/signup`,
             AllowHTTPMethods: []string{"post"},
             MethodParams: param.Make(),
             Filters: nil,

+ 48 - 0
services/yidong/yidong.go

@@ -0,0 +1,48 @@
+package yidong
+
+import (
+	"errors"
+	"fmt"
+	"hongze/hongze_open_api/models/tables/open_api_user"
+	"hongze/hongze_open_api/utils"
+	"strings"
+)
+
+// 弘则易董的回调签名秘钥
+const YIDONG_HONGZE_KEY = "YIDONG20220816QxbNHiYNnnhT"
+
+// GetSign 获取签名
+func GetSign(signStr string) string {
+	signStr += YIDONG_HONGZE_KEY
+	fmt.Print(signStr)
+	return strings.ToUpper(utils.MD5(signStr))
+}
+
+func CheckAppidAndIp(appid, ip string) (err error) {
+	if appid == "" {
+		err = errors.New("缺少appid参数")
+		return
+	}
+	openApiUserInfo, tmpErr := open_api_user.GetByAppid(appid)
+	if tmpErr != nil {
+		if tmpErr.Error() == utils.ErrNoRow() {
+			err = errors.New("appid异常,请联系管理员")
+		} else {
+			err = errors.New("系统异常,请联系管理员")
+		}
+		return
+	}
+
+	if openApiUserInfo == nil {
+		err = errors.New("系统异常,请联系管理员")
+		return
+	}
+	//如果有ip限制,那么就添加ip
+	if openApiUserInfo.Ip != "" {
+		if !strings.Contains(openApiUserInfo.Ip, ip) {
+			err = errors.New(fmt.Sprintf("无权限访问该接口,ip:%v,请联系管理员", ip))
+			return
+		}
+	}
+	return
+}