Commit 38d0be18 by 胡悦

添加埋点,接口页面等

1 parent 78ad3e8d
import api from './request'
import ssoApi from './ssoApi'
const common = 'scrm.api.carlife.common.'
module.exports = {
ssoApi,
commonApi: {
/**上传图片
*/
uploadImg(data) {
return api.post({
method: `${common}uploadImg`,
...data,
noSignAttr: ['file']
})
},
/**视频上传
*/
uploadVideo(data) {
return api.post({
method: `${common}uploadVideo`,
...data,
noSignAttr: ['file']
})
},
// /**根据sceneId 查参数 */
// getSceneById(data) {
// return api.post('dj.api.smartcarlife.common.getSceneById', data)
// },
/** 首页、发现列表 搜索接口 */
search(data) {
return api.post({
method: 'scrm.api.carlife.weapp.homeSearch.search',
pageSize: 10,
...data
})
},
/** 根据sap编码获取商户详情 */
getBySapCode(data) {
return api.post({
method: `scrm.api.carlife.merchant.getBySapCode`,
...data,
}, true)
},
/**获取图片版本号 */
getImgVersion() {
return api.getMock()
},
}
}
\ No newline at end of file \ No newline at end of file
import api from './request'
const path = 'scrm.api.sso.weapp.'
module.exports = {
/**wx.login登录 */
jsCodeLogin(data) {
return api.post({
method: `${path}jscode.login`,
...data
}, true)
},
/**授权用户信息 */
authUserInfo(data) {
return api.post({
method: 'scrm.api.sso.weapp.auth.userinfo',
...data
},true)
},
/**授权手机号 */
authUserPhone(data) {
return api.post({
method: `${path}auth.phone`,
...data
}, true)
},
/**退出 */
logout() {
return api.post({
method: `scrm.api.sso.user.logout`
}, true)
},
//保存用户授权信息
saveuserProfile(data) {
return api.post({
method: `scrm.api.wechat.auth.userProfile.save`,
...data
}, true)
},
/**更新客户手机号 */
updataCustomerPhone(data) {
return api.post({
method: `scrm.api.sso.weapp.mark.phone`,
...data
})
},
}
//app.js //app.js
import router from './router/index.js' import router from './router/index.js'
import util from './utils/util.js' import util from './utils/util.js'
import './utils/Track'
import {
ssoApi
} from './api/api'
App({ App({
onLaunch: function () { onLaunch: function () {
/**挂载到wx对象 */ /**挂载到wx对象 */
wx.$util = util wx.$util = util
wx.$router = router wx.$router = router
const systemInfo = wx.getStorageSync('srcm_shopmall_system_info')
if (!systemInfo) wx.getSystemInfo({
success(res) {
util.setStorageSync('srcm_shopmall_system_info', res)
}
})
this.login()
},
login(returnResponse) {
let _this = this
return new Promise((resolve, reject) => {
wx.login({
success({
code
}) {
ssoApi.jsCodeLogin({
code,
appId: _this.globalData.appId
}).then(res => {
wx.$authStatus = res.code != 0
if (returnResponse) {
resolve(res)
} else {
if (res.code != -1) {
if (res.code == 0) {
util.setUserInfo(res.data)
util.setSessionId(res.data.sessionId)
} else {
util.showToast(res.message || '网络异常')
}
}
resolve(res)
}
}).catch(() => {
wx.$authStatus = true
reject()
})
}
})
})
}, },
globalData: { globalData: {
......
{ {
"pages": [ "pages": [
"pages/tab/index/index" "pages/tab/index/index",
"pages/application/application"
], ],
"window": { "window": {
"backgroundTextStyle": "light", "backgroundTextStyle": "light",
......
// components/Login/Login.js
import {
ssoApi
} from '../../api/api'
import {
componentsMixin
} from '../../mixins/index'
Component({
behaviors: [componentsMixin],
/**
* 组件的属性列表
*/
properties: {
show: Boolean,
content: {
type: String,
value: '您还没有登录,请您登录后查看!'
},
returnResponse: {
type: Boolean,
value: true
}
},
observers: {
'show': function (newVal, oldVal) {
let {
returnResponse
} = this.data
if (newVal) {
wx.$util.showLoading()
getApp().login(returnResponse).then(({
code,
data
}) => {
this.triggerEvent('loginInfo', {
code,
data
})
wx.hideLoading()
if (data) {
let {
sessionId
} = data
this.setData({
sessionId,
openSet: code != 0
})
this.sessionId = sessionId
if (data.isLogin == 'Y'&& code == '0') {
let thisPage = wx.$router.getPageInstance()
if (thisPage.loginCallBack) thisPage.loginCallBack(data)
if (thisPage.onRoute) thisPage.onRoute()
}
}
})
} else {
this.hide()
}
},
},
/**
* 组件的初始数据
*/
data: {
},
/**
* 组件的方法列表
*/
methods: {
hide() {
this.setData({
openSet: false
})
},
cancel(flag) {
this.hide()
if (flag) this.triggerEvent('cancel')
},
noScroll: wx.$util.noScroll(),
getPhoneNumber({
detail
}) {
if (detail.errMsg && detail.errMsg == 'getPhoneNumber:fail user deny') {
this.cancel(true)
return
}
delete detail.errMsg
if (detail.iv && detail.encryptedData) {
const {
sessionId
} = this.data
wx.$util.showLoading()
detail.sessionId = sessionId
console.log(detail);
ssoApi.authUserPhone(detail).then(({
data,
code
}) => {
wx.$Track.setTrack({}, 3)
wx.hideLoading()
let thisPage = wx.$router.getPageInstance()
if (code == '0') {
thisPage.setData({
showLogin: false,
userInfo: data
})
wx.$authStatus = false
this.setData({
sessionId: ''
})
wx.$util.setUserInfo(data)
wx.$util.setSessionId(data.sessionId)
thisPage.onLoad()
thisPage.onShow()
if (thisPage.init) thisPage.init()
if (thisPage.onRoute) thisPage.onRoute()
if (thisPage.loginCallBack) thisPage.loginCallBack(data)
let customerId = wx.$util.getStorageSync('srcm_shopmall_customer_id')
let merchantId = wx.$util.getStorageSync('srcm_shopmall_merchant_id')
if (customerId) {
ssoApi.updataCustomerPhone({
customerId,
merchantId
}).then(() => {
wx.removeStorageSync('srcm_shopmall_customer_id')
})
}
wx.$util.customerbehavior()
wx.$eventbus.emit('EQUITY')
}
})
}
},
}
})
\ No newline at end of file \ No newline at end of file
{
"component": true,
"usingComponents": {}
}
\ No newline at end of file \ No newline at end of file
<view class="open-seting-bg text-center" wx:if="{{openSet}}" catchtouchmove="noScroll">
<view class="bg" ></view>
<view class="open-set-inner">
<view class="set-title">
<text class="title">温馨提示</text>
<view class="content">{{content}}</view>
</view>
<view class="btn-openset">
<!-- <view bindtap="cancel" class="btn text-center cancel" >取消</view> -->
<navigator class="btn text-center cancel" target="miniProgram" open-type="exit">取消</navigator>
<view class="btn text-center">
登录
<button open-type="getPhoneNumber" catchgetphonenumber="getPhoneNumber" class="button-style"></button>
</view>
</view>
</view>
</view>
\ No newline at end of file \ No newline at end of file
/* components/Login/Login.wxss */
@import '../Authorize/Authorize.wxss';
.bg {
position: absolute;
left: 0;
top: 0;
z-index: -1;
width: 100%;
height: 100%;
}
.title {
margin-bottom: 60rpx;
font-size: 36rpx;
font-weight: 500;
}
.content {
font-size: 28rpx;
color: #A3A3A3;
}
.btn {
color: #F13C3D;
}
.cancel {
color: #999;
}
\ No newline at end of file \ No newline at end of file
// pages/application/application.js
Page({
/**
* 页面的初始数据
*/
data: {
},
/**
* 生命周期函数--监听页面加载
*/
onLoad(options) {
},
/**
* 生命周期函数--监听页面初次渲染完成
*/
onReady() {
},
/**
* 生命周期函数--监听页面显示
*/
onShow() {
},
/**
* 生命周期函数--监听页面隐藏
*/
onHide() {
},
/**
* 生命周期函数--监听页面卸载
*/
onUnload() {
},
/**
* 页面相关事件处理函数--监听用户下拉动作
*/
onPullDownRefresh() {
},
/**
* 页面上拉触底事件的处理函数
*/
onReachBottom() {
},
/**
* 用户点击右上角分享
*/
onShareAppMessage() {
}
})
\ No newline at end of file \ No newline at end of file
{
"usingComponents": {}
}
\ No newline at end of file \ No newline at end of file
<!--pages/application/application.wxml-->
<text>pages/application/application.wxml</text>
/* pages/application/application.wxss */
\ No newline at end of file \ No newline at end of file
...@@ -12,7 +12,9 @@ Page({ ...@@ -12,7 +12,9 @@ Page({
* 生命周期函数--监听页面加载 * 生命周期函数--监听页面加载
*/ */
onLoad(options) { onLoad(options) {
setTimeout(() => {
wx.$router.go('application')
}, 2e3)
}, },
/** /**
......
...@@ -42,11 +42,12 @@ ...@@ -42,11 +42,12 @@
"useStaticServer": true, "useStaticServer": true,
"showES6CompileOption": false, "showES6CompileOption": false,
"disableUseStrict": false, "disableUseStrict": false,
"useCompilerPlugins": false "useCompilerPlugins": false,
"minifyWXML": true
}, },
"compileType": "miniprogram", "compileType": "miniprogram",
"libVersion": "2.15.0", "libVersion": "2.15.0",
"appid": "wx0b962934a20ad371", "appid": "wxfe82b8d65d07808f",
"projectname": "%E5%B9%BF%E8%81%94%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%A8%A1%E7%89%88", "projectname": "%E5%B9%BF%E8%81%94%E5%B0%8F%E7%A8%8B%E5%BA%8F%E6%A8%A1%E7%89%88",
"simulatorType": "wechat", "simulatorType": "wechat",
"simulatorPluginLibVersion": {}, "simulatorPluginLibVersion": {},
......
/**
* 使用方法 wx.$Track.setTrack({
* 参数1:val,
* 参数2:val,
* 参数...
* }, act_id)
* act_id :{
* 1:点击,
* 2:页面离开,
* 3:登陆
* 4:退出登录
* 5:地理位置授权
* 6:页面预览
* 7:业务行为
*
* }
*/
import api from '../api/request'
let TRACK_TIME = null,
ADDRESS_INFO = null
const track_interval_time = 60 * 1e3
class TRACK {
constructor() {
console.log('---------------- 埋点初始化 ----------------')
this.initPageProxy()
this.sendTrack()
this.getAddressInfo().then(res => ADDRESS_INFO = res)
this.appHide()
}
/**监听页面 */
pageLeaveProxy(thisPage) {
let _this = this,
onHide = thisPage.onHide,
onUnload = thisPage.onUnload,
onLoad = thisPage.onLoad,
onShow = thisPage.onShow,
onTabItemTap = thisPage.onTabItemTap
thisPage.onTabItemTap = function (options) {
onTabItemTap && onTabItemTap.apply(this, arguments),
_this.setTrack({}, 1)
}
thisPage.onLoad = function () {
onLoad && onLoad.apply(this, arguments)
//, _this.setTrack({
// enterTime: new Date().format('yyyy-MM-dd hh:mm:ss')
// })
}
thisPage.onShow = function () {
onShow && onShow.apply(this, arguments), _this.getPageInstance().page_show_time = Date.now()
}
thisPage.onHide = function () {
let {
page_show_time
} = _this.getPageInstance()
onHide && onHide.apply(this, arguments), _this.setTrack({
duration: getDuration(page_show_time)
}, 2)
}
thisPage.onUnload = function () {
let {
page_show_time
} = _this.getPageInstance()
onUnload && onUnload.apply(this, arguments), _this.setTrack({
duration: getDuration(page_show_time)
}, 2)
}
const getDuration = (page_show_time) => (Date.now() - page_show_time) / 1e3
}
/**全局注册事件 */
initPageProxy() {
let thisPage = Page
let _this = this
Page = function (el) {
try {
el || (el = {}), _this.pageLeaveProxy(el), thisPage.apply(this, arguments)
} catch (el) {
thisPage.apply(this, arguments)
}
}
}
appShow() {
wx.onAppShow(() => {
if (TRACK_TIME) clearInterval(TRACK_TIME)
this.sendTrack()
wx.offAppShow()
this.appHide()
console.log('---------------- 重新打开埋点 ----------------')
})
}
appHide() {
wx.onAppHide(() => {
if (TRACK_TIME) clearInterval(TRACK_TIME)
wx.offAppHide()
this.appShow()
console.log('---------------- APP隐藏 埋点停止推送 ----------------')
})
}
/**设置埋点数据 */
setTrack(e, act_id = 1) {
// console.log('---------------- 获取小程序启动时的参数 ----------------', wx.getLaunchOptionsSync())
let {
route
} = this.getPageInstance(), {
query
} = wx.getLaunchOptionsSync(),
prepare_data = wx.getStorageSync('prepare_data'),
last_time = wx.getStorageSync('track_last_time'),
now = new Date().format('yyyy-MM-dd hh:mm:ss'),
merchantInfo = this.getMerchantInfo(),
userInfo = wx.$util.getUserInfo(),
systemInfo = this.getSystemInfo(),
page_title = this.getPageTitle(),
guest_no = this.getGuestNo(),
storageQuery = wx.getStorageSync('track_query')
if (!last_time) last_time = now
let data = {
burying_pointa: 'GH_MINIPROGRAM',
plateform: 'miniprogram',
create_time: now,
...merchantInfo,
add_way: '104',
page_id: route,
act_status: 0,
last_time,
guest_no,
act_id,
ext: {
...systemInfo,
page_title,
...e
}
}
wx.setStorageSync('track_last_time', now)
if (userInfo) {
/**用户信息 */
if (userInfo.openId) data.open_id = userInfo.openId
if (userInfo.unionId) data.union_id = userInfo.unionId
if (userInfo.phone) data.customer_phone = userInfo.phone
}
if (ADDRESS_INFO) {
data.city = ADDRESS_INFO.address
data.gps = ADDRESS_INFO.location
}
if (Object.keys(query).length) {
data.entry_id = query
wx.setStorageSync('storageQuery', query)
} else data.entry_id = storageQuery
console.log('---------------- 触发了埋点 ----------------', data)
if (!prepare_data) prepare_data = []
prepare_data.push(JSON.stringify(data))
wx.setStorageSync('prepare_data', prepare_data)
}
getGuestNo() {
let guest_no = wx.getStorageSync('guest_no')
if (!guest_no) {
guest_no = this.setGuestNo()
wx.setStorageSync('guest_no', guest_no)
}
return guest_no
}
/**定时发送埋点数据 */
sendTrack() {
TRACK_TIME = setInterval(() => {
let prepare_data = wx.getStorageSync('prepare_data')
let data = {
method: 'entmicro.api.customercollect.reportCustomerList',
jsonList: prepare_data
}
if (prepare_data && prepare_data.length) api.reportTrack(data).then(({
code,
data,
message
}) => {
if (code == 0 && data) {
wx.setStorageSync('prepare_data', [])
console.log('---------------- 埋点定时推送成功 ----------------', new Date().format('yyyy-MM-dd hh:mm:ss'), ':', prepare_data)
} else console.log('---------------- 埋点定时推送失败 ----------------', new Date().format('yyyy-MM-dd hh:mm:ss'), ':', message)
})
}, track_interval_time)
}
/**获取页面对象实例 */
getPageInstance = (size = 1) => {
const pages = getCurrentPages()
return pages[pages.length - size]
}
/**获取页面标题 */
getPageTitle() {
let {
route,
navigationBarTitleText
} = this.getPageInstance()
if ('\u672a\u53d6\u5230' === route || !route) return ''
let title = ''
if (__wxConfig) {
let {
window
} = __wxConfig.page[`${route}.html`]
title = window.navigationStyle == 'custom' ? navigationBarTitleText : window.navigationBarTitleText
}
if (!title) title = '广汇车生活'
return title
}
/**获取设备信息 */
getSystemInfo() {
let {
SDKVersion,
brand,
model,
system,
version
} = JSON.parse(wx.getStorageSync('srcm_shopmall_system_info'))
return {
SDK_version: SDKVersion,
brand,
model,
system,
version
}
}
/**获取门店信息 */
getMerchantInfo() {
let {
merchantId
} = wx.getStorageSync('merchant_info')
return {
merchant_id: merchantId
}
}
/**获取用户位置信息 */
getAddressInfo() {
let addressInfo = wx.getStorageSync('address_info')
return new Promise((resolve) => {
if (addressInfo && Date.now() - addressInfo.createTime < 2 * 60 * 1e3) resolve(addressInfo)
else wx.getLocation({
type: 'gcj02'
}).then(({
latitude,
longitude
}) => {
if (!addressInfo) this.setTrack({}, 5)
console.log('---------------- 更新地理位置 ----------------')
return
mapApi.detailedAdrress([latitude, longitude]).then(detail => {
let info = {
...detail,
createTime: Date.now()
}
resolve(info)
wx.setStorageSync('address_info', info)
})
})
})
}
/**获取游客ID */
setGuestNo(length = 30) {
let name = '',
n = [
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z',
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z'
]
for (let i = 0; i < Number(length); i++) {
name += n[Math.round(Math.random() * (n.length - 1))]
}
return `GH_${name}_${Date.now()}`
}
}
wx.$Track = new TRACK()
\ No newline at end of file \ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!