Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
qianlong66
uni-app
提交
e9a4959d
U
uni-app
项目概览
qianlong66
/
uni-app
与 Fork 源项目一致
Fork自
DCloud / uni-app
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
U
uni-app
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
e9a4959d
编写于
7月 28, 2021
作者:
d-u-a
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
feat(App): ad
上级
14514ad0
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
166 addition
and
3 deletion
+166
-3
packages/uni-app-plus/src/service/framework/app/subscriber/ad.ts
...s/uni-app-plus/src/service/framework/app/subscriber/ad.ts
+42
-2
packages/uni-app-plus/src/view/components/ad/index.tsx
packages/uni-app-plus/src/view/components/ad/index.tsx
+124
-1
未找到文件。
packages/uni-app-plus/src/service/framework/app/subscriber/ad.ts
浏览文件 @
e9a4959d
import
{
registerServiceMethod
}
from
'
@dcloudio/uni-core
'
const
_adDataCache
:
Record
<
string
,
any
>
=
{}
function
getAdData
(
data
:
any
,
onsuccess
:
Function
,
onerror
:
Function
)
{
const
{
adpid
,
width
}
=
data
const
key
=
adpid
+
'
-
'
+
width
const
adDataList
=
_adDataCache
[
key
]
if
(
adDataList
&&
adDataList
.
length
>
0
)
{
onsuccess
(
adDataList
.
splice
(
0
,
1
)[
0
])
return
}
plus
.
ad
.
getAds
(
data
,
(
res
)
=>
{
const
list
=
res
.
ads
onsuccess
(
list
.
splice
(
0
,
1
)[
0
])
_adDataCache
[
key
]
=
adDataList
?
adDataList
.
concat
(
list
)
:
list
},
(
err
)
=>
{
onerror
({
errCode
:
err
.
code
,
errMsg
:
err
.
message
,
})
}
)
}
export
function
subscribeAd
()
{
// view 层通过 UniViewJSBridge.invokeServiceMethod('getAdData', args, function({data})=>{console.log(data)})
registerServiceMethod
(
'
getAdData
'
,
(
args
,
resolve
)
=>
{
// TODO args: view 层传输的参数,处理完之后,resolve:回传给 service,如resolve({data})
getAdData
(
args
,
(
res
:
any
)
=>
{
resolve
({
code
:
0
,
data
:
res
,
})
},
(
err
:
any
)
=>
{
resolve
({
code
:
1
,
message
:
err
,
})
}
)
})
}
packages/uni-app-plus/src/view/components/ad/index.tsx
浏览文件 @
e9a4959d
import
{
defineBuiltInComponent
}
from
'
@dcloudio/uni-components
'
import
{
Ref
,
ref
,
watch
,
onBeforeUnmount
}
from
'
vue
'
import
{
defineBuiltInComponent
,
useCustomEvent
,
EmitEvent
,
}
from
'
@dcloudio/uni-components
'
import
{
useNativeAttrs
,
useNative
}
from
'
../../../helpers/useNative
'
export
default
/*#__PURE__*/
defineBuiltInComponent
({
name
:
'
Ad
'
,
props
:
{
adpid
:
{
type
:
[
Number
,
String
],
default
:
''
,
},
data
:
{
type
:
Object
,
default
:
null
,
},
dataCount
:
{
type
:
Number
,
default
:
5
,
},
channel
:
{
type
:
String
,
default
:
''
,
},
},
setup
(
props
,
{
emit
})
{
const
rootRef
:
Ref
<
HTMLElement
|
null
>
=
ref
(
null
)
const
containerRef
:
Ref
<
HTMLElement
|
null
>
=
ref
(
null
)
const
trigger
=
useCustomEvent
<
EmitEvent
<
typeof
emit
>>
(
rootRef
,
emit
)
const
attrs
=
useNativeAttrs
(
props
,
[
'
id
'
])
const
{
position
,
onParentReady
}
=
useNative
(
containerRef
)
let
adView
:
ReturnType
<
typeof
plus
.
ad
.
createAdView
>
onParentReady
(()
=>
{
adView
=
plus
.
ad
.
createAdView
(
Object
.
assign
({},
attrs
.
value
,
position
))
plus
.
webview
.
currentWebview
().
append
(
adView
as
any
)
adView
.
setDislikeListener
((
data
)
=>
{
;(
containerRef
.
value
as
HTMLElement
).
style
.
height
=
'
0
'
window
.
dispatchEvent
(
new
CustomEvent
(
'
updateview
'
))
trigger
(
'
close
'
,
{}
as
Event
,
data
)
})
adView
.
setRenderingListener
((
data
)
=>
{
if
(
data
.
result
===
0
)
{
;(
containerRef
.
value
as
HTMLElement
).
style
.
height
=
data
.
height
+
'
px
'
window
.
dispatchEvent
(
new
CustomEvent
(
'
updateview
'
))
}
else
{
trigger
(
'
error
'
,
{}
as
Event
,
{
errCode
:
data
.
result
,
})
}
})
adView
.
setAdClickedListener
(()
=>
{
trigger
(
'
adclicked
'
,
{}
as
Event
,
{})
})
watch
(
()
=>
position
,
(
position
)
=>
adView
.
setStyle
(
position
),
{
deep
:
true
}
)
watch
(
()
=>
props
.
adpid
,
(
val
)
=>
{
if
(
val
)
{
loadData
()
}
}
)
watch
(
()
=>
props
.
data
,
(
val
)
=>
{
if
(
val
)
{
adView
.
renderingBind
(
val
)
}
}
)
function
loadData
()
{
let
args
=
{
adpid
:
props
.
adpid
,
width
:
position
.
width
,
count
:
props
.
dataCount
,
}
if
(
props
.
channel
!==
undefined
)
{
;(
args
as
any
).
ext
=
{
channel
:
props
.
channel
,
}
}
UniViewJSBridge
.
invokeServiceMethod
(
'
getAdData
'
,
args
,
({
code
,
data
,
message
})
=>
{
if
(
code
===
0
)
{
adView
.
renderingBind
(
data
)
}
else
{
trigger
(
'
error
'
,
{}
as
Event
,
{
errMsg
:
message
,
})
}
}
)
}
if
(
props
.
adpid
)
{
loadData
()
}
})
onBeforeUnmount
(()
=>
{
if
(
adView
)
{
adView
.
close
()
}
})
return
()
=>
{
return
(
<
uni
-
ad
ref
=
{
rootRef
}
>
<
div
ref
=
{
containerRef
}
class
=
"uni-ad-container"
/>
</
uni
-
ad
>
)
}
},
})
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录