123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505 |
- package front
- import (
- "database/sql"
- "fmt"
- "jfw/config"
- "jfw/jyutil"
- "jfw/public"
- "jfw/wx"
- "log"
- "math/rand"
- . "mongodb"
- "net/url"
- "qfw/util"
- "qfw/util/elastic"
- "qfw/util/jy"
- "qfw/util/redis"
- "regexp"
- "strconv"
- "strings"
- "time"
- "github.com/SKatiyar/qr"
- "github.com/go-xweb/xweb"
- "go.mongodb.org/mongo-driver/bson"
- )
- type Short struct {
- *xweb.Action
- article xweb.Mapper `xweb:"/article/(\\w+)/(.*).html"` //([pm])
- qr xweb.Mapper `xweb:"/biddetail/(\\w+)/qr/(.+)"`
- replication xweb.Mapper `xweb:"/front/shorturl/replication"` //用户复制操作记录入库
- }
- var mobileReg = regexp.MustCompile("(?i)(Android|Mobile|Phone)")
- var DateFullLayout = "2006-01-02 15:04:05"
- var Map_stype = map[string]bool{
- "content": true,
- "bdprivate": true,
- "mailprivate": true,
- "bdcontent": true,
- "indexcontent": true,
- }
- func (s *Short) Article(stype, id string) error {
- //先判断是否有session
- source := s.GetString("source")
- disWord := s.GetString("disWord")
- aboutUrl := "/swordfish/about"
- //是否已经检查过,默认是检查过-true,分享过来的是未检查过-false
- // checkIsSubscribeFlag := true
- // if source == "app_infocontentshare" || source == "wx_infocontentshare" {
- // checkIsSubscribeFlag = false
- // }
- from_userid := ""
- if source != "" {
- aboutUrl += "?source=" + source
- if strings.Contains(source, "wx_infocontentshare") {
- decodeArr := util.DecodeArticleId2ByCheck(id)
- if len(decodeArr) > 1 {
- from_userid = se.Encode2Hex(GetUserId(decodeArr[1])) //加密用户userid
- }
- } else if strings.Contains(source, "app_infocontentshare") {
- if from := s.GetString("from"); from != "" {
- from_userid = from
- }
- }
- aboutUrl += "&from=" + from_userid
- }
- bm := false
- userId, _ := s.GetSession("userId").(string)
- if !Map_stype[stype] {
- s.Redirect("/not/nottype", 302)
- return nil
- } else if stype == "content" || stype == "bdcontent" {
- if s.Session().Get("userId") == nil {
- if s.GetString("state") == "wx" {
- //微信跳回来的
- code := s.GetString("code")
- if code != "" {
- openid := jyutil.Getopenid(code)
- if openid != "" {
- isSubscribe := CheckUserIsSubscribe(openid)
- if isSubscribe {
- FindUserAndCreateSess(openid, s.Session(), "wx", false)
- }
- }
- }
- } else {
- if public.CheckWxBrowser(s.Request) {
- //所有参数都不再使用,跳到微信验证用户
- return s.Redirect(fmt.Sprintf(config.Wxoauth, url.QueryEscape(s.Site()+s.Url()), "wx"), 302)
- }
- }
- }
- if disWord != "" {
- redisDis := redis.GetStr("other", "DIS_"+disWord[1:])
- if redisDis != "" {
- suffix := disWord[len(disWord)-3 : len(disWord)]
- //公告三级页处理
- if suffix == suffix_msgt {
- effectiveTimeStr := strings.Split(redisDis, "##")[3]
- effectiveTime, _ := strconv.ParseInt(effectiveTimeStr, 10, 64)
- //是否计算佣金
- if time.Now().Unix() <= effectiveTime {
- belongUserId := strings.Split(redisDis, "##")[1]
- //if (belongUserId != userId) {
- if public.Mysql.ExecTx("口号使用", func(tx *sql.Tx) bool {
- //口号是否使用过
- wordInfo := public.Mysql.Find("dis_word", map[string]interface{}{"userId": userId, "password": disWord}, "id", "", 0, 0)
- if len((*wordInfo)) == 0 {
- //新增口号使用
- wordInfo = public.Mysql.Find("dis_word", map[string]interface{}{"userId": userId}, "id,start_time,stop_time", "stop_time", 0, 1)
- start_time := time.Now().Format(DateFullLayout)
- termValidityInt, _ := strconv.Atoi(fmt.Sprint(config.Sysconfig["termValidity"]))
- stop_time := TimeProcessing(time.Now().Format(DateFullLayout), termValidityInt).Format(DateFullLayout)
- insert := map[string]interface{}{
- "password": disWord,
- "userId": userId,
- "belong_userid": belongUserId,
- "start_time": start_time,
- "stop_time": stop_time,
- }
- insert_1 := public.Mysql.InsertByTx(tx, "dis_word", insert) //口号使用表
- log.Println("插入口号使用表", insert_1)
- from_userid = se.Encode2Hex(belongUserId)
- }
- return true
- }) {
- }
- }
- //}
- }
- }
- }
- client := s.Header("User-Agent")
- bm = mobileReg.MatchString(client)
- if s.Session().Get("userId") == nil { //} && !regclient.MatchString(client) { //没有用户session //搜索引擎让过
- s.Session().Set("referer", s.Request.RequestURI)
- if bm { //是否是移动端访问
- s.Redirect(aboutUrl, 302)
- } else {
- if stype == "bdcontent" {
- var retMap = make(map[string]interface{})
- stype = "content"
- _id := util.CommonDecodeArticle(stype, id)[0]
- _, _, _, obj := pcVRT(_id, "", true)
- if obj != nil && len(obj) > 0 {
- retMap["_id"] = id
- retMap["title"], _ = obj["title"].(string)
- retMap["area"], _ = obj["area"].(string)
- retMap["subtype"], _ = obj["subtype"].(string)
- retMap["subscopeclass"], _ = obj["s_subscopeclass"].(string)
- retMap["publishtime"] = util.IntAll(obj["publishtime"])
- }
- s.T["shareid"] = config.Seoconfig["baiduSEM-p"].(string)
- s.T["logid"] = config.Seoconfig["baiduSEM-p"].(string)
- s.T["obj"] = retMap
- s.Render("/pc/biddetail_bd.html", &s.T)
- } else {
- s.Redirect("/notin/page", 302)
- }
- }
- return nil
- }
- if stype == "bdcontent" {
- stype = "content"
- }
- }
- ssOpenid := s.Session().Get("s_m_openid")
- var res *map[string]interface{}
- if userId == "" && ssOpenid != nil {
- res, _ = mongodb.FindOneByField("user", bson.M{"s_m_openid": ssOpenid, "s_unionid": bson.M{"$ne": ssOpenid}}, `{"i_vip_status":1,"i_member_status":1,"s_m_phone":1,"s_phone":1}`)
- userId = BsonIdToSId((*res)["_id"])
- } else {
- //判断用户是否是vip
- res, _ = public.MQFW.FindById("user", userId, `{"i_vip_status":1,"i_member_status":1,"s_m_phone":1,"s_phone":1}`)
- }
- isVip, isMember, isEntniche, privatedata := false, false, false, false
- isVip = util.IntAll((*res)["i_vip_status"]) == 1 || util.IntAll((*res)["i_vip_status"]) == 2
- isMember = util.IntAll((*res)["i_member_status"]) > 0
- if phone, _ := util.If(util.ObjToString((*res)["s_phone"]) != "", util.ObjToString((*res)["s_phone"]), util.ObjToString((*res)["s_m_phone"])).(string); phone != "" {
- isEntniche = public.Mysql.CountBySql(`SELECT count(1) from entniche_user a INNER JOIN entniche_info b on (a.phone=? and a.power=1 and a.ent_id=b.id and b.status>0)`, phone) > 0
- privatedata = public.Mysql.CountBySql(`select count(1) from privatedata where phone = ?`, phone) > 0
- }
- if !isEntniche && privatedata {
- isEntniche = true
- }
- s.T["isVip"] = isVip
- s.T["isMember"] = isMember
- s.T["isEntniche"] = isEntniche
- if bm {
- //判断有没有取关,取关的话,跳转到关于剑鱼标讯页面
- if ssOpenid != nil && ssOpenid != "" {
- isSubscribe := CheckUserIsSubscribe(ssOpenid.(string))
- if !isSubscribe {
- return s.Redirect(aboutUrl, 302)
- }
- //之前sessionkeep.go中没有放userId,造成关注有问题
- if s.GetSession("userId") == nil {
- FindUserAndCreateSess(ssOpenid.(string), s.Session(), "wx", false)
- }
- }
- surl := s.GetString("url")
- kds := s.GetString("keywords")
- industry := s.GetString("industry")
- var shareopenid, sid string
- sid_openid := util.CommonDecodeArticle(stype, id)
- if len(sid_openid) == 0 {
- s.Redirect(aboutUrl, 302)
- return nil
- } else if len(sid_openid) > 1 {
- sid = sid_openid[0]
- shareopenid = sid_openid[1]
- } else {
- sid = sid_openid[0]
- }
- s.T["keywords"] = kds
- if shareopenid != "" {
- s.T["shareopenid"] = shareopenid
- }
- myopenid, _ := s.Session().Get("s_m_openid").(string)
- if myopenid == "" {
- myopenid = shareopenid
- s.T["openid"] = myopenid //"-1"
- } else {
- s.T["openid"] = se.EncodeString(myopenid) //"-1"
- }
- mynickname, _ := s.GetSession("s_nickname").(string)
- myavatar, _ := s.GetSession("s_avatar").(string)
- s.T["nickname"] = mynickname
- s.T["avatar"] = myavatar
- s.T["signature"] = wx.SignJSSDK(s.Site() + s.Url())
- var obj map[string]interface{}
- obj = wxvisitD(sid, userId, myopenid, isVip || isMember || isEntniche)
- if len(obj) > 0 {
- //获取打赏文案
- s.T["rewardText"], s.T["advertText"] = getRewardText()
- //纠错随机回复
- recoveryRight, _ := config.Sysconfig["recoveryRight"].([]interface{})
- recVal := rand.New(rand.NewSource(time.Now().UnixNano())).Intn(len(recoveryRight))
- s.T["recVal"] = util.ObjToString(recoveryRight[recVal])
- s.T["advertImg"] = config.Sysconfig["advertImg"]
- s.T["advertName"] = config.Sysconfig["advertName"]
- s.T["advertUrl"] = config.Sysconfig["advertUrl"]
- obj["industry"] = industry
- if ssOpenid != nil {
- obj["ucbsId"] = util.EncodeArticleId2ByCheck("ucbs#" + ssOpenid.(string) + "#" + id)
- }
- if obj["winnertel"] == nil && isbid(obj["subtype"]) {
- obj["winner_enttel"] = getwinnertel(obj["winner"])
- }
- if !(isVip || isMember || isEntniche) { //非会员不展示电话字段
- if obj["winnertel"] != nil {
- obj["winnertel"] = "无权限"
- }
- if obj["winner_enttel"] != "" {
- obj["winner_enttel"] = "无权限"
- }
- }
- s.T["obj"] = obj
- if obj["entidlist"] != nil { //大会员中标企业跳转至画像
- if winner := util.ObjToString(obj["winner"]); winner != "" {
- //临时更改为企业名称查询企业id
- rData := elastic.Get("qyxy", "qyxy", fmt.Sprintf(`{"query":{"bool":{"should":[{"term":{"company_name":"%s"}},{"term":{"hname":"%s"}}],"minimum_should_match":1}},"_source":["name","_id","capital","company_phone"],"size":1}`, winner, winner))
- if rData != nil && len(*rData) == 1 {
- if entId := util.ObjToString((*rData)[0]["_id"]); entId != "" {
- obj["entId"] = util.EncodeArticleId2ByCheck(util.ObjToString((*rData)[0]["_id"]))
- }
- }
- //s_winner := util.ObjToString(obj["s_winner"])
- //idObjs, _ := obj["entidlist"].([]interface{})
- //winnerId := ""
- //if names := strings.Split(s_winner, ","); len(names) > 1 {
- // index := 0
- // for i := 0; i < len(names); i++ {
- // if names[i] == winner {
- // index = i
- // break
- // }
- // }
- // if len(idObjs) > index {
- // winnerId = util.ObjToString(idObjs[index])
- // }
- //} else { //中标企业仅一个
- // if len(idObjs) > 0 {
- // winnerId = util.ObjToString(idObjs[0])
- // }
- //}
- //if winnerId != "" {
- // obj["entId"] = util.EncodeArticleId2ByCheck(winnerId)
- //}
- }
- }
- if from_userid != "" && se.Decode4Hex(from_userid) != "" && from_userid != userId && util.ObjToString(obj["subtype"]) != "拟建" { //分享开打的
- article_id := util.CommonDecodeArticle(stype, id)[0]
- key := fmt.Sprintf("integral_article_%s_%s_%s", article_id, from_userid, userId)
- if redis.Incr("other", key) == 1 {
- redis.SetExpire("other", key, 60*60*24)
- err := jy.Publish(public.Mgo_Log, config.Sysconfig["nsq"].(string), jy.Jyweb_article_open, se.Decode4Hex(from_userid), jy.Jywx_node1)
- if err != nil {
- log.Println("nsq队列写入失败-->", jy.Jyweb_article_open, se.Decode4Hex(from_userid))
- }
- }
- }
- s.T["advertcode"] = s.GetString("advertcode")
- content, _ := s.Render4Cache("/weixin/wxinfocontent_rec.html", &s.T)
- return s.SetBody(content)
- }
- if surl != "" {
- return s.Redirect(surl)
- }
- } else {
- sids := util.CommonDecodeArticle(stype, id)
- if len(sids) == 0 {
- s.Redirect("/notin/page", 302)
- return nil
- }
- sid := sids[0]
- catchKey := fmt.Sprintf("jypcdetail_%s_%v_%v_%v", sid, isVip, isEntniche, isMember)
- //缓存读取
- res := redis.Get("other", catchKey)
- if res == nil || res == "" {
- industry := s.GetString("industry")
- var shareid = s.GetString("id")
- if len(shareid) == 0 {
- shareid = "10"
- }
- s.T["logid"] = config.Seoconfig["jysskzy"].(string)
- s.T["shareid"] = se.EncodeString(shareid)
- s.T["keywords"] = s.GetString("kds")
- s.DisableHttpCache()
- po, bo, wo, obj := pcVRT(sid, industry, isVip || isMember || isEntniche)
- if obj != nil && len(obj) > 0 {
- href, _ := obj["href"].(string)
- href = strings.Replace(href, "\n", "", -1)
- if href != "" && !strings.HasPrefix(href, "http") {
- href = "http://" + href
- }
- if len(po) > 0 {
- s.T["projectOther"] = po
- }
- if len(bo) > 0 {
- s.T["buyerOther"] = bo
- }
- if len(wo) > 0 {
- s.T["winnerOther"] = wo
- }
- obj["url"] = href
- obj["industry"] = industry
- if ssOpenid != nil {
- obj["ucbsId"] = util.EncodeArticleId2ByCheck("ucbs#" + ssOpenid.(string) + "#" + id)
- }
- if obj["winnertel"] == nil && isbid(obj["subtype"]) {
- obj["winner_enttel"] = getwinnertel(obj["winner"])
- }
- if !(isVip || isMember || isEntniche) { //非会员不展示电话字段
- if obj["winnertel"] != nil {
- obj["winnertel"] = "无权限"
- }
- if obj["winner_enttel"] != "" {
- obj["winner_enttel"] = "无权限"
- }
- }
- s.T["obj"] = obj
- if obj["projectname"] != nil {
- s.SetSession("projectname", obj["projectname"])
- }
- if obj["entidlist"] != nil { //大会员中标企业跳转至画像
- winner := util.ObjToString(obj["winner"])
- s_winner := util.ObjToString(obj["s_winner"])
- idObjs, _ := obj["entidlist"].([]interface{})
- winnerId := ""
- if names := strings.Split(s_winner, ","); len(names) > 1 {
- index := 0
- for i := 0; i < len(names); i++ {
- if names[i] == winner {
- index = i
- break
- }
- }
- if len(idObjs) > index {
- winnerId = util.ObjToString(idObjs[index])
- }
- } else { //中标企业仅一个
- if len(idObjs) > 0 {
- winnerId = util.ObjToString(idObjs[0])
- }
- }
- if winnerId != "" {
- obj["entId"] = util.EncodeArticleId2ByCheck(winnerId)
- }
- }
- content, _ := s.Render4Cache("/pc/biddetail_rec.html", &s.T)
- timeout := 60 * 20
- if stype == "indexcontent" {
- timeout = 60 * 10
- }
- redis.Put("other", catchKey, string(content), timeout)
- return s.SetBody(content)
- }
- } else {
- return s.SetBody([]byte(res.(string)))
- }
- }
- return nil
- }
- //检查用户是否关注
- func CheckUserIsSubscribe(openid string) bool {
- user, ok := mongodb.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 || util.IntAllDef((*user)["i_ispush"], 1) == 0 {
- return false
- } else {
- return true
- }
- }
- return false
- }
- func (s *Short) Qr(t, id string) error {
- w := s.ResponseWriter
- w.Header().Set("Cache-Control", "no-cache, no-store, must-revalidate")
- w.Header().Set("Pragma", "no-cache")
- w.Header().Set("Expires", "0")
- w.Header().Set("Content-Type", "image/png")
- kds := s.GetString("kds")
- industry := s.GetString("industry")
- param := "?scan_source=pc"
- if t == "force" {
- param += "&ispcforceshare=1"
- }
- if kds != "" {
- param += "&keywords=" + kds
- }
- if industry != "" {
- param += "&industry=" + industry
- }
- data := config.Sysconfig["webdomain"].(string) + "/article/content/" + id + ".html" + param
- r, _ := qr.Encode(data, qr.M)
- pngdat := r.PNG()
- _, err := w.Write(pngdat)
- return err
- }
- func (s *Short) Replication() {
- userId, OK := s.GetSession("userId").(string)
- if OK && userId != "" {
- mongodb.Save("copyaction", map[string]interface{}{
- "userid": userId,
- "createtime": time.Now().Unix(),
- "url": s.Request.Referer(),
- "client": s.Header("User-Agent"),
- })
- }
- }
- func isbid(typ interface{}) bool {
- if typ != nil {
- subtype := util.ObjToString(typ)
- if subtype == "中标" || subtype == "合同" || subtype == "成交" {
- return true
- }
- }
- return false
- }
- func getwinnertel(company interface{}) string {
- if company != nil {
- data, _ := public.Mgo_Ent.FindOne("winner_enterprise", map[string]interface{}{
- "company_name": util.ObjToString(company),
- })
- if (*data)["company_phone"] != nil {
- return util.ObjToString((*data)["company_phone"])
- }
- }
- return ""
- }
- func GetUserId(openid string) string {
- data, ok := public.MQFW.FindOne("user", map[string]interface{}{"s_m_openid": openid})
- if data != nil && len(*data) > 0 && ok {
- userid := BsonIdToSId((*data)["_id"])
- return userid
- }
- return ""
- }
|