register.vue
6.66 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
<template>
<view class="page">
<view
class="header"
:style="{ paddingTop: statusBarHeight + 'px' }"
>
<image
class="header-bg"
:src="headerBg"
mode="aspectFill"
/>
<view class="header-mask" />
<view class="header-inner">
<text class="greet">创建账号</text>
<text class="welcome">手机号注册</text>
</view>
</view>
<view class="sheet">
<view v-if="loading" class="loading">
<text>提交中…</text>
</view>
<template v-else>
<view class="field">
<text class="label">手机号</text>
<input
v-model="phone"
class="input"
type="number"
maxlength="11"
placeholder="请输入11位手机号"
placeholder-class="ph"
/>
<view class="line" />
</view>
<view class="field">
<text class="label">密码</text>
<input
v-model="password"
class="input"
password
placeholder="至少6位"
placeholder-class="ph"
/>
<view class="line" />
</view>
<view class="field">
<text class="label">确认密码</text>
<input
v-model="password2"
class="input"
password
placeholder="再次输入密码"
placeholder-class="ph"
/>
<view class="line" />
</view>
<button class="btn btn-main" @click="onSubmit">注册</button>
<text class="to-login" @click="goLogin">已有账号?去登录</text>
<text v-if="errMsg" class="err">{{ errMsg }}</text>
</template>
</view>
</view>
</template>
<script setup lang="ts">
import { onShow } from "@dcloudio/uni-app";
import { ref } from "vue";
import { apiEnabled, appendLoopbackConnectHint } from "@/config/api";
import { registerWithPhone } from "@/utils/api/mpSession";
import { staticAsset } from "@/utils/staticAsset";
const statusBarHeight = ref(20);
const headerBg = staticAsset("login-header.png");
const loading = ref(false);
const errMsg = ref("");
const phone = ref("");
const password = ref("");
const password2 = ref("");
function goHome() {
uni.switchTab({ url: "/pages/home/home" });
}
function goLogin() {
uni.navigateBack({ fail: () => uni.redirectTo({ url: "/pages/login/login" }) });
}
onShow(() => {
statusBarHeight.value = uni.getSystemInfoSync().statusBarHeight || 20;
if (!apiEnabled()) {
uni.showToast({ title: "当前为离线模式,无法注册", icon: "none" });
}
});
function pickErr(e: unknown, fallback: string): string {
if (e && typeof e === "object" && "msg" in e && typeof (e as { msg: string }).msg === "string") {
return (e as { msg: string }).msg;
}
if (e && typeof e === "object" && "errMsg" in e && typeof (e as { errMsg: string }).errMsg === "string") {
const m = (e as { errMsg: string }).errMsg.trim();
if (m) return m;
}
if (e instanceof Error && e.message.trim()) {
return e.message;
}
return fallback;
}
async function onSubmit() {
errMsg.value = "";
if (!apiEnabled()) {
uni.showToast({ title: "API 未配置", icon: "none" });
return;
}
const p = phone.value.trim();
if (!/^1\d{10}$/.test(p)) {
errMsg.value = "请输入11位手机号";
uni.showToast({ title: errMsg.value, icon: "none" });
return;
}
if (password.value.length < 6) {
errMsg.value = "密码至少6位";
uni.showToast({ title: errMsg.value, icon: "none" });
return;
}
if (password.value !== password2.value) {
errMsg.value = "两次密码不一致";
uni.showToast({ title: errMsg.value, icon: "none" });
return;
}
loading.value = true;
try {
await registerWithPhone(p, password.value, password2.value);
uni.showToast({ title: "注册成功", icon: "success" });
setTimeout(() => goHome(), 400);
} catch (e: unknown) {
const msg = appendLoopbackConnectHint(pickErr(e, "注册失败"));
errMsg.value = msg;
uni.showToast({
title: msg.length > 28 ? "网络失败:请改用电脑局域网 IP(见底部红字)" : msg,
icon: "none",
duration: 3200,
});
} finally {
loading.value = false;
}
}
</script>
<style scoped>
.page {
display: flex;
flex-direction: column;
height: 100vh;
box-sizing: border-box;
overflow: hidden;
background: #f1f5f9;
}
.header {
position: relative;
display: flex;
flex-direction: column;
flex-shrink: 0;
overflow: hidden;
min-height: 430rpx;
padding-bottom: 56rpx;
box-sizing: border-box;
}
.header-bg {
position: absolute;
top: 0;
right: 0;
left: 0;
bottom: 0;
width: 100%;
height: 100%;
}
.header-mask {
position: absolute;
inset: 0;
background: linear-gradient(
180deg,
rgba(0, 0, 0, 0.12) 0%,
rgba(0, 0, 0, 0.35) 100%
);
pointer-events: none;
}
.header-inner {
position: relative;
z-index: 1;
display: flex;
flex: 1;
flex-direction: column;
justify-content: center;
padding: 0 48rpx;
min-height: 0;
}
.greet {
display: block;
margin-bottom: 8rpx;
font-size: 44rpx;
font-weight: 600;
color: #fff;
letter-spacing: 0.02em;
text-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.25);
}
.welcome {
display: block;
font-size: 44rpx;
font-weight: 700;
color: #fff;
letter-spacing: 0.02em;
text-shadow: 0 2rpx 12rpx rgba(0, 0, 0, 0.25);
}
.sheet {
flex: 1;
min-height: 0;
margin-top: 0;
padding: 36rpx 48rpx calc(24rpx + env(safe-area-inset-bottom));
border-radius: 24rpx 24rpx 0 0;
background: #fff;
box-shadow: 0 -8rpx 40rpx rgba(0, 0, 0, 0.06);
}
.field {
margin-bottom: 8rpx;
}
.label {
display: block;
margin-bottom: 16rpx;
font-size: 30rpx;
font-weight: 600;
color: #111827;
}
.input {
display: block;
width: 100%;
height: 72rpx;
padding: 0 4rpx;
font-size: 30rpx;
color: #111827;
border: none;
background: transparent;
}
.ph {
color: #c4c4c4;
font-size: 28rpx;
}
.line {
height: 1rpx;
margin-top: 8rpx;
background: #eeeeee;
}
.loading {
padding: 80rpx 0;
font-size: 28rpx;
color: #9ca3af;
text-align: center;
}
.btn {
display: flex;
width: 100%;
height: 88rpx;
align-items: center;
justify-content: center;
margin-top: 28rpx;
margin-bottom: 20rpx;
padding: 0;
font-size: 32rpx;
font-weight: 500;
border: none;
border-radius: 999rpx;
}
.btn::after {
display: none;
border: none;
}
.btn-main {
color: #fff;
background: #2185ff;
}
.to-login {
display: block;
font-size: 26rpx;
color: #2185ff;
text-align: center;
}
.err {
display: block;
margin-top: 24rpx;
font-size: 24rpx;
line-height: 1.4;
color: #ef4444;
text-align: center;
}
</style>