Skip to content

Commit

Permalink
前端analyze sdk的PV功能完成
Browse files Browse the repository at this point in the history
  • Loading branch information
刘珅 committed Apr 8, 2017
1 parent 4cc5d60 commit 39fc973
Show file tree
Hide file tree
Showing 13 changed files with 360 additions and 7 deletions.
5 changes: 3 additions & 2 deletions configuration.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@
"officialURL" : "https://www.shen100.com"
},
"api": {
"Prefix": "/api",
"URL": "http://127.0.0.1:8012"
"Prefix" : "/api",
"URL" : "/api",
"NodeURL" : "http://127.0.0.1:8012/api"
},
"docs": {
"github": "https://xxx.com/shen100"
Expand Down
1 change: 1 addition & 0 deletions go/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func initServer() {
type apiConfig struct {
Prefix string
URL string
NodeURL string
}

// APIConfig api相关配置
Expand Down
71 changes: 71 additions & 0 deletions go/controller/visit/visit.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
package visit

import (
"../../config"
"../../model"
"strconv"
"time"
"gopkg.in/kataras/iris.v6"
"github.com/jinzhu/gorm"
)

// Latest30Day 近30天,每天的PV
Expand All @@ -25,3 +29,70 @@ func Latest30Day(ctx *iris.Context) {
"data" : data,
})
}

// PV 增加一次页面访问
func PV(ctx *iris.Context) {
var err error
var msg = ""
var userVisit model.UserVisit
userVisit.ClientID = ctx.FormValue("clientId")
userVisit.OSName = ctx.FormValue("osName")
userVisit.OSVersion = ctx.FormValue("osVersion")
userVisit.Language = ctx.FormValue("language")
userVisit.Country = ctx.FormValue("country")
userVisit.DeviceModel = ctx.FormValue("deviceModel")
userVisit.DeviceWidth, err = strconv.Atoi(ctx.FormValue("deviceWidth"))
if err != nil {
msg = "无效的deviceWidth"
}
userVisit.DeviceHeight, err = strconv.Atoi(ctx.FormValue("deviceHeight"))
if err != nil {
msg = "无效的deviceHeight"
}
userVisit.IP = ctx.RemoteAddr()
userVisit.VisitTime = time.Now()
userVisit.Referrer = ctx.FormValue("referrer")
userVisit.URL = ctx.FormValue("url")
userVisit.BrowserName = ctx.FormValue("browserName")
userVisit.BrowserVersion = ctx.FormValue("browserVersion")

if userVisit.ClientID == "" {
msg = "clientId不能为空"
}
if msg != "" {
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.ERROR,
"msg" : msg,
"data" : iris.Map{},
})
return
}

db, connErr := gorm.Open(config.DBConfig.Dialect, config.DBConfig.URL)

if connErr != nil {
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.ERROR,
"msg" : "error",
"data" : iris.Map{},
})
return
}

defer db.Close()

if err := db.Create(&userVisit).Error; err != nil {
ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.ERROR,
"msg" : "error.",
"data" : iris.Map{},
})
return
}

