zhuguifei
2025-09-02 9cc59c17892a7e69de54e06b5931e78c9b05551c
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
<template>
    <view class="app">
        <cu-custom bgColor="bg-gradual-blue" :isBack="true">
            <block slot="content">切换模式</block>
        </cu-custom>
 
        <view class="cu-bar bg-white margin-top">
            <view class="action sub-title">
                <text class="text-xl text-bold text-blue text-shadow">当前模式</text>
                <text class="text-ABC text-blue">current</text>
            </view>
        </view>
        <u-form labelPosition="left" ref="form">
            <view class="padding-lr info-box">
 
                <view class="cover-view" @tap.catch="showSelect">
 
                </view>
 
                <view style="position: relative;">
                    <u-form-item label="模式" prop="username" borderBottom>
                        <u-input v-model="sModel" border="none" suffixIcon="arrow-right" readonly></u-input>
                    </u-form-item>
                </view>
 
 
 
 
                <u-form-item label="地址" prop="password" borderBottom v-show="showIp">
                    <u-input v-model="ip" placeholder="请输入IP地址,如:192.168.1.1" border="none" clearable>
                    </u-input>
                </u-form-item>
 
                <view class="submitBtn" @click="saveMode">保存</view>
                <view style="height: 20rpx;background-color: white"></view>
            </view>
        </u-form>
 
        <u-action-sheet :closeOnClickAction="true" @close="show=false" @select="selectMode" :safeAreaInsetBottom="true"
            :actions="switchList" title="选择模式" cancelText="取消" :show="show"></u-action-sheet>
 
    </view>
</template>
 
<script>
    import {
        vShow
    } from "vue";
 
    export default {
        data() {
            return {
                switchList: [{
                    name: '云服务',
                    index: 0
                }, {
                    name: '局域网',
                    index: 1
                }],
                show: false,
                sModel: '云服务',
                ip: '',
                showIp: false,
                selectModeIndex: 0,
 
            };
        },
        mounted() {
            const mode = uni.getStorageSync('mode');
            const lan_ip = uni.getStorageSync('lan_ip');
            if (mode) {
                if (mode == 'cloud') {
                    this.sModel = '云服务'
                    this.showIp = false
                } else if (mode == 'lan') {
                    this.sModel = '局域网'
                    this.showIp = true
                }
 
                if (this.sModel == '局域网') {
                    this.ip = lan_ip
                }
 
            }
        },
        methods: {
            showSelect() {
                this.show = true
            },
            selectMode(item) {
                this.sModel = item.name
                console.error(item.index)
                this.showIp = item.index === 1
                this.selectModeIndex = item.index
 
            },
            saveMode() {
                if (!this.ip | !this.isValidIPv4(this.ip)) {
 
                    uni.$u.toast('请输入正确IP地址~')
                    return false
 
                }
 
                if (this.selectModeIndex === 0) {
                    uni.removeStorageSync('lan_ip')
                    uni.setStorageSync('mode', 'cloud');
                } else if (this.selectModeIndex === 1) {
                    uni.setStorageSync('mode', 'lan');
                    uni.setStorageSync('lan_ip', this.ip);
                }
 
                uni.$u.toast('配置成功,请完全退出程序后重新启动~')
            },
            isValidIPv4(ip) {
                const ipv4Regex =
                    /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/;
                return ipv4Regex.test(ip);
            }
        }
    }
</script>
 
<style lang="scss">
    .cover-view {
        position: absolute;
        /* 必须是相对定位或绝对定位 */
        z-index: 10;
        /* 确保 z-index 值高于 u-input */
        width: 100%;
        height: 80rpx;
        background-color: transparent;
    }
 
    .info-box {
        position: relative;
        background-color: white;
    }
 
    .distinguishBox {
        padding: 5rpx 30rpx 20rpx;
        background: white;
 
        view {
            margin-bottom: 20rpx;
        }
    }
 
    .disLabel {
        text-align-last: justify;
        text-align: justify;
        text-justify: distribute-all-lines;
        min-width: 142rpx;
        display: inline-block;
        margin-right: 5rpx;
    }
 
    .submitBtn {
        width: 90%;
        height: 90rpx;
        background: #007aec;
        line-height: 90rpx;
        margin: 50rpx auto;
        text-align: center;
        font-size: 34rpx;
        color: #fff;
        border-radius: 12rpx;
    }
</style>