| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- const ReqBase = require('./ReqBase.js');
- const save_browse_api = '/api/behavior/saveBrowse'; //保存用户浏览行为数据
- const save_behavior_api = '/api/behavior/save'; //保存用户行为数据
- let reqBase;
- const req = require('../utils/request.js');
- const initAction = () => {
- if (!reqBase) {
- reqBase = new ReqBase({
- appId: req.getStorage('appId')
- });
- }
- getStartTime();
- };
- /**
- * 记录浏览行为
- * @param type 1"产品"、2 "活动"、3"文章"、4"社区"、5"海报"、6"发现"、7"名片"、8"素材内容"
- * @param bindId 操作对象ID、code(必填)
- * @param uid 当前用户ID(选填)
- * @param shareUid 分享用户ID(选填)
- * @param parentCode 父级分享生成的code(选填:别人的连接二次分享时获取)
- * @param typeName (选填:需要自定义type名称的时候传,比如通过文章做的风采,typeName传风采,type为3)
- */
- const saveBrowse = ({
- type = 3,
- bindId = '',
- uid = '',
- shareUid = '',
- parentCode = '',
- typeName = ''
- } = {}) => {
- if (!bindId) {
- console.log('保存浏览行为 Error:请传入浏览对象bindId');
- return;
- }
- var dataP = {
- behaviorType: 4,
- type: type,
- bindId: bindId,
- uid: uid,
- shareUid: shareUid,
- code: creatBehaviorCode(),
- parentCode: parentCode,
- typeName:typeName
- };
- var endTime = getCurrentDateTiem();
- dataP.startTime = reqBase.getStorage('startTime');
- dataP.endTime = endTime;
- reqBase.postRequest(save_browse_api, dataP, data => {
- });
- };
- /**
- * 记录用户操作行为
- * @param behaviorType 行为类型
- * @param type 1"产品"、2 "活动"、3"文章"、4"社区"、5"海报"、6"发现"、7"名片"、8"素材内容"
- * @param bindId 操作对象ID、code(必填)
- * @param uid 当前用户ID(选填)
- * @param shareUid 分享用户ID(选填)
- * @param parentCode 父级分享生成的code(选填:别人的连接二次分享时获取)
- * @param typeName (选填:需要自定义type名称的时候传,比如通过文章做的风采,typeName传风采,type为3)
- * @param success 成功后回调,返回生成的code,(选填:获取当前行为code时传)
- * @param code 行为code(选填:需要先生成code时或code生成操作和分享操作不同时发送时(如小程序码分享携带该code)传,先调用creatBehaviorCode方法)
- */
- const saveBehavior = ({
- behaviorType = 4,
- type = 3,
- bindId = '',
- uid = '',
- shareUid = '',
- code = '',
- parentCode = '',
- typeName = '',
- success = (res)=>{}
- } = {}) => {
- var dataP = {
- behaviorType: behaviorType,
- type: type,
- bindId: bindId,
- uid: uid,
- shareUid: shareUid,
- code:code?code:creatBehaviorCode(),
- parentCode: parentCode,
- typeName:typeName
- };
- reqBase.postRequest(save_behavior_api, dataP, data => {
- if(data==-1){//接口请求异常时返回空
- // success.call(this,null)
- }else{
- // success.call(this,dataP.code)
- }
- });
- success.call(this,dataP.code)
- };
- function creatBehaviorCode() {
- var date = new Date();
- var code = randomText(6) + date.getTime();
- return code;
- };
- function randomText(num) {
- var num = num || 32,
- t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
- a = t.length,
- text = "";
- for (i = 0; i < num; i++) text += t.charAt(Math.floor(Math.random() * a));
- console.log('randomText>>>>>', text);
- return text
- };
- /**
- * 获取当前开始时间
- */
- function getStartTime() {
- var startTime = getCurrentDateTiem();
- reqBase.setStorage('startTime', startTime);
- };
- //当前时间
- function getCurrentDateTiem() {
- var date = new Date();
- return getBaseCurrentDateTiem(date);
- };
- function getBaseCurrentDateTiem(date, isNoTiem) {
- var year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
- var month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
- var day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
- var hours = date.getHours(); //小时 ,返回 Date 对象的小时 (0 ~ 23)
- var minutes = date.getMinutes(); //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
- var seconds = date.getSeconds(); //秒 ,返回 Date 对象的秒数 (0 ~ 59)
- //获取当前系统时间
- // var currentDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
- // alert(currentDate);
- //修改月份格式
- if (month >= 1 && month <= 9) {
- month = "0" + month;
- }
- //修改日期格式
- if (day >= 0 && day <= 9) {
- day = "0" + day;
- }
- //修改小时格式
- if (hours >= 0 && hours <= 9) {
- hours = "0" + hours;
- }
- //修改分钟格式
- if (minutes >= 0 && minutes <= 9) {
- minutes = "0" + minutes;
- }
- // //修改秒格式
- // if (seconds >= 0 && seconds <= 9) {
- // seconds = "0" + seconds;
- // }
- //获取当前系统时间 格式(yyyy-mm-dd hh:mm:ss)
- var currentFormatDate = '';
- if (isNoTiem) {
- currentFormatDate = year + "-" + month + "-" + day
- } else {
- currentFormatDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes;
- }
- return currentFormatDate;
- };
- module.exports = {
- initAction: initAction,
- saveBrowse: saveBrowse,
- saveBehavior: saveBehavior,
- creatBehaviorCode:creatBehaviorCode
- };
|