provider.uvue 5.1 KB
Newer Older
1
<template>
M
mahaifeng 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
  <!-- #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 -->
22

M
mahaifeng 已提交
23 24 25 26 27 28 29 30
        </view>
      </view>
      <button class="btn-get-provider" type="primary" @click="getProviderIds">getProviderIds</button>
      <button class="btn-get-provider" type="primary" @click="getProviderObject">getProviderObject</button>
    </view>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
31 32 33
</template>

<script>
M
mahaifeng 已提交
34 35 36 37 38 39 40
  type ProviderItem = {
    service : string,
    name : string,
    provider : string[],
    providerObj : UniProvider[],
    providerObjMap : Object[],
  }
41

M
mahaifeng 已提交
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
  export default {
    data() {
      return {
        title: 'provider',
        flag: false,
        serviceList: [
          { service: "oauth", name: "登陆", provider: [], providerObj: [], providerObjMap: [] },
          { service: "share", name: "分享", provider: [], providerObj: [], providerObjMap: [] },
          { service: "payment", name: "支付", provider: [], providerObj: [], providerObjMap: [] },
          { service: "push", name: "推送", provider: [], providerObj: [], providerObjMap: [] },
          { service: "location", name: "定位", provider: [], providerObj: [], providerObjMap: [] }
        ] as ProviderItem[],
        providerObjList: [] as UniProvider[]
      }
    },
    methods: {
      getProviderIds() {
        this.serviceList.forEach((item : ProviderItem) => {
          let providerIds = uni.getProviderIds({
            service: item.service
          })
          this.updateProvider(item.service, providerIds);
        })
      },
      updateProvider(service : string, provider ?: string[] | null) {
        const item : ProviderItem | null = this.serviceList.find((item : ProviderItem) : boolean => {
          return item.service == service
        });
        if (item != null && provider != null) {
          item.provider = provider
        }
      },
      getProviderObject() {
        this.flag = true
        this.getProviderIds()
        let objList = []
        this.serviceList.forEach((value) => {
          value.provider.forEach((provider) => {
            let obj = uni.getProviderObject({
              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)
M
mahaifeng 已提交
92
              }
M
mahaifeng 已提交
93
              // #endif
94

M
mahaifeng 已提交
95 96 97 98 99 100 101 102 103 104 105
              // #ifdef APP-IOS
              const providerObjMap = this.getProviderObjectMap(obj)
              console.log(providerObjMap)
              value.providerObjMap.push(providerObjMap)
              // #endif
            }
          })
        })
      },
      // #ifdef APP-IOS
      getProviderObjectMap(provider : UniProvider) {
106

M
mahaifeng 已提交
107 108 109 110
        const map = new Map()
        if (this.hasProperty(provider, 'id') && provider.id != null) {
          map.set('id', provider.id)
        }
111

M
mahaifeng 已提交
112 113 114
        if (this.hasProperty(provider, 'description') && provider.description != null) {
          map.set('description', provider.description)
        }
115

M
mahaifeng 已提交
116 117 118
        if (this.hasProperty(provider, 'isAppExist') && provider.isAppExist != null) {
          map.set('isAppExist', provider.isAppExist)
        }
119

M
mahaifeng 已提交
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
        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
    }
  }
138 139 140
</script>

<style>
M
mahaifeng 已提交
141 142 143
  .page {
    padding: 15px;
  }
144

M
mahaifeng 已提交
145 146 147
  .service-item {
    margin-top: 10px;
  }
148

M
mahaifeng 已提交
149 150 151
  .service-name {
    font-weight: bold;
  }
152

M
mahaifeng 已提交
153 154 155
  .provider-list {
    margin-left: 32px;
  }
156

M
mahaifeng 已提交
157 158 159
  .provider-item {
    line-height: 1.5;
  }
160

M
mahaifeng 已提交
161 162 163
  .btn-get-provider {
    margin-top: 30px;
  }
164
</style>