Commit fd815e7c authored by liupengfei's avatar liupengfei

栗子

parent 047b16e0
/* eslint-disable */
/* /*
* @Author: flyharvest * @Author: flyharvest
* @Date: 2020-07-14 14:09:40 * @Date: 2020-07-14 14:09:40
* @LastEditTime: 2020-07-20 15:54:52 * @LastEditTime: 2020-07-19 13:49:33
* @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 } from '@lib/handleDojoin' import { HandleDojoin } from '@lib/handleDojoin'
import { CustomError } from '@lib/utils'
import { CouponModal } from '@lib/couponModal' import { CouponModal } from '@lib/couponModal'
import { isFn } from '@lib/utils'
import $ from 'jquery' import $ from 'jquery'
import './index.less' import './index.less'
...@@ -18,132 +17,94 @@ rem.setViewPort() ...@@ -18,132 +17,94 @@ rem.setViewPort()
rem.init(750, 750) rem.init(750, 750)
services.use(ajax) services.use(ajax)
const Dojoin = services.DoJoin const DoJoin = services.DoJoin
const dojoin = new Dojoin() /**
* @description: 初始化并挂载,并挂载通用的周期
const couponModal = new CouponModal() * @example:
* @param {type}
const handleDojoin = new HandleDojoin() * @return:
*/
const handOut = (opts, sign, external = {}) => { const dojoin = new DoJoin({
const steam = [dojoin, handleDojoin, couponModal]
steam.forEach(e => {
e.hooksDefine.forEach(name => {
const cb = (data) => {
const fn = opts[name]
if (fn && isFn(fn) && fn.then) {
return fn(data, external[name])
} else if (fn && isFn(fn)) {
return Promise.resolve(fn(data, external[name]))
} else {
return Promise.resolve()
}
}
e.hooks[name].tapPromise(sign, cb)
})
})
}
const coreMethods = {
beforeDojoin (data) { beforeDojoin (data) {
console.log('我被调用-core')
return { return {
haha: 123123 haha: 123123
} }
}
})
const couponModal = new CouponModal({
close (type) {
return type
}, },
mediaMsg () { replaceHtml (data) {
console.log(media + 'ss') $(data).find('.content').css({
},
modifyDom ({ el }) {
$(el).find('.content').css({
background: 'red' background: 'red'
}) })
} }
} })
const handleDojoin = new HandleDojoin({
mediaMsg (media) {
console.log(media + 'ss')
}
})
/** /**
* @description: 假装这个methods 是由继承提供的。 * @description: 分配到不同模块的hooks
* @example: * @example:
* @param {type} * @param {type}
* @return: * @return:
*/ */
const methods = { const allHooks = {
beforeDojoin(data) { dojoin: dojoin.allHooksName,
console.log('我被调用了-skinName') handleDojoin: handleDojoin.allHooksName,
return { couponModal: couponModal.allHooksName
lala: '123213' }
const handout = (opts = {}) => {
const cbs = {
dojoin: {},
handleDojoin: {},
couponModal: {}
} }
}, for (const i in opts) {
if (opts[i]) {
modifyDom ({ el }, fn) { for (const name in allHooks) {
console.log(fn) if (allHooks[name].indexOf(i) > -1) {
$(el).find('.confirm').css({ cbs[name][i] = opts[i]
background: 'green'
})
return {el}
} }
}
handOut(methods, 'skinDefault', coreMethods)
handOut(coreMethods, 'core')
let inited = false
const tempHandOut = (opts, sign, external1 = {}, external2 = {}) => {
if (inited) {
tempHandOut.setOpts(opts)
return
} }
inited = true
tempHandOut.setOpts(opts)
const steam = [dojoin, handleDojoin, couponModal]
steam.forEach(e => {
e.hooksDefine.forEach(name => {
const cb = (data) => {
const fn = tempHandOut.getOpts()[name]
if (fn && isFn(fn) && fn.then) {
return fn(data, external1[name], external2[name])
} else if (fn && isFn(fn)) {
return Promise.resolve(fn(data, external1[name], external2[name]))
} else {
return Promise.resolve()
} }
} }
e.hooks[name].tapPromise({ return cbs
name: sign,
stage: -1
}, cb)
})
})
}
tempHandOut.setOpts = (opts) => {
tempHandOut.opts = opts
return opts
}
tempHandOut.getOpts = () => {
return tempHandOut.opts
} }
const $doJoin = (opts = {}) => { /**
tempHandOut(opts, 'skinTemp') * @description: 类比现在的dojoin
console.log(dojoin) * @example:
return dojoin.start().then(res => { * @param {type}
return handleDojoin.start(res) * @return:
*/
const $doJoin = (opts) => {
const cbs = handout(opts)
console.log(cbs)
return dojoin.start(cbs.dojoin)
.then(res => {
return handleDojoin.handle(res, cbs.handleDojoin)
}) })
.then(res => { .then(res => {
return couponModal.start(res) return couponModal.show(res, cbs.couponModal)
}) })
.then((data) => { .catch(err => {
console.log(data) if (err instanceof CustomError) {
console.log(err)
}
console.log(err)
}) })
} }
let times = 0 const getDom = ({ couponModalResolve }) => {
const replaceHtml = ({ couponModalResolve }) => {
console.log(++times)
const loading = $('<div class="loading"></div>') const loading = $('<div class="loading"></div>')
loading.text('加载中...') loading.text('加载中...')
$('body').append(loading) $('body').append(loading)
...@@ -163,23 +124,25 @@ const replaceHtml = ({ couponModalResolve }) => { ...@@ -163,23 +124,25 @@ const replaceHtml = ({ couponModalResolve }) => {
}, 2000) }, 2000)
}) })
} }
$doJoin({
beforeDojoin (data) { /**
console.log('我被调用了-特殊') * @description: 调用
} * @example:
}) * @param {type}
* @return:
*/
$doJoin()
.then(() => { .then(() => {
return $doJoin({ return $doJoin({
replaceHtml getDom
}) })
}) })
.then(() => { .then(() => {
$doJoin({ $doJoin({
modifyDom ({el}) { replaceHtml (data) {
$(el).find('.close').css({ $(data).find('.close').css({
background: 'blue' background: 'blue'
}) })
return {el}
} }
}) })
}) })
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment