actionsdk-1.0.0.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. const ReqBase = require('./ReqBase.js');
  2. const save_browse_api = '/api/behavior/saveBrowse'; //保存用户浏览行为数据
  3. const save_behavior_api = '/api/behavior/save'; //保存用户行为数据
  4. let reqBase;
  5. const req = require('../utils/request.js');
  6. const initAction = () => {
  7. if (!reqBase) {
  8. reqBase = new ReqBase({
  9. appId: req.getStorage('appId')
  10. });
  11. }
  12. getStartTime();
  13. };
  14. /**
  15. * 记录浏览行为
  16. * @param type 1"产品"、2 "活动"、3"文章"、4"社区"、5"海报"、6"发现"、7"名片"、8"素材内容"
  17. * @param bindId 操作对象ID、code(必填)
  18. * @param uid 当前用户ID(选填)
  19. * @param shareUid 分享用户ID(选填)
  20. * @param parentCode 父级分享生成的code(选填:别人的连接二次分享时获取)
  21. * @param typeName (选填:需要自定义type名称的时候传,比如通过文章做的风采,typeName传风采,type为3)
  22. */
  23. const saveBrowse = ({
  24. type = 3,
  25. bindId = '',
  26. uid = '',
  27. shareUid = '',
  28. parentCode = '',
  29. typeName = ''
  30. } = {}) => {
  31. if (!bindId) {
  32. console.log('保存浏览行为 Error:请传入浏览对象bindId');
  33. return;
  34. }
  35. var dataP = {
  36. behaviorType: 4,
  37. type: type,
  38. bindId: bindId,
  39. uid: uid,
  40. shareUid: shareUid,
  41. code: creatBehaviorCode(),
  42. parentCode: parentCode,
  43. typeName:typeName
  44. };
  45. var endTime = getCurrentDateTiem();
  46. dataP.startTime = reqBase.getStorage('startTime');
  47. dataP.endTime = endTime;
  48. reqBase.postRequest(save_browse_api, dataP, data => {
  49. });
  50. };
  51. /**
  52. * 记录用户操作行为
  53. * @param behaviorType 行为类型
  54. * @param type 1"产品"、2 "活动"、3"文章"、4"社区"、5"海报"、6"发现"、7"名片"、8"素材内容"
  55. * @param bindId 操作对象ID、code(必填)
  56. * @param uid 当前用户ID(选填)
  57. * @param shareUid 分享用户ID(选填)
  58. * @param parentCode 父级分享生成的code(选填:别人的连接二次分享时获取)
  59. * @param typeName (选填:需要自定义type名称的时候传,比如通过文章做的风采,typeName传风采,type为3)
  60. * @param success 成功后回调,返回生成的code,(选填:获取当前行为code时传)
  61. * @param code 行为code(选填:需要先生成code时或code生成操作和分享操作不同时发送时(如小程序码分享携带该code)传,先调用creatBehaviorCode方法)
  62. */
  63. const saveBehavior = ({
  64. behaviorType = 4,
  65. type = 3,
  66. bindId = '',
  67. uid = '',
  68. shareUid = '',
  69. code = '',
  70. parentCode = '',
  71. typeName = '',
  72. success = (res)=>{}
  73. } = {}) => {
  74. var dataP = {
  75. behaviorType: behaviorType,
  76. type: type,
  77. bindId: bindId,
  78. uid: uid,
  79. shareUid: shareUid,
  80. code:code?code:creatBehaviorCode(),
  81. parentCode: parentCode,
  82. typeName:typeName
  83. };
  84. reqBase.postRequest(save_behavior_api, dataP, data => {
  85. if(data==-1){//接口请求异常时返回空
  86. // success.call(this,null)
  87. }else{
  88. // success.call(this,dataP.code)
  89. }
  90. });
  91. success.call(this,dataP.code)
  92. };
  93. function creatBehaviorCode() {
  94. var date = new Date();
  95. var code = randomText(6) + date.getTime();
  96. return code;
  97. };
  98. function randomText(num) {
  99. var num = num || 32,
  100. t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
  101. a = t.length,
  102. text = "";
  103. for (i = 0; i < num; i++) text += t.charAt(Math.floor(Math.random() * a));
  104. console.log('randomText>>>>>', text);
  105. return text
  106. };
  107. /**
  108. * 获取当前开始时间
  109. */
  110. function getStartTime() {
  111. var startTime = getCurrentDateTiem();
  112. reqBase.setStorage('startTime', startTime);
  113. };
  114. //当前时间
  115. function getCurrentDateTiem() {
  116. var date = new Date();
  117. return getBaseCurrentDateTiem(date);
  118. };
  119. function getBaseCurrentDateTiem(date, isNoTiem) {
  120. var year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
  121. var month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
  122. var day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
  123. var hours = date.getHours(); //小时 ,返回 Date 对象的小时 (0 ~ 23)
  124. var minutes = date.getMinutes(); //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
  125. var seconds = date.getSeconds(); //秒 ,返回 Date 对象的秒数 (0 ~ 59)
  126. //获取当前系统时间
  127. // var currentDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
  128. // alert(currentDate);
  129. //修改月份格式
  130. if (month >= 1 && month <= 9) {
  131. month = "0" + month;
  132. }
  133. //修改日期格式
  134. if (day >= 0 && day <= 9) {
  135. day = "0" + day;
  136. }
  137. //修改小时格式
  138. if (hours >= 0 && hours <= 9) {
  139. hours = "0" + hours;
  140. }
  141. //修改分钟格式
  142. if (minutes >= 0 && minutes <= 9) {
  143. minutes = "0" + minutes;
  144. }
  145. // //修改秒格式
  146. // if (seconds >= 0 && seconds <= 9) {
  147. // seconds = "0" + seconds;
  148. // }
  149. //获取当前系统时间 格式(yyyy-mm-dd hh:mm:ss)
  150. var currentFormatDate = '';
  151. if (isNoTiem) {
  152. currentFormatDate = year + "-" + month + "-" + day
  153. } else {
  154. currentFormatDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes;
  155. }
  156. return currentFormatDate;
  157. };
  158. module.exports = {
  159. initAction: initAction,
  160. saveBrowse: saveBrowse,
  161. saveBehavior: saveBehavior,
  162. creatBehaviorCode:creatBehaviorCode
  163. };