ctx.JSON(iris.StatusOK, iris.Map{
"errNo" : model.ErrorCode.SUCCESS,
"msg" : "success",
"data" : iris.Map{},
})
}
4 changes: 2 additions & 2 deletions go/model/uservisit.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ type UserVisit struct {
UserID uint `json:"userID"`
VisitTime time.Time `json:"visitTime"`
IP string `json:"ip"`
DeviceWidth uint `json:"deviceWidth"`
DeviceHeight uint `json:"deviceHeight"`
DeviceWidth int `json:"deviceWidth"`
DeviceHeight int `json:"deviceHeight"`
BrowserName string `json:"browserName"`
BrowserVersion string `json:"browserVersion"`
DeviceModel string `json:"deviceModel"`
Expand Down
6 changes: 6 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ func main() {
app.Adapt(httprouter.New())

apiPrefix := config.APIConfig.Prefix

router := app.Party(apiPrefix)
{
router.Get("/visit", visit.PV)
}

adminRouter := app.Party(apiPrefix + "/admin", admin.Authentication)
{
adminRouter.Get("/categories", category.List)
Expand Down
5 changes: 3 additions & 2 deletions nodejs/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ var config = {
sitePath : configData.nodejs.page.sitePath,
jsPath : configData.nodejs.page.jsPath,
imagePath : configData.nodejs.page.imagePath,
cssPath : configData.nodejs.page.cssPath
cssPath : configData.nodejs.page.cssPath,
apiURL : configData.api.URL
},
software: {
name : configData.software.name,
Expand All @@ -35,7 +36,7 @@ var config = {
};

(function() {
var url = configData.api.URL + configData.api.Prefix;
var url = configData.api.NodeURL;
for (var key in config.api) {
if (config.api.hasOwnProperty(key)) {
config.api[key] = url + config.api[key];
Expand Down
1 change: 1 addition & 0 deletions nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"redux-devtools": "3.3.2",
"redux-thunk": "2.2.0",
"style-loader": "0.16.1",
"ua-parser-js": "0.7.12",
"webpack": "2.3.2",
"webpack-dev-middleware": "1.10.1",
"webpack-hot-middleware": "2.17.1"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function receiveUserAnalyze(json) {

export default function() {
return dispatch => {
var url = pageConfig.sitePath + '/admin/user/analyze';
var url = pageConfig.apiURL + '/admin/user/analyze';
return fetch(url)
.then(response => response.json())
.then(json => dispatch(receiveUserAnalyze(json.data)))
Expand Down
2 changes: 2 additions & 0 deletions nodejs/static/javascripts/admin/app.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'isomorphic-fetch';

import React from 'react';
import ReactDOM from 'react-dom';

Expand Down
2 changes: 2 additions & 0 deletions nodejs/static/javascripts/admin/containers/Index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import React, { Component } from 'react';
import { connect } from 'react-redux';
import { Row, Col } from 'antd';
import requestSystemIndex from '../actions/requestSystemIndex';
import analyze from '../../sdk/analyze';
import '../../../styles/admin/index.css';

class Index extends Component {
Expand All @@ -12,6 +13,7 @@ class Index extends Component {
componentDidMount() {
const { dispatch } = this.props;
dispatch(requestSystemIndex());
analyze.pv();
}
render() {
let { systemIndex } = this.props;
Expand Down
67 changes: 67 additions & 0 deletions nodejs/static/javascripts/sdk/analyze.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import UAParser from 'ua-parser-js';
import storage from '../storage';
import uuid from './uuid';

let ua;

function init() {
ua = new UAParser().getResult();

let lang = navigator.language
|| navigator.browserLanguage
|| navigator.systemLanguage
|| navigator.userLanguage
|| '';
let country = '';
let language = '';
let index = lang.indexOf('-');
if (index > 0) {
country = lang.substr(index + 1).toUpperCase();
language = lang.substr(0, index);
}
ua.country = country;
ua.language = language;
ua.device.screenWidth = window.screen.width;
ua.device.screenHeight = window.screen.height;
ua.device.model = ua.device.model || '';
}

init();

export default {
pv() {
const LOCAL_DATA = 'analyzeLS';
const LOCAL_DATA_VERSION = 'v1.0';

var localData = storage.getItem(LOCAL_DATA);
if (!localData || localData.version < LOCAL_DATA_VERSION) {
localData = {
clientId : uuid(),
version : LOCAL_DATA_VERSION
};
storage.setItem(LOCAL_DATA, localData);
}

let params = {
clientId : localData.clientId,
osName : ua.os.name,
osVersion : ua.os.version,
language : ua.language,
country : ua.country,
deviceModel : ua.device.model,
deviceWidth : ua.device.screenWidth,
deviceHeight : ua.device.screenHeight,
referrer : encodeURIComponent(document.referrer),
url : encodeURIComponent(location.href),
browserName : ua.browser.name,
browserVersion : ua.browser.version
};
let paramArr = [];
for (let key in params) {
paramArr.push(key + '=' + params[key]);
}
let url = pageConfig.apiURL + '/visit?' + paramArr.join('&');
fetch(url)
.then(response => response.json())
}
};
Loading

0 comments on commit 39fc973

Please sign in to comment.