provider.uvue 5.1 KB
Newer Older
1
<template>
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
	<!-- #ifdef APP -->
	<scroll-view class="page-scroll-view">
	<!-- #endif -->
		<view class="page">
			<page-head :title="title"></page-head>
			<view class="service-item" v-for="(item, index) in serviceList" :key="index">
				<text class="service-name">{{item.name}}:</text>
				<view class="provider-list">
					<!-- #ifdef APP-IOS -->
					<text class="provider-item" v-for="(item2, index2) in item.provider" :key="index2">
						{{item2}}
						{{item.providerObjMap.length > 0 ? ':' + JSON.stringify(item.providerObjMap[index2]) : '' }}
					</text>
					<!-- #endif -->
					<!-- #ifdef APP-ANDROID -->
					<text class="provider-item" v-for="(item2, index2) in item.provider" :key="index2">
						{{item2}}
						{{item.providerObj.length > 0 ? ':' + JSON.stringify(item.providerObj[index2]) : '' }}
					</text>
					<!-- #endif -->

				</view>
			</view>
			<button class="btn-get-provider" type="primary" @click="getProviderSync">getProviderSync</button>
			<button class="btn-get-provider" type="primary" @click="getProvider">getProvider</button>
			<button class="btn-get-provider" type="primary"
				@click="getProviderObjectById">getProviderObjectById</button>
		</view>
	<!-- #ifdef APP -->
	</scroll-view>
	<!-- #endif -->
33 34 35
</template>

<script>
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
	type ProviderItem = {
		service : string,
		name : string,
		provider : string[],
		providerObj : UniProvider[],
		providerObjMap : Object[],
	}

	export default {
		data() {
			return {
				title: 'provider',
				flag: false,
				serviceList: [
					{ service: "payment", name: "支付", provider: [], providerObj: [], providerObjMap: [] },
					{ service: "location", name: "定位", provider: [], providerObj: [], providerObjMap: [] }
				] as ProviderItem[],
				providerObjList: [] as UniProvider[]
			}
		},
		methods: {
57 58 59 60 61 62
      clearProviderObj() {
      	this.serviceList.forEach((item : ProviderItem) => {
      		item.providerObj = []
      		item.providerObjMap = []
      	})
      },
63
			getProviderSync() {
64
        this.clearProviderObj()
65 66 67 68 69 70 71 72 73
				this.serviceList.forEach((item : ProviderItem) => {
					let provider = uni.getProviderSync({
						service: item.service
					} as GetProviderOptions)

					this.updateProvider(item.service, provider.providerIds);
				})
			},
			getProvider() {
74
        this.clearProviderObj()
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
				this.serviceList.forEach((item : ProviderItem) => {
					uni.getProvider({
						service: item.service,
						success: (e) => {
							this.updateProvider(item.service, e.providerIds);
						},
						fail: (e) => {
							console.log("获取provider失败:", e);
						}
					} as GetProviderOptions)
				})
			},
			updateProvider(service : string, provider ?: string[] | null) {
				const item : ProviderItem | null = this.serviceList.find((item : ProviderItem) : boolean => {
					item.providerObj = []
					return item.service == service
				});
				if (item != null && provider != null) {
					item.provider = provider
				}

			},
			getProviderObjectById() {
				this.flag = true
				this.getProvider()
				let objList = []
101
        this.clearProviderObj()
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
				this.serviceList.forEach((value) => {
					value.provider.forEach((provider) => {
						let obj = uni.getProviderObjectById({
							service: value.service,
							provider: provider
						})
						if (obj != null) {
							console.log(obj)
							console.log(JSON.stringify(obj))
							value.providerObj.push(obj)
							// #ifdef APP-ANDROID
							if (obj instanceof UniPaymentWxpayProvider) {//判断微信是否安装
								var isWeChatInstalled = (obj as UniPaymentWxpayProvider).isWeChatInstalled
								console.log(isWeChatInstalled)
							}
							// #endif

							// #ifdef APP-IOS
							const providerObjMap = this.getProviderObjectMap(obj)
							console.log(providerObjMap)
							value.providerObjMap.push(providerObjMap)
							// #endif
						}
					})
				})
			},
			// #ifdef APP-IOS
			getProviderObjectMap(provider : UniProvider) {

				const map = new Map()
				if (this.hasProperty(provider, 'id') && provider.id != null) {
					map.set('id', provider.id)
				}

				if (this.hasProperty(provider, 'description') && provider.description != null) {
					map.set('description', provider.description)
				}

				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
		}
	}
162 163 164
</script>

<style>
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
	.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;
	}
188
</style>