ad-interactive.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view @click="onclick">
  3. <slot :options="options" :data="adData" :loading="loading" :error="errorMessage" />
  4. </view>
  5. </template>
  6. <script>
  7. const AD_URL = 'https://wxac1.dcloud.net.cn/openPage/acs'
  8. const AD_REPORT_URL = 'https://wxac1.dcloud.net.cn/openPage/acs'
  9. //const WEBVIEW_PATH = '/uni_modules/uni-ad-interactive/pages/uni-ad-interactive/uni-ad-interactive'
  10. const events = {
  11. load: 'load',
  12. close: 'close',
  13. error: 'error'
  14. }
  15. const OpenTypes = {
  16. Interactive: 'interactive'
  17. }
  18. export default {
  19. name: 'AdInteractive',
  20. props: {
  21. options: {
  22. type: [Object, Array],
  23. default () {
  24. return {}
  25. }
  26. },
  27. disabled: {
  28. type: [Boolean, String],
  29. default: false
  30. },
  31. adpid: {
  32. type: [Number, String],
  33. default: ''
  34. },
  35. openType: {
  36. type: String,
  37. default: 'interactive'
  38. },
  39. openPagePath: {
  40. type: String,
  41. default: ""
  42. }
  43. },
  44. data() {
  45. return {
  46. adData: null,
  47. loading: false,
  48. errorMessage: ""
  49. }
  50. },
  51. created() {
  52. this._interactiveUrl = null
  53. if (this.openPagePath) {
  54. this.getAdData()
  55. }
  56. },
  57. methods: {
  58. getAdData() {
  59. if (!this.adpid) {
  60. this.$emit(events.error, {
  61. code: -5002,
  62. message: 'invalid adpid'
  63. })
  64. return
  65. }
  66. this.loading = true
  67. uni.request({
  68. url: AD_URL,
  69. method: 'POST',
  70. data: {
  71. adpid: this.adpid
  72. },
  73. timeout: 5000,
  74. dataType: 'json',
  75. success: (res) => {
  76. if (res.statusCode !== 200) {
  77. this.$emit(events.error, {
  78. errCode: res.statusCode,
  79. errMsg: res.statusCode
  80. })
  81. return
  82. }
  83. const responseData = res.data
  84. if (responseData.ret === 0) {
  85. this._interactiveUrl = responseData.data.adp_url
  86. this.adData = {
  87. imgUrl: responseData.data.icon_url,
  88. openPath: this.openPagePath + '?url=' + encodeURIComponent(this._interactiveUrl)
  89. }
  90. this.$emit(events.load, this.adData)
  91. } else {
  92. const errMsg = {
  93. errCode: responseData.ret,
  94. errMsg: responseData.msg
  95. }
  96. this.errorMessage = errMsg
  97. this.$emit(events.error, errMsg)
  98. }
  99. },
  100. fail: (err) => {
  101. this.$emit(events.error, {
  102. errCode: '',
  103. errMsg: err.errMsg
  104. })
  105. },
  106. complete: () => {
  107. this.loading = false
  108. }
  109. })
  110. },
  111. onclick() {
  112. if (this.disabled) {
  113. return
  114. }
  115. if (!this._interactiveUrl) {
  116. return
  117. }
  118. uni.navigateTo({
  119. url: this.adData.openPath
  120. })
  121. this._report()
  122. },
  123. _report() {
  124. uni.request({
  125. url: AD_REPORT_URL,
  126. data: {
  127. adpid: this.adpid,
  128. t: '10019'
  129. },
  130. timeout: 5000,
  131. dataType: 'json'
  132. })
  133. }
  134. }
  135. }
  136. </script>
  137. <style>
  138. view {
  139. display: block;
  140. }
  141. </style>