wd-button.vue 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <template>
  2. <button
  3. :id="buttonId"
  4. :hover-class="`${disabled || loading ? '' : 'wd-button--active'}`"
  5. :style="customStyle"
  6. :class="[
  7. 'wd-button',
  8. 'is-' + type,
  9. 'is-' + size,
  10. round ? 'is-round' : '',
  11. hairline ? 'is-hairline' : '',
  12. plain ? 'is-plain' : '',
  13. disabled ? 'is-disabled' : '',
  14. block ? 'is-block' : '',
  15. loading ? 'is-loading' : '',
  16. customClass
  17. ]"
  18. :hover-start-time="hoverStartTime"
  19. :hover-stay-time="hoverStayTime"
  20. :open-type="disabled || loading ? undefined : openType"
  21. :send-message-title="sendMessageTitle"
  22. :send-message-path="sendMessagePath"
  23. :send-message-img="sendMessageImg"
  24. :app-parameter="appParameter"
  25. :show-message-card="showMessageCard"
  26. :session-from="sessionFrom"
  27. :lang="lang"
  28. :hover-stop-propagation="hoverStopPropagation"
  29. :scope="scope"
  30. @click="handleClick"
  31. @getAuthorize="handleGetAuthorize"
  32. @getuserinfo="handleGetuserinfo"
  33. @contact="handleConcat"
  34. @getphonenumber="handleGetphonenumber"
  35. @getrealtimephonenumber="handleGetrealtimephonenumber"
  36. @error="handleError"
  37. @launchapp="handleLaunchapp"
  38. @opensetting="handleOpensetting"
  39. @chooseavatar="handleChooseavatar"
  40. @agreeprivacyauthorization="handleAgreePrivacyAuthorization"
  41. >
  42. <view class="wd-button__content">
  43. <view v-if="loading" class="wd-button__loading">
  44. <view class="wd-button__loading-svg" :style="loadingStyle"></view>
  45. </view>
  46. <wd-icon v-else-if="icon" custom-class="wd-button__icon" :name="icon" :classPrefix="classPrefix"></wd-icon>
  47. <view class="wd-button__text"><slot /></view>
  48. </view>
  49. </button>
  50. </template>
  51. <script lang="ts">
  52. export default {
  53. name: 'wd-button',
  54. options: {
  55. addGlobalClass: true,
  56. virtualHost: true,
  57. styleIsolation: 'shared'
  58. }
  59. }
  60. </script>
  61. <script lang="ts" setup>
  62. import wdIcon from '../wd-icon/wd-icon.vue'
  63. import { computed, watch } from 'vue'
  64. import { ref } from 'vue'
  65. import base64 from '../common/base64'
  66. import { buttonProps } from './types'
  67. const loadingIcon = (color = '#4D80F0', reverse = true) => {
  68. return `<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 42 42"><defs><linearGradient x1="100%" y1="0%" x2="0%" y2="0%" id="a"><stop stop-color="${
  69. reverse ? color : '#fff'
  70. }" offset="0%" stop-opacity="0"/><stop stop-color="${
  71. reverse ? color : '#fff'
  72. }" offset="100%"/></linearGradient></defs><g fill="none" fill-rule="evenodd"><path d="M21 1c11.046 0 20 8.954 20 20s-8.954 20-20 20S1 32.046 1 21 9.954 1 21 1zm0 7C13.82 8 8 13.82 8 21s5.82 13 13 13 13-5.82 13-13S28.18 8 21 8z" fill="${
  73. reverse ? '#fff' : color
  74. }"/><path d="M4.599 21c0 9.044 7.332 16.376 16.376 16.376 9.045 0 16.376-7.332 16.376-16.376" stroke="url(#a)" stroke-width="3.5" stroke-linecap="round"/></g></svg>`
  75. }
  76. const props = defineProps(buttonProps)
  77. const emit = defineEmits([
  78. 'click',
  79. 'getuserinfo',
  80. 'contact',
  81. 'getphonenumber',
  82. 'getrealtimephonenumber',
  83. 'error',
  84. 'launchapp',
  85. 'opensetting',
  86. 'chooseavatar',
  87. 'agreeprivacyauthorization'
  88. ])
  89. const hoverStartTime = ref<number>(20)
  90. const hoverStayTime = ref<number>(70)
  91. const loadingIconSvg = ref<string>('')
  92. const loadingStyle = computed(() => {
  93. return `background-image: url(${loadingIconSvg.value});`
  94. })
  95. watch(
  96. () => props.loading,
  97. () => {
  98. buildLoadingSvg()
  99. },
  100. { deep: true, immediate: true }
  101. )
  102. function handleClick(event: any) {
  103. if (!props.disabled && !props.loading) {
  104. emit('click', event)
  105. }
  106. }
  107. /**
  108. * 支付宝小程序授权
  109. * @param event
  110. */
  111. function handleGetAuthorize(event: any) {
  112. if (props.scope === 'phoneNumber') {
  113. handleGetphonenumber(event)
  114. } else if (props.scope === 'userInfo') {
  115. handleGetuserinfo(event)
  116. }
  117. }
  118. function handleGetuserinfo(event: any) {
  119. emit('getuserinfo', event.detail)
  120. }
  121. function handleConcat(event: any) {
  122. emit('contact', event.detail)
  123. }
  124. function handleGetphonenumber(event: any) {
  125. emit('getphonenumber', event.detail)
  126. }
  127. function handleGetrealtimephonenumber(event: any) {
  128. emit('getrealtimephonenumber', event.detail)
  129. }
  130. function handleError(event: any) {
  131. emit('error', event.detail)
  132. }
  133. function handleLaunchapp(event: any) {
  134. emit('launchapp', event.detail)
  135. }
  136. function handleOpensetting(event: any) {
  137. emit('opensetting', event.detail)
  138. }
  139. function handleChooseavatar(event: any) {
  140. emit('chooseavatar', event.detail)
  141. }
  142. function handleAgreePrivacyAuthorization(event: any) {
  143. emit('agreeprivacyauthorization', event.detail)
  144. }
  145. function buildLoadingSvg() {
  146. const { loadingColor, type, plain } = props
  147. let color = loadingColor
  148. if (!color) {
  149. switch (type) {
  150. case 'primary':
  151. color = '#4D80F0'
  152. break
  153. case 'success':
  154. color = '#34d19d'
  155. break
  156. case 'info':
  157. color = '#333'
  158. break
  159. case 'warning':
  160. color = '#f0883a'
  161. break
  162. case 'error':
  163. color = '#fa4350'
  164. break
  165. case 'default':
  166. color = '#333'
  167. break
  168. }
  169. }
  170. const svg = loadingIcon(color, !plain)
  171. loadingIconSvg.value = `"data:image/svg+xml;base64,${base64(svg)}"`
  172. }
  173. </script>
  174. <style lang="scss" scoped>
  175. @import './index.scss';
  176. </style>