liujie
昨天 384bce80449b17651937429038c4370dbce60e7b
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
<template>
    <view class="add-account-page">
        <view class="form-area">
            <!-- 等级选择 -->
            <view class="form-item">
                <text class="form-label">等级</text>
                <picker
                    mode="selector"
                    :range="levelLabels"
                    :value="levelIndex"
                    @change="_handleLevelChange"
                >
                    <view class="select-box">
                        <text
                            class="select-text"
                            :class="{ 'select-text--placeholder': !form.level }"
                        >
                            {{ form.level || '请选择' }}
                        </text>
                        <!-- 纯 CSS 下拉箭头 -->
                        <view class="chevron-icon"></view>
                    </view>
                </picker>
                <text class="quota-tip">{{ quotaTip }}</text>
            </view>
 
            <!-- 登录手机号输入 -->
            <view class="form-item">
                <text class="form-label">登录手机号</text>
                <input
                    v-model="form.phone"
                    class="input-box"
                    type="number"
                    maxlength="11"
                    placeholder="请输入账户"
                    placeholder-style="color:#b8bec7;font-size:28rpx"
                />
            </view>
        </view>
 
        <!-- 底部提交按钮 -->
        <view class="bottom-bar">
            <view class="submit-btn" @tap="_handleSubmit">新增</view>
        </view>
    </view>
</template>
 
<script setup>
import { ref, reactive, computed } from 'vue'
import { onLoad } from '@dcloudio/uni-app'
import { getSubAccountQuota, createSubAccount } from '@/api/index.js'
 
// ======================== 数据定义区域 ========================
 
// 表单数据
const form = reactive({
    level: '',
    phone: ''
})
 
// 各等级剩余可开通数量
const quota = ref({ L1: 0, L2: 0, L3: 0 })
 
// 可选等级值列表(与 levelLabels 下标一一对应)
const levelValues = ['L1', 'L2', 'L3']
 
// 当前选中的等级下标
const levelIndex = ref(0)
 
// 等级选择器的展示文案(带剩余数量提示)
const levelLabels = computed(() =>
    levelValues.map((level) => level + '(剩余' + (quota.value[level] || 0) + '个)')
)
 
// 剩余额度提示文案
const quotaTip = computed(
    () => `当前剩余可用:${quota.value.L1}个L1 ${quota.value.L2}个L2 ${quota.value.L3}个L3`
)
 
// ======================== 方法定义区域 ========================
 
/**
 * 加载各等级剩余可开通额度
 * 后端接口就绪前以本地模拟数据兜底,保证页面可视
 */
const _loadQuota = async () => {
    try {
        const { data } = await getSubAccountQuota()
        quota.value = data || quota.value
    } catch (e) {
        quota.value = { L1: 6, L2: 8, L3: 3 }
    }
}
 
/** 切换等级 */
const _handleLevelChange = (e) => {
    const index = Number(e.detail.value)
    levelIndex.value = index
    form.level = levelValues[index]
}
 
/** 校验表单,返回校验失败原因(通过则返回空串) */
const _validateForm = () => {
    if (!form.level) return '请选择等级'
    if (!/^1[3-9]\d{9}$/.test(form.phone)) return '请输入正确的手机号'
    return ''
}
 
/** 提交新增子账号 */
const _handleSubmit = async () => {
    const errorMsg = _validateForm()
    if (errorMsg) {
        uni.showToast({ title: errorMsg, icon: 'none' })
        return
    }
 
    try {
        await createSubAccount({ level: form.level, phone: form.phone })
        uni.showToast({ title: '新增成功', icon: 'success' })
        setTimeout(() => uni.navigateBack(), 1200)
    } catch (e) {
        uni.showToast({ title: e.message || '新增失败', icon: 'none' })
    }
}
 
onLoad(() => {
    _loadQuota()
})
</script>
 
<style scoped lang="scss">
/* 页面最外层:纵向弹性布局,撑满整屏 */
.add-account-page {
    display: flex;
    flex-direction: column;
    height: 100vh;
    background-color: #ffffff;
}
 
.form-area {
    flex: 1;
    padding: 30rpx 28rpx;
}
 
/* 表单项容器 */
.form-item {
    margin-bottom: 40rpx;
}
 
.form-label {
    display: block;
    font-size: 28rpx;
    color: #1f2329;
    margin-bottom: 20rpx;
}
 
/* 下拉选择框 */
.select-box {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 88rpx;
    padding: 0 24rpx;
    border: 1rpx solid #dcdfe6;
    border-radius: 8rpx;
    box-sizing: border-box;
}
 
.select-text {
    font-size: 28rpx;
    color: #1f2329;
}
 
.select-text--placeholder {
    color: #b8bec7;
}
 
/* 纯 CSS 下拉箭头(向下) */
.chevron-icon {
    width: 16rpx;
    height: 16rpx;
    border-right: 3rpx solid #9aa1ab;
    border-bottom: 3rpx solid #9aa1ab;
    transform: rotate(45deg);
    margin-right: 6rpx;
    flex-shrink: 0;
}
 
/* 剩余额度提示 */
.quota-tip {
    display: block;
    margin-top: 16rpx;
    font-size: 24rpx;
    color: #1a73e8;
}
 
/* 手机号输入框 */
.input-box {
    height: 88rpx;
    padding: 0 24rpx;
    border: 1rpx solid #dcdfe6;
    border-radius: 8rpx;
    font-size: 28rpx;
    color: #1f2329;
    box-sizing: border-box;
}
 
/* 底部按钮区 */
.bottom-bar {
    padding: 20rpx 28rpx;
    padding-bottom: calc(20rpx + constant(safe-area-inset-bottom));
    padding-bottom: calc(20rpx + env(safe-area-inset-bottom));
    background-color: #ffffff;
    border-top: 1rpx solid #f0f2f5;
}
 
.submit-btn {
    height: 88rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    background-color: #0b3b68;
    color: #ffffff;
    font-size: 30rpx;
    font-weight: 600;
    border-radius: 8rpx;
}
</style>