This commit is contained in:
43
node_modules/@dcloudio/uni-components/lib-x/ad-interstitial/ad-interstitial.vue
generated
vendored
Normal file
43
node_modules/@dcloudio/uni-components/lib-x/ad-interstitial/ad-interstitial.vue
generated
vendored
Normal file
@@ -0,0 +1,43 @@
|
||||
<template>
|
||||
<view @click="_onclick">
|
||||
<slot :options="options" :loading="loading" :error="errorMessage" />
|
||||
<!-- #ifdef MP-WEIXIN -->
|
||||
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror" @nextChannel="_onnextchannel"></uniad-plugin>
|
||||
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
|
||||
<!-- #endif -->
|
||||
<!-- #ifdef MP-ALIPAY -->
|
||||
<uniad-plugin class="uniad-plugin" :adpid="adpid" @create="_handleAdRef" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
|
||||
<!-- #endif -->
|
||||
<!-- #ifdef H5 -->
|
||||
<div ref="container" />
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
// #ifdef APP
|
||||
import adMixin from "../ad/ad.mixin.app.js"
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
import adMixin from "../ad/ad-interstitial.web.js"
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
import adMixin from "../ad/ad.mixin.mp-weixin.js"
|
||||
// #endif
|
||||
// #ifdef MP-ALIPAY
|
||||
import adMixin from "../ad/ad.mixin.mp-alipay.js"
|
||||
// #endif
|
||||
|
||||
export default {
|
||||
name: 'AdInterstitial',
|
||||
mixins: [adMixin],
|
||||
props: {
|
||||
adType: {
|
||||
type: String,
|
||||
default: 'Interstitial'
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
}
|
||||
}
|
||||
</script>
|
||||
39
node_modules/@dcloudio/uni-components/lib-x/ad-rewarded-video/ad-rewarded-video.vue
generated
vendored
Normal file
39
node_modules/@dcloudio/uni-components/lib-x/ad-rewarded-video/ad-rewarded-video.vue
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
<template>
|
||||
<view @click="_onclick">
|
||||
<slot :options="options" :loading="loading" :error="errorMessage" />
|
||||
<!-- #ifdef MP-WEIXIN -->
|
||||
<uniad-plugin class="uniad-plugin" :adpid="adpid" :unit-id="unitId" @load="_onmpload" @close="_onmpclose" @error="_onmperror" @nextChannel="_onnextchannel"></uniad-plugin>
|
||||
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
|
||||
<!-- #endif -->
|
||||
<!-- #ifdef MP-ALIPAY -->
|
||||
<uniad-plugin class="uniad-plugin" :adpid="adpid" @create="_handleAdRef" @load="_onmpload" @close="_onmpclose" @error="_onmperror"></uniad-plugin>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
// #ifdef APP
|
||||
import adMixin from "../ad/ad.mixin.app.js"
|
||||
// #endif
|
||||
// #ifdef H5
|
||||
import adMixin from "../ad/ad.mixin.web.js"
|
||||
// #endif
|
||||
// #ifdef MP-WEIXIN
|
||||
import adMixin from "../ad/ad.mixin.mp-weixin.js"
|
||||
// #endif
|
||||
// #ifdef MP-ALIPAY
|
||||
import adMixin from "../ad/ad.mixin.mp-alipay.js"
|
||||
// #endif
|
||||
|
||||
export default {
|
||||
name: 'AdRewardedVideo',
|
||||
mixins: [adMixin],
|
||||
props: {
|
||||
adType: {
|
||||
type: String,
|
||||
default: 'RewardedVideo'
|
||||
}
|
||||
},
|
||||
methods: {}
|
||||
}
|
||||
</script>
|
||||
327
node_modules/@dcloudio/uni-components/lib-x/ad/ad-interstitial.web.js
generated
vendored
Normal file
327
node_modules/@dcloudio/uni-components/lib-x/ad/ad-interstitial.web.js
generated
vendored
Normal file
@@ -0,0 +1,327 @@
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
export default {
|
||||
props: {
|
||||
options: {
|
||||
type: [Object, Array],
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
adpid: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
preload: {
|
||||
type: [Boolean, String],
|
||||
default: true
|
||||
},
|
||||
loadnext: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
}
|
||||
},
|
||||
watch: {
|
||||
adpid (val) {
|
||||
if (val) {
|
||||
this._loadData(val)
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
loading: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this._pc = {}
|
||||
this._pl = []
|
||||
this._loadData()
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
},
|
||||
|
||||
show () {
|
||||
this.errorMessage = null
|
||||
|
||||
const data = this._pl[0]
|
||||
const providerConfig = this._pc[data.a1][data.t]
|
||||
|
||||
AdScript.instance.load(data.t, providerConfig.script, () => {
|
||||
this._renderData(data)
|
||||
}, (err) => {
|
||||
this.errorMessage = err.message
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
})
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
this.show()
|
||||
},
|
||||
|
||||
_loadData (adpid) {
|
||||
this.loading = true
|
||||
const id = adpid || this.adpid
|
||||
AdConfig.instance.get(id, (a, b) => {
|
||||
this._pc = a
|
||||
this._pl = b
|
||||
this.loading = false
|
||||
}, (err) => {
|
||||
this.loading = false
|
||||
this.errorMessage = err
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
})
|
||||
},
|
||||
|
||||
_renderData (data) {
|
||||
const id = this._createView()
|
||||
|
||||
const coral = new window.CoralAdv({
|
||||
app_id: data.a2,
|
||||
placement_id: data.a3,
|
||||
type: data.a4,
|
||||
display_type: data.a5,
|
||||
container_id: id,
|
||||
count: 1
|
||||
})
|
||||
coral.ready().then(async (res) => {
|
||||
if (res.ret === 0) {
|
||||
} else {
|
||||
this._dispatchEvent(EventType.Error, res)
|
||||
}
|
||||
}).catch((err) => {
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
})
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, {
|
||||
detail: data
|
||||
})
|
||||
},
|
||||
|
||||
_createView () {
|
||||
const id = this._randomId()
|
||||
const adView = document.createElement('div')
|
||||
adView.setAttribute('id', id)
|
||||
this.$refs.container.innerHTML = ''
|
||||
this.$refs.container.append(adView)
|
||||
return id
|
||||
},
|
||||
|
||||
_randomId () {
|
||||
let result = ''
|
||||
for (let i = 0; i < 4; i++) {
|
||||
result += (65536 * (1 + Math.random()) | 0).toString(16).substring(1)
|
||||
}
|
||||
return '_u' + result
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// let IC = 0
|
||||
// let IS = 0
|
||||
|
||||
class AdConfig {
|
||||
static get instance () {
|
||||
if (this._instance == null) {
|
||||
this._instance = new AdConfig()
|
||||
this._instance._init()
|
||||
}
|
||||
return this._instance
|
||||
}
|
||||
|
||||
constructor () {
|
||||
this._instance = null
|
||||
this._adConfig = null
|
||||
this._isLoading = false
|
||||
this._lastError = null
|
||||
this._callbacks = []
|
||||
}
|
||||
|
||||
get adConfig () {
|
||||
return this._adConfig
|
||||
}
|
||||
|
||||
get isExpired () {
|
||||
if (this._adConfig == null) {
|
||||
return true
|
||||
}
|
||||
return (Math.abs(Date.now() - this._adConfig.last) > this.CACHE_TIME)
|
||||
}
|
||||
|
||||
_init () {
|
||||
var config = this._getConfig()
|
||||
if (config === null || !config.last) {
|
||||
return
|
||||
}
|
||||
|
||||
if (!this.isExpired) {
|
||||
this._adConfig = config.data
|
||||
}
|
||||
}
|
||||
|
||||
get (adpid, success, fail) {
|
||||
// IC++
|
||||
if (this._adConfig != null) {
|
||||
this._doCallback(adpid, success, fail)
|
||||
if (this.isExpired) {
|
||||
this._loadAdConfig(adpid)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
this._callbacks.push({
|
||||
adpid: adpid,
|
||||
success: success,
|
||||
fail: fail
|
||||
})
|
||||
|
||||
this._loadAdConfig(adpid)
|
||||
}
|
||||
|
||||
_doCallback (adpid, success, fail) {
|
||||
// IS++
|
||||
var { a, b } = this._adConfig
|
||||
if (a[adpid]) {
|
||||
success(b, a[adpid])
|
||||
} else {
|
||||
fail(this.ERROR_INVALID_ADPID)
|
||||
}
|
||||
}
|
||||
|
||||
_loadAdConfig (adpid) {
|
||||
if (this._isLoading === true) {
|
||||
return
|
||||
}
|
||||
this._isLoading = true
|
||||
|
||||
uni.request({
|
||||
url: this.URL,
|
||||
method: 'GET',
|
||||
timeout: 8000,
|
||||
data: {
|
||||
d: location.hostname,
|
||||
a: adpid
|
||||
},
|
||||
dataType: 'json',
|
||||
success: (res) => {
|
||||
const rd = res.data
|
||||
if (rd.ret === 0) {
|
||||
const data = rd.data
|
||||
|
||||
this._adConfig = data
|
||||
this._setConfig(data)
|
||||
|
||||
this._callbacks.forEach(({ adpid, success, fail }) => {
|
||||
this._doCallback(adpid, success, fail)
|
||||
})
|
||||
} else {
|
||||
this._callbacks.forEach((i) => {
|
||||
i.fail({ errCode: rd.ret, errMsg: rd.msg })
|
||||
})
|
||||
}
|
||||
this._callbacks = []
|
||||
},
|
||||
fail: (err) => {
|
||||
this._callbacks.forEach((i) => {
|
||||
i.fail(err)
|
||||
})
|
||||
this._callbacks = []
|
||||
},
|
||||
complete: (c) => {
|
||||
this._isLoading = false
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
_getConfig () {
|
||||
if (!navigator.cookieEnabled || !window.localStorage) {
|
||||
return null
|
||||
}
|
||||
var data = localStorage.getItem(this.KEY)
|
||||
return data ? JSON.parse(data) : null
|
||||
}
|
||||
|
||||
_setConfig (data) {
|
||||
if (!navigator.cookieEnabled || !window.localStorage) {
|
||||
return null
|
||||
}
|
||||
localStorage.setItem(this.KEY, JSON.stringify({
|
||||
last: Date.now(),
|
||||
data: data
|
||||
}))
|
||||
}
|
||||
}
|
||||
Object.assign(AdConfig.prototype, {
|
||||
URL: 'https://hac1.dcloud.net.cn/ah5',
|
||||
KEY: 'uni_app_ad_config',
|
||||
CACHE_TIME: 1000 * 60 * 10,
|
||||
ERROR_INVALID_ADPID: {
|
||||
'-5002': 'invalid adpid'
|
||||
}
|
||||
})
|
||||
|
||||
class AdScript {
|
||||
static get instance () {
|
||||
if (this._instance == null) {
|
||||
this._instance = new AdScript()
|
||||
}
|
||||
return this._instance
|
||||
}
|
||||
|
||||
constructor () {
|
||||
this._instance = null
|
||||
this._callback = {}
|
||||
this._cache = {}
|
||||
}
|
||||
|
||||
load (provider, script, success, fail) {
|
||||
if (this._cache[provider] === undefined) {
|
||||
this.loadScript(provider, script)
|
||||
}
|
||||
|
||||
if (this._cache[provider] === 1) {
|
||||
success()
|
||||
} else {
|
||||
if (!this._callback[provider]) {
|
||||
this._callback[provider] = []
|
||||
}
|
||||
this._callback[provider].push({
|
||||
success,
|
||||
fail
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
loadScript (provider, script) {
|
||||
this._cache[provider] = 0
|
||||
var ads = document.createElement('script')
|
||||
ads.setAttribute('id', 'uniad_provider' + provider)
|
||||
for (const var1 in script) {
|
||||
ads.setAttribute(var1, script[var1])
|
||||
}
|
||||
ads.onload = () => {
|
||||
this._cache[provider] = 1
|
||||
this._callback[provider].forEach(({ success }) => {
|
||||
success()
|
||||
})
|
||||
this._callback[provider].length = 0
|
||||
}
|
||||
ads.onerror = (err) => {
|
||||
this._cache[provider] = undefined
|
||||
this._callback[provider].forEach(({ fail }) => {
|
||||
fail(err)
|
||||
})
|
||||
this._callback[provider].length = 0
|
||||
}
|
||||
document.body.append(ads)
|
||||
}
|
||||
}
|
||||
367
node_modules/@dcloudio/uni-components/lib-x/ad/ad.mixin.mp-weixin.js
generated
vendored
Normal file
367
node_modules/@dcloudio/uni-components/lib-x/ad/ad.mixin.mp-weixin.js
generated
vendored
Normal file
@@ -0,0 +1,367 @@
|
||||
const adPlugin = requirePlugin("uni-ad");
|
||||
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
const AdType = {
|
||||
Banner: "banner",
|
||||
RewardedVideo: "rewardedVideo",
|
||||
Interstitial: "interstitial"
|
||||
}
|
||||
|
||||
const ProviderType = {
|
||||
WeChat: 10018,
|
||||
UserWeChat: 10017,
|
||||
ShanHu: 10020
|
||||
}
|
||||
|
||||
export default {
|
||||
props: {
|
||||
options: {
|
||||
type: [Object, Array],
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
adpid: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
unitId: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
preload: {
|
||||
type: [Boolean, String],
|
||||
default: true
|
||||
},
|
||||
loadnext: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
urlCallback: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
loading: false,
|
||||
userwx: false,
|
||||
userUnitId: "",
|
||||
wxchannel: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this._ad = null
|
||||
this._loading = false
|
||||
this._wxRewardedAd = null
|
||||
this._wxInterstitialAd = null
|
||||
this._providerType = ProviderType.ShanHu
|
||||
if (this.preload && this._canCreateAd()) {
|
||||
this.load()
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
if (this.loading) {
|
||||
return
|
||||
}
|
||||
this._startLoading()
|
||||
if (this._providerType == ProviderType.ShanHu) {
|
||||
} else if (this._providerType == ProviderType.WeChat) {
|
||||
this.selectComponent('.uniad-plugin-wx').load()
|
||||
} else if (this._providerType == ProviderType.UserWeChat) {
|
||||
this._loadWxAd()
|
||||
}
|
||||
},
|
||||
|
||||
show (e) {
|
||||
this.errorMessage = null
|
||||
if (this._providerType == ProviderType.ShanHu) {
|
||||
this._showAdInPlugin(this.selectComponent('.uniad-plugin'))
|
||||
} else if (this._providerType == ProviderType.WeChat) {
|
||||
this._showAdInPlugin(this.selectComponent('.uniad-plugin-wx'))
|
||||
} else if (this._providerType == ProviderType.UserWeChat) {
|
||||
this._showWxAd(e)
|
||||
}
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
this.show()
|
||||
},
|
||||
|
||||
_startLoading () {
|
||||
this.loading = true
|
||||
this.errorMessage = null
|
||||
},
|
||||
|
||||
_canCreateAd () {
|
||||
let result = false
|
||||
if (typeof this.adpid === 'string' && this.adpid.length > 0) {
|
||||
result = true
|
||||
} else if (typeof this.adpid === 'number') {
|
||||
result = true
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
_hasCallback () {
|
||||
return (typeof this.urlCallback === 'object' && Object.keys(this.urlCallback).length > 0)
|
||||
},
|
||||
|
||||
_onmpload (e) {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
},
|
||||
|
||||
_onmpclose (e) {
|
||||
this._dispatchEvent(EventType.Close, e.detail)
|
||||
if (e.detail.adsdata) {
|
||||
const adv = e.detail.adv
|
||||
const adsdata = e.detail.adsdata
|
||||
const version = e.detail.version
|
||||
|
||||
/* eslint-disable no-undef */
|
||||
uniCloud.callFunction({
|
||||
name: 'uniAdCallback',
|
||||
data: {
|
||||
adv: adv,
|
||||
adsdata: adsdata,
|
||||
version: version
|
||||
},
|
||||
secretType: 'both',
|
||||
success: (res) => {
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
}
|
||||
})
|
||||
|
||||
delete e.detail.adv
|
||||
delete e.detail.adsdata
|
||||
delete e.detail.version
|
||||
}
|
||||
},
|
||||
|
||||
_onmperror (e) {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(e.detail)
|
||||
this._dispatchEvent(EventType.Error, e.detail)
|
||||
},
|
||||
|
||||
_onnextchannel (e) {
|
||||
this.wxchannel = true
|
||||
const adData = e.detail[0];
|
||||
this.$nextTick(() => {
|
||||
if (adData.provider == 10017) {
|
||||
this._providerType = ProviderType.UserWeChat
|
||||
switch(adData._nt_) {
|
||||
case 4:
|
||||
this.wxAdType = AdType.Banner
|
||||
this.userwx = true
|
||||
this.userUnitId = adData.posid
|
||||
break;
|
||||
case 9:
|
||||
this.wxAdType = AdType.RewardedVideo
|
||||
this._createRewardedAd(adData.posid)
|
||||
break;
|
||||
case 15:
|
||||
this.wxAdType = AdType.Interstitial
|
||||
this._createInterstitialAd(adData.posid)
|
||||
break;
|
||||
}
|
||||
} else if (adData.provider == 10018) {
|
||||
this._providerType = ProviderType.WeChat
|
||||
this.selectComponent('.uniad-plugin-wx').setConfig(adData)
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
_onwxchannelerror(e) {
|
||||
this.wxchannel = false
|
||||
this.$nextTick(() => {
|
||||
this._providerType = ProviderType.ShanHu
|
||||
this.selectComponent('.uniad-plugin').shanhuChannel()
|
||||
})
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, {
|
||||
detail: data
|
||||
})
|
||||
},
|
||||
|
||||
_showAdInPlugin(adComponent) {
|
||||
if (this._hasCallback()) {
|
||||
const userCryptoManager = wx.getUserCryptoManager()
|
||||
userCryptoManager.getLatestUserKey({
|
||||
success: ({
|
||||
encryptKey,
|
||||
iv,
|
||||
version,
|
||||
expireTime
|
||||
}) => {
|
||||
adComponent.show({
|
||||
userId: this.urlCallback.userId || '',
|
||||
extra: this.urlCallback.extra || '',
|
||||
encryptKey,
|
||||
iv,
|
||||
version,
|
||||
expireTime
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
adComponent.show({
|
||||
userId: this.urlCallback.userId || '',
|
||||
extra: this.urlCallback.extra || ''
|
||||
})
|
||||
}
|
||||
},
|
||||
|
||||
_loadWxAd () {
|
||||
switch (this.wxAdType) {
|
||||
case AdType.RewardedVideo:
|
||||
if (this._wxRewardedAd) {
|
||||
this._wxRewardedAd.load();
|
||||
}
|
||||
break;
|
||||
case AdType.Interstitial:
|
||||
if (this._wxInterstitialAd) {
|
||||
this._wxInterstitialAd.load();
|
||||
}
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
// 加载/显示广告
|
||||
_showWxAd (options) {
|
||||
this._urlCallback = options || this.urlCallback;
|
||||
if (this.loading == true) {
|
||||
this._userInvokeShowFlag = true
|
||||
return
|
||||
}
|
||||
switch (this.wxAdType) {
|
||||
case AdType.RewardedVideo:
|
||||
if (!this._wxRewardedAd) {
|
||||
return;
|
||||
}
|
||||
this._wxRewardedAd.show().catch((err) => {
|
||||
this._wxRewardedAd.load().then(() => {
|
||||
this._wxRewardedAd.show();
|
||||
}).catch((err) => {
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
});
|
||||
});
|
||||
break;
|
||||
case AdType.Interstitial:
|
||||
if (!this._wxInterstitialAd) {
|
||||
return;
|
||||
}
|
||||
this._wxInterstitialAd.show().catch((err) => {
|
||||
this._wxInterstitialAd.load().then(() => {
|
||||
this._wxInterstitialAd.show();
|
||||
}).catch((err) => {
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
});
|
||||
});
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
// 微信激励视频
|
||||
_createRewardedAd(adUnitId) {
|
||||
if (this._wxRewardedAd) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._wxRewardedAd = wx.createRewardedVideoAd({ adUnitId: adUnitId, multiton: true });
|
||||
|
||||
this._wxRewardedAd.onLoad(() => {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
if (this._userInvokeShowFlag) {
|
||||
this._userInvokeShowFlag = false;
|
||||
this._wxRewardedAd.show();
|
||||
}
|
||||
});
|
||||
|
||||
this._wxRewardedAd.onError(err => {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
});
|
||||
|
||||
this._wxRewardedAd.onClose(res => {
|
||||
if (res.isEnded) {
|
||||
this._callServer()
|
||||
} else {
|
||||
this._dispatchEvent(EventType.Close, res);
|
||||
}
|
||||
});
|
||||
|
||||
this.loading = true
|
||||
},
|
||||
|
||||
// 微信插屏
|
||||
_createInterstitialAd(adUnitId) {
|
||||
if (this._wxInterstitialAd) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._wxInterstitialAd = wx.createInterstitialAd({ adUnitId: adUnitId });
|
||||
|
||||
this._wxInterstitialAd.onLoad(() => {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
if (this._userInvokeShowFlag) {
|
||||
this._userInvokeShowFlag = false;
|
||||
this._wxInterstitialAd.show();
|
||||
}
|
||||
});
|
||||
|
||||
this._wxInterstitialAd.onError(err => {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
});
|
||||
|
||||
this._wxInterstitialAd.onClose(res => {
|
||||
this._dispatchEvent(EventType.Close, res);
|
||||
});
|
||||
|
||||
this.loading = true
|
||||
},
|
||||
|
||||
_callServer() {
|
||||
const userCryptoManager = wx.getUserCryptoManager()
|
||||
userCryptoManager.getLatestUserKey({
|
||||
success: (encryptConfig) => {
|
||||
const callbackData = adPlugin.buildCallbackData(this.adpid, this.urlCallback, {}, encryptConfig)
|
||||
uniCloud.callFunction({
|
||||
name: 'uniAdCallback',
|
||||
data: callbackData,
|
||||
secretType: 'both',
|
||||
success: (res) => {
|
||||
this._dispatchEvent(EventType.Close, res);
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
}
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err);
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
159
node_modules/@dcloudio/uni-components/lib-x/ad/ad.mixin.mp.js
generated
vendored
Normal file
159
node_modules/@dcloudio/uni-components/lib-x/ad/ad.mixin.mp.js
generated
vendored
Normal file
@@ -0,0 +1,159 @@
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
export default {
|
||||
props: {
|
||||
options: {
|
||||
type: [Object, Array],
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
adpid: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
unitId: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
preload: {
|
||||
type: [Boolean, String],
|
||||
default: true
|
||||
},
|
||||
loadnext: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
urlCallback: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
loading: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this._ad = null
|
||||
setTimeout(() => {
|
||||
if (this.preload && this._canCreateAd()) {
|
||||
this.load()
|
||||
}
|
||||
}, 100)
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
if (this.loading) {
|
||||
return
|
||||
}
|
||||
this._startLoading()
|
||||
},
|
||||
|
||||
show () {
|
||||
this.errorMessage = null
|
||||
this._ad = this.selectComponent('.uniad-plugin')
|
||||
if (this._hasCallback()) {
|
||||
const userCryptoManager = wx.getUserCryptoManager()
|
||||
userCryptoManager.getLatestUserKey({
|
||||
success: ({
|
||||
encryptKey,
|
||||
iv,
|
||||
version,
|
||||
expireTime
|
||||
}) => {
|
||||
this._ad.show({
|
||||
userId: this.urlCallback.userId || '',
|
||||
extra: this.urlCallback.extra || '',
|
||||
encryptKey,
|
||||
iv,
|
||||
version,
|
||||
expireTime
|
||||
})
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this._ad.show()
|
||||
}
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
this.show()
|
||||
},
|
||||
|
||||
_startLoading () {
|
||||
this.loading = true
|
||||
this.errorMessage = null
|
||||
},
|
||||
|
||||
_canCreateAd () {
|
||||
let result = false
|
||||
if (typeof this.adpid === 'string' && this.adpid.length > 0) {
|
||||
result = true
|
||||
} else if (typeof this.adpid === 'number') {
|
||||
result = true
|
||||
}
|
||||
return result
|
||||
},
|
||||
|
||||
_hasCallback () {
|
||||
return (typeof this.urlCallback === 'object' && Object.keys(this.urlCallback).length > 0)
|
||||
},
|
||||
|
||||
_onmpload (e) {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
},
|
||||
|
||||
_onmpclose (e) {
|
||||
this._dispatchEvent(EventType.Close, e.detail)
|
||||
if (e.detail.adsdata) {
|
||||
const adv = e.detail.adv
|
||||
const adsdata = e.detail.adsdata
|
||||
const version = e.detail.version
|
||||
|
||||
/* eslint-disable no-undef */
|
||||
uniCloud.callFunction({
|
||||
name: 'uniAdCallback',
|
||||
data: {
|
||||
adv: adv,
|
||||
adsdata: adsdata,
|
||||
version: version
|
||||
},
|
||||
secretType: 'both',
|
||||
success: (res) => {
|
||||
},
|
||||
fail: (err) => {
|
||||
this._dispatchEvent(EventType.Error, err)
|
||||
}
|
||||
})
|
||||
|
||||
delete e.detail.adv
|
||||
delete e.detail.adsdata
|
||||
delete e.detail.version
|
||||
}
|
||||
},
|
||||
|
||||
_onmperror (e) {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(e.detail)
|
||||
this._dispatchEvent(EventType.Error, e.detail)
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, {
|
||||
detail: data
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
173
node_modules/@dcloudio/uni-components/lib-x/ad/ad.mixin.web.js
generated
vendored
Normal file
173
node_modules/@dcloudio/uni-components/lib-x/ad/ad.mixin.web.js
generated
vendored
Normal file
@@ -0,0 +1,173 @@
|
||||
const AD_SERVER_URL = 'https://hac1.dcloud.net.cn/h5/gs'
|
||||
|
||||
const EventType = {
|
||||
Load: 'load',
|
||||
Close: 'close',
|
||||
Error: 'error'
|
||||
}
|
||||
|
||||
const ProviderType = 'wm'
|
||||
|
||||
class Process {
|
||||
static Start (cmd, args) {
|
||||
return new Process().openScheme(cmd)
|
||||
}
|
||||
|
||||
constructor () {
|
||||
this._a = null
|
||||
}
|
||||
|
||||
openScheme (url) {
|
||||
if (this._a == null) {
|
||||
this._a = document.createElement('a')
|
||||
}
|
||||
this._a.href = url
|
||||
this._a.click()
|
||||
}
|
||||
}
|
||||
|
||||
export default {
|
||||
props: {
|
||||
options: {
|
||||
type: [Object, Array],
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
},
|
||||
adpid: {
|
||||
type: [Number, String],
|
||||
default: ''
|
||||
},
|
||||
preload: {
|
||||
type: [Boolean, String],
|
||||
default: true
|
||||
},
|
||||
loadnext: {
|
||||
type: [Boolean, String],
|
||||
default: false
|
||||
},
|
||||
urlCallback: {
|
||||
type: Object,
|
||||
default () {
|
||||
return {}
|
||||
}
|
||||
}
|
||||
},
|
||||
data () {
|
||||
return {
|
||||
adData: null,
|
||||
loading: false,
|
||||
showModel: false,
|
||||
errorMessage: null
|
||||
}
|
||||
},
|
||||
created () {
|
||||
this._loading = false
|
||||
this.adConfigData = null
|
||||
},
|
||||
methods: {
|
||||
load () {
|
||||
setTimeout(() => {
|
||||
this._onmpload()
|
||||
}, 200)
|
||||
},
|
||||
|
||||
show (options) {
|
||||
if (!this._isMobile()) {
|
||||
this._dispatchEvent(EventType.Error, {
|
||||
errCode: -1,
|
||||
errMsg: '当前设备环境无效'
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
this.errorMessage = null
|
||||
if (this._loading) {
|
||||
return
|
||||
}
|
||||
this._loading = true
|
||||
|
||||
this._requestScheme(options)
|
||||
},
|
||||
|
||||
getProvider () {
|
||||
return ProviderType
|
||||
},
|
||||
|
||||
_onclick () {
|
||||
if (this.disabled) {
|
||||
return
|
||||
}
|
||||
|
||||
this.show()
|
||||
},
|
||||
|
||||
_requestScheme (options = {}) {
|
||||
const urlCallback = options.urlCallback || this.urlCallback
|
||||
uni.request({
|
||||
url: AD_SERVER_URL,
|
||||
method: 'POST',
|
||||
data: {
|
||||
adpid: this.adpid,
|
||||
userId: urlCallback.userId,
|
||||
extra: urlCallback.extra
|
||||
},
|
||||
timeout: 5000,
|
||||
dataType: 'json',
|
||||
success: (res) => {
|
||||
if (res.statusCode !== 200) {
|
||||
this._dispatchEvent(EventType.Error, {
|
||||
errCode: res.statusCode,
|
||||
errMsg: res.statusCode
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
const responseData = res.data
|
||||
if (responseData.ret === 0) {
|
||||
Process.Start(responseData.data.openlink)
|
||||
} else {
|
||||
this._dispatchEvent(EventType.Error, {
|
||||
errCode: responseData.ret,
|
||||
errMsg: responseData.msg
|
||||
})
|
||||
}
|
||||
},
|
||||
fail: (err) => {
|
||||
this.$emit(EventType.Error, {
|
||||
errCode: '',
|
||||
errMsg: err.errMsg
|
||||
})
|
||||
},
|
||||
complete: () => {
|
||||
this.loading = false
|
||||
}
|
||||
})
|
||||
},
|
||||
|
||||
_isMobile () {
|
||||
return /android|iphone/i.test(navigator.userAgent.toLowerCase())
|
||||
},
|
||||
|
||||
_onmpload (e) {
|
||||
this.loading = false
|
||||
this._dispatchEvent(EventType.Load, {})
|
||||
},
|
||||
|
||||
_onmpclose (e) {
|
||||
this._dispatchEvent(EventType.Close, e.detail)
|
||||
},
|
||||
|
||||
_onmperror (e) {
|
||||
this.loading = false
|
||||
this.errorMessage = JSON.stringify(e.detail)
|
||||
this._dispatchEvent(EventType.Error, e.detail)
|
||||
},
|
||||
|
||||
_dispatchEvent (type, data) {
|
||||
this.$emit(type, {
|
||||
detail: data
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
127
node_modules/@dcloudio/uni-components/lib-x/custom-tab-bar/custom-tab-bar.vue
generated
vendored
Normal file
127
node_modules/@dcloudio/uni-components/lib-x/custom-tab-bar/custom-tab-bar.vue
generated
vendored
Normal file
@@ -0,0 +1,127 @@
|
||||
<template>
|
||||
<uni-tabbar v-if="hasTabBar" v-show="showTabBar">
|
||||
<div
|
||||
:style="{
|
||||
'flex-direction': direction === 'vertical' ? 'column' : 'row',
|
||||
backgroundColor: tabBar.backgroundColor,
|
||||
}"
|
||||
class="uni-tabbar"
|
||||
>
|
||||
<template v-for="(item, index) in tabBar.list" :key="item.pagePath">
|
||||
<div
|
||||
v-if="item.visible !== false"
|
||||
class="uni-tabbar__item"
|
||||
@click="switchTab(item, index)"
|
||||
>
|
||||
<div class="uni-tabbar__bd">
|
||||
<div
|
||||
v-if="showIcon && item.iconPath"
|
||||
:class="{ 'uni-tabbar__icon__diff': !item.text }"
|
||||
class="uni-tabbar__icon"
|
||||
>
|
||||
<img
|
||||
:src="
|
||||
getRealPath(
|
||||
selectedIndex === index
|
||||
? item.selectedIconPath
|
||||
: item.iconPath
|
||||
)
|
||||
"
|
||||
/>
|
||||
<div
|
||||
v-if="item.redDot"
|
||||
:class="{ 'uni-tabbar__badge': !!item.badge }"
|
||||
class="uni-tabbar__reddot"
|
||||
>
|
||||
{{ item.badge }}
|
||||
</div>
|
||||
</div>
|
||||
<div
|
||||
v-if="item.text"
|
||||
:style="{
|
||||
color:
|
||||
selectedIndex === index ? tabBar.selectedColor : tabBar.color,
|
||||
fontSize: showIcon && item.iconPath ? '10px' : '14px',
|
||||
}"
|
||||
class="uni-tabbar__label"
|
||||
>
|
||||
{{ item.text }}
|
||||
<div
|
||||
v-if="item.redDot && (!showIcon || !item.iconPath)"
|
||||
:class="{ 'uni-tabbar__badge': !!item.badge }"
|
||||
class="uni-tabbar__reddot"
|
||||
>
|
||||
{{ item.badge }}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
</div>
|
||||
</uni-tabbar>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
import { computed, ref, watch } from 'vue'
|
||||
import { useRoute } from 'vue-router'
|
||||
import { getRealPath } from '@dcloudio/uni-h5'
|
||||
import { useTabBar } from '@dcloudio/uni-h5'
|
||||
|
||||
export default {
|
||||
name: 'CustomTabBar',
|
||||
props: {
|
||||
selected: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
showIcon: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
direction: {
|
||||
type: String,
|
||||
default: 'horizontal'
|
||||
}
|
||||
},
|
||||
setup(props, { emit }) {
|
||||
const tabBar = useTabBar()
|
||||
const route = useRoute()
|
||||
const hasTabBar = computed(() => tabBar.list && tabBar.list.length)
|
||||
const selectedIndex = ref(props.selected)
|
||||
watch(() => props.selected, value => selectedIndex.value = value)
|
||||
watch(() => selectedIndex.value, value => tabBar.selectedIndex = value)
|
||||
watch(() => {
|
||||
const meta = route.meta
|
||||
return [meta.isTabBar, meta.route]
|
||||
}, ([isTabBar, pagePath]) => {
|
||||
if (isTabBar) {
|
||||
const index = tabBar.list.findIndex(item => pagePath === item.pagePath)
|
||||
if (index > -1) {
|
||||
selectedIndex.value = index
|
||||
}
|
||||
}
|
||||
})
|
||||
function switchTab(item, index) {
|
||||
selectedIndex.value = index
|
||||
const detail = {
|
||||
index,
|
||||
text: item.text,
|
||||
pagePath: item.pagePath,
|
||||
}
|
||||
emit('onTabItemTap', detail)
|
||||
}
|
||||
|
||||
return {
|
||||
tabBar,
|
||||
getRealPath,
|
||||
selectedIndex,
|
||||
hasTabBar,
|
||||
showTabBar: true,
|
||||
switchTab,
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<style>
|
||||
</style>
|
||||
10
node_modules/@dcloudio/uni-components/lib-x/page-meta-head/page-meta-head.vue
generated
vendored
Normal file
10
node_modules/@dcloudio/uni-components/lib-x/page-meta-head/page-meta-head.vue
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<template>
|
||||
<teleport to="head">
|
||||
<slot />
|
||||
</teleport>
|
||||
</template>
|
||||
<script>
|
||||
export default {
|
||||
name: 'PageMetaHead',
|
||||
}
|
||||
</script>
|
||||
10
node_modules/@dcloudio/uni-components/lib-x/page-meta/page-meta.vue
generated
vendored
Normal file
10
node_modules/@dcloudio/uni-components/lib-x/page-meta/page-meta.vue
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
<template>
|
||||
<view style="display: none;">
|
||||
<slot />
|
||||
</view>
|
||||
</template>
|
||||
<script>
|
||||
export default {
|
||||
name: 'PageMeta'
|
||||
}
|
||||
</script>
|
||||
31
node_modules/@dcloudio/uni-components/lib-x/uniad/uniad.vue
generated
vendored
Normal file
31
node_modules/@dcloudio/uni-components/lib-x/uniad/uniad.vue
generated
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
<template>
|
||||
<view @click="onclick">
|
||||
<uniad-plugin
|
||||
class="uniad-plugin"
|
||||
:adpid="adpid"
|
||||
:unit-id="unitId"
|
||||
@load="_onmpload"
|
||||
@close="_onmpclose"
|
||||
@error="_onmperror"
|
||||
@nextChannel="_onnextchannel"
|
||||
/>
|
||||
<!-- #ifdef MP-WEIXIN -->
|
||||
<ad-custom v-if="userwx" :unit-id="userUnitId"></ad-custom>
|
||||
<uniad-plugin-wx v-if="wxchannel" class="uniad-plugin-wx" @error="_onwxchannelerror"></uniad-plugin-wx>
|
||||
<!-- #endif -->
|
||||
</view>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
// #ifdef MP-WEIXIN
|
||||
import adMixin from "../ad/ad.mixin.mp-weixin.js"
|
||||
// #endif
|
||||
// #ifdef MP-ALIPAY
|
||||
import adMixin from "../ad/ad.mixin.mp-alipay.js"
|
||||
// #endif
|
||||
|
||||
export default {
|
||||
name: 'Uniad',
|
||||
mixins: [adMixin]
|
||||
}
|
||||
</script>
|
||||
317
node_modules/@dcloudio/uni-components/lib-x/unicloud-db/index.uts
generated
vendored
Normal file
317
node_modules/@dcloudio/uni-components/lib-x/unicloud-db/index.uts
generated
vendored
Normal file
@@ -0,0 +1,317 @@
|
||||
type SuccessCallback<T> = (res : T | null) => void | null
|
||||
type FailCallback = (err : any | null) => void | null
|
||||
type CompleteCallback = () => void | null
|
||||
|
||||
export type MixinDatacomPaginationType = {
|
||||
current : number,
|
||||
size : number,
|
||||
count : number
|
||||
}
|
||||
|
||||
export type MixinDatacomGetOptions = {
|
||||
collection ?: UTSJSONObject,
|
||||
field ?: string,
|
||||
orderBy ?: string,
|
||||
where ?: any,
|
||||
pageData ?: string,
|
||||
pageCurrent ?: number,
|
||||
pageSize ?: number,
|
||||
getCount ?: boolean,
|
||||
getTree ?: any,
|
||||
getTreePath ?: UTSJSONObject,
|
||||
startWith ?: string,
|
||||
limitLevel ?: number,
|
||||
groupBy ?: string,
|
||||
groupField ?: string,
|
||||
distinct ?: boolean,
|
||||
pageIndistinct ?: boolean,
|
||||
foreignKey ?: string,
|
||||
loadtime ?: string,
|
||||
manual ?: boolean
|
||||
}
|
||||
|
||||
export type MixinDatacomEasyGetOptions = {
|
||||
success ?: SuccessCallback<UniCloudDBGetResult>,
|
||||
fail ?: FailCallback,
|
||||
complete ?: CompleteCallback,
|
||||
}
|
||||
|
||||
export const mixinDatacom = defineMixin({
|
||||
slots: Object as SlotsType<{
|
||||
default : {
|
||||
data : Array<UTSJSONObject>,
|
||||
loading : boolean,
|
||||
hasMore : boolean,
|
||||
pagination : MixinDatacomPaginationType,
|
||||
error : UniCloudError | null
|
||||
}
|
||||
}>,
|
||||
props: {
|
||||
localdata: {
|
||||
type: Array as PropType<Array<UTSJSONObject>>,
|
||||
default: [] as Array<UTSJSONObject>
|
||||
},
|
||||
collection: {
|
||||
type: Object,
|
||||
default: ''
|
||||
},
|
||||
field: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
orderby: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
where: {
|
||||
type: Object,
|
||||
default: ''
|
||||
},
|
||||
pageData: {
|
||||
type: String,
|
||||
default: 'add'
|
||||
},
|
||||
pageCurrent: {
|
||||
type: Number,
|
||||
default: 1
|
||||
},
|
||||
pageSize: {
|
||||
type: Number,
|
||||
default: 20
|
||||
},
|
||||
getcount: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
gettree: {
|
||||
type: Object,
|
||||
default: ''
|
||||
},
|
||||
gettreepath: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
startwith: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
limitlevel: {
|
||||
type: Number,
|
||||
default: 10
|
||||
},
|
||||
groupby: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
groupField: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
distinct: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
pageIndistinct: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
foreignKey: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
loadtime: {
|
||||
type: String,
|
||||
default: 'auto'
|
||||
},
|
||||
manual: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
mixinDatacomResData: [] as Array<UTSJSONObject>, // 请求返回的数据,调用 loadData 后会更新
|
||||
mixinDatacomLoading: false, // 网络请求状态
|
||||
mixinDatacomHasMore: false, // 是否有更多数据
|
||||
mixinDatacomPage: {
|
||||
current: 1,
|
||||
size: 20,
|
||||
count: 0,
|
||||
} as MixinDatacomPaginationType, // 分页信息,详情见 created 生命周期
|
||||
mixinDatacomError: null as UniCloudError | null, // 请求出错时的错误消息
|
||||
}
|
||||
},
|
||||
created() {
|
||||
this.mixinDatacomPage.current = this.pageCurrent
|
||||
this.mixinDatacomPage.size = this.pageSize
|
||||
|
||||
const PROPS_NAME = ['', '', 'collection', 'field', 'getcount', 'orderby', 'where', 'groupby', 'groupField', 'distinct']
|
||||
|
||||
this.$watch(
|
||||
() : any => [
|
||||
this.pageCurrent,
|
||||
this.pageSize,
|
||||
this.collection,
|
||||
this.field,
|
||||
this.getcount,
|
||||
this.orderby,
|
||||
this.where,
|
||||
this.groupby,
|
||||
this.groupField,
|
||||
this.distinct
|
||||
],
|
||||
(newValue : Array<any>, oldValue : Array<any>) => {
|
||||
this.mixinDatacomPage.size = this.pageSize
|
||||
if (newValue[0] !== oldValue[0]) {
|
||||
this.mixinDatacomPage.current = this.pageCurrent
|
||||
}
|
||||
|
||||
let needReset = false
|
||||
let changed : Array<string> = []
|
||||
for (let i = 2; i < newValue.length; i++) {
|
||||
if (newValue[i] !== oldValue[i]) {
|
||||
needReset = true
|
||||
changed.push(PROPS_NAME[i])
|
||||
}
|
||||
}
|
||||
|
||||
this.onMixinDatacomPropsChange(needReset, changed)
|
||||
}
|
||||
)
|
||||
},
|
||||
methods: {
|
||||
// props发生变化时被调用,在组件中覆盖此方法
|
||||
// 非 pageCurrent,pageSize 改变时 needReset=true,需要重置数据
|
||||
// changed,发生变化的属性名,类型为Array,例如 ['collection', 'action']
|
||||
onMixinDatacomPropsChange(_ : boolean, changed : Array<string>) {
|
||||
},
|
||||
mixinDatacomEasyGet(options ?: MixinDatacomEasyGetOptions) {
|
||||
if (this.mixinDatacomLoading) {
|
||||
return
|
||||
}
|
||||
|
||||
this.mixinDatacomLoading = true
|
||||
this.mixinDatacomError = null
|
||||
|
||||
this.mixinDatacomGet(null).then((res : UniCloudDBGetResult) => {
|
||||
const data = res.data
|
||||
const count = res.count
|
||||
|
||||
if (this.getcount && count != null) {
|
||||
this.mixinDatacomPage.count = count
|
||||
}
|
||||
|
||||
this.mixinDatacomHasMore = !((count != null) ? (this.mixinDatacomPage.current * this.mixinDatacomPage.size >= count) : (data.length < this.pageSize))
|
||||
this.mixinDatacomResData = data
|
||||
|
||||
options?.success?.(res)
|
||||
}).catch((err : any | null) => {
|
||||
this.mixinDatacomError = err as UniCloudError
|
||||
options?.fail?.(err)
|
||||
}).then(() => {
|
||||
this.mixinDatacomLoading = false
|
||||
options?.complete?.()
|
||||
}, () => {
|
||||
this.mixinDatacomLoading = false
|
||||
options?.complete?.()
|
||||
})
|
||||
},
|
||||
mixinDatacomGet(options: MixinDatacomGetOptions | null) : Promise<UniCloudDBGetResult> {
|
||||
let db = uniCloud.databaseForJQL()
|
||||
|
||||
let collection = Array.isArray(this.collection) ? db.collection(...(this.collection as Array<any>)) : db.collection(this.collection)
|
||||
|
||||
let filter : UniCloudDBFilter | null = null
|
||||
if (this.foreignKey.length > 0) {
|
||||
filter = collection.foreignKey(this.foreignKey)
|
||||
}
|
||||
|
||||
const where : any = options?.where ?? this.where
|
||||
if (typeof where == 'string') {
|
||||
const whereString = where as string
|
||||
if (whereString.length > 0) {
|
||||
filter = (filter != null) ? filter.where(where) : collection.where(where)
|
||||
}
|
||||
} else {
|
||||
filter = (filter != null) ? filter.where(where) : collection.where(where)
|
||||
}
|
||||
|
||||
let query : UniCloudDBQuery | null = null
|
||||
if (this.field.length > 0) {
|
||||
query = (filter != null) ? filter.field(this.field) : collection.field(this.field)
|
||||
}
|
||||
if (this.groupby.length > 0) {
|
||||
if (query != null) {
|
||||
query = query.groupBy(this.groupby)
|
||||
} else if (filter != null) {
|
||||
query = filter.groupBy(this.groupby)
|
||||
}
|
||||
}
|
||||
if (this.groupField.length > 0) {
|
||||
if (query != null) {
|
||||
query = query.groupField(this.groupField)
|
||||
} else if (filter != null) {
|
||||
query = filter.groupField(this.groupField)
|
||||
}
|
||||
}
|
||||
if (this.distinct == true) {
|
||||
if (query != null) {
|
||||
query = query.distinct(this.field)
|
||||
} else if (filter != null) {
|
||||
query = filter.distinct(this.field)
|
||||
}
|
||||
}
|
||||
if (this.orderby.length > 0) {
|
||||
if (query != null) {
|
||||
query = query.orderBy(this.orderby)
|
||||
} else if (filter != null) {
|
||||
query = filter.orderBy(this.orderby)
|
||||
}
|
||||
}
|
||||
|
||||
const size = this.mixinDatacomPage.size
|
||||
const current = this.mixinDatacomPage.current
|
||||
if (query != null) {
|
||||
query = query.skip(size * (current - 1)).limit(size)
|
||||
} else if (filter != null) {
|
||||
query = filter.skip(size * (current - 1)).limit(size)
|
||||
} else {
|
||||
query = collection.skip(size * (current - 1)).limit(size)
|
||||
}
|
||||
|
||||
const getOptions = {} as UTSJSONObject
|
||||
const treeOptions = {
|
||||
limitLevel: this.limitlevel,
|
||||
startWith: this.startwith
|
||||
}
|
||||
const getCount : boolean = options?.getCount ?? this.getcount
|
||||
if (this.getcount == true) {
|
||||
getOptions['getCount'] = getCount
|
||||
}
|
||||
|
||||
const getTree : any = options?.getTree ?? this.gettree
|
||||
if (typeof getTree == 'string') {
|
||||
const getTreeString = getTree as string
|
||||
if (getTreeString.length > 0) {
|
||||
getOptions['getTree'] = treeOptions
|
||||
}
|
||||
} else if (typeof getTree == 'object') {
|
||||
getOptions['getTree'] = treeOptions
|
||||
} else {
|
||||
getOptions['getTree'] = getTree
|
||||
}
|
||||
|
||||
const getTreePath = options?.getTreePath ?? this.gettreepath
|
||||
if (typeof getTreePath == 'string') {
|
||||
const getTreePathString = getTreePath as string
|
||||
if (getTreePathString.length > 0) {
|
||||
getOptions['getTreePath'] = getTreePath
|
||||
}
|
||||
} else {
|
||||
getOptions['getTreePath'] = getTreePath
|
||||
}
|
||||
|
||||
return query.get(getOptions)
|
||||
}
|
||||
}
|
||||
})
|
||||
732
node_modules/@dcloudio/uni-components/lib-x/unicloud-db/unicloud-db.vue
generated
vendored
Normal file
732
node_modules/@dcloudio/uni-components/lib-x/unicloud-db/unicloud-db.vue
generated
vendored
Normal file
@@ -0,0 +1,732 @@
|
||||
<template>
|
||||
<uni-cloud-db-element ref="UniCloudDB">
|
||||
<slot :data="dataList" :loading="loading" :hasMore="hasMore" :pagination="pagination" :error="error" />
|
||||
</uni-cloud-db-element>
|
||||
</template>
|
||||
|
||||
<script lang="uts">
|
||||
//#ifdef APP
|
||||
import { SlotsType } from 'vue'
|
||||
//#endif
|
||||
|
||||
//#ifdef WEB || APP-IOS || MP || APP-HARMONY
|
||||
let registerFlag = false
|
||||
//#endif
|
||||
|
||||
const EVENT_LOAD = 'load'
|
||||
const EVENT_ERROR = 'error'
|
||||
const PAGE_MODE_ADD = 'add'
|
||||
const PAGE_MODE_REPLACE = 'replace'
|
||||
const LOAD_MODE_AUTO = 'auto'
|
||||
const LOAD_MODE_MANUAL = 'manual'
|
||||
// const LOAD_MODE_ONREADY = 'onready'
|
||||
|
||||
type SuccessCallback<T> = (res : T | null) => void | null
|
||||
type FailCallback = (err : any | null) => void | null
|
||||
type CompleteCallback = () => void | null
|
||||
|
||||
type GetSuccessCallback = SuccessCallback<UniCloudDBGetResult>
|
||||
type AddSuccessCallback = SuccessCallback<UniCloudDBAddResult>
|
||||
type RemoveSuccessCallback = SuccessCallback<UniCloudDBRemoveResult>
|
||||
type UpdateSuccessCallback = SuccessCallback<UniCloudDBUpdateResult>
|
||||
|
||||
export type UniCloudDBComponentPaginationType = {
|
||||
current : number,
|
||||
size : number,
|
||||
count : number
|
||||
}
|
||||
|
||||
export type UniCloudDBComponentLoadDataOptions = {
|
||||
clear ?: boolean | null,
|
||||
current ?: number | null,
|
||||
success ?: GetSuccessCallback,
|
||||
fail ?: FailCallback,
|
||||
complete ?: CompleteCallback,
|
||||
}
|
||||
|
||||
export type UniCloudDBComponentAddOptions = {
|
||||
/**
|
||||
* @default true
|
||||
*/
|
||||
showToast ?: boolean | null,
|
||||
toastTitle ?: string | null,
|
||||
/**
|
||||
* @default true
|
||||
*/
|
||||
needLoading ?: boolean | null,
|
||||
loadingTitle ?: string | null,
|
||||
success ?: AddSuccessCallback,
|
||||
fail ?: FailCallback,
|
||||
complete ?: CompleteCallback,
|
||||
}
|
||||
|
||||
export type UniCloudDBComponentRemoveOptions = {
|
||||
confirmTitle ?: string | null,
|
||||
confirmContent ?: string | null,
|
||||
/**
|
||||
* @default true
|
||||
*/
|
||||
needConfirm ?: boolean | null,
|
||||
/**
|
||||
* @default true
|
||||
*/
|
||||
needLoading ?: boolean | null,
|
||||
loadingTitle ?: string | null,
|
||||
success ?: RemoveSuccessCallback,
|
||||
fail ?: FailCallback,
|
||||
complete ?: CompleteCallback,
|
||||
}
|
||||
|
||||
export type UniCloudDBComponentUpdateOptions = {
|
||||
/**
|
||||
* @default true
|
||||
*/
|
||||
showToast ?: boolean | null,
|
||||
toastTitle ?: string | null,
|
||||
confirmTitle ?: string | null,
|
||||
confirmContent ?: string | null,
|
||||
/**
|
||||
* @default true
|
||||
*/
|
||||
needConfirm ?: boolean | null,
|
||||
/**
|
||||
* @default true
|
||||
*/
|
||||
needLoading ?: boolean | null,
|
||||
loadingTitle ?: string | null,
|
||||
success ?: UpdateSuccessCallback,
|
||||
fail ?: FailCallback,
|
||||
complete ?: CompleteCallback,
|
||||
}
|
||||
|
||||
function cast_callback<T>(options : any | null) : T | null {
|
||||
return options as T | null
|
||||
}
|
||||
|
||||
//#ifdef APP-ANDROID
|
||||
export class UniCloudDBElement extends UniViewElementImpl {
|
||||
constructor(data : INodeData, pageNode : PageNode) {
|
||||
super(data, pageNode)
|
||||
}
|
||||
//#endif
|
||||
//#ifdef WEB || APP-IOS || MP
|
||||
const RealUniElementImpl = typeof UniElementImpl === 'undefined' ? class {} : UniElementImpl
|
||||
//#endif
|
||||
//#ifdef APP-HARMONY
|
||||
const RealUniElementImpl = typeof UniViewElementImpl === 'undefined' ? class {} : UniViewElementImpl
|
||||
//#endif
|
||||
//#ifdef WEB || APP-IOS || MP || APP-HARMONY
|
||||
export class UniCloudDBElement extends RealUniElementImpl {
|
||||
constructor(data : INodeData, pageNode : PageNode) {
|
||||
super(data, pageNode);
|
||||
const TagName = 'UNICLOUD-DB';
|
||||
Object.defineProperty(this, 'tagName', {
|
||||
value: TagName,
|
||||
writable: false
|
||||
});
|
||||
Object.defineProperty(this, 'nodeName', {
|
||||
value: TagName,
|
||||
writable: false
|
||||
});
|
||||
}
|
||||
//#endif
|
||||
|
||||
dataList : Array<UTSJSONObject> = []
|
||||
|
||||
loadData(options : UTSJSONObject = {}) {
|
||||
this.onLoadData({
|
||||
clear: options.getBoolean('clear'),
|
||||
current: options.getNumber('current'),
|
||||
success: cast_callback<GetSuccessCallback>(options['success']),
|
||||
fail: cast_callback<FailCallback>(options['fail']),
|
||||
complete: cast_callback<CompleteCallback>(options['complete'])
|
||||
} as UniCloudDBComponentLoadDataOptions)
|
||||
}
|
||||
|
||||
loadMore() {
|
||||
this.onLoadMore()
|
||||
}
|
||||
|
||||
add(value : UTSJSONObject, options : UTSJSONObject) {
|
||||
this.onAdd(value, {
|
||||
showToast: options.getBoolean('showToast') ?? true,
|
||||
toastTitle: options.getString('toastTitle'),
|
||||
needLoading: options.getBoolean('needLoading') ?? true,
|
||||
loadingTitle: options.getString('loadingTitle'),
|
||||
success: cast_callback<AddSuccessCallback>(options['success']),
|
||||
fail: cast_callback<FailCallback>(options['fail']),
|
||||
complete: cast_callback<CompleteCallback>(options['complete'])
|
||||
} as UniCloudDBComponentAddOptions)
|
||||
}
|
||||
// @ts-ignore
|
||||
remove(id : any, options : UTSJSONObject) {
|
||||
//#ifdef WEB || APP-IOS || APP-HARMONY
|
||||
// @ts-ignore
|
||||
if (arguments.length == 0) {
|
||||
super.remove()
|
||||
return
|
||||
}
|
||||
//#endif
|
||||
|
||||
this.onRemove(id, {
|
||||
confirmTitle: options.getString('confirmTitle'),
|
||||
confirmContent: options.getString('confirmContent'),
|
||||
needConfirm: options.getBoolean('needConfirm') ?? true,
|
||||
needLoading: options.getBoolean('needLoading') ?? true,
|
||||
loadingTitle: options.getString('loadingTitle'),
|
||||
success: cast_callback<RemoveSuccessCallback>(options['success']),
|
||||
fail: cast_callback<FailCallback>(options['fail']),
|
||||
complete: cast_callback<CompleteCallback>(options['complete'])
|
||||
} as UniCloudDBComponentRemoveOptions)
|
||||
}
|
||||
|
||||
update(id : string, value : UTSJSONObject, options : UTSJSONObject) {
|
||||
this.onUpdate(id, value, {
|
||||
showToast: options.getBoolean('showToast') ?? true,
|
||||
toastTitle: options.getString('toastTitle'),
|
||||
confirmTitle: options.getString('confirmTitle'),
|
||||
confirmContent: options.getString('confirmContent'),
|
||||
needConfirm: options.getBoolean('needConfirm') ?? true,
|
||||
needLoading: options.getBoolean('needLoading') ?? true,
|
||||
loadingTitle: options.getString('loadingTitle'),
|
||||
success: cast_callback<UpdateSuccessCallback>(options['success']),
|
||||
fail: cast_callback<FailCallback>(options['fail']),
|
||||
complete: cast_callback<CompleteCallback>(options['complete'])
|
||||
} as UniCloudDBComponentUpdateOptions)
|
||||
}
|
||||
|
||||
onLoadData! : (_ : UniCloudDBComponentLoadDataOptions) => Promise<void>
|
||||
onLoadMore! : () => void
|
||||
onAdd! : (value : UTSJSONObject, options : UniCloudDBComponentAddOptions) => void
|
||||
onUpdate!: (id : string, value : UTSJSONObject, options : UniCloudDBComponentUpdateOptions) => void
|
||||
onRemove!: (id : any, options : UniCloudDBComponentRemoveOptions) => void
|
||||
}
|
||||
|
||||
export default {
|
||||
name: 'UniCloudDB',
|
||||
rootElement: {
|
||||
name: 'uni-cloud-db-element',
|
||||
class: UniCloudDBElement
|
||||
},
|
||||
slots: Object as SlotsType<{
|
||||
default : {
|
||||
data : Array<UTSJSONObject>,
|
||||
loading : boolean,
|
||||
hasMore : boolean,
|
||||
pagination : UniCloudDBComponentPaginationType,
|
||||
error : UniCloudError | null
|
||||
}
|
||||
}>,
|
||||
props: {
|
||||
collection: {
|
||||
type: [String, Object],
|
||||
default: ''
|
||||
},
|
||||
field: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
orderby: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
where: {
|
||||
type: [String, Object],
|
||||
default: ''
|
||||
},
|
||||
pageData: {
|
||||
type: String,
|
||||
default: 'add'
|
||||
},
|
||||
pageCurrent: {
|
||||
type: Number,
|
||||
default: 1
|
||||
},
|
||||
pageSize: {
|
||||
type: Number,
|
||||
default: 20
|
||||
},
|
||||
getcount: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
gettree: {
|
||||
type: [String, Object],
|
||||
default: ''
|
||||
},
|
||||
gettreepath: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
startwith: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
limitlevel: {
|
||||
type: Number,
|
||||
default: 10
|
||||
},
|
||||
groupby: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
groupField: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
distinct: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
pageIndistinct: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
foreignKey: {
|
||||
type: String,
|
||||
default: ''
|
||||
},
|
||||
loadtime: {
|
||||
type: String,
|
||||
default: 'auto'
|
||||
},
|
||||
manual: {
|
||||
type: Boolean,
|
||||
default: false
|
||||
},
|
||||
ssrKey: {
|
||||
type: String,
|
||||
default: ""
|
||||
}
|
||||
},
|
||||
data() {
|
||||
return {
|
||||
//#ifdef WEB || MP || APP-HARMONY
|
||||
// TODO 修复类型错误
|
||||
// @ts-ignore
|
||||
dataList: ssrRef([] as Array<UTSJSONObject>) as Array<UTSJSONObject>,
|
||||
//#endif
|
||||
//#ifndef WEB
|
||||
dataList: [] as Array<UTSJSONObject>,
|
||||
//#endif
|
||||
// dataList: [] as Array<UTSJSONObject>,
|
||||
loading: false,
|
||||
hasMore: false,
|
||||
isEnded: false,
|
||||
pagination: {
|
||||
current: 1,
|
||||
size: 20,
|
||||
count: 0,
|
||||
} as UniCloudDBComponentPaginationType,
|
||||
error: null as UniCloudError | null
|
||||
}
|
||||
},
|
||||
//#ifdef WEB || APP-IOS || MP || APP-HARMONY
|
||||
beforeCreate() {
|
||||
if (!registerFlag) {
|
||||
registerFlag = true
|
||||
// @ts-ignore
|
||||
typeof customElements !== 'undefined' && customElements.define(
|
||||
'uni-cloud-db-element',
|
||||
// @ts-ignore
|
||||
UniCloudDBElement,
|
||||
)
|
||||
}
|
||||
},
|
||||
//#endif
|
||||
//#ifdef WEB
|
||||
async serverPrefetch() : Promise<any> {
|
||||
// @ts-ignore
|
||||
if (!this.manual && this.loadtime === 'auto') {
|
||||
// @ts-ignore
|
||||
return this.loadData({})
|
||||
}
|
||||
},
|
||||
//#endif
|
||||
created() {
|
||||
this.pagination.current = this.pageCurrent
|
||||
this.pagination.size = this.pageSize
|
||||
|
||||
this.$watch(
|
||||
() : any => [
|
||||
this.pageCurrent,
|
||||
this.pageSize,
|
||||
this.collection,
|
||||
this.field,
|
||||
this.getcount,
|
||||
this.orderby,
|
||||
this.where,
|
||||
this.groupby,
|
||||
this.groupField,
|
||||
this.distinct
|
||||
],
|
||||
(newValue : Array<any>, oldValue : Array<any>) => {
|
||||
this.pagination.size = this.pageSize
|
||||
if (newValue[0] !== oldValue[0]) {
|
||||
this.pagination.current = this.pageCurrent
|
||||
}
|
||||
|
||||
if (this.loadtime == LOAD_MODE_MANUAL) {
|
||||
return
|
||||
}
|
||||
|
||||
let needReset = false
|
||||
for (let i = 2; i < newValue.length; i++) {
|
||||
if (newValue[i] !== oldValue[i]) {
|
||||
needReset = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if (needReset) {
|
||||
this.clear()
|
||||
this.reset()
|
||||
}
|
||||
|
||||
this.get(null)
|
||||
}
|
||||
)
|
||||
|
||||
if (!this.manual && this.loadtime == LOAD_MODE_AUTO && this.dataList.length == 0) {
|
||||
if (typeof this.collection == 'string') {
|
||||
const collectionString = this.collection as string
|
||||
if (collectionString.length == 0) {
|
||||
return
|
||||
}
|
||||
} else if (Array.isArray(this.collection)) {
|
||||
const collectionArray = this.collection as Array<any>
|
||||
if (collectionArray.length == 0) {
|
||||
return
|
||||
}
|
||||
}
|
||||
this.get(null)
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
const uniCloudDBElement = this.$refs['UniCloudDB'] as UniCloudDBElement
|
||||
uniCloudDBElement.dataList = this.dataList;
|
||||
|
||||
//#ifdef APP
|
||||
uniCloudDBElement.onLoadData = this.loadData;
|
||||
uniCloudDBElement.onLoadMore = this.loadMore;
|
||||
uniCloudDBElement.onAdd = this.add;
|
||||
uniCloudDBElement.onUpdate = this.update;
|
||||
uniCloudDBElement.onRemove = this.remove;
|
||||
//#endif
|
||||
//#ifdef WEB || MP || APP-HARMONY
|
||||
uniCloudDBElement.onLoadData = this.loadData.bind(this);
|
||||
uniCloudDBElement.onLoadMore = this.loadMore.bind(this);
|
||||
uniCloudDBElement.onAdd = this.add.bind(this);
|
||||
uniCloudDBElement.onUpdate = this.update.bind(this);
|
||||
uniCloudDBElement.onRemove = this.remove.bind(this);
|
||||
//#endif
|
||||
},
|
||||
methods: {
|
||||
async loadData(options : UniCloudDBComponentLoadDataOptions) : Promise<void> {
|
||||
let clear = (options.clear != null && options.clear == true)
|
||||
if (clear == true) {
|
||||
if (this.pageData == PAGE_MODE_REPLACE) {
|
||||
this.clear()
|
||||
}
|
||||
this.reset()
|
||||
}
|
||||
|
||||
await this.get(options)
|
||||
},
|
||||
loadMore() {
|
||||
if (this.isEnded || this.loading) {
|
||||
return
|
||||
}
|
||||
|
||||
if (this.pageData == PAGE_MODE_ADD) {
|
||||
this.pagination.current++
|
||||
}
|
||||
|
||||
this.get(null)
|
||||
},
|
||||
refresh() {
|
||||
this.clear()
|
||||
this.get(null)
|
||||
},
|
||||
clear() {
|
||||
this.isEnded = false
|
||||
this.dataList.length = 0
|
||||
},
|
||||
reset() {
|
||||
this.pagination.current = 1
|
||||
},
|
||||
async get(options? : UniCloudDBComponentLoadDataOptions | null) : Promise<void> {
|
||||
let loadAfterClear = false
|
||||
if (options != null && options.clear != null && options.clear == true) {
|
||||
loadAfterClear = true
|
||||
}
|
||||
if (options != null && options.current != null) {
|
||||
this.pagination.current = options.current!
|
||||
}
|
||||
|
||||
this.error = null
|
||||
|
||||
this.loading = true
|
||||
await this.getExec().then((res : UniCloudDBGetResult) => {
|
||||
const data = res.data
|
||||
const count = res.count
|
||||
|
||||
this.isEnded = (count != null) ? (this.pagination.current * this.pagination.size >= count) : (data.length < this.pageSize)
|
||||
this.hasMore = !this.isEnded
|
||||
|
||||
if (this.getcount && count != null) {
|
||||
this.pagination.count = count
|
||||
}
|
||||
|
||||
this._dispatchEvent(EVENT_LOAD, data)
|
||||
|
||||
if (loadAfterClear || this.pageData == PAGE_MODE_REPLACE) {
|
||||
this.dataList = data
|
||||
} else {
|
||||
this.dataList.push(...data)
|
||||
}
|
||||
|
||||
options?.success?.(res)
|
||||
}).catch((err : any | null) => {
|
||||
this._requestFail(err, null)
|
||||
options?.fail?.(err)
|
||||
}).then(() => {
|
||||
this.loading = false
|
||||
options?.complete?.()
|
||||
}, () => {
|
||||
this.loading = false
|
||||
options?.complete?.()
|
||||
})
|
||||
},
|
||||
add(value : UTSJSONObject, options : UniCloudDBComponentAddOptions) {
|
||||
this._needLoading(options.needLoading, options.loadingTitle)
|
||||
const db = uniCloud.databaseForJQL()
|
||||
db.collection(this._getMainCollection()).add(value).then<void>((res : UniCloudDBAddResult) => {
|
||||
options.success?.(res)
|
||||
this._isShowToast(options.showToast ?? false, options.toastTitle ?? 'add success')
|
||||
}).catch((err) => {
|
||||
this._requestFail(err, options.fail)
|
||||
}).then(() => {
|
||||
this._requestComplete(options.complete, options.needLoading)
|
||||
}, () => {
|
||||
this._requestComplete(options.complete, options.needLoading)
|
||||
})
|
||||
},
|
||||
update(id : string, value : UTSJSONObject, options : UniCloudDBComponentUpdateOptions) {
|
||||
if (options.needConfirm == true) {
|
||||
uni.showModal({
|
||||
title: options.confirmTitle,
|
||||
content: options.confirmContent,
|
||||
showCancel: true,
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
this._update(id, value, options)
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this._update(id, value, options)
|
||||
}
|
||||
},
|
||||
remove(id : any, options : UniCloudDBComponentRemoveOptions) {
|
||||
const ids = Array.isArray(id) ? (id as Array<any>) : [id]
|
||||
if (options.needConfirm == true) {
|
||||
uni.showModal({
|
||||
title: options.confirmTitle,
|
||||
content: options.confirmContent,
|
||||
showCancel: true,
|
||||
success: (res) => {
|
||||
if (res.confirm) {
|
||||
this._remove(ids, options)
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
this._remove(ids, options)
|
||||
}
|
||||
},
|
||||
_update(id : string, value : UTSJSONObject, options : UniCloudDBComponentUpdateOptions) {
|
||||
this._needLoading(options.needLoading, options.loadingTitle)
|
||||
const db = uniCloud.databaseForJQL()
|
||||
db.collection(this._getMainCollection()).doc(id).update(value).then((res) => {
|
||||
options.success?.(res)
|
||||
this._isShowToast(options.showToast ?? false, options.toastTitle ?? 'update success')
|
||||
}).catch((err : any | null) => {
|
||||
this._requestFail(err, options.fail)
|
||||
}).then(() => {
|
||||
this._requestComplete(options.complete, options.needLoading)
|
||||
}, () => {
|
||||
this._requestComplete(options.complete, options.needLoading)
|
||||
})
|
||||
},
|
||||
_remove(ids : Array<any>, options : UniCloudDBComponentRemoveOptions) {
|
||||
this._needLoading(options.needLoading, options.loadingTitle)
|
||||
const db = uniCloud.databaseForJQL()
|
||||
const dbCommand = db.command
|
||||
db.collection(this._getMainCollection()).where({
|
||||
_id: dbCommand.in(ids)
|
||||
}).remove().then((res) => {
|
||||
options.success?.(res)
|
||||
if (this.pageData == PAGE_MODE_REPLACE) {
|
||||
this.refresh()
|
||||
} else {
|
||||
this._removeData(ids)
|
||||
}
|
||||
}).catch((err : any | null) => {
|
||||
this._requestFail(err, options.fail)
|
||||
}).then(() => {
|
||||
this._requestComplete(options.complete, options.needLoading)
|
||||
}, () => {
|
||||
this._requestComplete(options.complete, options.needLoading)
|
||||
})
|
||||
},
|
||||
_removeData(ids : Array<any>) {
|
||||
const il = ids.slice(0)
|
||||
const dl = this.dataList
|
||||
for (let i = dl.length - 1; i >= 0; i--) {
|
||||
const index = il.indexOf(dl[i]['_id'])
|
||||
if (index >= 0) {
|
||||
dl.splice(i, 1)
|
||||
il.splice(index, 1)
|
||||
}
|
||||
}
|
||||
},
|
||||
_isShowToast(showToast : boolean, title : string) {
|
||||
if (showToast == true) {
|
||||
uni.showToast({
|
||||
title: title
|
||||
})
|
||||
}
|
||||
},
|
||||
_needLoading(needLoading ?: boolean | null, title ?: string | null) {
|
||||
if (needLoading == true) {
|
||||
uni.showLoading({
|
||||
mask: true,
|
||||
title: title ?? ''
|
||||
})
|
||||
}
|
||||
},
|
||||
_requestFail(err ?: any | null, callback ?: FailCallback | null) {
|
||||
callback?.(err)
|
||||
this.error = err as UniCloudError
|
||||
this.$emit(EVENT_ERROR, err)
|
||||
},
|
||||
_requestComplete(callback ?: CompleteCallback | null, needLoading ?: boolean | null) {
|
||||
callback?.()
|
||||
if (needLoading == true) {
|
||||
uni.hideLoading()
|
||||
}
|
||||
},
|
||||
getExec() : Promise<UniCloudDBGetResult> {
|
||||
return this.getTemp()
|
||||
},
|
||||
getTemp() : Promise<UniCloudDBGetResult> {
|
||||
let db = uniCloud.databaseForJQL()
|
||||
|
||||
let collection = Array.isArray(this.collection) ? db.collection(...(this.collection as Array<any>)) : db.collection(this.collection)
|
||||
|
||||
let filter : UniCloudDBFilter | null = null
|
||||
if (this.foreignKey.length > 0) {
|
||||
filter = collection.foreignKey(this.foreignKey)
|
||||
}
|
||||
|
||||
if (typeof this.where == 'string') {
|
||||
const whereString = this.where as string
|
||||
if (whereString.length > 0) {
|
||||
filter = (filter != null) ? filter.where(this.where) : collection.where(this.where)
|
||||
}
|
||||
} else if (typeof this.where == 'object') {
|
||||
filter = (filter != null) ? filter.where(this.where) : collection.where(this.where)
|
||||
}
|
||||
|
||||
let query : UniCloudDBQuery | null = null
|
||||
if (this.field.length > 0) {
|
||||
query = (filter != null) ? filter.field(this.field) : collection.field(this.field)
|
||||
}
|
||||
if (this.groupby.length > 0) {
|
||||
if (query != null) {
|
||||
query = query.groupBy(this.groupby)
|
||||
} else if (filter != null) {
|
||||
query = filter.groupBy(this.groupby)
|
||||
}
|
||||
}
|
||||
if (this.groupField.length > 0) {
|
||||
if (query != null) {
|
||||
query = query.groupField(this.groupField)
|
||||
} else if (filter != null) {
|
||||
query = filter.groupField(this.groupField)
|
||||
}
|
||||
}
|
||||
if (this.distinct == true) {
|
||||
if (query != null) {
|
||||
query = query.distinct(this.field)
|
||||
} else if (filter != null) {
|
||||
query = filter.distinct(this.field)
|
||||
}
|
||||
}
|
||||
if (this.orderby.length > 0) {
|
||||
if (query != null) {
|
||||
query = query.orderBy(this.orderby)
|
||||
} else if (filter != null) {
|
||||
query = filter.orderBy(this.orderby)
|
||||
} else {
|
||||
query = collection.orderBy(this.orderby)
|
||||
}
|
||||
}
|
||||
|
||||
const size = this.pagination.size
|
||||
const current = this.pagination.current
|
||||
const skipSize = size * (current - 1)
|
||||
if (query != null) {
|
||||
query = query.skip(skipSize).limit(size)
|
||||
} else if (filter != null) {
|
||||
query = filter.skip(skipSize).limit(size)
|
||||
} else {
|
||||
query = collection.skip(skipSize).limit(size)
|
||||
}
|
||||
|
||||
const getOptions = {}
|
||||
const treeOptions = {
|
||||
limitLevel: this.limitlevel,
|
||||
startWith: this.startwith
|
||||
}
|
||||
if (this.getcount == true) {
|
||||
getOptions['getCount'] = this.getcount
|
||||
}
|
||||
if (typeof this.gettree == 'string') {
|
||||
const getTreeString = this.gettree as string
|
||||
if (getTreeString.length > 0) {
|
||||
getOptions['getTree'] = treeOptions
|
||||
}
|
||||
} else if (typeof this.gettree == 'object') {
|
||||
getOptions['getTree'] = treeOptions
|
||||
}
|
||||
if (this.gettreepath == true) {
|
||||
getOptions['getTreePath'] = treeOptions
|
||||
}
|
||||
|
||||
return query.get(getOptions)
|
||||
},
|
||||
_getMainCollection() : string {
|
||||
if (typeof this.collection === 'string') {
|
||||
return (this.collection as string).split(',')[0]
|
||||
}
|
||||
|
||||
if (Array.isArray(this.collection)) {
|
||||
const array = this.collection as Array<any>
|
||||
const index = array[0] as UTSJSONObject
|
||||
const collection = index.getString('$db[0].$param[0]')
|
||||
return collection ?? ''
|
||||
}
|
||||
|
||||
return ''
|
||||
},
|
||||
_dispatchEvent(type : string, data : Array<UTSJSONObject>) {
|
||||
this.$emit(type, data, this.isEnded, {
|
||||
current: this.pagination.current,
|
||||
size: this.pagination.size,
|
||||
count: this.pagination.count
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
</script>
|
||||
Reference in New Issue
Block a user