Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
A
activity-core
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
liupengfei
activity-core
Commits
18d81d7b
Commit
18d81d7b
authored
Jul 20, 2020
by
liupengfei
Browse files
Options
Browse Files
Download
Plain Diff
栗子
parents
56d673ea
047b16e0
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
177 additions
and
516 deletions
+177
-516
index.js
src/couponModal/index.js
+37
-138
index.js
src/handleDojoin/index.js
+46
-160
index.js
src/services/index.js
+20
-69
index.js
src/utils/index.js
+6
-74
index.js
test/tapable-test/index.js
+68
-75
No files found.
src/couponModal/index.js
View file @
18d81d7b
/*
/*
* @Author: flyharvest
* @Author: flyharvest
* @Date: 2020-07-15 15:50:45
* @Date: 2020-07-15 15:50:45
* @LastEditTime: 2020-07-
19 14:09:11
* @LastEditTime: 2020-07-
20 15:38:44
* @LastEditors: flyharvest
* @LastEditors: flyharvest
*/
*/
/* eslint-disable*/
/* eslint-disable*/
import
$
from
'jquery'
import
$
from
'jquery'
import
{
normallizeOptions
,
normallizeEvent
,
WATERFALL
,
ASYNCWATERFALL
}
from
'@lib/utils'
import
{
createHooks
}
from
'@lib/utils'
import
'./index.less'
import
'./index.less'
class
CouponModal
{
class
CouponModal
{
constructor
(
opts
)
{
constructor
()
{
this
.
hooksDefine
=
[
this
.
hooksDefine
=
[
{
name
:
'confirmClick'
,
type
:
1
}
,
'confirmClick'
,
{
name
:
'closeClick'
,
type
:
1
}
,
'closeClick'
,
{
name
:
'afterClose'
,
type
:
1
}
,
'afterClose'
,
{
name
:
'contentClose'
,
type
:
1
}
,
'contentClick'
,
{
name
:
'replaceHtml'
,
type
:
2
}
,
'replaceHtml'
,
{
name
:
'getDom'
,
type
:
4
}
'modifyDom'
]
]
this
.
hooks
=
normallizeOptions
(
opts
,
this
.
hooksDefine
)
this
.
hooks
=
createHooks
(
this
.
hooksDefine
)
this
.
allHooksName
=
this
.
getHooksName
(
this
.
hooksDefine
)
this
.
isShow
=
false
this
.
isShow
=
false
this
.
el
=
null
this
.
el
=
null
this
.
resolve
=
null
this
.
resolve
=
null
this
.
showPromsie
=
null
this
.
showPromsie
=
null
}
}
getHooksName
(
hooksDefine
)
{
return
hooksDefine
.
map
(
e
=>
e
.
name
)
}
init
()
{
const
dom
=
`
<div class="wrapper">
<div class="content">广告</div>
<div class="close">关闭</div>
<div class="confirm">领奖</div>
</div>
`
return
Promise
.
resolve
(
dom
)
}
show
(
data
,
opts
=
{})
{
this
.
tempHooks
=
normallizeOptions
(
opts
,
this
.
hooksDefine
)
this
.
data
=
data
this
.
showPromise
=
new
Promise
(
resolve
=>
{
this
.
resolve
=
resolve
})
const
domResolve
=
(
content
)
=>
{
return
this
.
resolve
(
Object
.
assign
({
isCustom
:
true
},
content
))
}
// 不用默认的弹层,替换自定义弹层。
this
.
hooks
.
getDom
({
couponModalResolve
:
domResolve
})
.
then
(
mid
=>
{
console
.
log
(
mid
)
this
.
tempHooks
.
getDom
(
mid
)
.
then
(
res
=>
{
console
.
log
(
res
)
if
(
res
.
isReplaced
)
{
//
}
else
{
this
.
init
(
data
)
.
then
(
dom
=>
{
this
.
el
=
$
(
dom
)
let
mid
=
this
.
hooks
.
replaceHtml
(
this
.
el
)
this
.
el
=
this
.
tempHooks
.
replaceHtml
(
mid
)
this
.
events
()
$
(
'body'
).
append
(
this
.
el
)
})
}
})
})
return
this
.
showPromise
}
events
()
{
const
confirm
=
$
(
this
.
el
).
find
(
'.confirm'
)
const
close
=
$
(
this
.
el
).
find
(
'.close'
)
const
content
=
$
(
this
.
el
).
find
(
'.content'
)
$
(
content
).
on
(
'click'
,
()
=>
{
const
mid
=
this
.
hooks
.
contentClick
()
const
res
=
this
.
tempHooks
.
contentClick
(
mid
)
if
(
res
===
true
)
{
this
.
close
(
'contentClick'
)
}
})
$
(
confirm
).
on
(
'click'
,
()
=>
{
const
mid
=
this
.
hooks
.
confirmClick
()
const
res
=
this
.
tempHooks
.
confirmClick
(
mid
)
if
(
res
===
true
||
res
===
undefined
)
{
this
.
close
(
'confirmClick'
)
}
})
$
(
close
).
on
(
'click'
,
()
=>
{
const
mid
=
this
.
hooks
.
closeClick
()
const
res
=
this
.
tempHooks
.
closeClick
(
mid
)
if
(
res
===
true
||
res
===
undefined
)
{
this
.
close
(
'closeClick'
)
}
})
}
close
(
source
)
{
this
.
el
.
remove
()
this
.
resolve
({
type
:
source
,
data
:
this
.
data
})
}
}
class
CouponModalEvent
{
constructor
(
opts
)
{
this
.
hooksDefine
=
[
{
name
:
'confirmClick'
,
type
:
WATERFALL
},
{
name
:
'closeClick'
,
type
:
WATERFALL
},
{
name
:
'afterClose'
,
type
:
WATERFALL
},
{
name
:
'contentClose'
,
type
:
WATERFALL
},
{
name
:
'replaceHtml'
,
type
:
WATERFALL
},
{
name
:
'getDom'
,
type
:
ASYNCWATERFALL
}
]
this
.
hooks
=
normallizeEvent
(
this
.
hooksDefine
)
this
.
allHooksName
=
this
.
getHooksName
(
this
.
hooksDefine
)
this
.
isShow
=
false
this
.
el
=
null
this
.
resolve
=
null
this
.
showPromsie
=
null
}
getHooksName
(
hooksDefine
)
{
return
hooksDefine
.
map
(
e
=>
e
.
name
)
}
init
()
{
init
()
{
const
dom
=
`
const
dom
=
`
<div class="wrapper">
<div class="wrapper">
...
@@ -144,7 +36,7 @@ class CouponModalEvent {
...
@@ -144,7 +36,7 @@ class CouponModalEvent {
`
`
return
Promise
.
resolve
(
dom
)
return
Promise
.
resolve
(
dom
)
}
}
s
how
(
data
)
{
s
tart
(
data
)
{
this
.
data
=
data
this
.
data
=
data
this
.
showPromise
=
new
Promise
(
resolve
=>
{
this
.
showPromise
=
new
Promise
(
resolve
=>
{
this
.
resolve
=
resolve
this
.
resolve
=
resolve
...
@@ -155,18 +47,20 @@ class CouponModalEvent {
...
@@ -155,18 +47,20 @@ class CouponModalEvent {
},
content
))
},
content
))
}
}
// 不用默认的弹层,替换自定义弹层。
// 不用默认的弹层,替换自定义弹层。
this
.
hooks
.
getDom
.
promise
({
couponModalResolve
:
domResolve
})
this
.
hooks
.
replaceHtml
.
promise
({
couponModalResolve
:
domResolve
})
.
then
(
res
=>
{
.
then
(
res
=>
{
if
(
res
.
isReplaced
)
{
if
(
res
&&
res
.
isReplaced
)
{
}
else
{
}
else
{
this
.
init
(
data
)
this
.
init
(
data
)
.
then
(
dom
=>
{
.
then
(
dom
=>
{
this
.
el
=
$
(
dom
)
this
.
el
=
$
(
dom
)
this
.
el
=
this
.
hooks
.
replaceHtml
.
call
(
this
.
el
)
this
.
hooks
.
modifyDom
.
promise
({
el
:
this
.
el
})
.
then
(()
=>
{
this
.
events
()
this
.
events
()
$
(
'body'
).
append
(
this
.
el
)
$
(
'body'
).
append
(
this
.
el
)
})
})
})
}
}
})
})
return
this
.
showPromise
return
this
.
showPromise
...
@@ -178,25 +72,31 @@ class CouponModalEvent {
...
@@ -178,25 +72,31 @@ class CouponModalEvent {
const
content
=
$
(
this
.
el
).
find
(
'.content'
)
const
content
=
$
(
this
.
el
).
find
(
'.content'
)
$
(
content
).
on
(
'click'
,
()
=>
{
$
(
content
).
on
(
'click'
,
()
=>
{
let
res
=
this
.
hooks
.
closeClick
.
call
()
this
.
hooks
.
closeClick
.
promise
()
.
then
(
res
=>
{
if
(
res
===
true
)
{
if
(
res
===
true
)
{
this
.
close
(
'contentClick'
)
this
.
close
(
'contentClick'
)
}
}
})
})
})
$
(
confirm
).
on
(
'click'
,
()
=>
{
$
(
confirm
).
on
(
'click'
,
()
=>
{
let
res
=
this
.
hooks
.
closeClick
.
call
()
this
.
hooks
.
closeClick
.
promise
()
.
then
(
res
=>
{
if
(
res
===
true
||
res
===
undefined
)
{
if
(
res
===
true
||
res
===
undefined
)
{
this
.
close
(
'confirmClick'
)
this
.
close
(
'confirmClick'
)
}
}
})
})
})
$
(
close
).
on
(
'click'
,
()
=>
{
$
(
close
).
on
(
'click'
,
()
=>
{
let
res
=
this
.
hooks
.
closeClick
.
call
()
this
.
hooks
.
closeClick
.
promise
()
.
then
(
res
=>
{
if
(
res
===
true
||
res
===
undefined
)
{
if
(
res
===
true
||
res
===
undefined
)
{
this
.
close
(
'closeClick'
)
this
.
close
(
'closeClick'
)
}
}
})
})
})
}
}
close
(
source
)
{
close
(
source
)
{
...
@@ -210,6 +110,5 @@ class CouponModalEvent {
...
@@ -210,6 +110,5 @@ class CouponModalEvent {
}
}
export
{
export
{
CouponModal
,
CouponModal
CouponModalEvent
}
}
src/handleDojoin/index.js
View file @
18d81d7b
...
@@ -2,13 +2,11 @@
...
@@ -2,13 +2,11 @@
/*
/*
* @Author: flyharvest
* @Author: flyharvest
* @Date: 2020-07-15 11:38:44
* @Date: 2020-07-15 11:38:44
* @LastEditTime: 2020-07-
19 13:56:30
* @LastEditTime: 2020-07-
20 16:54:19
* @LastEditors: flyharvest
* @LastEditors: flyharvest
*/
*/
import
{
import
{
normallizeOptions
,
createHooks
normallizeEvent
,
WATERFALL
}
from
'@lib/utils'
}
from
'@lib/utils'
// mock 是否需要增值
// mock 是否需要增值
...
@@ -19,240 +17,128 @@ function enable () {
...
@@ -19,240 +17,128 @@ function enable () {
const
source
=
'handleDojoin'
const
source
=
'handleDojoin'
class
HandleDojoin
{
class
HandleDojoin
{
constructor
(
opts
=
{})
{
constructor
()
{
this
.
hooksDefine
=
[
this
.
hooksDefine
=
[
'mediaMsg'
,
'afterHandleDojoin'
]
{
name
:
'mediaMsg'
,
type
:
1
}
this
.
hooks
=
createHooks
(
this
.
hooksDefine
)
]
this
.
hooks
=
normallizeOptions
(
opts
,
[
{
name
:
'mediaMsg'
,
type
:
1
}
])
this
.
allHooksName
=
this
.
getHooksName
(
this
.
hooksDefine
)
}
}
getHooksName
(
hooksDefine
)
{
start
(
res
)
{
return
hooksDefine
.
map
(
e
=>
e
.
name
)
}
handle
(
res
,
opts
=
{})
{
this
.
tempHooks
=
normallizeOptions
(
opts
,
this
.
hooksDefine
)
// 声明自己的promise
// 声明自己的promise
let
solve
let
solve
const
handlePromise
=
new
Promise
(
resolve
=>
{
const
handlePromise
=
new
Promise
(
resolve
=>
{
solve
=
resolve
solve
=
resolve
})
})
const
{
success
,
data
,
code
}
=
res
const
{
success
,
data
,
code
}
=
res
let
modalType
=
{}
let
mediaMsg
=
''
if
(
success
)
{
if
(
success
)
{
const
{
result
}
=
data
const
{
result
}
=
data
const
{
lottery
,
creditsConsumeAlertMsg
}
=
result
const
{
lottery
,
creditsConsumeAlertMsg
}
=
result
creditsConsumeAlertMsg
&&
this
.
hooks
.
mediaMsg
(
creditsConsumeAlertMsg
)
&&
this
.
tempHooks
.
mediaMsg
(
creditsConsumeAlertMsg
)
mediaMsg
=
creditsConsumeAlertMsg
if
(
enable
()
&&
[
'lucky'
,
'thanks'
,
'coupon'
].
includes
(
lottery
.
type
))
{
if
(
enable
()
&&
[
'lucky'
,
'thanks'
,
'coupon'
].
includes
(
lottery
.
type
))
{
solve
(
{
modalType
=
{
type
:
'showIncitve'
,
type
:
'showIncitve'
,
data
:
data
,
data
:
data
,
source
source
}
)
}
}
else
{
}
else
{
switch
(
lottery
.
type
)
{
switch
(
lottery
.
type
)
{
case
'lucky'
:
case
'lucky'
:
case
'coupon'
:
case
'coupon'
:
solve
(
{
modalType
=
{
type
:
'showCoupon'
,
type
:
'showCoupon'
,
data
,
data
,
source
source
}
)
}
break
break
case
'thanks'
:
case
'thanks'
:
solve
(
{
modalType
=
{
type
:
'showThanks'
,
type
:
'showThanks'
,
data
,
data
,
source
source
}
)
}
break
break
case
'alipay'
:
case
'alipay'
:
solve
(
{
modalType
=
{
type
:
'showAlipay'
,
type
:
'showAlipay'
,
data
,
data
,
source
source
}
)
}
break
break
case
'qb'
:
case
'qb'
:
solve
(
{
modalType
=
{
type
:
'showQb'
,
type
:
'showQb'
,
data
,
data
,
source
source
}
)
}
break
break
case
'bill'
:
case
'bill'
:
solve
(
{
modalType
=
{
type
:
'showBill'
,
type
:
'showBill'
,
data
,
data
,
source
source
}
)
}
break
break
case
'virtual'
:
case
'virtual'
:
solve
(
{
modalType
=
{
type
:
'showVirtual'
,
type
:
'showVirtual'
,
data
,
data
,
source
source
}
)
}
break
break
case
'physical'
:
case
'physical'
:
solve
(
{
modalType
=
{
type
:
'showVirtual'
,
type
:
'showVirtual'
,
data
,
data
,
source
source
}
)
}
break
break
default
:
default
:
solve
(
{
modalType
=
{
type
:
'showEror'
,
type
:
'showEror'
,
data
,
data
,
source
source
}
)
}
break
break
}
}
}
}
}
else
if
(
code
===
'0100001'
)
{
}
else
if
(
code
===
'0100001'
)
{
solve
(
{
modalType
=
{
type
:
'showRecomend'
,
type
:
'showRecomend'
,
data
,
data
,
source
source
}
)
}
}
else
if
(
code
===
'0200004'
)
{
}
else
if
(
code
===
'0200004'
)
{
solve
(
{
modalType
=
{
type
:
'showPreview'
,
type
:
'showPreview'
,
data
,
data
,
source
source
})
}
else
{
solve
({
type
:
'showError'
,
data
,
source
})
}
}
return
handlePromise
}
}
class
HandleDojoinEvent
{
constructor
(
opts
=
{})
{
this
.
hooksDefine
=
[
{
name
:
'mediaMsg'
,
type
:
WATERFALL
}
]
this
.
hooks
=
normallizeEvent
([
{
name
:
'mediaMsg'
,
type
:
WATERFALL
}
])
this
.
allHooksName
=
this
.
getHooksName
(
this
.
hooksDefine
)
}
getHooksName
(
hooksDefine
)
{
return
hooksDefine
.
map
(
e
=>
e
.
name
)
}
handle
(
res
)
{
// 声明自己的promise
let
solve
const
handlePromise
=
new
Promise
(
resolve
=>
{
solve
=
resolve
})
const
{
success
,
data
,
code
}
=
res
if
(
success
)
{
const
{
result
}
=
data
const
{
lottery
,
creditsConsumeAlertMsg
}
=
result
creditsConsumeAlertMsg
&&
this
.
hooks
.
mediaMsg
(
creditsConsumeAlertMsg
)
if
(
enable
()
&&
[
'lucky'
,
'thanks'
,
'coupon'
].
includes
(
lottery
.
type
))
{
solve
({
type
:
'showIncitve'
,
data
:
data
,
source
})
}
else
{
}
else
{
switch
(
lottery
.
type
)
{
modalType
=
{
case
'lucky'
:
type
:
'showError'
,
case
'coupon'
:
solve
({
type
:
'showCoupon'
,
data
,
source
})
break
case
'thanks'
:
solve
({
type
:
'showThanks'
,
data
,
source
})
break
case
'alipay'
:
solve
({
type
:
'showAlipay'
,
data
,
source
})
break
case
'qb'
:
solve
({
type
:
'showQb'
,
data
,
source
})
break
case
'bill'
:
solve
({
type
:
'showBill'
,
data
,
source
})
break
case
'virtual'
:
solve
({
type
:
'showVirtual'
,
data
,
source
})
break
case
'physical'
:
solve
({
type
:
'showVirtual'
,
data
,
source
})
break
default
:
solve
({
type
:
'showEror'
,
data
,
data
,
source
source
})
break
}
}
}
}
}
else
if
(
code
===
'0100001'
)
{
if
(
mediaMsg
)
{
solve
({
return
this
.
hooks
.
mediaMsg
.
promise
(
mediaMsg
)
type
:
'showRecomend'
,
.
then
(()
=>
{
data
,
this
.
hooks
.
afterHandleDojoin
.
promise
(
modalType
)
source
.
then
(
res
=>
{
solve
(
res
)
})
})
}
else
if
(
code
===
'0200004'
)
{
solve
({
type
:
'showPreview'
,
data
,
source
})
})
}
else
{
}
else
{
solve
({
this
.
hooks
.
afterHandleDojoin
.
promise
(
modalType
)
type
:
'showError'
,
.
then
(
res
=>
{
data
,
solve
(
res
)
source
})
})
}
}
return
handlePromise
return
handlePromise
}
}
}
}
export
{
export
{
HandleDojoin
,
HandleDojoin
HandleDojoinEvent
}
}
src/services/index.js
View file @
18d81d7b
...
@@ -2,10 +2,10 @@
...
@@ -2,10 +2,10 @@
/*
/*
* @Author: flyharvest
* @Author: flyharvest
* @Date: 2020-07-15 09:46:20
* @Date: 2020-07-15 09:46:20
* @LastEditTime: 2020-07-
19 12:41:12
* @LastEditTime: 2020-07-
20 15:43:40
* @LastEditors: flyharvest
* @LastEditors: flyharvest
*/
*/
import
{
normallizeOptions
,
CustomError
,
WATERFALL
,
normallizeEvent
}
from
'@lib/utils'
import
{
createHooks
}
from
'@lib/utils'
var
http
=
null
var
http
=
null
var
params
=
{}
var
params
=
{}
...
@@ -18,82 +18,33 @@ function use (ajax, opts = {}) {
...
@@ -18,82 +18,33 @@ function use (ajax, opts = {}) {
}
}
class
DoJoin
{
class
DoJoin
{
constructor
(
opts
=
{})
{
constructor
()
{
this
.
hooksDefine
=
[
this
.
hooksDefine
=
[
'beforeDojoin'
,
'afterDojoin'
]
{
this
.
hooks
=
createHooks
(
this
.
hooksDefine
)
name
:
'beforeDojoin'
,
type
:
1
}
]
this
.
hooks
=
normallizeOptions
(
opts
,
this
.
hooksDefine
)
this
.
allHooksName
=
this
.
getHooksName
(
this
.
hooksDefine
)
}
getHooksName
(
hooksDefine
)
{
return
hooksDefine
.
map
(
e
=>
e
.
name
)
}
start
(
opts
=
{})
{
this
.
tempHooks
=
normallizeOptions
(
opts
,
this
.
hooksDefine
)
let
data
=
{
ownData
:
'测试'
}
// 请求前给默认配置的钩子
const
mid
=
this
.
hooks
.
beforeDojoin
(
data
)
// 给临时传递进来的钩子
data
=
this
.
tempHooks
.
beforeDojoin
(
mid
)
return
http
.
get
(
'/activity/doJoin'
,
{
data
}).
then
(
res
=>
{
return
res
}).
catch
((
err
)
=>
{
return
Promise
.
reject
(
new
CustomError
({
name
:
'toast'
,
message
:
'获取网络失败'
,
source
:
'services'
,
raw
:
err
}))
})
}
}
class
DoJoinEvent
{
constructor
()
{
this
.
hooksDefine
=
[
{
name
:
'beforeDojoin'
,
type
:
WATERFALL
}
]
this
.
hooks
=
normallizeEvent
(
this
.
hooksDefine
)
this
.
allHooksName
=
this
.
getHooksName
(
this
.
hooksDefine
)
}
getHooksName
(
hooksDefine
)
{
return
hooksDefine
.
map
(
e
=>
e
.
name
)
}
}
start
()
{
start
()
{
let
data
=
{
let
data
=
{
ownData
:
'测试'
ownData
:
'测试'
}
}
data
=
this
.
hooks
.
beforeDojoin
.
call
(
data
)
console
.
log
(
this
.
hooks
.
beforeDojoin
)
return
http
.
get
(
'/activity/doJoin'
,
{
data
}).
then
(
res
=>
{
return
this
.
hooks
.
beforeDojoin
.
promise
(
data
)
return
res
.
then
(
beforeDojoin
=>
{
}).
catch
((
err
)
=>
{
data
=
beforeDojoin
?
beforeDojoin
:
data
return
Promise
.
reject
(
new
CustomError
({
return
http
.
get
(
'/activity/doJoin'
,
{
data
})
name
:
'toast'
,
.
then
(
doJoinResult
=>
{
message
:
'获取网络失败'
,
return
this
.
hooks
.
afterDojoin
.
promise
(
doJoinResult
)
source
:
'services'
,
.
then
(
afterDojoin
=>
{
raw
:
err
console
.
log
(
afterDojoin
)
}))
return
afterDojoin
?
afterDojoin
:
doJoinResult
})
})
})
})
}
}
}
}
const
services
=
{
const
services
=
{
use
,
use
,
DoJoin
,
DoJoin
DoJoinEvent
}
}
export
default
services
export
default
services
src/utils/index.js
View file @
18d81d7b
/*
/*
* @Author: flyharvest
* @Author: flyharvest
* @Date: 2020-07-15 09:51:43
* @Date: 2020-07-15 09:51:43
* @LastEditTime: 2020-07-
19 16:25:44
* @LastEditTime: 2020-07-
20 15:25:57
* @LastEditors: flyharvest
* @LastEditors: flyharvest
*/
*/
import
{
SyncWaterfallHook
,
AsyncSeriesWaterfallHook
}
from
'tapable'
import
{
AsyncSeriesBailHook
}
from
'tapable'
const
WATERFALL
=
'SyncWaterfallHook'
const
ASYNCWATERFALL
=
'AsyncSeriesWaterfallHook'
function
isObject
(
obj
)
{
function
isObject
(
obj
)
{
return
Object
.
prototype
.
toString
.
call
(
obj
)
===
'[object Object]'
return
Object
.
prototype
.
toString
.
call
(
obj
)
===
'[object Object]'
...
@@ -18,73 +16,10 @@ function isFn (fn) {
...
@@ -18,73 +16,10 @@ function isFn (fn) {
function
noop
(
data
)
{
return
data
}
function
noop
(
data
)
{
return
data
}
/**
function
createHooks
(
hooksDefine
=
[])
{
* @description: 规范钩子。
* 1. 如果是同步钩子,没有或者不是函数就使用noop。
* 没有返回值,就返回入参
* 2. 如果是异步钩子,没有或者不是函数就直接返回参数。
* 没有then方法,包裹一层promise
* @example:
* @param {type}
* @return:
*/
function
normallizeOptions
(
hookOpts
=
{},
hooksDefine
=
[])
{
if
(
!
isObject
(
hookOpts
))
{
hookOpts
=
{}
}
const
hooks
=
{}
hooksDefine
.
forEach
(
hook
=>
{
const
fn
=
hookOpts
[
hook
.
name
]
switch
(
hook
.
type
)
{
// 同步钩子,且忽略返回值
case
1
:
case
2
:
if
(
!
fn
||
!
isFn
(
fn
))
{
hooks
[
hook
.
name
]
=
noop
}
else
{
hooks
[
hook
.
name
]
=
(
playload
)
=>
{
let
result
=
fn
(
playload
)
result
=
result
===
undefined
?
playload
:
result
return
result
===
undefined
?
playload
:
result
}
}
break
case
3
:
case
4
:
if
(
fn
&&
isFn
(
fn
)
&&
fn
.
then
)
{
hooks
[
hook
.
name
]
=
fn
}
else
if
(
fn
&&
isFn
(
fn
)
&&
!
fn
.
then
)
{
hooks
[
hook
.
name
]
=
(
playload
)
=>
{
let
result
=
fn
(
playload
)
result
=
result
===
undefined
?
playload
:
result
return
Promise
.
resolve
(
result
)
}
}
else
{
hooks
[
hook
.
name
]
=
(
data
)
=>
{
return
Promise
.
resolve
(
data
)
}
}
break
default
:
break
}
})
return
hooks
}
function
normallizeEvent
(
hooksDefine
=
[])
{
const
hooks
=
{}
const
hooks
=
{}
hooksDefine
.
forEach
(
hook
=>
{
hooksDefine
.
forEach
(
name
=>
{
switch
(
hook
.
type
)
{
hooks
[
name
]
=
new
AsyncSeriesBailHook
([
'data'
])
case
WATERFALL
:
hooks
[
hook
.
name
]
=
new
SyncWaterfallHook
([
'data'
])
break
case
ASYNCWATERFALL
:
hooks
[
hook
.
name
]
=
new
AsyncSeriesWaterfallHook
([
'data'
])
break
default
:
break
}
})
})
return
hooks
return
hooks
}
}
...
@@ -102,10 +37,7 @@ CustomError.prototype.constructor = CustomError
...
@@ -102,10 +37,7 @@ CustomError.prototype.constructor = CustomError
export
{
export
{
isFn
,
isFn
,
isObject
,
isObject
,
normallizeOptions
,
noop
,
noop
,
CustomError
,
CustomError
,
normallizeEvent
,
createHooks
WATERFALL
,
ASYNCWATERFALL
}
}
test/tapable-test/index.js
View file @
18d81d7b
...
@@ -2,15 +2,15 @@
...
@@ -2,15 +2,15 @@
/*
/*
* @Author: flyharvest
* @Author: flyharvest
* @Date: 2020-07-14 14:09:40
* @Date: 2020-07-14 14:09:40
* @LastEditTime: 2020-07-20 17:1
3:01
* @LastEditTime: 2020-07-20 17:1
4:24
* @LastEditors: flyharvest
* @LastEditors: flyharvest
*/
*/
import
*
as
rem
from
'@lib/flexible'
import
*
as
rem
from
'@lib/flexible'
import
{
ajax
}
from
'@lib/xhr'
import
{
ajax
}
from
'@lib/xhr'
import
services
from
'@lib/services'
import
services
from
'@lib/services'
import
{
HandleDojoin
Event
}
from
'@lib/handleDojoin'
import
{
HandleDojoin
}
from
'@lib/handleDojoin'
import
{
CouponModal
Event
}
from
'@lib/couponModal'
import
{
CouponModal
}
from
'@lib/couponModal'
import
{
WATERFALL
,
isFn
}
from
'@lib/utils'
import
{
isFn
}
from
'@lib/utils'
import
$
from
'jquery'
import
$
from
'jquery'
import
'./index.less'
import
'./index.less'
...
@@ -18,56 +18,46 @@ rem.setViewPort()
...
@@ -18,56 +18,46 @@ rem.setViewPort()
rem
.
init
(
750
,
750
)
rem
.
init
(
750
,
750
)
services
.
use
(
ajax
)
services
.
use
(
ajax
)
const
Do
JoinEvent
=
services
.
DoJoinEvent
const
Do
join
=
services
.
DoJoin
const
dojoin
=
new
Do
JoinEvent
()
const
dojoin
=
new
Do
join
()
const
couponModal
=
new
CouponModal
Event
()
const
couponModal
=
new
CouponModal
()
const
handleDojoin
=
new
HandleDojoin
Event
()
const
handleDojoin
=
new
HandleDojoin
()
const
handOut
=
(
opts
,
sign
)
=>
{
const
handOut
=
(
opts
,
sign
,
external
=
{}
)
=>
{
const
steam
=
[
dojoin
,
handleDojoin
,
couponModal
]
const
steam
=
[
dojoin
,
handleDojoin
,
couponModal
]
steam
.
forEach
(
e
=>
{
steam
.
forEach
(
e
=>
{
e
.
hooksDefine
.
forEach
(
hooksDesc
=>
{
e
.
hooksDefine
.
forEach
(
name
=>
{
if
(
hooksDesc
.
type
===
WATERFALL
)
{
const
cb
=
(
data
)
=>
{
const
cb
=
(
data
)
=>
{
const
fn
=
opts
[
hooksDesc
.
name
]
const
fn
=
opts
[
name
]
if
(
fn
&&
isFn
(
fn
))
{
return
fn
(
data
)
}
}
e
.
hooks
[
hooksDesc
.
name
].
tap
(
sign
,
cb
)
}
else
{
const
cb
=
(
data
)
=>
{
const
fn
=
opts
[
hooksDesc
.
name
]
if
(
fn
&&
isFn
(
fn
)
&&
fn
.
then
)
{
if
(
fn
&&
isFn
(
fn
)
&&
fn
.
then
)
{
return
fn
(
data
)
return
fn
(
data
,
external
[
name
]
)
}
else
if
(
fn
&&
isFn
(
fn
))
{
}
else
if
(
fn
&&
isFn
(
fn
))
{
return
Promise
.
resolve
(
fn
(
data
))
return
Promise
.
resolve
(
fn
(
data
,
external
[
name
]
))
}
else
{
}
else
{
return
Promise
.
resolve
(
data
)
return
Promise
.
resolve
()
}
}
}
e
.
hooks
[
hooksDesc
.
name
].
tapPromise
(
sign
,
cb
)
}
}
e
.
hooks
[
name
].
tapPromise
(
sign
,
cb
)
})
})
})
})
}
}
const
coreMethods
=
{
const
coreMethods
=
{
beforeDojoin
()
{
beforeDojoin
(
data
)
{
console
.
log
(
'我被调用-core'
)
return
{
return
{
haha
:
123123
haha
:
123123
}
}
},
},
mediaMsg
(
media
)
{
mediaMsg
()
{
console
.
log
(
media
+
'ss'
)
console
.
log
(
media
+
'ss'
)
},
},
replaceHtml
(
data
)
{
modifyDom
({
el
})
{
console
.
log
(
1
)
$
(
el
).
find
(
'.content'
).
css
({
$
(
data
).
find
(
'.content'
).
css
({
background
:
'red'
background
:
'red'
})
})
}
}
...
@@ -81,20 +71,28 @@ const coreMethods = {
...
@@ -81,20 +71,28 @@ const coreMethods = {
* @return:
* @return:
*/
*/
const
methods
=
{
const
methods
=
{
replaceHtml
(
data
)
{
beforeDojoin
(
data
)
{
console
.
log
(
'skinDefault'
)
console
.
log
(
'我被调用了-skinName'
)
$
(
data
).
find
(
'.confirm'
).
css
({
return
{
lala
:
'123213'
}
},
modifyDom
({
el
},
fn
)
{
console
.
log
(
fn
)
$
(
el
).
find
(
'.confirm'
).
css
({
background
:
'green'
background
:
'green'
})
})
return
{
el
}
}
}
}
}
handOut
(
methods
,
'skinDefault'
)
handOut
(
methods
,
'skinDefault'
,
coreMethods
)
handOut
(
coreMethods
,
'core'
)
handOut
(
coreMethods
,
'core'
)
let
inited
=
false
let
inited
=
false
const
tempHandOut
=
(
opts
,
sign
)
=>
{
const
tempHandOut
=
(
opts
,
sign
,
external1
=
{},
external2
=
{}
)
=>
{
if
(
inited
)
{
if
(
inited
)
{
tempHandOut
.
setOpts
(
opts
)
tempHandOut
.
setOpts
(
opts
)
return
return
...
@@ -103,34 +101,21 @@ const tempHandOut = (opts, sign) => {
...
@@ -103,34 +101,21 @@ const tempHandOut = (opts, sign) => {
tempHandOut
.
setOpts
(
opts
)
tempHandOut
.
setOpts
(
opts
)
const
steam
=
[
dojoin
,
handleDojoin
,
couponModal
]
const
steam
=
[
dojoin
,
handleDojoin
,
couponModal
]
steam
.
forEach
(
e
=>
{
steam
.
forEach
(
e
=>
{
e
.
hooksDefine
.
forEach
(
hooksDesc
=>
{
e
.
hooksDefine
.
forEach
(
name
=>
{
if
(
hooksDesc
.
type
===
WATERFALL
)
{
const
cb
=
(
data
)
=>
{
const
cb
=
(
data
)
=>
{
const
fn
=
tempHandOut
.
getOpts
()[
hooksDesc
.
name
]
const
fn
=
tempHandOut
.
getOpts
()[
name
]
if
(
fn
&&
isFn
(
fn
))
{
return
fn
(
data
)
}
}
e
.
hooks
[
hooksDesc
.
name
].
tap
({
name
:
sign
,
stage
:
100
},
cb
)
}
else
{
const
cb
=
(
data
)
=>
{
const
fn
=
tempHandOut
.
getOpts
()[
hooksDesc
.
name
]
if
(
fn
&&
isFn
(
fn
)
&&
fn
.
then
)
{
if
(
fn
&&
isFn
(
fn
)
&&
fn
.
then
)
{
return
fn
(
data
)
return
fn
(
data
,
external1
[
name
],
external2
[
name
]
)
}
else
if
(
fn
&&
isFn
(
fn
))
{
}
else
if
(
fn
&&
isFn
(
fn
))
{
return
Promise
.
resolve
(
fn
(
data
))
return
Promise
.
resolve
(
fn
(
data
,
external1
[
name
],
external2
[
name
]
))
}
else
{
}
else
{
return
Promise
.
resolve
(
data
)
return
Promise
.
resolve
(
)
}
}
}
}
e
.
hooks
[
hooksDesc
.
name
].
tapPromise
({
e
.
hooks
[
name
].
tapPromise
({
name
:
sign
,
name
:
sign
,
stage
:
100
stage
:
-
1
},
cb
)
},
cb
)
}
})
})
})
})
}
}
...
@@ -144,17 +129,20 @@ tempHandOut.getOpts = () => {
...
@@ -144,17 +129,20 @@ tempHandOut.getOpts = () => {
const
$doJoin
=
(
opts
=
{})
=>
{
const
$doJoin
=
(
opts
=
{})
=>
{
tempHandOut
(
opts
,
'skinTemp'
)
tempHandOut
(
opts
,
'skinTemp'
)
return
dojoin
.
start
(
)
console
.
log
(
dojoin
)
.
then
(
res
=>
{
return
dojoin
.
start
()
.
then
(
res
=>
{
return
handleDojoin
.
handle
(
res
)
return
handleDojoin
.
start
(
res
)
})
})
.
then
(
res
=>
{
.
then
(
res
=>
{
return
couponModal
.
show
(
res
)
return
couponModal
.
start
(
res
)
})
.
then
((
data
)
=>
{
console
.
log
(
data
)
})
})
}
}
let
times
=
0
let
times
=
0
const
getDom
=
({
couponModalResolve
})
=>
{
const
replaceHtml
=
({
couponModalResolve
})
=>
{
console
.
log
(
++
times
)
console
.
log
(
++
times
)
const
loading
=
$
(
'<div class="loading"></div>'
)
const
loading
=
$
(
'<div class="loading"></div>'
)
loading
.
text
(
'加载中...'
)
loading
.
text
(
'加载中...'
)
...
@@ -175,18 +163,23 @@ const getDom = ({ couponModalResolve }) => {
...
@@ -175,18 +163,23 @@ const getDom = ({ couponModalResolve }) => {
},
2000
)
},
2000
)
})
})
}
}
$doJoin
()
$doJoin
({
beforeDojoin
(
data
)
{
console
.
log
(
'我被调用了-特殊'
)
}
})
.
then
(()
=>
{
.
then
(()
=>
{
return
$doJoin
({
return
$doJoin
({
getDom
replaceHtml
})
})
})
})
.
then
(()
=>
{
.
then
(()
=>
{
$doJoin
({
$doJoin
({
replaceHtml
(
data
)
{
modifyDom
({
el
}
)
{
$
(
data
).
find
(
'.close'
).
css
({
$
(
el
).
find
(
'.close'
).
css
({
background
:
'blue'
background
:
'blue'
})
})
return
{
el
}
}
}
})
})
})
})
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment