提交 e7160c3b 编写于 作者: zhaofengliang920817's avatar zhaofengliang920817

优化 provider 示例。

上级 6a9810ed
<template> <template>
<!-- #ifdef APP --> <!-- #ifdef APP -->
<scroll-view class="page-scroll-view"> <scroll-view class="page-scroll-view">
<!-- #endif --> <!-- #endif -->
<view class="page"> <view class="page">
<page-head :title="title"></page-head> <page-head :title="title"></page-head>
<view class="service-item" v-for="(item, index) in serviceList" :key="index"> <view class="service-item" v-for="(item, index) in serviceList" :key="index">
<text class="service-name">{{item.name}}:</text> <text class="service-name">{{item.name}}:</text>
<view class="provider-list"> <view class="provider-list">
<!-- #ifdef APP-IOS --> <text class="provider-item" v-for="(item2, index2) in item.provider" :key="index2">
<text class="provider-item" v-for="(item2, index2) in item.provider" :key="index2"> {{item2}}
{{item2}} {{item.providerObj.length > 0 ? ':' + JSON.stringify(item.providerObj[index2]) : '' }}
{{item.providerObjMap.length > 0 ? ':' + JSON.stringify(item.providerObjMap[index2]) : '' }} </text>
</text> </view>
<!-- #endif --> </view>
<!-- #ifdef APP-ANDROID --> <button class="btn-get-provider" type="primary" @click="getProvider">getProviderSync</button>
<text class="provider-item" v-for="(item2, index2) in item.provider" :key="index2"> </view>
{{item2}} <!-- #ifdef APP -->
{{item.providerObj.length > 0 ? ':' + JSON.stringify(item.providerObj[index2]) : '' }} </scroll-view>
</text> <!-- #endif -->
<!-- #endif --> </template>
</view> <script>
</view> type ProviderItem = {
<button class="btn-get-provider" type="primary" @click="getProvider">getProviderSync</button> service : string,
</view> name : string,
<!-- #ifdef APP --> provider : string[],
</scroll-view> providerObj : UniProvider[]
<!-- #endif --> }
</template>
export default {
<script> data() {
type ProviderItem = { return {
service : string, title: 'provider',
name : string, flag: false,
provider : string[], serviceList: [
providerObj : UniProvider[], { service: "payment", name: "支付", provider: [], providerObj: []},
providerObjMap : Object[], { service: "location", name: "定位", provider: [], providerObj: []}
} ] as ProviderItem[],
providerIds: [] as string[],
export default { providerObjects: [] as UniProvider[],
data() { }
return { },
title: 'provider', methods: {
flag: false, getProvider() {
serviceList: [ this.serviceList.forEach((item : ProviderItem) => {
{ service: "payment", name: "支付", provider: [], providerObj: [], providerObjMap: [] }, var provider = uni.getProviderSync({
{ service: "location", name: "定位", provider: [], providerObj: [], providerObjMap: [] } service: item.service,
] as ProviderItem[], } as GetProviderSyncOptions)
providerIds: [] as string[], console.log('getProvider',provider,provider.providerIds.length)
providerObjects: [] as UniProvider[], provider.providerIds.forEach((value) => {
} console.log('getProvider','value',value)
}, this.providerIds.push(value)
methods: { })
getProvider() { this.updateProvider(item.service, provider.providerIds, provider.providerObjects);
this.serviceList.forEach((item : ProviderItem) => { })
var provider = uni.getProviderSync({ },
service: item.service, updateProvider(service : string, provider : string[] | null, uniProvider : UniProvider[]) {
} as GetProviderSyncOptions) const item : ProviderItem | null = this.serviceList.find((item : ProviderItem) : boolean => {
provider.providerIds.forEach((value) => { return item.service == service
this.providerIds.push(value) });
}) if (item != null && provider != null) {
this.updateProvider(item.service, provider.providerIds, provider.providerObjects); item.provider = provider
})
}, item.providerObj = uniProvider
updateProvider(service : string, provider : string[] | null, uniProvider : UniProvider[]) { item.providerObj.forEach((obj) => {
const item : ProviderItem | null = this.serviceList.find((item : ProviderItem) : boolean => { this.providerObjects.push(obj)
return item.service == service // #ifdef APP-ANDROID
}); if (obj instanceof UniPaymentWxpayProvider) {//判断微信是否安装
if (item != null && provider != null) { var isWeChatInstalled = (obj as UniPaymentWxpayProvider).isWeChatInstalled
item.provider = provider console.log(isWeChatInstalled)
}
item.providerObj = uniProvider // #endif
item.providerObj.forEach((obj) => { })
this.providerObjects.push(obj) }
// #ifdef APP-ANDROID }
if (obj instanceof UniPaymentWxpayProvider) {//判断微信是否安装 }
var isWeChatInstalled = (obj as UniPaymentWxpayProvider).isWeChatInstalled }
console.log(isWeChatInstalled) </script>
}
// #endif <style>
.page {
// #ifdef APP-IOS padding: 15px;
const providerObjMap = this.getProviderObjectMap(obj) }
console.log(providerObjMap)
item.providerObjMap.push(providerObjMap) .service-item {
// #endif margin-top: 10px;
}
})
} .service-name {
}, font-weight: bold;
}
// #ifdef APP-IOS
getProviderObjectMap(provider : UniProvider) { .provider-list {
margin-left: 32px;
const map = new Map() }
if (this.hasProperty(provider, 'id') && provider.id != null) {
map.set('id', provider.id) .provider-item {
} line-height: 1.5;
}
if (this.hasProperty(provider, 'description') && provider.description != null) {
map.set('description', provider.description) .btn-get-provider {
} margin-top: 30px;
}
if (this.hasProperty(provider, 'isAppExist') && provider.isAppExist != null) {
map.set('isAppExist', provider.isAppExist)
}
if (this.hasProperty(provider, 'isWeChatInstalled') && provider.isWeChatInstalled != null) {
map.set('isWeChatInstalled', provider.isWeChatInstalled)
}
if (this.hasMethod(provider, 'uniqueMethodForProvider')) {
map.set('func-uniqueMethodForProvider', provider.uniqueMethodForProvider())
}
return Object.fromEntries(map)
},
hasMethod(provider : UniProvider, methodName : string) {
return typeof provider[methodName] === 'function';
},
hasProperty(provider : UniProvider, propName : string) {
return propName in provider
}
// #endif
}
}
</script>
<style>
.page {
padding: 15px;
}
.service-item {
margin-top: 10px;
}
.service-name {
font-weight: bold;
}
.provider-list {
margin-left: 32px;
}
.provider-item {
line-height: 1.5;
}
.btn-get-provider {
margin-top: 30px;
}
</style> </style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册