dengjunjie
2025-03-12 f43b7df8400f4fcffc9f19dca0888d61e2b33d5f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<template>
  <view v-if="value" class="tn-modal-class tn-modal">
    <tn-popup
      v-model="value"
      mode="center"
      :popup="false"
      :borderRadius="radius"
      :width="width"
      :zoom="zoom"
      :safeAreaInsetBottom="safeAreaInsetBottom"
      :maskCloseable="maskCloseable"
      :zIndex="zIndex"
      :closeBtn="showCloseBtn"
      @close="close"
    >
      <!-- 内容 -->
      <view
        class="tn-modal__box"
        :class="[
          backgroundColorClass
        ]"
        :style="[boxStyle]"
      >
        <!-- 不是自定义弹框内容 -->
        <view v-if="!custom">
          <view class="tn-modal__box__title" v-if="title && title !== ''">{{ title }}</view>
          <view
            class="tn-modal__box__content"
            :class="[
              fontColorClass,
              contentClass
            ]"
            :style="contentStyle"
          >{{ content }}</view>
          <view v-if="button && button.length" class="tn-modal__box__btn-box" :class="[button.length != 2 ? 'tn-flex-direction-column' : '']">
            <block v-for="(item, index) in button" :key="index">
              <tn-button
                width="100%"
                height="68rpx"
                :fontSize="26"
                :backgroundColor="item.backgroundColor || ''"
                :fontColor="item.fontColor || ''"
                :plain="item.plain || false"
                :shape="item.shape || 'round'"
                :class="[
                  button.length > 2 ? 'tn-margin-bottom' : ''
                ]"
                @click="handleClick(index)"
                :style="{
                  width: button.length != 2 ? '80%' : '46%'
                }"
              >
                {{ item.text }}
              </tn-button>
            </block>
          </view>
        </view>
        <view v-else>
          <slot></slot>
        </view>
      </view>
    </tn-popup>
  </view>
</template>
 
<script>
  import componentsColorMixin from '../../libs/mixin/components_color.js'
  export default {
    mixins: [componentsColorMixin],
    name: 'tn-modal',
    props: {
      // 显示控制
      value: {
        type: Boolean,
        default: false
      },
      // 弹框宽度
      width: {
        type: String,
        default: '84%'
      },
      // 内边距
      padding: {
        type: String,
        default: ''
      },
      // 圆角
      radius: {
        type: Number,
        default: 12
      },
      // 标题
      title: {
        type: String,
        default: ''
      },
      // 内容
      content: {
        type: String,
        default: ''
      },
      // 按钮内容 设置参数与button组件的参数一致
      // {
      //   text: '确定',
      //   backgroundColor: 'red',
      //   fontColor: 'white',
      //   plain: true,
      //   shape: ''
      // }
      button: {
        type: Array,
        default: () => {
          return []
        }
      },
      safeAreaInsetBottom: {
          type: Boolean,
          default: false
      },
      // 点击遮罩是否可以关闭
      maskCloseable: {
        type: Boolean,
        default: true
      },
      // 是否显示右上角关闭按钮
      showCloseBtn: {
        type: Boolean,
        default: false
      },
      // 放大动画
      zoom: {
        type: Boolean,
        default: true
      },
      // 自定义弹框内容
      custom: {
        type: Boolean,
        default: false
      },
      // 弹框的z-index
      zIndex: {
        type: Number,
        default: 0
      }
    },
    computed: {
      boxStyle() {
        let style = {}
        
        if (this.padding) {
          style.padding = this.padding
        }
        if (this.backgroundColorStyle) {
          style.backgroundColor = this.backgroundColorStyle
        }
        
        return style
      },
      contentClass() {
        let clazz = ''
        if (this.title) {
          clazz += ' tn-margin-top'
        } else {
          clazz += ' tn-modal__box__content--no-title'
        }
        
        return clazz
      },
      contentStyle() {
        let style = {}
        
        if (this.fontSize) {
          style.fontSize = this.fontSize + this.fontUnit
        }
        if (this.fontColorStyle) {
          style.color = this.fontColorStyle
        }
        
        return style
      },
    },
    data() {
      return {
        
      }
    },
    methods: {
      // 处理按钮点击事件
      handleClick(index) {
        if (!this.value) return
        this.$emit("click", {
          index: Number(index)
        })
      },
      // 处理关闭事件
      close() {
        this.$emit("cancel")
        this.$emit('input', false)
      }
    }
  }
</script>
 
<style lang="scss" scoped>
  
  .tn-modal {
    
    &__box {
      position: relative;
      box-sizing: border-box;
      background-color: #FFFFFF;
      padding: 40rpx 64rpx;
      
      &__title {
        text-align: center;
        font-size: 34rpx;
        color: #333;
        padding-top: 20rpx;
        font-weight: bold;
      }
      
      &__content {
        text-align: center;
        padding-bottom: 30rpx;
        color: $tn-font-color;
        font-size: 28rpx;
        
        &--no-title {
          padding-bottom: 0rpx !important;
        }
      }
      
      &__btn-box {
        width: 100%;
        display: flex;
        align-items: center;
        justify-content: space-between;
      }
      
      &__content ~ &__btn-box {
        margin-top: 30rpx;
      }
    }
  }
  
</style>