<template>
  <div :type="type" :openid="oid" v-text="oid"/>
</template>

<script>
import {mapActions} from "vuex";

export default {
  name: "AiOpenData",
  props: ['type', 'openid'],
  computed: {
    oid() {
      /**
       * 后端返回格式(cropId|userId)
       */
      return this.openid?.split("|")?.[1] || this.openid || ""
    }
  },
  methods: {
    ...mapActions(['injectJWeixin']),
    bindWWOpenData() {
      if (window.WWOpenData) {
        WWOpenData.bind(this.$el)
      } else this.injectJWeixin().then(() => {
        this.$nextTick(() => {
          WWOpenData.bind(this.$el)
        })
      })
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.bindWWOpenData()
    })
  }
}
</script>
