UserLog.go 4.99 KB
package Api

import (
	"encoding/json"
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"go.mongodb.org/mongo-driver/bson"
	"go.mongodb.org/mongo-driver/mongo/options"
	"letu/DB"
	"math"
	"strconv"
	"time"
)

// @Title 增加访问日志
// @Description 增加用户行为日志
// @Accept  json
// @Produce  json
// @Param   Type     访问页面    string     true        "安装;卸载;访问页面;使用功能;缩放地图;进入景区"
// @Param   SubType     景区详情    string     true        "推荐;景区详情;登陆;商城;投诉建议;问券调查....(app中能点的都加上)"
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Param   UserId     5dfb03070a9ac17ac7a82054    string     true        "用户ID"
// @Param   UserName     Aaron    string     true        "用户名称"
// @Param   Location     {"Latitude": 119, "Longitude": 39}    string     true        "位置"
// @Param   Remarks     备注    string     true        "备注"
// @Param   Source     用户分享    string     true        "来源"
// @Param   DeviceId     abc123    string     true        "手机唯一识别码,不重复(存放于http.header中)"
// @Param   Mac     abc123    string     true        "网卡Mac地址(存放于http.header中)"
// @Param   SystemType     ios    string     true        "ios,android(存放于http.header中)"
// @Param   SystemVersion     13.01    string     true        "手机版本(存放于http.header中)"
// @Param   SystemModel     iphone8    string     true        "手机型号(存放于http.header中)"
// @Param   AppVersion     1.0    string     true        "app版本号(存放于http.header中)"
// @Param   DeviceToken     abc    string     true        "推送token(存放于http.header中)"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"result":"ok"}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /UserLog? [post]
func UserLog(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")

	if c.Request.Header.Get("DeviceId") == "" {
		c.JSON(200, tools.ResponseError{
			1,
			"DeviceId不正确",
		})
		return
	}

	var Location DB.SLocation
	json.Unmarshal([]byte(c.PostForm("Location")), &Location)

	DB.CUserLog.InsertOne(tools.GetContext(),DB.SUserLog{
		c.PostForm("Type"),
		c.PostForm("SubType"),
		c.PostForm("ScenicId"),
		c.PostForm("UserId"),
		c.PostForm("UserName"),
		time.Now().Unix(),
		Location,
		c.PostForm("Remarks"),
		c.Request.Host,
		c.PostForm("Source"),
		DB.SDevice{
			c.Request.Header.Get("DeviceId"),
			c.Request.Header.Get("Mac"),
			c.Request.Header.Get("UDID"),
			c.Request.Header.Get("SystemVersion"),
			c.Request.Header.Get("SystemModel"),
			c.Request.Header.Get("AppVersion"),
			c.Request.Header.Get("AppVersion"),
			c.Request.Header.Get("DeviceToken"),
			time.Now().Unix(),
		},
	})

	upsert := true
	DB.CDevice.FindOneAndUpdate(tools.GetContext(),
		bson.M{"DeviceId": c.Request.Header.Get("DeviceId")},
		bson.M{"$set": bson.M{
			"Mac":           c.Request.Header.Get("Mac"),
			"UDID":          c.Request.Header.Get("UDID"),
			"SystemType":    c.Request.Header.Get("SystemType"),
			"SystemVersion": c.Request.Header.Get("SystemVersion"),
			"SystemModel":   c.Request.Header.Get("SystemModel"),
			"AppVersion":    c.Request.Header.Get("AppVersion"),
			"DeviceToken":   c.Request.Header.Get("DeviceToken"),
		}}, &options.FindOneAndUpdateOptions{
			Upsert: &upsert,
		},
	)

	c.JSON(200, tools.ResponseSeccess{
		0,
		"ok",
	})

}


// @Title 查询所有用户行为
// @Description 查询所有用户行为
// @Accept  json
// @Produce  json
// @Param   ScenicId     5dfb03070a9ac17ac7a82054    string     true        "景区id"
// @Param   Page     1    int     true        "当前第几页"
// @Success 200 {object} tools.ResponseSeccess "{"errcode":0,"total":1,"currpage":1,"totalpages":1,"prepage":20,"result":}"
// @Failure 500 {object} tools.ResponseError "{"errcode":1,"errmsg":"错误原因"}"
// @Router /AllUserLog? [get]
func AllUserLog(c *gin.Context) {
	c.Header("Access-Control-Allow-Origin", c.Request.Header.Get("Origin"))
	c.Header("Access-Control-Allow-Credentials", "true")


	total,_ := DB.CUserLog.CountDocuments(tools.GetContext(), bson.M{"ScenicId":c.Query("ScenicId")})
	limit, _ := strconv.ParseInt(c.Query("Limit"),10,64)
	if limit == 0 {
		limit = 50
	}
	currPage, _ := strconv.ParseInt(c.Query("Page"),10,64)
	if currPage == 0 {
		currPage = 1
	}
	skip := (currPage - 1) * limit

	var aUserLog []DB.SUserLog
	cur, err := DB.CUserLog.Find(tools.GetContext(), bson.M{"ScenicId":c.Query("ScenicId")},  &options.FindOptions{Limit: &limit, Skip: &skip, Sort: bson.M{"_id": -1}})
	defer cur.Close(tools.GetContext())
	if err == nil {
		for cur.Next(tools.GetContext()) {
			var e DB.SUserLog
			cur.Decode(&e)
			aUserLog = append(aUserLog,e)
		}
	}

	c.JSON(200, tools.Page{
		0,
		total,
		currPage,
		int64(math.Ceil(float64(total) / float64(limit))),
		limit,
		aUserLog,
	})

}