actionsdk-1.0.0.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. const ReqBase = require('./ReqBase.js');
  2. const save_browse_api = '/api/behavior/saveBrowse'; //保存用户浏览行为数据
  3. const save_behavior_api = '/api/behavior/save'; //保存用户行为数据
  4. const remove_behavior_api = '/api/behavior/remove'; //移出用户行为数据(取消点赞等)
  5. let reqBase;
  6. const req = require('../utils/request.js');
  7. const initAction = () => {
  8. if (!reqBase) {
  9. reqBase = new ReqBase({
  10. appId: req.getStorage('appId')
  11. });
  12. }
  13. getStartTime();
  14. return reqBase;
  15. };
  16. /**
  17. * 记录浏览行为
  18. * @param type 1"产品"、2 "活动"、3"文章"、4"社区"、5"海报"、6"发现"、7"名片"、8"素材内容"、9"专题"
  19. * @param bindId 操作对象ID、code(必填)
  20. * @param uid 当前用户ID(选填)
  21. * @param shareUid 分享用户ID(选填)
  22. * @param parentCode 父级分享生成的code(选填:别人的连接二次分享时获取)
  23. * @param typeName (选填:需要自定义type名称的时候传,比如通过文章做的风采,typeName传风采,type为3)
  24. * @param code 行为code(选填:需要先生成code时或code生成操作和分享操作不同时发送时(如小程序码分享携带该code)传,先调用creatBehaviorCode方法)
  25. * @param content 行为日志文案(选填:页面浏览时传)
  26. * @param pageUri 当前访问的页面(选填:页面路径)
  27. * @param readTime 当前访问的页面浏览时长(选填:单位s)
  28. */
  29. const saveBrowse = ({
  30. type = 3,
  31. bindId = '',
  32. uid = '',
  33. shareUid = '',
  34. code = '',
  35. parentCode = '',
  36. typeName = '',
  37. content = '',
  38. pageUri = '',
  39. readTime = ''
  40. } = {}) => {
  41. // if (bindId === '' || bindId === undefined || bindId === null) {
  42. // console.log('保存浏览行为 Error:请传入浏览对象bindId');
  43. // return;
  44. // }
  45. var dataP = {
  46. behaviorType: 4,
  47. type: type,
  48. bindId: bindId,
  49. uid: uid,
  50. shareUid: shareUid,
  51. code:code?code:creatBehaviorCode(),
  52. parentCode: parentCode,
  53. typeName:typeName,
  54. content:content,
  55. pageUri:pageUri,
  56. readTime:readTime
  57. };
  58. var endTime = getCurrentDateTiem();
  59. dataP.startTime = reqBase.getStorage('startTime');
  60. dataP.endTime = endTime;
  61. reqBase.postRequest(save_browse_api, dataP, data => {
  62. });
  63. };
  64. /**
  65. * 记录用户操作行为
  66. * @param behaviorType 行为类型
  67. * @param type 1"产品"、2 "活动"、3"文章"、4"社区"、5"海报"、6"发现"、7"名片"、8"素材内容"
  68. * @param bindId 操作对象ID、code(必填)
  69. * @param uid 当前用户ID(选填)
  70. * @param shareUid 分享用户ID(选填)
  71. * @param parentCode 父级分享生成的code(选填:别人的连接二次分享时获取)
  72. * @param typeName (选填:需要自定义type名称的时候传,比如通过文章做的风采,typeName传风采,type为3)
  73. * @param success 成功后回调,返回生成的code,(选填:获取当前行为code时传)
  74. * @param code 行为code(选填:需要先生成code时或code生成操作和分享操作不同时发送时(如小程序码分享携带该code)传,先调用creatBehaviorCode方法)
  75. * @param content 行为日志文案(选填:页面浏览时传)
  76. * @param pageUri 当前访问的页面(选填:页面路径)
  77. * @param readTime 当前访问的页面浏览时长(选填:单位s)
  78. */
  79. const saveBehavior = ({
  80. behaviorType = 4,
  81. type = 3,
  82. bindId = '',
  83. uid = '',
  84. shareUid = '',
  85. code = '',
  86. parentCode = '',
  87. typeName = '',
  88. content = '',
  89. pageUri = '',
  90. readTime = '',
  91. success = (res)=>{}
  92. } = {}) => {
  93. var dataP = {
  94. behaviorType: behaviorType,
  95. type: type,
  96. bindId: bindId,
  97. uid: uid,
  98. shareUid: shareUid,
  99. code:code?code:creatBehaviorCode(),
  100. parentCode: parentCode,
  101. typeName:typeName,
  102. content:content,
  103. pageUri:pageUri,
  104. readTime:readTime
  105. };
  106. reqBase.postRequest(save_behavior_api, dataP, data => {
  107. if(data==-1){//接口请求异常时返回空
  108. // success.call(this,null)
  109. }else{
  110. // success.call(this,dataP.code)
  111. }
  112. });
  113. // 直接回调行为code,防止影响引用行为操作的其他代码执行
  114. success.call(this,dataP.code)
  115. };
  116. /**
  117. * 记录用户取消操作行为
  118. * @param behaviorType 行为类型
  119. * @param type 1"产品"、2 "活动"、3"文章"、4"社区"、5"海报"、6"发现"、7"名片"、8"素材内容"
  120. * @param bindId 操作对象ID、code(必填)
  121. * @param uid 当前用户ID(选填)
  122. * @param shareUid 分享用户ID(选填)
  123. * @param parentCode 父级分享生成的code(选填:别人的连接二次分享时获取)
  124. * @param typeName (选填:需要自定义type名称的时候传,比如通过文章做的风采,typeName传风采,type为3)
  125. * @param success 成功后回调,返回生成的code,(选填:获取当前行为code时传)
  126. * @param code 行为code(选填:需要先生成code时或code生成操作和分享操作不同时发送时(如小程序码分享携带该code)传,先调用creatBehaviorCode方法)
  127. * @param content 行为日志文案(选填:页面浏览时传)
  128. * @param pageUri 当前访问的页面(选填:页面路径)
  129. * @param readTime 当前访问的页面浏览时长(选填:单位s)
  130. */
  131. const removeBehavior = ({
  132. behaviorType = 4,
  133. type = 3,
  134. bindId = '',
  135. uid = '',
  136. shareUid = '',
  137. code = '',
  138. parentCode = '',
  139. typeName = '',
  140. content = '',
  141. pageUri = '',
  142. readTime = '',
  143. success = (res)=>{}
  144. } = {}) => {
  145. var dataP = {
  146. behaviorType: behaviorType,
  147. type: type,
  148. bindId: bindId,
  149. uid: uid,
  150. shareUid: shareUid,
  151. code:code?code:creatBehaviorCode(),
  152. parentCode: parentCode,
  153. typeName:typeName,
  154. content:content,
  155. pageUri:pageUri,
  156. readTime:readTime
  157. };
  158. reqBase.postRequest(remove_behavior_api, dataP, data => {
  159. if(data==-1){//接口请求异常时返回空
  160. // success.call(this,null)
  161. }else{
  162. // success.call(this,dataP.code)
  163. }
  164. });
  165. // 直接回调行为code,防止影响引用行为操作的其他代码执行
  166. success.call(this,dataP.code)
  167. };
  168. function creatBehaviorCode() {
  169. var date = new Date();
  170. var code = randomText(6) + date.getTime();
  171. return code;
  172. };
  173. function randomText(num) {
  174. var num = num || 32,
  175. t = "ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678",
  176. a = t.length,
  177. text = "";
  178. for (i = 0; i < num; i++) text += t.charAt(Math.floor(Math.random() * a));
  179. console.log('randomText>>>>>', text);
  180. return text
  181. };
  182. /**
  183. * 获取当前开始时间
  184. */
  185. function getStartTime() {
  186. var startTime = getCurrentDateTiem();
  187. reqBase.setStorage('startTime', startTime);
  188. };
  189. //当前时间
  190. function getCurrentDateTiem() {
  191. var date = new Date();
  192. return getBaseCurrentDateTiem(date);
  193. };
  194. function getBaseCurrentDateTiem(date, isNoTiem) {
  195. var year = date.getFullYear(); //年 ,从 Date 对象以四位数字返回年份
  196. var month = date.getMonth() + 1; //月 ,从 Date 对象返回月份 (0 ~ 11) ,date.getMonth()比实际月份少 1 个月
  197. var day = date.getDate(); //日 ,从 Date 对象返回一个月中的某一天 (1 ~ 31)
  198. var hours = date.getHours(); //小时 ,返回 Date 对象的小时 (0 ~ 23)
  199. var minutes = date.getMinutes(); //分钟 ,返回 Date 对象的分钟 (0 ~ 59)
  200. var seconds = date.getSeconds(); //秒 ,返回 Date 对象的秒数 (0 ~ 59)
  201. //获取当前系统时间
  202. // var currentDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes + ":" + seconds;
  203. // alert(currentDate);
  204. //修改月份格式
  205. if (month >= 1 && month <= 9) {
  206. month = "0" + month;
  207. }
  208. //修改日期格式
  209. if (day >= 0 && day <= 9) {
  210. day = "0" + day;
  211. }
  212. //修改小时格式
  213. if (hours >= 0 && hours <= 9) {
  214. hours = "0" + hours;
  215. }
  216. //修改分钟格式
  217. if (minutes >= 0 && minutes <= 9) {
  218. minutes = "0" + minutes;
  219. }
  220. // //修改秒格式
  221. // if (seconds >= 0 && seconds <= 9) {
  222. // seconds = "0" + seconds;
  223. // }
  224. //获取当前系统时间 格式(yyyy-mm-dd hh:mm:ss)
  225. var currentFormatDate = '';
  226. if (isNoTiem) {
  227. currentFormatDate = year + "-" + month + "-" + day
  228. } else {
  229. currentFormatDate = year + "-" + month + "-" + day + " " + hours + ":" + minutes;
  230. }
  231. return currentFormatDate;
  232. };
  233. module.exports = {
  234. initAction: initAction,
  235. saveBrowse: saveBrowse,
  236. saveBehavior: saveBehavior,
  237. removeBehavior:removeBehavior,
  238. creatBehaviorCode:creatBehaviorCode
  239. };