ad.nvue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <template>
  2. <view>
  3. <text class="example-info">此示例展示了ad广告组件在 uni-list 中的应用</text>
  4. <uni-section title="列表嵌套广告" type="line"></uni-section>
  5. <uni-list>
  6. <template v-for="(item,index) in listData">
  7. <!-- <uni-list-ad v-if="(index > 0 && (index+1) % 10 == 0)"></uni-list-ad> -->
  8. <ad adpid="1111111111" v-if="(index > 0 && (index+1) % 10 == 0)" unit-id="" appid="" apid="" type="feed"></ad>
  9. <uni-list-item v-else :title="item.title" :note="item.author_name" :thumb="item.cover" thumb-size="lg" />
  10. </template>
  11. </uni-list>
  12. </view>
  13. </template>
  14. <script>
  15. export default {
  16. components: {},
  17. data() {
  18. return {
  19. UNITS: {
  20. '年': 31557600000,
  21. '月': 2629800000,
  22. '天': 86400000,
  23. '小时': 3600000,
  24. '分钟': 60000,
  25. '秒': 1000
  26. },
  27. listData: []
  28. }
  29. },
  30. onLoad() {
  31. this.getList()
  32. },
  33. methods: {
  34. getList() {
  35. var data = {
  36. column: 'id,post_id,title,author_name,cover,published_at' //需要的字段名
  37. };
  38. uni.request({
  39. url: 'https://unidemo.dcloud.net.cn/api/news',
  40. data: data,
  41. success: data => {
  42. if (data.statusCode == 200) {
  43. let list = this.setTime(data.data);
  44. this.listData = this.reload ? list : this.listData.concat(list);
  45. console.log(this.listData);
  46. }
  47. },
  48. fail: (data, code) => {
  49. console.log('fail' + JSON.stringify(data));
  50. }
  51. });
  52. },
  53. setTime(items) {
  54. var newItems = [];
  55. items.forEach(e => {
  56. newItems.push({
  57. author_name: e.author_name,
  58. cover: e.cover,
  59. id: e.id,
  60. post_id: e.post_id,
  61. published_at: this.format(e.published_at),
  62. title: e.title
  63. });
  64. });
  65. return newItems;
  66. },
  67. format(dateStr) {
  68. var date = this.parse(dateStr)
  69. var diff = Date.now() - date.getTime();
  70. if (diff < this.UNITS['天']) {
  71. return this.humanize(diff);
  72. }
  73. var _format = function(number) {
  74. return (number < 10 ? ('0' + number) : number);
  75. };
  76. return date.getFullYear() + '/' + _format(date.getMonth() + 1) + '/' + _format(date.getDate()) + '-' +
  77. _format(date.getHours()) + ':' + _format(date.getMinutes());
  78. },
  79. parse(str) { //将"yyyy-mm-dd HH:MM:ss"格式的字符串,转化为一个Date对象
  80. var a = str.split(/[^0-9]/);
  81. return new Date(a[0], a[1] - 1, a[2], a[3], a[4], a[5]);
  82. },
  83. }
  84. }
  85. </script>
  86. <style>
  87. @charset "UTF-8";
  88. /* 头条小程序组件内不能引入字体 */
  89. /* #ifdef MP-TOUTIAO */
  90. @font-face {
  91. font-family: uniicons;
  92. font-weight: normal;
  93. font-style: normal;
  94. src: url("~@/static/uni.ttf") format("truetype");
  95. }
  96. /* #endif */
  97. /* #ifndef APP-NVUE */
  98. page {
  99. display: flex;
  100. flex-direction: column;
  101. box-sizing: border-box;
  102. background-color: #efeff4;
  103. min-height: 100%;
  104. height: auto;
  105. }
  106. view {
  107. font-size: 14px;
  108. line-height: inherit;
  109. }
  110. .example {
  111. padding: 0 15px 15px;
  112. }
  113. .example-info {
  114. padding: 15px;
  115. color: #3b4144;
  116. background: #ffffff;
  117. }
  118. .example-body {
  119. /* #ifndef APP-NVUE */
  120. display: flex;
  121. /* #endif */
  122. flex-direction: row;
  123. flex-wrap: wrap;
  124. justify-content: center;
  125. padding: 0;
  126. font-size: 14px;
  127. background-color: #ffffff;
  128. }
  129. /* #endif */
  130. .example {
  131. padding: 0 15px;
  132. }
  133. .example-info {
  134. /* #ifndef APP-NVUE */
  135. display: block;
  136. /* #endif */
  137. padding: 15px;
  138. color: #3b4144;
  139. background-color: #ffffff;
  140. font-size: 14px;
  141. line-height: 20px;
  142. }
  143. .example-info-text {
  144. font-size: 14px;
  145. line-height: 20px;
  146. color: #3b4144;
  147. }
  148. .example-body {
  149. flex-direction: column;
  150. padding: 15px;
  151. background-color: #ffffff;
  152. }
  153. .word-btn-white {
  154. font-size: 18px;
  155. color: #FFFFFF;
  156. }
  157. .word-btn {
  158. /* #ifndef APP-NVUE */
  159. display: flex;
  160. /* #endif */
  161. flex-direction: row;
  162. align-items: center;
  163. justify-content: center;
  164. border-radius: 6px;
  165. height: 48px;
  166. margin: 15px;
  167. background-color: #007AFF;
  168. }
  169. .word-btn--hover {
  170. background-color: #4ca2ff;
  171. }
  172. </style>