Browse Source

wip:年终报告后端提交

wangkaiyue 3 years ago
parent
commit
c8256a20b9

+ 7 - 0
src/active_report.json

@@ -0,0 +1,7 @@
+{
+  "yearEndReportEnd": 1644854400,
+  "shareUrl": "https://web-wky.jydev.jianyu360.com/active/yearEndReport?from=share",
+  "replyTitle": "「我的剑鱼标讯2021」年度报告已经生成,请查收",
+  "replyHref": "https://web-wky.jydev.jianyu360.com/active/yearEndReport?from=reply",
+  "userSourceFlag": "21yearEndReport"
+}

+ 116 - 0
src/jfw/active/21yearEndReport.go

@@ -0,0 +1,116 @@
+package active
+
+import (
+	"fmt"
+	"github.com/go-xweb/httpsession"
+	"github.com/go-xweb/xweb"
+	"go.mongodb.org/mongo-driver/bson"
+	"go.mongodb.org/mongo-driver/bson/primitive"
+	"jfw/config"
+	"jfw/jyutil"
+	"jfw/public"
+	"net/url"
+	qutil "qfw/util"
+	"qfw/util/redis"
+	"time"
+)
+
+const (
+	activeDefaultEndTimeStamp = 1644854400 //2022-02-15
+)
+
+type YearEndReportConfig struct {
+	YearEndReportEnd int64  `json:"yearEndReportEnd"`
+	ShareUrl         string `json:"shareUrl"`
+	ReplyTitle       string `json:"replyTitle"`
+	ReplyHref        string `json:"replyHref"`
+	UserSourceFlag   string `json:"userSourceFlag"`
+}
+
+var (
+	yearEndReportEndTimeStamp int64 = activeDefaultEndTimeStamp
+	yearEndReportConfig       YearEndReportConfig
+)
+
+func init() {
+	xweb.AddAction(&YearEndReport{})
+	qutil.ReadConfig("./active_report.json", &yearEndReportConfig)
+	if yearEndReportConfig.YearEndReportEnd > 0 {
+		yearEndReportEndTimeStamp = yearEndReportConfig.YearEndReportEnd
+	}
+}
+
+//YearEndReport 21年,年终报告
+type YearEndReport struct {
+	*xweb.Action
+	yearEndReportPage xweb.Mapper `xweb:"/active/yearEndReport"` //21年终报告wx页面
+}
+
+func (this *YearEndReport) YearEndReportPage() error {
+	openid := qutil.ObjToString(this.Session().Get("s_m_openid"))
+	//没有用户session 或  有session但是没有关注(刚刚取关 session信息存留)
+	if time.Now().Unix() > yearEndReportEndTimeStamp {
+		return this.Redirect("/swordfish/about")
+	}
+	if openid == "" || (openid != "" && !checkUserIsSubscribe(openid)) {
+		stateKey := this.GetString("state")
+		if stateKey == "" { //公众号回调
+			return this.Redirect(fmt.Sprintf(config.Wxoauth, url.QueryEscape(this.Site()+this.Url()), "report"), 302)
+		}
+		//获取用户openid
+		openid = jyutil.Getopenid(this.GetString("code"))
+	}
+	//判断是否关注
+	isSubscribe := checkUserIsSubscribe(openid)
+	if isSubscribe { //关注跳转
+		if this.GetSession("userId") == nil {
+			loginCreateSess(openid, this.Session())
+		}
+	} else { //未关注,关注回复发送
+		redis.Put("other", fmt.Sprintf("SingleLogin_%s", openid), map[string]interface{}{
+			"href":       yearEndReportConfig.ReplyHref,
+			"title":      yearEndReportConfig.ReplyTitle,
+			"hrefsource": yearEndReportConfig.UserSourceFlag,
+		}, 60*30)
+	}
+	return this.Render("/active/yearEndReport/index.html")
+}
+
+func loginCreateSess(openid string, sess *httpsession.Session) bool {
+	_person, ok := public.MQFW.FindOne("user", bson.M{"s_m_openid": openid, "s_unionid": bson.M{"$ne": openid}, "i_ispush": 1})
+	if ok && *_person != nil && len(*_person) > 0 {
+		person := *_person
+		if person["i_shareknow"] != nil {
+			sess.Set("shareknow", person["i_shareknow"])
+		}
+		sess.Set("userId", (person["_id"].(primitive.ObjectID)).Hex())
+		sess.Set("s_m_openid", person["s_m_openid"])
+		sess.Set("openid", person["s_m_openid"])
+		sess.Set("s_nickname", person["s_nickname"])
+		if person["s_avatar"] == nil {
+			sess.Set("s_avatar", person["s_headimage"])
+		} else {
+			sess.Set("s_avatar", person["s_avatar"])
+		}
+		return true
+	} else {
+		return false
+	}
+}
+
+//checkUserIsSubscribe 检查用户是否关注
+func checkUserIsSubscribe(openid string) bool {
+	user, ok := public.MQFW.FindOneByField("user", map[string]interface{}{
+		"i_appid":    2,
+		"s_m_openid": openid,
+		"s_unionid":  map[string]interface{}{"$ne": openid},
+	}, `{"i_ispush":1}`)
+	if ok && user != nil {
+		if (*user)["_id"] == nil || qutil.IntAllDef((*user)["i_ispush"], 1) == 0 {
+			return false
+		} else {
+			return true
+		}
+	}
+	return false
+}

