uni-ad-native.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. const EventType = {
  2. Load: 'load',
  3. Close: 'close',
  4. Error: 'error'
  5. }
  6. const AdType = {
  7. Banner: "banner",
  8. RewardedVideo: "rewardedVideo",
  9. Interstitial: "interstitial"
  10. }
  11. const ProviderType = {
  12. WeChat: 10018,
  13. UserWeChat: 10017,
  14. ShanHu: 10020
  15. }
  16. Component({
  17. properties: {
  18. adpid: {
  19. type: String,
  20. value: '',
  21. observer(newVal, oldVal, changedPath) {
  22. }
  23. },
  24. plugins: {
  25. type: String,
  26. value: '[{"name":"uni-ad","version":"1.3.7","provider":"wxf72d316417b6767f"},{"name":"uni-ad-wx","version":"1.0.2","provider":"wxe6129e9cc9619c07"}]',
  27. observer(newVal, oldVal, changedPath) {
  28. }
  29. }
  30. },
  31. data: {
  32. wxchannel: false,
  33. customFullscreen: "",
  34. },
  35. created: function () {
  36. this.loading = false;
  37. this._wxRewardedAd = null;
  38. this._wxInterstitialAd = null;
  39. this._userInvokeShowFlag = false;
  40. this._providerType = ProviderType.ShanHu;
  41. this.load();
  42. },
  43. detached: function() {
  44. if (this._wxRewardedAd) {
  45. this._wxRewardedAd.destroy()
  46. }
  47. if (this._wxInterstitialAd) {
  48. this._wxInterstitialAd.destroy()
  49. }
  50. },
  51. methods: {
  52. load () {
  53. if (this.loading) {
  54. return
  55. }
  56. this.loading = true
  57. this.errorMessage = null
  58. if (this._providerType == ProviderType.ShanHu) {
  59. } else if (this._providerType == ProviderType.WeChat) {
  60. this.selectComponent('.uniad-plugin-wx').load()
  61. } else if (this._providerType == ProviderType.UserWeChat) {
  62. this._loadWxAd()
  63. }
  64. },
  65. show (e) {
  66. this.errorMessage = null
  67. if (this.loading == true) {
  68. this._userInvokeShowFlag = true
  69. return
  70. }
  71. if (this._providerType == ProviderType.ShanHu) {
  72. this.selectComponent('.uniad-plugin').show()
  73. } else if (this._providerType == ProviderType.WeChat) {
  74. this.selectComponent('.uniad-plugin-wx').show()
  75. } else if (this._providerType == ProviderType.UserWeChat) {
  76. this._showWxAd(e)
  77. }
  78. },
  79. destroy() {
  80. // console.log('组件被销毁时自动关闭并销毁广告实例')
  81. },
  82. _onclick () {
  83. this.show()
  84. },
  85. _canCreateAd () {
  86. if (typeof this.adpid === 'string' && this.adpid.length > 0) {
  87. return true
  88. } else if (typeof this.adpid === 'number') {
  89. return true
  90. }
  91. return false
  92. },
  93. _onmpload (e) {
  94. this.loading = false
  95. this.triggerEvent(EventType.Load, {})
  96. if (this._userInvokeShowFlag) {
  97. this._userInvokeShowFlag = false;
  98. setTimeout(() => {
  99. this.show();
  100. }, 1)
  101. }
  102. },
  103. _onmpclose (e) {
  104. this.triggerEvent(EventType.Close, e.detail)
  105. },
  106. _onmperror (e) {
  107. this.loading = false
  108. this.errorMessage = JSON.stringify(e.detail)
  109. this.triggerEvent(EventType.Error, e.detail)
  110. },
  111. _onnextchannel (e) {
  112. this.setData({
  113. wxchannel: true
  114. });
  115. const adData = e.detail[0];
  116. setTimeout(() => {
  117. if (adData.provider == 10017) {
  118. this._providerType = ProviderType.UserWeChat
  119. switch(adData._nt_) {
  120. case 4:
  121. this.wxAdType = AdType.Banner
  122. this.userwx = true
  123. this.userUnitId = adData.posid
  124. if (adData.tmpl_type === 24) {
  125. this.setData({
  126. customFullscreen: 'uni-ad-custom-fullscreen'
  127. })
  128. }
  129. break;
  130. case 9:
  131. this.wxAdType = AdType.RewardedVideo
  132. this._createRewardedAd(adData.posid)
  133. break;
  134. case 15:
  135. this.wxAdType = AdType.Interstitial
  136. this._createInterstitialAd(adData.posid)
  137. break;
  138. }
  139. } else if (adData.provider == 10018) {
  140. this._providerType = ProviderType.WeChat
  141. if (adData.tmpl_type === 24) {
  142. this.setData({
  143. customFullscreen: 'uni-ad-custom-fullscreen'
  144. })
  145. }
  146. this.loading = true
  147. this.selectComponent('.uniad-plugin-wx').setConfig(adData)
  148. }
  149. })
  150. },
  151. _onwxchannelerror(e) {
  152. this.triggerEvent(EventType.Error, e.detail)
  153. },
  154. _loadWxAd () {
  155. switch (this.wxAdType) {
  156. case AdType.RewardedVideo:
  157. if (this._wxRewardedAd) {
  158. this._wxRewardedAd.load();
  159. }
  160. break;
  161. case AdType.Interstitial:
  162. if (this._wxInterstitialAd) {
  163. this._wxInterstitialAd.load();
  164. }
  165. break;
  166. }
  167. },
  168. _showWxAd () {
  169. if (this.loading == true) {
  170. this._userInvokeShowFlag = true
  171. return
  172. }
  173. switch (this.wxAdType) {
  174. case AdType.RewardedVideo:
  175. if (!this._wxRewardedAd) {
  176. return;
  177. }
  178. this._wxRewardedAd.show().catch((err) => {
  179. this._wxRewardedAd.load().then(() => {
  180. this._wxRewardedAd.show();
  181. }).catch((err) => {
  182. this.triggerEvent(EventType.Error, err);
  183. });
  184. });
  185. break;
  186. case AdType.Interstitial:
  187. if (!this._wxInterstitialAd) {
  188. return;
  189. }
  190. this._wxInterstitialAd.show().catch((err) => {
  191. this._wxInterstitialAd.load().then(() => {
  192. this._wxInterstitialAd.show();
  193. }).catch((err) => {
  194. this.triggerEvent(EventType.Error, err);
  195. });
  196. });
  197. break;
  198. }
  199. },
  200. _createRewardedAd(adUnitId) {
  201. if (this._wxRewardedAd) {
  202. return;
  203. }
  204. this._wxRewardedAd = wx.createRewardedVideoAd({ adUnitId: adUnitId, multiton: true });
  205. this._wxRewardedAd.onLoad(() => {
  206. this.loading = false
  207. this.triggerEvent(EventType.Load, {})
  208. if (this._userInvokeShowFlag) {
  209. this._userInvokeShowFlag = false;
  210. this._wxRewardedAd.show();
  211. }
  212. });
  213. this._wxRewardedAd.onError(err => {
  214. this.loading = false
  215. this.triggerEvent(EventType.Error, err);
  216. });
  217. this._wxRewardedAd.onClose(res => {
  218. this.triggerEvent(EventType.Close, res);
  219. });
  220. this._wxRewardedAd?.load().then(() => { }).catch((err) => { });
  221. this.loading = true
  222. },
  223. _createInterstitialAd(adUnitId) {
  224. if (this._wxInterstitialAd) {
  225. return;
  226. }
  227. this._wxInterstitialAd = wx.createInterstitialAd({ adUnitId: adUnitId });
  228. this._wxInterstitialAd.onLoad(() => {
  229. this.loading = false
  230. this.triggerEvent(EventType.Load, {})
  231. if (this._userInvokeShowFlag) {
  232. this._userInvokeShowFlag = false;
  233. this._wxInterstitialAd.show();
  234. }
  235. });
  236. this._wxInterstitialAd.onError(err => {
  237. this.loading = false
  238. this.triggerEvent(EventType.Error, err);
  239. });
  240. this._wxInterstitialAd.onClose(res => {
  241. this.triggerEvent(EventType.Close, res);
  242. });
  243. this._wxInterstitialAd?.load().then(() => { }).catch((err) => { });
  244. this.loading = true
  245. }
  246. }
  247. })