uni-link.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <a v-if="isShowA" class="uni-link" :href="href"
  3. :class="{'uni-link--withline':showUnderLine===true||showUnderLine==='true'}"
  4. :style="{color,fontSize:fontSize+'px'}">{{text}}</a>
  5. <text v-else class="uni-link" :class="{'uni-link--withline':showUnderLine===true||showUnderLine==='true'}"
  6. :style="{color,fontSize:fontSize+'px'}" @click="openURL">{{text}}</text>
  7. </template>
  8. <script>
  9. /**
  10. * Link 外部网页超链接组件
  11. * @description uni-link是一个外部网页超链接组件,在小程序内复制url,在app内打开外部浏览器,在h5端打开新网页
  12. * @tutorial https://ext.dcloud.net.cn/plugin?id=1182
  13. * @property {String} href 点击后打开的外部网页url
  14. * @property {String} text 显示的文字
  15. * @property {Boolean} showUnderLine 是否显示下划线
  16. * @property {String} copyTips 在小程序端复制链接时显示的提示语
  17. * @property {String} color 链接文字颜色
  18. * @property {String} fontSize 链接文字大小
  19. * @example * <uni-link href="https://ext.dcloud.net.cn" text="https://ext.dcloud.net.cn"></uni-link>
  20. */
  21. export default {
  22. name: 'uniLink',
  23. props: {
  24. href: {
  25. type: String,
  26. default: ''
  27. },
  28. text: {
  29. type: String,
  30. default: ''
  31. },
  32. showUnderLine: {
  33. type: [Boolean, String],
  34. default: true
  35. },
  36. copyTips: {
  37. type: String,
  38. default: '已自动复制网址,请在手机浏览器里粘贴该网址'
  39. },
  40. color: {
  41. type: String,
  42. default: '#999999'
  43. },
  44. fontSize: {
  45. type: [Number, String],
  46. default: 14
  47. }
  48. },
  49. computed: {
  50. isShowA() {
  51. // #ifdef H5
  52. this._isH5 = true;
  53. // #endif
  54. if ((this.isMail() || this.isTel()) && this._isH5 === true) {
  55. return true;
  56. }
  57. return false;
  58. }
  59. },
  60. created() {
  61. this._isH5 = null;
  62. },
  63. methods: {
  64. isMail() {
  65. return this.href.startsWith('mailto:');
  66. },
  67. isTel() {
  68. return this.href.startsWith('tel:');
  69. },
  70. openURL() {
  71. // #ifdef APP-PLUS
  72. if (this.isTel()) {
  73. this.makePhoneCall(this.href.replace('tel:', ''));
  74. } else {
  75. plus.runtime.openURL(this.href);
  76. }
  77. // #endif
  78. // #ifdef H5
  79. window.open(this.href)
  80. // #endif
  81. // #ifdef MP
  82. uni.setClipboardData({
  83. data: this.href
  84. });
  85. uni.showModal({
  86. content: this.copyTips,
  87. showCancel: false
  88. });
  89. // #endif
  90. },
  91. makePhoneCall(phoneNumber) {
  92. uni.makePhoneCall({
  93. phoneNumber
  94. })
  95. }
  96. }
  97. }
  98. </script>
  99. <style>
  100. /* #ifndef APP-NVUE */
  101. .uni-link {
  102. cursor: pointer;
  103. }
  104. /* #endif */
  105. .uni-link--withline {
  106. text-decoration: underline;
  107. }
  108. </style>