+ 3 - 0
src/jfw/front/singleLogin.go

@@ -28,6 +28,7 @@ func init() {
 	xweb.AddAction(&SingleLogin{})
 }
 
+//ShortUrl
 //短地址跳转
 func (this *SingleLogin) ShortUrl(key string) error {
 	var redirectUrl = "/swordfish/about"
@@ -37,6 +38,7 @@ func (this *SingleLogin) ShortUrl(key string) error {
 	return this.Redirect(redirectUrl)
 }
 
+//SingleLogin
 //toHref 已关注跳转页面
 //unHref 为关注跳转页面,若无此字段会跳至关注二维码页面
 //	*已有逻辑 1.线上课程推文,用户点击可登陆跳转;未关注扫码会回复课程链接
@@ -106,6 +108,7 @@ func (this *SingleLogin) SingleLogin() error {
 		for k, v := range hrefSourse {
 			if strings.Contains(this.GetString("toHref"), qutil.ObjToString(v)) {
 				hsource = k
+				break
 			}
 		}
 		redis.Put("other", fmt.Sprintf("SingleLogin_%s", openid), map[string]interface{}{

+ 46 - 0
src/jfw/modules/app/src/app/active/21yearEndReport.go

@@ -0,0 +1,46 @@
+package active
+
+import (
+	"github.com/go-xweb/xweb"
+	qutil "qfw/util"
+	"time"
+)
+
+const (
+	activeDefaultEndTimeStamp = 1644854400 //2022-02-15
+)
+
+type YearEndReportConfig struct {
+	YearEndReportEnd int64  `json:"yearEndReportEnd"`
+	ShareUrl         string `json:"shareUrl"`
+	ReplyTitle       string `json:"replyTitle"`
+	ReplyHref        string `json:"replyHref"`
+	UserSourceFlag   string `json:"userSourceFlag"`
+}
+
+var (
+	yearEndReportEndTimeStamp int64 = activeDefaultEndTimeStamp
+	yearEndReportConfig       YearEndReportConfig
+)
+
+func init() {
+	xweb.AddAction(&YearEndReport{})
+	qutil.ReadConfig("./active_report.json", &yearEndReportConfig)
+	if yearEndReportConfig.YearEndReportEnd > 0 {
+		yearEndReportEndTimeStamp = yearEndReportConfig.YearEndReportEnd
+	}
+}
+
+//YearEndReport 21年,年终报告
+type YearEndReport struct {
+	*xweb.Action
+	yearEndReportPage xweb.Mapper `xweb:"/jyapp/active/yearEndReport"` //21年终报告app页面
+}
+
+func (this *YearEndReport) YearEndReportPage() error {
+	userId := this.GetSession("userId")
+	if userId == "" || time.Now().Unix() > yearEndReportEndTimeStamp {
+		return this.Redirect("/jyapp/free/swordfish/about")
+	}
+	return this.Render("/active/yearEndReport/index.html")
+}

+ 10 - 0
src/jfw/modules/app/src/web/templates/active/yearEndReport/index.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>this is year report</title>
+</head>
+<body>
+app年报
+</body>
+</html>

+ 134 - 0
src/jfw/modules/publicapply/src/active/21yearEndReport.go

@@ -0,0 +1,134 @@
+package active
+
+import (
+	. "api"
+	"db"
+	"fmt"
+	"github.com/go-xweb/xweb"
+	"math"
+	qutil "qfw/util"
+	"strconv"
+	"strings"
+	"time"
+)
+
+type YearEndReport struct {
+	*xweb.Action
+	yearEndReportApi xweb.Mapper `xweb:"/active/yearEndReport"` //21年终报告接口
+}
+
+type YearEndReportConfig struct {
+	YearEndReportEnd int64  `json:"yearEndReportEnd"`
+	ShareUrl         string `json:"shareUrl"`
+	ReplyTitle       string `json:"replyTitle"`
+	ReplyHref        string `json:"replyHref"`
+	UserSourceFlag   string `json:"userSourceFlag"`
+}
+
+const (
+	year2022StartTimeStamp    = 1640966400 //2022-01-01
+	activeDefaultEndTimeStamp = 1644854400 //2022-02-15
+)
+
+var (
+	yearEndReportEndTimeStamp int64 = activeDefaultEndTimeStamp
+	yearEndReportConfig       YearEndReportConfig
+)
+
+func init() {
+	xweb.AddAction(&YearEndReport{})
+	qutil.ReadConfig("./active_report.json", &yearEndReportConfig)
+	if yearEndReportConfig.YearEndReportEnd > 0 {
+		yearEndReportEndTimeStamp = yearEndReportConfig.YearEndReportEnd
+	}
+}
+
+type YearEndReportData struct {
+	UserName         string   `json:"user_name"`         //昵称
+	UserHead         string   `json:"user_head"`         //头像地址
+	RegistrationTime int64    `json:"registration_time"` //注册时间 l_registedate
+	LoginDays        int32    `json:"login_days"`        //近一年登录天数 login_count
+	ActiveDays       int32    `json:"active_days"`       //深夜登录天数 login_night_count
+	ViewTotal        int32    `json:"view_total"`        //浏览信息次数 content_count
+	SearchTotal      int32    `json:"search_total"`      //搜索次数 search_count
+	FollowTotal      int32    `json:"follow_total"`      //企业/项目关注数量 ent_count
+	ActiveMonth      int32    `json:"active_month"`      //繁忙月份 month
+	IndustryTotal    int32    `json:"industry_total"`    //行业发布数据条数 bidding_count
+	ExpList          []string `json:"exp_list"`          //体验新功能列表 product_type
+	FocusProject     []string `json:"focus_project"`     //最关注的项目名称 vip_key
+	TotalDays        int32    `json:"total_days"`        //距离今天的天数 ~~
+	IsNew            bool     `json:"is_new"`            //是否新用户 ~~
+	Result           int32    `json:"result"`            //结果页type ~~
+	ShareLink        string   `json:"share_link"`        //报告二维码链接 ~~
+}
+
+func (this *YearEndReport) YearEndReportApi() {
+	userId, _ := this.GetSession("userId").(string)
+	rData, errMsg := func() (interface{}, error) {
+		if userId == "" {
+			return nil, fmt.Errorf("未登录")
+		}
+		if time.Now().Unix() > yearEndReportEndTimeStamp {
+			return nil, fmt.Errorf("不在活动期限内")
+		}
+		yerd := new(YearEndReportData)
+		if reportRes, _ := db.Mgo.FindOne("annual_report_new", map[string]interface{}{"userid": userId}); reportRes != nil && len(*reportRes) > 0 {
+			yerd.UserName, _ = (*reportRes)["s_name"].(string)
+			yerd.RegistrationTime, _ = (*reportRes)["l_registedate"].(int64)
+			yerd.LoginDays, _ = (*reportRes)["login_count"].(int32)
+			yerd.ActiveDays, _ = (*reportRes)["login_night_count"].(int32)
+			yerd.ViewTotal, _ = (*reportRes)["content_count"].(int32)
+			yerd.SearchTotal, _ = (*reportRes)["search_count"].(int32)
+			yerd.FollowTotal, _ = (*reportRes)["ent_count"].(int32)
+			yerd.ActiveMonth, _ = (*reportRes)["month"].(int32)
+			yerd.IndustryTotal, _ = (*reportRes)["bidding_count"].(int32)
+
+			if projectStr, _ := (*reportRes)["vip_key"].(string); projectStr != "" {
+				yerd.FocusProject = strings.Split(projectStr, ",")
+			}
+			expListStr, _ := (*reportRes)["product_type"].(string)
+			if expListStr != "" {
+				yerd.ExpList = strings.Split(expListStr, ",")
+			}
+		}
+		if baseRes, _ := db.Mgo.FindById("user", userId, `{"s_nickname":1,"s_headimage":1,"s_headimageurl":1,"l_registedate":1}`); baseRes != nil && len(*baseRes) > 0 {
+			if head, _ := (*baseRes)["s_headimage"].(string); head != "" {
+				yerd.UserHead = head
+			} else if head, _ := (*baseRes)["s_headimageurl"].(string); head != "" {
+				yerd.UserHead = head
+			}
+			if yerd.UserName == "" {
+				yerd.UserName, _ = (*baseRes)["s_nickname"].(string)
+			}
+			if yerd.RegistrationTime == 0 {
+				yerd.RegistrationTime, _ = (*baseRes)["l_registedate"].(int64)
+			}
+		}
+		//距离今天的天数
+		if yerd.RegistrationTime > 0 {
+			yerd.TotalDays = int32(math.Ceil(float64(time.Now().Sub(time.Unix(yerd.RegistrationTime, 0)).Hours()) / 24.0))
+		}
+		//是否新用户
+		if yerd.RegistrationTime > year2022StartTimeStamp || yerd.LoginDays == 0 {
+			yerd.IsNew = true
+		}
+		//结果页type
+		if !yerd.IsNew {
+			yerd.Result = getRandom(userId, yerd.RegistrationTime)
+		}
+		//报告二维码链接,及微信端地址
+		yerd.ShareLink = yearEndReportConfig.ShareUrl
+		return yerd, nil
+	}()
+	this.ServeJson(NewResult(rData, errMsg))
+}
+
+//getRandom 根据用户id和注册时间生成1-9随机数
+func getRandom(userId string, t int64) int32 {
+	defer qutil.Catch()
+	i, e := strconv.ParseInt(userId[len(userId)-5:len(userId)-1], 16, 32)
+	if e != nil {
+		i = t
+	}
+	return int32(i)%9 + 1
+}

+ 7 - 0
src/jfw/modules/publicapply/src/active_report.json

@@ -0,0 +1,7 @@
+{
+  "yearEndReportEnd": 1644854400,
+  "shareUrl": "https://web-wky.jydev.jianyu360.com/active/yearEndReport?from=share",
+  "replyTitle": "「我的剑鱼标讯2021」年度报告已经生成,请查收",
+  "replyHref": "https://web-wky.jydev.jianyu360.com/active/yearEndReport?from=reply",
+  "userSourceFlag": "21yearEndReport"
+}

+ 1 - 0
src/jfw/modules/publicapply/src/main.go

@@ -2,6 +2,7 @@ package main
 
 import (
 	_ "a"
+	_ "active"
 	_ "adLeague"
 	_ "applocation"
 	_ "attachmentdow"

+ 10 - 0
src/web/templates/active/yearEndReport/index.html

@@ -0,0 +1,10 @@
+<!DOCTYPE html>
+<html lang="en">
+<head>
+    <meta charset="UTF-8">
+    <title>this is year report</title>
+</head>
+<body>
+年报
+</body>
+</html>