app-confirm.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. <template>
  2. <!-- 全局确认弹窗,依赖已全局注册的 uni-popup -->
  3. <uni-popup ref="popup" type="center" :mask-click="false">
  4. <view class="confirm-card" :class="[type]">
  5. <view class="title" v-if="title">{{ title }}</view>
  6. <view class="content" v-if="content">{{ content }}</view>
  7. <view class="actions">
  8. <view class="btn btn-cancel" @click="onCancel">{{ cancelText }}</view>
  9. <view class="btn btn-confirm" :class="[type]" @click="onConfirm">{{ confirmText }}</view>
  10. </view>
  11. </view>
  12. </uni-popup>
  13. </template>
  14. <script>
  15. export default {
  16. data() {
  17. return {
  18. // 文案配置
  19. title: '',
  20. content: '',
  21. confirmText: '确定',
  22. cancelText: '取消',
  23. // 类型:default | danger
  24. type: 'default',
  25. // Promise 的 resolve/reject 引用
  26. _resolve: null,
  27. _reject: null
  28. }
  29. },
  30. methods: {
  31. // 打开弹窗
  32. open(options = {}) {
  33. // 合并配置
  34. this.title = options.title || '提示';
  35. this.content = options.content || '';
  36. this.confirmText = options.confirmText || '确定';
  37. this.cancelText = options.cancelText || '取消';
  38. this.type = options.type || 'default';
  39. // 打开并返回 Promise
  40. this.$refs.popup && this.$refs.popup.open();
  41. return new Promise((resolve) => {
  42. this._resolve = resolve;
  43. });
  44. },
  45. // 确认
  46. onConfirm() {
  47. this.$refs.popup && this.$refs.popup.close();
  48. this._resolve && this._resolve(true);
  49. },
  50. // 取消
  51. onCancel() {
  52. this.$refs.popup && this.$refs.popup.close();
  53. this._resolve && this._resolve(false);
  54. }
  55. }
  56. }
  57. </script>
  58. <style lang="less">
  59. .confirm-card {
  60. min-width: 560rpx;
  61. max-width: 680rpx;
  62. background: #fff;
  63. border-radius: 16rpx;
  64. padding: 28rpx 28rpx 20rpx;
  65. box-sizing: border-box;
  66. }
  67. .confirm-card .title {
  68. font-size: 32rpx;
  69. font-weight: 600;
  70. color: #1f2333;
  71. margin-bottom: 16rpx;
  72. }
  73. .confirm-card .content {
  74. font-size: 28rpx;
  75. line-height: 42rpx;
  76. color: #4b5563;
  77. margin-bottom: 24rpx;
  78. }
  79. .confirm-card .actions {
  80. display: flex;
  81. justify-content: flex-end;
  82. gap: 16rpx;
  83. }
  84. .confirm-card .btn {
  85. min-width: 140rpx;
  86. height: 64rpx;
  87. border-radius: 12rpx;
  88. display: flex;
  89. align-items: center;
  90. justify-content: center;
  91. font-size: 28rpx;
  92. padding: 0 24rpx;
  93. box-sizing: border-box;
  94. }
  95. .confirm-card .btn-cancel {
  96. background: #f3f4f6;
  97. color: #374151;
  98. }
  99. .confirm-card .btn-confirm.default {
  100. background: #3377FF;
  101. color: #fff;
  102. }
  103. .confirm-card .btn-confirm.danger {
  104. background: #ef4444;
  105. color: #fff;
  106. }
  107. </style>