da-bbs.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*!
  2. * @project :
  3. * @version : 1.0.0
  4. * @author : baichun
  5. * @update : 2018-04-19
  6. */
  7. // import './sass/bbs.scss';
  8. (function (window, $) {
  9. var cssStr = `.m-recommend__hd{padding:0 10px;height:38px;background-color:#ececec;border-top:1px solid #dfdfdf;border-bottom:1px solid #dfdfdf;overflow:hidden;line-height:38px}
  10. .m-recommend__hd h5{font-size: 18px;color: #333;}
  11. .m-recommend{border:1px solid #dfdfdf;border-top:none;margin-bottom:10px}
  12. .m-recommend .m-game_link{display:-webkit-box;display: flex;padding:10px}
  13. .m-recommend .m-game_fl{-webkit-box-flex: 1;-ms-flex:1;flex:1;width:100px;margin-right:10px}
  14. .m-recommend .m-game_fl img{width:100%;display:block;overflow:hidden}
  15. .m-recommend .m-game_fr{-webkit-box-flex: 2;-ms-flex:2;flex:2;margin-right: 10px;}
  16. .m-recommend .m-game_fr h2{font-size:16px;line-height:22px;margin-top:-4px;height:44px;overflow:hidden}
  17. .m-recommend .m-game_fr p{font-size:12px;color:#999;text-align:right}
  18. .m-forumList__item.ad-forumList__item .m-forumList__info{font-size:12px;color:#999}
  19. .ico-common.ico-cg{background:url(//pub.dwstatic.com/common/img/da-bbs-cg.png) 50%/100% no-repeat}
  20. .m-comment__item--top .da-link{display:block}
  21. .m-comment__item--top .comment-control .control-da-tag{float:left;color:#999}
  22. .comment-header-da img{width:100%}`;
  23. var Util = {
  24. getParam : function(name) {
  25. //先获取#后面的参数
  26. var str = document.location.hash.substr(2);
  27. var value = this.getParam2(name, str);
  28. if (value == null) {
  29. str = document.location.search.substr(1);
  30. value = this.getParam2(name, str);
  31. }
  32. return value;
  33. },
  34. getParam2 : function(name, str) {
  35. //获取参数name的值
  36. var reg = new RegExp("(^|!|&|\\?)" + name + "=([^&]*)(&|$)");
  37. //再获取?后面的参数
  38. var r = str.match(reg);
  39. if (r != null) {
  40. try {
  41. return decodeURIComponent(r[2]);
  42. } catch (e) {
  43. console.log(e + "r[2]:" + r[2]);
  44. return null;
  45. }
  46. }
  47. return null;
  48. },
  49. isFunction: function (obj) {
  50. return Object.prototype.toString.call(obj) === "[object Function]";
  51. },
  52. getScript: function (url, callback) {
  53. var head = document.getElementsByTagName("head")[0] || document.documentElement;
  54. var script = document.createElement("script");
  55. script.type = "text/javascript";
  56. script.async = true;
  57. script.charset = "utf-8";
  58. script.src = url;
  59. script.onload = script.onreadystatechange = function () {
  60. if (!script.readyState || /loaded|complete/.test(script.readyState)) {
  61. if (Util.isFunction(callback)) {
  62. callback();
  63. }
  64. script.onload = script.onreadystatechange = null;
  65. if (script.parentNode) {
  66. script.parentNode.removeChild(script);
  67. }
  68. }
  69. };
  70. head.insertBefore(script,null);
  71. },
  72. getScriptArg: function (key) {
  73. var scripts = document.getElementsByTagName("script"),
  74. script = scripts[scripts.length - 1],
  75. src = script.src;
  76. return (src.match(new RegExp("(?:\\?|&)" + key + "=(.*?)(?=&|$)")) || ['', null])[1];
  77. },
  78. createStyleSheet : function(cssText) {
  79. var stylesheet;
  80. if (document.createStyleSheet) {
  81. stylesheet = document.createStyleSheet();
  82. stylesheet.cssText = cssText;
  83. } else {
  84. stylesheet = document.createElement("style");
  85. stylesheet.appendChild(document.createTextNode(cssText));
  86. document.getElementsByTagName("head")[0].appendChild(stylesheet);
  87. }
  88. return stylesheet;
  89. }
  90. }
  91. var winHeight = $(window).height()
  92. var adv = {
  93. isiOS : navigator.userAgent.match(/(iPhone|iPod|iPad);?/i), //ios终端
  94. indexRecommendLocid : 175, //每日推荐175
  95. indexBBsLocid : 176, //帖子列表176
  96. detailPostLocid : 177, //帖子详情177
  97. init(){
  98. // if(Util.getParam("isdebug")!=1) return
  99. $("head").append(`<style>${cssStr}</style>`)
  100. let curPage = Util.getScriptArg("type"),
  101. self = this;
  102. if (curPage=="detail") { //详情页
  103. self.initPostList()
  104. } else { //首页
  105. self.initRecommendAdv()
  106. self.initBbsList()
  107. }
  108. Util.getScript("//pub.dwstatic.com/common/js/dastat.js",()=>{
  109. $(window).scroll(self.initScroll);
  110. });
  111. },
  112. recommendTmpl(t){
  113. var dom = `<div class="m-recommend">
  114. <div class="m-recommend__hd">
  115. <h5>每日推荐</h5>
  116. </div>
  117. <div class="m-recommend__bd">
  118. <a href="${t.adLink}" class="m-game_link da-block-wrapper" locid="${t.adId}" pid="${t.adPid}" target="_blank">
  119. <div class="m-game_fl">
  120. <img src="${t.adSrc}">
  121. </div>
  122. <div class="m-game_fr">
  123. <h2>${t.title}</h2>
  124. <p></p>
  125. </div>
  126. </a>
  127. </div>
  128. </div>`
  129. return dom;
  130. },
  131. bbsListTmpl(t){
  132. var dom = `<div class="m-forumList__item ad-forumList__item">
  133. <a href="${t.adLink}" class="da-block-wrapper" locid="${t.adId}" pid="${t.adPid}" not-auto-report="1">
  134. <p class="m-forumList__detail">
  135. ${t.title}
  136. <i class="ico-common ico-cg"></i>
  137. <i class="ico-common ico-pic"></i>
  138. </p>
  139. <p class="m-forumList__info">
  140. <span class="m-forumList__info--fl"></span>
  141. <span class="m-forumList__info--fr">广告</span>
  142. </p>
  143. </a>
  144. </div>`
  145. return dom
  146. },
  147. bbsPostTmpl(t){
  148. let dom = `<div class="m-comment__item m-comment__item--top">
  149. <a href="${t.adLink}" target="_blank" class="da-link da-block-wrapper" locid="${t.adId}" pid="${t.adPid}" not-auto-report="1">
  150. <div class="comment-header-da">
  151. <img src="${t.adSrc}">
  152. </div>
  153. <div class="comment-detail">
  154. <p>${t.title}</p>
  155. </div>
  156. <div class="comment-control">
  157. <span class="control-da-tag">广告</span>
  158. <span class="control-da-cheak">查看详情</span>
  159. </div>
  160. </a>
  161. </div>`
  162. return dom
  163. },
  164. //首页-推荐位广告-每个UV每5小时展示一次
  165. initRecommendAdv(){
  166. let advInfoStr = localStorage.getItem("advInfo")
  167. if (advInfoStr) {
  168. let advInfo = JSON.parse(advInfoStr)
  169. let lastTime = advInfo["lastTime"]
  170. let curTime = (new Date()).getTime()
  171. let isShowRecommendAdv = curTime-lastTime > 5*60*60*1000 ? true : false
  172. // console.log("上次展示时间:"+lastTime+";当前时间:"+curTime)
  173. // console.log("时间间隔"+(curTime-lastTime)+"是否展示:"+isShowRecommendAdv)
  174. if(isShowRecommendAdv) {
  175. this.getDa(this.indexRecommendLocid,(ret)=>{
  176. if(!ret.length) return
  177. advInfo["lastTime"] = curTime
  178. localStorage.setItem("advInfo",JSON.stringify(advInfo))
  179. $(".m-forumList").prepend(this.recommendTmpl(ret[0]))
  180. this.initScroll()
  181. })
  182. }
  183. } else {
  184. console.log("从未展示过")
  185. this.getDa(this.indexRecommendLocid,(ret)=>{
  186. if(!ret.length) return
  187. let advInfo = {}
  188. advInfo["lastTime"] = (new Date()).getTime()
  189. $(".m-forumList").prepend(this.recommendTmpl(ret[0]))
  190. localStorage.setItem("advInfo",JSON.stringify(advInfo))
  191. this.initScroll()
  192. });
  193. }
  194. },
  195. //首页-帖子列表置顶后的每3条内容显示1个游戏广告,最多显示3条广告(即第4、8、12位置)
  196. initBbsList (){
  197. this.getDa(this.indexBBsLocid,(ret)=>{
  198. if(!ret.length) return
  199. let adStartIndex = 0,
  200. advPos = [2,6,10],
  201. advPosLen = advPos.length,
  202. retLen = ret.length,
  203. ads = ret;
  204. if(retLen==0) return
  205. //一个广告位只有1/2推广计划时,重复展示,共3个
  206. switch(retLen) {
  207. case 1 :
  208. ads = ads.concat(ret[0],ret[0])
  209. break;
  210. case 2 :
  211. var index = Math.random()<0.5?0:1
  212. ads = ads.concat(ret[index])
  213. break;
  214. default :
  215. break;
  216. }
  217. $(".m-forumList__item").each(function(){
  218. let isHasZd = $(this).find(".m-forumList__detail").children(".ico-zd").length>0 ? true : false
  219. if(isHasZd) ++adStartIndex
  220. })
  221. ads.forEach((item,i)=>{
  222. if(i>=advPosLen) return
  223. $(".m-forumList__item").eq(adStartIndex+advPos[i]).after(this.bbsListTmpl(item))
  224. });
  225. this.initScroll()
  226. })
  227. },
  228. //详情页-沙发和板凳,10楼与11楼
  229. initPostList(){
  230. this.getDa(this.detailPostLocid,(ret)=>{
  231. let advPos = [1,10],
  232. advPosLen = advPos.length,
  233. retLen = ret.length,
  234. ads = ret;
  235. //一个广告位只有1推广计划时,重复展示,共2个
  236. if(retLen==0) return
  237. if(retLen==1) ads.push(ret[0])
  238. ads.forEach((item,i)=>{
  239. if(i>=advPosLen) return
  240. $(".m-comment__item").eq(advPos[i]).after(this.bbsPostTmpl(item))
  241. });
  242. this.initScroll()
  243. })
  244. },
  245. initScroll(){
  246. let itemTop=[],
  247. scrollTop = $(window).scrollTop(),
  248. $daItems = $(".da-block-wrapper"),
  249. len = $daItems.length;
  250. if(!len) return
  251. $daItems.each((index,element)=>{
  252. itemTop.push($(element).offset().top-winHeight+20)
  253. })
  254. itemTop.forEach((item,i)=>{
  255. if(itemTop[i] <= scrollTop) {
  256. let $curItem = $daItems.eq(i)
  257. if($curItem.attr("is-report")==1 || !window.dwDaStat) return
  258. // console.log("上报第"+i+"个广告")
  259. $curItem.attr("is-report",1);
  260. window.dwDaStat.reportData($curItem.get(0),'loaded')
  261. }
  262. })
  263. },
  264. getDa(locid,callback){
  265. var platform = this.isiOS ? "ios" : "android"
  266. $.ajax({
  267. url: "http://da.duowan.com/loc/"+locid+"?platform="+platform,
  268. dataType: 'jsonp',
  269. success: function(ret) {
  270. callback && callback(ret)
  271. }
  272. })
  273. }
  274. }
  275. adv.init()
  276. })(window, $);