绿满眶商城微信小程序-uniapp
zhanghua
2025-09-01 aea82fc6285fd37a33e0efd6cd0e5089dfa32faf
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<template>
    <view class="l-dialer" :style="rootStyles">
        <view class="l-dialer__inner" :style="innerStyle">
            <view class="l-dialer__inner-border" v-if="$slots['border'] != null">
                <slot name="border" />
            </view>
            <view class="l-dialer__inner-wrap" ref="drawbleRef" :style="wrapStyle">
                <view 
                    class="l-dialer__inner-item" 
                    v-for="(item, index) in prizeList" :key="index"
                    :style="itemStyle(index)">
                    <view class="l-dialer__inner-content" :style="contentStyle(index)">
                        <slot name="prize" :item="item" :even="index % 2">
                            <text class="l-dialer__inner-name" :style="nameStyle">{{ item['name'] }}</text>
                            <image class="l-dialer__inner-img" :src="item['img']"></image>
                        </slot>
                    </view>
                </view>
            </view>
        </view>
        <view class="l-dialer__pointer" :style="pointerStyle">
            <slot v-if="$slots['pointer'] != null" name="pointer" />
            <image v-else :class="!isTurnIng ? 'heart' : ''" src="/pages/prize/lime-dialer_0.2.7/static/turnable_btn.png"
                style="width: 100%" mode="widthFix" @tap="$emit('click')" />
        </view>
    </view>
