ad.mixin.mp.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. const EventType = {
  2. Load: 'load',
  3. Close: 'close',
  4. Error: 'error'
  5. }
  6. export default {
  7. props: {
  8. options: {
  9. type: [Object, Array],
  10. default () {
  11. return {}
  12. }
  13. },
  14. adpid: {
  15. type: [Number, String],
  16. default: ''
  17. },
  18. unitId: {
  19. type: [Number, String],
  20. default: ''
  21. },
  22. preload: {
  23. type: [Boolean, String],
  24. default: true
  25. },
  26. loadnext: {
  27. type: [Boolean, String],
  28. default: false
  29. },
  30. urlCallback: {
  31. type: Object,
  32. default () {
  33. return {}
  34. }
  35. }
  36. },
  37. data () {
  38. return {
  39. loading: false,
  40. errorMessage: null
  41. }
  42. },
  43. created () {
  44. this._ad = null
  45. setTimeout(() => {
  46. if (this.preload && this._canCreateAd()) {
  47. this.load()
  48. }
  49. }, 100)
  50. },
  51. methods: {
  52. load () {
  53. if (this.loading) {
  54. return
  55. }
  56. this._startLoading()
  57. },
  58. show () {
  59. this.errorMessage = null
  60. this._ad = this.selectComponent('.uniad-plugin')
  61. if (this._hasCallback()) {
  62. const userCryptoManager = wx.getUserCryptoManager()
  63. userCryptoManager.getLatestUserKey({
  64. success: ({
  65. encryptKey,
  66. iv,
  67. version,
  68. expireTime
  69. }) => {
  70. this._ad.show({
  71. userId: this.urlCallback.userId || '',
  72. extra: this.urlCallback.extra || '',
  73. encryptKey,
  74. iv,
  75. version,
  76. expireTime
  77. })
  78. },
  79. fail: (err) => {
  80. this._dispatchEvent(EventType.Error, err)
  81. }
  82. })
  83. } else {
  84. this._ad.show()
  85. }
  86. },
  87. _onclick () {
  88. this.show()
  89. },
  90. _startLoading () {
  91. this.loading = true
  92. this.errorMessage = null
  93. },
  94. _canCreateAd () {
  95. let result = false
  96. if (typeof this.adpid === 'string' && this.adpid.length > 0) {
  97. result = true
  98. } else if (typeof this.adpid === 'number') {
  99. result = true
  100. }
  101. return result
  102. },
  103. _hasCallback () {
  104. return (typeof this.urlCallback === 'object' && Object.keys(this.urlCallback).length > 0)
  105. },
  106. _onmpload (e) {
  107. this.loading = false
  108. this._dispatchEvent(EventType.Load, {})
  109. },
  110. _onmpclose (e) {
  111. this._dispatchEvent(EventType.Close, e.detail)
  112. if (e.detail.adsdata) {
  113. const adv = e.detail.adv
  114. const adsdata = e.detail.adsdata
  115. const version = e.detail.version
  116. /* eslint-disable no-undef */
  117. uniCloud.callFunction({
  118. name: 'uniAdCallback',
  119. data: {
  120. adv: adv,
  121. adsdata: adsdata,
  122. version: version
  123. },
  124. secretType: 'both',
  125. success: (res) => {
  126. },
  127. fail: (err) => {
  128. this._dispatchEvent(EventType.Error, err)
  129. }
  130. })
  131. delete e.detail.adv
  132. delete e.detail.adsdata
  133. delete e.detail.version
  134. }
  135. },
  136. _onmperror (e) {
  137. this.loading = false
  138. this.errorMessage = JSON.stringify(e.detail)
  139. this._dispatchEvent(EventType.Error, e.detail)
  140. },
  141. _dispatchEvent (type, data) {
  142. this.$emit(type, {
  143. detail: data
  144. })
  145. }
  146. }
  147. }