提交 e6a802fe 编写于 作者: M mehaotian

fix(stat): build stat

上级 0efe1ac4
此差异已折叠。
import pagesTitle from 'uni-pages?{"type":"style"}';
import Vue from 'vue';
/**
* 获取系统信息
*/
......@@ -15,11 +12,35 @@ const APP_PVER_TIME = 300; // 应用在后台结束访问时间 单位s
const OPERATING_TIME = 10; // 数据上报时间 单位s
const DIFF_TIME = 60 * 1000 * 60 * 24;
let pagesData = pagesTitle.pages;
let statConfig = {
appid: ''
};
let titleJsons = {};
// #ifdef VUE3
statConfig.appid = process.env.UNI_APP_ID;
titleJsons = process.env.UNI_STAT_TITLE_JSON;
// #endif
// #ifndef VUE3
statConfig = require('uni-stat-config').default || require('uni-stat-config');
const pagesTitle = require('uni-pages?{"type":"style"}').default;
let pagesData = pagesTitle.pages;
for (let i in pagesData) {
titleJsons[i] = pagesData[i].navigationBarTitleText || '';
const style = pagesData[i];
const titleText =
// MP
style.navigationBarTitleText ||
// ali
style.defaultTitle||
// H5 || App
style.navigationBar?.titleText ||
'';
if (titleText) {
titleJsons[i] = titleText;
}
}
// #endif
const UUID_KEY = '__DC_STAT_UUID';
const UUID_VALUE = '__DC_UUID_VALUE';
......@@ -51,6 +72,10 @@ function getUuid() {
}
return uuid
}
/**
* 获取配置信息 如 appid
*/
const stat_config = statConfig;
const get_uuid = (statData) => {
// 有可能不存在 deviceId(一般不存在就是出bug了),就自己生成一个
......@@ -110,15 +135,14 @@ const get_platform_name = () => {
'mp-qq': 'qq',
'quickapp-native': 'qn',
'mp-kuaishou': 'ks',
'mp-xhs': 'xhs',
'mp-lark': 'lark',
'quickapp-webview': 'qw'
'quickapp-webview':'qw'
};
if (platformList[process.env.VUE_APP_PLATFORM] === 'ali') {
if (my && my.env) {
if(platformList[process.env.VUE_APP_PLATFORM] === 'ali'){
if(my&&my.env){
const clientName = my.env.clientName;
if (clientName === 'ap') return 'ali'
if (clientName === 'dingtalk') return 'dt'
if(clientName === 'ap') return 'ali'
if(clientName === 'dingtalk') return 'dt'
// TODO 缺少 ali 下的其他平台
}
}
......@@ -210,7 +234,8 @@ const get_page_route = (pageVm) => {
let page = pageVm.$page || (pageVm.$scope && pageVm.$scope.$page);
let lastPageRoute = uni.getStorageSync('_STAT_LAST_PAGE_ROUTE');
if (!page) return lastPageRoute || ''
return page.fullPath === '/' ? page.route : page.fullPath
// 如果找不到 fullPath 就取 route 的值
return page.fullPath === '/' ? page.route : (page.fullPath||page.route)
};
/**
......@@ -307,7 +332,6 @@ const get_page_name = (routepath) => {
return (titleJsons && titleJsons[routepath]) || ''
};
const Report_Data_Time = 'Report_Data_Time';
const Report_Status = 'Report_Status';
const is_report_data = () => {
......@@ -392,8 +416,8 @@ const requestData = (done) => {
});
};
let data = uni.getStorageSync('$$STAT__DBDATA') || {};
const dbSet = (name, value) => {
let data = uni.getStorageSync('$$STAT__DBDATA') || {};
if (!data) {
data = {};
}
......@@ -402,18 +426,20 @@ const dbSet = (name, value) => {
};
const dbGet = (name) => {
let data = uni.getStorageSync('$$STAT__DBDATA') || {};
if (!data) {
data = {};
}
if (!data[name]) {
let dbdata = uni.getStorageSync('$$STAT__DBDATA');
if (!dbdata) {
dbdata = {};
}
if (!dbdata[name]) {
return undefined
}
data[name] = dbdata[name];
}
return data[name]
};
const dbRemove = (name) => {
let data = uni.getStorageSync('$$STAT__DBDATA') || {};
if (data[name]) {
delete data[name];
uni.setStorageSync('$$STAT__DBDATA', data);
......@@ -469,12 +495,14 @@ const get_last_visit_time = () => {
// 页面停留时间记录key
const PAGE_RESIDENCE_TIME = '__page__residence__time';
let First_Page_Residence_Time = 0;
let Last_Page_Residence_Time = 0;
/**
* 设置页面停留时间
*/
const set_page_residence_time = () => {
let First_Page_Residence_Time = get_time();
First_Page_Residence_Time = get_time();
dbSet(PAGE_RESIDENCE_TIME, First_Page_Residence_Time);
return First_Page_Residence_Time
};
......@@ -483,8 +511,8 @@ const set_page_residence_time = () => {
* 获取页面停留时间
*/
const get_page_residence_time = () => {
let Last_Page_Residence_Time = get_time();
let First_Page_Residence_Time = dbGet(PAGE_RESIDENCE_TIME);
Last_Page_Residence_Time = get_time();
First_Page_Residence_Time = dbGet(PAGE_RESIDENCE_TIME);
return Last_Page_Residence_Time - First_Page_Residence_Time
};
......@@ -555,14 +583,12 @@ const get_residence_time = (type) => {
}
};
let statConfig = require('uni-stat-config').default || require('uni-stat-config');
// 统计数据默认值
let statData = {
uuid: get_uuid(), // 设备标识
ut: get_platform_name(), // 平台类型
mpn: get_pack_name(), // 原生平台包名、小程序 appid
ak: statConfig.appid, // uni-app 应用 Appid
ak: stat_config.appid, // uni-app 应用 Appid
usv: STAT_VERSION, // 统计 sdk 版本
v: get_version(), // 应用版本,仅app
ch: get_channel(), // 渠道信息
......@@ -710,12 +736,15 @@ class Report {
* @param {Object} type
*/
applicationHide(self, type) {
if(!self){
// 表示应用切换到后台 ,此时需要从页面栈获取页面实例
self = get_page_vm();
}
// 进入应用后台保存状态,方便进入前台后判断是否上报应用数据
this.__licationHide = true;
get_last_time();
const time = get_residence_time();
const route = get_page_route(self);
// this._lastPageRoute = route
uni.setStorageSync('_STAT_LAST_PAGE_ROUTE', route);
this.sendHideRequest({
urlref: route,
......@@ -922,7 +951,7 @@ class Report {
* 获取位置信息
*/
getLocation() {
if (statConfig.getLocation) {
if (stat_config.getLocation) {
uni.getLocation({
type: 'wgs84',
geocode: true,
......@@ -983,11 +1012,6 @@ class Report {
// 重置队列
dbRemove('__UNI__STAT__DATA');
if (data.ut === 'h5') {
this.imageRequest(optionsData);
return
}
// XXX 安卓需要延迟上报 ,否则会有未知错误,需要验证处理
if (get_platform_name() === 'n' && this.statData.p === 'a') {
setTimeout(() => {
......@@ -998,6 +1022,7 @@ class Report {
this.sendRequest(optionsData);
}
getIsReportData(){
return is_report_data()
}
......@@ -1007,25 +1032,19 @@ class Report {
* @param {Object} optionsData 需要上报的数据
*/
sendRequest(optionsData) {
this.getIsReportData().then(() => {
uni.request({
url: STAT_URL,
method: 'POST',
// header: {
// 'content-type': 'application/json' // 默认值
// },
data: optionsData,
success: () => {},
fail: (e) => {
if (++this._retry < 3) {
setTimeout(() => {
this.sendRequest(optionsData);
}, 1000);
{
if (!uniCloud.config) {
console.error('当前尚未绑定服务空间.');
return
}
},
});
uniCloud.callFunction({
name: 'uni-stat-report',
data: optionsData,
success: (res) => {},
fail: (err) => {}
});
}
}
/**
* h5 请求
......@@ -1055,18 +1074,15 @@ class Report {
}
}
let vue = (Vue.default || Vue);
class Stat extends Report {
static getInstance() {
if (!vue.instance) {
vue.instance = new Stat();
if (!uni.__stat_instance) {
uni.__stat_instance = new Stat();
}
return vue.instance
return uni.__stat_instance
}
constructor() {
super();
this.instance = null;
}
/**
......@@ -1076,7 +1092,7 @@ class Stat extends Report {
*/
launch(options, self) {
// 初始化页面停留时间 start
let residence_time = set_page_residence_time();
set_page_residence_time();
this.__licationShow = true;
this.sendReportRequest(options, true);
}
......@@ -1098,20 +1114,44 @@ class Stat extends Report {
if (get_page_types(self) === 'page') {
this.pageShow(self);
}
// #ifdef VUE3
if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') {
if (get_page_types(self) === 'app') {
this.appShow(self);
this.appShow();
}
}
// #endif
// #ifndef VUE3
if (get_page_types(self) === 'app') {
this.appShow();
}
// #endif
}
hide(self) {
this.self = self;
if (get_page_types(self) === 'page') {
this.pageHide(self);
}
// #ifdef VUE3
if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') {
if (get_page_types(self) === 'app') {
this.appHide(self);
this.appHide();
}
}
// #endif
// #ifndef VUE3
if (get_page_types(self) === 'app') {
this.appHide();
}
// #endif
}
error(em) {
// 开发工具内不上报错误
......@@ -1144,8 +1184,9 @@ class Stat extends Report {
this.request(options);
}
}
var Stat$1 = Stat;
const stat = Stat.getInstance();
const stat = Stat$1.getInstance();
// 用于判断是隐藏页面还是卸载页面
let isHide = false;
......@@ -1188,15 +1229,37 @@ const lifecycle = {
function main() {
{
console.log('uni统计开启,version:2');
}
if (process.env.NODE_ENV === 'development') {
uni.report = function(type, options) {};
} else {
console.log('统计已开启');
// #ifdef VUE3
uni.onCreateVueApp((app) => {
app.mixin(lifecycle);
uni.report = function(type, options) {
stat.sendEvent(type, options);
};
});
if (get_platform_name() !== 'h5' && get_platform_name() !== 'n') {
uni.onAppHide(() => {
stat.appHide(get_page_vm());
});
uni.onAppShow(() => {
stat.appShow(get_page_vm());
});
}
// #endif
// #ifndef VUE3
const Vue = require('vue');
(Vue.default || Vue).mixin(lifecycle);
uni.report = function(type, options) {
stat.sendEvent(type, options);
};
// #endif
}
}
......
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册