</template>
<script lang="uts" setup>
    const emits = defineEmits(['click', 'done'])
    const slots = defineSlots<{
        prize : {
            item : UTSJSONObject,
            even : number
        }
    }>()
    const props = defineProps({
        size: {
            type: [String, Number],
            default: 300
        },
        prizeList: {
            type: Array as PropType<UTSJSONObject[]>,
            default: () : UTSJSONObject[] => []
        },
        turns: {
            type: Number,
            default: 10
        },
        duration: {
            type: Number,
            default: 3
        },
        styleOpt: {
            type: Object as PropType<UTSJSONObject>,
            default: () : UTSJSONObject => ({
                // 每一块扇形的背景色,默认值,可通过父组件来改变
                prizeBgColors: ['#fff0a3', '#fffce6'],
                // 每一块扇形的外边框颜色,默认值,可通过父组件来改变
                borderColor: '#ffd752',
            } as UTSJSONObject)
        },
        customStyle: {
            type: String,
        },
        dialStyle: {
            type: String,
        },
        pointerStyle: {
            type: String,
            default: `width: 30%`
        }
    })
 
    const drawbleRef = ref<UniElement | null>(null)
    const startRotateDegree = ref(0)
    const rotateAngle = ref('rotate(0deg)')
    const rotateTransition = ref('')
    const isTurnIng = ref(false)
 
 
    const getStyleOpt = computed(() : UTSJSONObject => {
        const style = {
            // 每一块扇形的背景色,默认值,可通过父组件来改变
            prizeBgColors: ['#fff0a3', '#fffce6'],
            // 每一块扇形的外边框颜色,默认值,可通过父组件来改变
            borderColor: '#ffd752',
        }
        return UTSJSONObject.assign(style, props.styleOpt)
    })
    const rootStyles = computed(() : string => {
        const size = /\d$/.test(`${props.size}`) ? `${props.size}px` : props.size;
        return `width: ${size}; height: ${size}; ${props.customStyle}`
    })
 
    const innerStyle = computed(() : string => {
        // const style = new Map<string, string>()
        let style = ''
        const padding = getStyleOpt.value['padding'] ?? 0
 
        style += `padding: ${padding};`
        style += `transform: ${rotateAngle.value};`
        style += `${rotateTransition.value};`//`transition: ${rotateTransition.value};`
        style += `${props.dialStyle};`
 
        return style
    })
 
    const wrapStyle = computed(() : string => {
        // #ifndef APP
        const borderColor = getStyleOpt.value['borderColor']
        if (borderColor != null) {
            return `border: 1rpx solid ${borderColor}`
        }
        // #endif
        return ''
    })
 
    const itemStyle = computed(() : ((index : number) => Map<string, any>) => {
        return (index : number) : Map<string, any> => {
            const length = props.prizeList.length;
            const prizeBgColors : string[] = (getStyleOpt.value['prizeBgColors'] ?? [] as string[]) as string[]
            const prizeBgColorsLength = prizeBgColors.length;
            const borderColor = getStyleOpt.value['borderColor']
            const style = new Map<string, any>();
            // #ifndef APP
            if (length == 2) {
                // style['transform'] = index == 0 ? 0 : `rotate(270deg)` 
                style.set('transform', index == 0 ? `rotate(0deg)` : `rotate(270deg)`)
                style.set('top', 0)
            } else {
                style.set('transform', `rotate(${(360 / length) * index}deg) skewX(0deg) skewY(${360 / length - 90}deg)`);
            }
            if (prizeBgColorsLength > 0) {
                style.set('backgroundColor', `${prizeBgColors[index % prizeBgColorsLength]}`)
            }
            // if (borderColor != null) {
            //     style.set('border', `1rpx solid ${borderColor}`)
            // }
            // #endif
            // #ifdef APP
            if (length == 2) {
                style.set('backgroundColor', `${prizeBgColors[index % prizeBgColorsLength]}`)
                style.set('transform', index == 0 ? `rotate(0deg)` : `rotate(270deg)`);
                style.set('top', 0)
                if (borderColor != null) {
                    style.set('border', `1rpx solid ${borderColor}`)
                }
            } else{
                style.set('transform', `rotate(${(360 / length) * index}deg)`);
            }
            // #endif
            return style
        }
    })
 
    const contentStyle = computed(() : ((index : number) => string) => {
        return (index : number) : string => {
            // #ifndef APP
            if (props.prizeList.length != 2) {
                return `transform: skewY(${90 - 360 / props.prizeList.length}deg) skewX(0deg) rotate(${180 / props.prizeList.length}deg)`
            } else {
                return index == 0
                    ? `transform: rotate(90deg); bottom: 0`
                    : `transform: rotate(0deg); bottom: -50%; left: 0`
            }
 
            // #endif
            
            if (props.prizeList.length != 2) {
                return `transform: rotate(${180 / props.prizeList.length}deg)`
            } else {
                return index == 0
                    ? `transform: rotate(90deg); bottom: 0`
                    : `transform: rotate(0deg); bottom: -50%; left: 0`
            }
            // #ifdef APP
            // #endif
        }
 
    })
 
    const nameStyle = computed(() : Map<string, any> => {
        const fontSize = getStyleOpt.value['fontSize']
        const color = getStyleOpt.value['color']
        const style = new Map<string, any>()
 
        if (fontSize != null) {
            style.set('fontSize', fontSize)
        }
        if (color != null) {
            style.set('color', color)
        }
        return style
    })
 
 
    const run = (index : number) => {
        if (isTurnIng.value) return
        const duration = props.duration;
        const length = props.prizeList.length;
 
        const _rotateAngle = startRotateDegree.value + props.turns * 360 + 360 - (180 / length + (360 / length) * index) - (startRotateDegree.value % 360);
        
        rotateTransition.value = `transition-duration: ${duration}s`;
        // 鸿蒙需要设置时间后 再设置旋转才能触发动画
        nextTick(()=>{
            startRotateDegree.value = _rotateAngle;
            rotateAngle.value = `rotate(${_rotateAngle}deg)`;
            isTurnIng.value = true
            
            setTimeout(() => {
                emits('done', index);
                isTurnIng.value = false
            }, duration * 1000 + 500);
        })
        
    }
    // #ifdef APP
    onMounted(() => {
        nextTick(() => {
            if (drawbleRef.value == null) return;
            const ctx = drawbleRef.value!.getDrawableContext()!;
            const size = drawbleRef.value!.offsetWidth;
            watch(props.prizeList, () => {
                ctx.reset()
                const length = props.prizeList.length;
                if (length == 2) return
                const prizeBgColors : string[] = (getStyleOpt.value['prizeBgColors'] ?? [] as string[]) as string[]
                const prizeBgColorsLength = prizeBgColors.length;
                const borderColor = getStyleOpt.value['borderColor'] as string | null
 
                const centerX = size / 2;
                const centerY = size / 2;
                const radius = size / 2;
 
                // const angle = (2 * Math.PI) / length;
                const angleStep = (2 * Math.PI) / length
                // 添加270度偏移(转换为弧度:270° = 3π/2)
                const startOffset = 3 * Math.PI / 2
                for (let i = 0; i < length; i++) {
                    ctx.beginPath();
                    ctx.moveTo(centerX, centerY);
                    
                    // 调整起始和结束角度
                    const startAngle = startOffset + i * angleStep
                    const endAngle = startOffset + (i + 1) * angleStep
                        
                    ctx.arc(centerX, centerY, radius,startAngle, endAngle);
                    
                    // ctx.arc(centerX, centerY, radius, i * angle, (i + 1) * angle);
                    ctx.lineTo(centerX, centerY);
                    ctx.closePath();
                    ctx.fillStyle = prizeBgColors[i % prizeBgColorsLength];
                    if (borderColor != null) {
                        ctx.lineWidth = 2
                        ctx.strokeStyle = borderColor;
                        ctx.stroke()
                    }
                    ctx.fill();
                }
                ctx.update()
            }, { immediate: true })
        })
    })
    // #endif
    defineExpose({
        run
    })
</script>
<style lang="scss" scoped>
    @import './index';
</style>