const ReqBase = require('./ReqBase.js'); const save_browse_api = '/api/behavior/saveBrowse'; //保存用户浏览行为数据 const save_behavior_api = '/api/behavior/save'; //保存用户行为数据 const remove_behavior_api = '/api/behavior/remove'; //移出用户行为数据(取消点赞等) let reqBase; const req = require('../utils/request.js'); const initAction = () => { if (!reqBase) { reqBase = new ReqBase({ appId: req.getStorage('appId') }); } getStartTime(); return reqBase; }; /** * 记录浏览行为 * @param type 1"产品"、2 "活动"、3"文章"、4"社区"、5"海报"、6"发现"、7"名片"、8"素材内容"、9"专题" * @param bindId 操作对象ID、code(必填) * @param uid 当前用户ID(选填) * @param shareUid 分享用户ID(选填) * @param parentCode 父级分享生成的code(选填:别人的连接二次分享时获取) * @param typeName (选填:需要自定义type名称的时候传,比如通过文章做的风采,typeName传风采,type为3) * @param code 行为code(选填:需要先生成code时或code生成操作和分享操作不同时发送时(如小程序码分享携带该code)传,先调用creatBehaviorCode方法) * @param content 行为日志文案(选填:页面浏览时传) * @param pageUri 当前访问的页面(选填:页面路径) * @param readTime 当前访问的页面浏览时长(选填:单位s) */ const saveBrowse = ({ type = 3, bindId = '', uid = '', shareUid = '', code = '', parentCode = '', typeName = '', content = '', pageUri = '', readTime = '' } = {}) => { // if (bindId === '' || bindId === undefined || bindId === null) { // console.log('保存浏览行为 Error:请传入浏览对象bindId'); // return; // } var dataP = { behaviorType: 4, type: type, bindId: bindId, uid: uid, shareUid: shareUid, code:code?code:creatBehaviorCode(), parentCode: parentCode, typeName:typeName, content:content, pageUri:pageUri, readTime:readTime }; 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方法) * @param content 行为日志文案(选填:页面浏览时传) * @param pageUri 当前访问的页面(选填:页面路径) * @param readTime 当前访问的页面浏览时长(选填:单位s) */ const saveBehavior = ({ behaviorType = 4, type = 3, bindId = '', uid = '', shareUid = '', code = '', parentCode = '', typeName = '', content = '', pageUri = '', readTime = '', success = (res)=>{} } = {}) => { var dataP = { behaviorType: behaviorType, type: type, bindId: bindId, uid: uid, shareUid: shareUid, code:code?code:creatBehaviorCode(), parentCode: parentCode, typeName:typeName, content:content, pageUri:pageUri, readTime:readTime }; reqBase.postRequest(save_behavior_api, dataP, data => { if(data==-1){//接口请求异常时返回空 // success.call(this,null) }else{ // success.call(this,dataP.code) } }); // 直接回调行为code,防止影响引用行为操作的其他代码执行 success.call(this,dataP.code) }; /** * 记录用户取消操作行为 * @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方法) * @param content 行为日志文案(选填:页面浏览时传) * @param pageUri 当前访问的页面(选填:页面路径) * @param readTime 当前访问的页面浏览时长(选填:单位s) */ const removeBehavior = ({ behaviorType = 4, type = 3, bindId = '', uid = '', shareUid = '', code = '', parentCode = '', typeName = '', content = '', pageUri = '', readTime = '', success = (res)=>{} } = {}) => { var dataP = { behaviorType: behaviorType, type: type, bindId: bindId, uid: uid, shareUid: shareUid, code:code?code:creatBehaviorCode(), parentCode: parentCode, typeName:typeName, content:content, pageUri:pageUri, readTime:readTime }; reqBase.postRequest(remove_behavior_api, dataP, data => { if(data==-1){//接口请求异常时返回空 // success.call(this,null) }else{ // success.call(this,dataP.code) } }); // 直接回调行为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, removeBehavior:removeBehavior, creatBehaviorCode:creatBehaviorCode };