Confirm.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <template>
  2. <div class="xtx-confirm">
  3. <div class="wrapper" ref="target">
  4. <div class="header" v-if="options.title">
  5. <h3>{{ options.title }}</h3>
  6. <a href="JavaScript:;" class="iconfont icon-close-new" @click="cancelCallback"></a>
  7. </div>
  8. <div class="body">
  9. <div class="typeImg">
  10. <img
  11. style="height: 48px; width: 48px"
  12. v-if="options.type == 'success'"
  13. src="@/assets/images/icon/success.png"
  14. alt=""
  15. />
  16. <img
  17. style="height: 48px; width: 48px"
  18. v-else-if="options.type == 'warn'"
  19. src="@/assets/images/icon/warn.png"
  20. alt=""
  21. />
  22. <img
  23. style="height: 48px; width: 48px"
  24. v-else
  25. src="@/assets/images/icon/error.png"
  26. alt=""
  27. />
  28. </div>
  29. <span>{{ options.text }}</span>
  30. </div>
  31. <div class="footer">
  32. <!-- <button @click="cancelCallback" size="mini" type="gray">取消</button> -->
  33. <button @click="confirmCallback" size="mini" type="primary">{{$t('confirm.text')}}</button>
  34. </div>
  35. </div>
  36. </div>
  37. </template>
  38. <script lang="ts">
  39. // 注意:当前组件不是在 #app 下进行渲染,无法使用 #app 下的环境(全局组件,全局指令,原型属性函数)
  40. import { ref } from 'vue'
  41. import { useI18n } from 'vue-i18n'
  42. // import { onClickOutside } from '@vueuse/core'
  43. export default {
  44. name: 'showConfirm',
  45. props: {
  46. type: {
  47. type: String,
  48. default: 'success'
  49. },
  50. options: {
  51. type: Object,
  52. default: () => {
  53. return {
  54. title: '',
  55. text: '',
  56. type: 'success',
  57. callback: () => {}
  58. }
  59. }
  60. },
  61. // 关闭方法
  62. close: {
  63. type: Function,
  64. default: () => {}
  65. },
  66. // 取消按钮
  67. // cancelCallback: {
  68. // type: Function,
  69. // default: () => {}
  70. // }
  71. },
  72. setup(props) {
  73. // 点击 target 目标元素外部相当于点击了取消
  74. const target = ref(null)
  75. const { locale: language, t } = useI18n()
  76. let current = language
  77. if (!current) {
  78. current = window.navigator.language || window.navigator.userLanguage || null
  79. if (current && !/^zh/.test(current)) {
  80. console.log('自动获取浏览器语言:' + current)
  81. current = 'en'
  82. }else{
  83. current = 'zh'
  84. }
  85. }
  86. // onClickOutside(target, () => {
  87. // props.cancelCallback()
  88. // })
  89. function cancelCallback() {
  90. props.close()
  91. }
  92. function confirmCallback() {
  93. props.options.callback()
  94. props.close()
  95. }
  96. return { options:props.options,target,confirmCallback,cancelCallback }
  97. }
  98. }
  99. </script>
  100. <style scoped lang="less">
  101. .xtx-confirm {
  102. position: fixed;
  103. left: 0;
  104. top: 0;
  105. width: 100%;
  106. height: 100%;
  107. z-index: 8888;
  108. background: rgba(0, 0, 0, 0.5);
  109. .wrapper {
  110. width: calc(100% - 40px);
  111. max-width: 400px;
  112. border-radius: 10px 10px 10px 10px;
  113. background: #fff;
  114. border-radius: 4px;
  115. position: absolute;
  116. top: 50%;
  117. left: 50%;
  118. transform: translate(-50%, -50%);
  119. .header,
  120. .footer {
  121. height: 50px;
  122. line-height: 50px;
  123. padding: 0 20px;
  124. text-align: center;
  125. }
  126. .body {
  127. padding: 30px 40px;
  128. font-size: 16px;
  129. font-family: PingFang SC-Regular, PingFang SC;
  130. font-weight: 400;
  131. color: #202020;
  132. line-height: 19px;
  133. text-align: center;
  134. .icon-warning {
  135. color: red;
  136. margin-right: 3px;
  137. font-size: 16px;
  138. }
  139. .typeImg {
  140. margin-bottom: 20px;
  141. }
  142. }
  143. .footer {
  144. // text-align: right;
  145. border-top: 1px solid #ebebeb;
  146. padding: 20px;
  147. .xtx-button {
  148. margin-left: 20px;
  149. }
  150. button {
  151. height: 40px;
  152. background: #29b2ff;
  153. border-radius: 4px 4px 4px 4px;
  154. opacity: 1;
  155. font-size: 14px;
  156. font-family: PingFang SC-Regular, PingFang SC;
  157. font-weight: 400;
  158. color: #ffffff;
  159. border: none;
  160. padding: 10px 50px;
  161. text-align: center;
  162. margin: 0 15px;
  163. }
  164. }
  165. .header {
  166. position: relative;
  167. h3 {
  168. font-weight: normal;
  169. font-size: 18px;
  170. }
  171. a {
  172. position: absolute;
  173. right: 15px;
  174. top: 15px;
  175. font-size: 20px;
  176. width: 20px;
  177. height: 20px;
  178. line-height: 20px;
  179. text-align: center;
  180. color: #999;
  181. &:hover {
  182. color: #666;
  183. }
  184. }
  185. }
  186. }
  187. }
  188. </style>