UserLog.go 3.33 KB
package Api

import (
	"encoding/json"
	"github.com/aarongao/tools"
	"github.com/gin-gonic/gin"
	"gopkg.in/mgo.v2/bson"
	"letu/DB"
	"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.Insert(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"),
		},
	})

	DB.CDevice.Upsert(
		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"),
		}},
	)

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

}