Blame view

稻城亚丁小程序/uniapp/src/pages/profile-setup/profile-setup.vue 7.04 KB
4598017b   王天杨   feat(profile): 添加...
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
  <template>
    <view class="page">
      <view class="header">
        <text class="title">完善个人资料</text>
        <text class="sub">设置头像、昵称和手机号,让作品更有辨识度</text>
      </view>
  
      <!-- 头像 -->
      <view class="avatar-section">
        <button class="avatar-btn" open-type="chooseAvatar" @chooseavatar="onChooseAvatar">
          <image
            v-if="avatarTemp"
            class="avatar-img"
            :src="avatarTemp"
            mode="aspectFill"
          />
          <view v-else class="avatar-placeholder">
            <text class="avatar-placeholder-icon">+</text>
            <text class="avatar-placeholder-text">点击设置头像</text>
          </view>
        </button>
      </view>
  
      <!-- 昵称 -->
      <view class="name-section">
        <text class="label">昵称</text>
        <input
          class="name-input"
          type="nickname"
          v-model="nickname"
          maxlength="16"
          placeholder="填写昵称"
          placeholder-class="name-ph"
        />
        <view class="line" />
      </view>
  
      <!-- 手机号 -->
      <!-- #ifdef MP-WEIXIN -->
      <view class="phone-section">
        <text class="label">手机号</text>
        <button
          class="phone-btn"
          open-type="getPhoneNumber"
          @getphonenumber="onGetPhoneNumber"
        >
          <text v-if="phoneNumber" class="phone-val">{{ phoneNumber }}</text>
          <text v-else class="phone-ph">点击获取微信绑定手机号</text>
        </button>
        <view class="line" />
      </view>
      <!-- #endif -->
  
      <view class="actions">
        <button class="btn-save" :disabled="saving" @click="onSubmit">
          <text v-if="saving">保存中…</text>
          <text v-else>完成</text>
        </button>
        <text class="skip-link" @click="goHome">跳过,以后再设</text>
      </view>
    </view>
  </template>
  
  <script setup lang="ts">
  import { ref } from "vue";
  import { onLoad } from "@dcloudio/uni-app";
  import { loadUserProfile, saveUserProfile } from "@/utils/userProfileStorage";
  import { uploadImage, post } from "@/utils/api/http";
  import { isAlreadyPersistedAvatarUrl } from "@/utils/avatarUpload";
  import { bindWechatPhone } from "@/utils/api/mpSession";
  
  const nickname = ref("");
  const avatarTemp = ref("");
  const phoneNumber = ref("");
  const saving = ref(false);
  
  onLoad(() => {
    const profile = loadUserProfile();
    nickname.value = profile.nickname || "";
    avatarTemp.value = profile.avatarUrl || "";
    phoneNumber.value = profile.phone || "";
  });
  
  function onChooseAvatar(e: any) {
    const url = e?.detail?.avatarUrl;
    if (url) {
      avatarTemp.value = url;
    }
  }
  
  async function onGetPhoneNumber(e: any) {
    const code = e?.detail?.code;
    if (!code) {
      uni.showToast({ title: "未获取到手机号授权", icon: "none" });
      return;
    }
    try {
      const phone = await bindWechatPhone(code);
      phoneNumber.value = phone;
      uni.showToast({ title: "手机号已获取", icon: "success" });
    } catch (err: unknown) {
      const msg =
        err && typeof err === "object" && "msg" in err
          ? (err as { msg: string }).msg
          : "手机号绑定失败";
      uni.showToast({ title: msg as string, icon: "none" });
    }
  }
  
  function goHome() {
    uni.switchTab({ url: "/pages/home/home" });
  }
  
  async function onSubmit() {
    const nick = nickname.value.trim();
    // #ifdef MP-WEIXIN
    const phone = phoneNumber.value.trim();
    if (!phone) {
      uni.showToast({ title: "请先绑定手机号", icon: "none" });
      return;
    }
    // #endif
  
    saving.value = true;
    try {
      let avatarUrl = avatarTemp.value.trim();
      if (avatarUrl && !isAlreadyPersistedAvatarUrl(avatarUrl)) {
        try {
          avatarUrl = await uploadImage(avatarUrl);
        } catch {
          uni.showToast({ title: "头像上传失败,请重试", icon: "none" });
          saving.value = false;
          return;
        }
      }
  
      // #ifdef MP-WEIXIN
      if (phone) {
        saveUserProfile({
          nickname: nick,
          avatarUrl,
          bio: loadUserProfile().bio,
          gender: loadUserProfile().gender,
          phone,
        });
      }
      // #endif
  
      await post("/api/v1/me/profile", {
        nickname: nick || undefined,
        avatarUrl: avatarUrl || undefined,
      });
  
      // #ifndef MP-WEIXIN
      saveUserProfile({
        nickname: nick,
        avatarUrl,
        bio: loadUserProfile().bio,
        gender: loadUserProfile().gender,
        phone: loadUserProfile().phone,
      });
      // #endif
  
      uni.showToast({ title: "已保存", icon: "success" });
      setTimeout(() => goHome(), 500);
    } catch {
      uni.showToast({ title: "保存失败,请重试", icon: "none" });
    } finally {
      saving.value = false;
    }
  }
  </script>
  
  <style scoped>
  .page {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 100rpx 48rpx 0;
    box-sizing: border-box;
    background: #fff;
  }
  
  .header {
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-bottom: 60rpx;
  }
  
  .title {
    font-size: 40rpx;
    font-weight: 700;
    color: #111827;
    margin-bottom: 16rpx;
  }
  
  .sub {
    font-size: 26rpx;
    color: #9ca3af;
  }
  
  .avatar-section {
    margin-bottom: 48rpx;
  }
  
  .avatar-btn {
    width: 160rpx;
    height: 160rpx;
    padding: 0;
    margin: 0;
    border: none;
    border-radius: 50%;
    background: transparent;
    overflow: visible;
    line-height: 1;
  }
  
  .avatar-btn::after {
    display: none;
  }
  
  .avatar-img {
    width: 160rpx;
    height: 160rpx;
    border-radius: 50%;
  }
  
  .avatar-placeholder {
    width: 160rpx;
    height: 160rpx;
    border-radius: 50%;
    border: 4rpx dashed #d1d5db;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    background: #f9fafb;
  }
  
  .avatar-placeholder-icon {
    font-size: 48rpx;
    color: #9ca3af;
    line-height: 1;
    margin-bottom: 4rpx;
  }
  
  .avatar-placeholder-text {
    font-size: 20rpx;
    color: #9ca3af;
  }
  
  .name-section,
  .phone-section {
    width: 100%;
    margin-bottom: 40rpx;
  }
  
  .label {
    display: block;
    font-size: 28rpx;
    font-weight: 600;
    color: #374151;
    margin-bottom: 16rpx;
  }
  
  .name-input {
    display: block;
    width: 100%;
    height: 80rpx;
    padding: 0 4rpx;
    font-size: 32rpx;
    color: #111827;
    background: transparent;
  }
  
  .name-ph {
    color: #c4c4c4;
    font-size: 28rpx;
  }
  
  .phone-btn {
    display: flex;
    align-items: center;
    width: 100%;
    height: 80rpx;
    padding: 0 4rpx;
    margin: 0;
    border: none;
    background: transparent;
    text-align: left;
    font-size: 32rpx;
    line-height: 1;
  }
  
  .phone-btn::after {
    display: none;
  }
  
  .phone-val {
    color: #111827;
  }
  
  .phone-ph {
    color: #c4c4c4;
    font-size: 28rpx;
  }
  
  .line {
    height: 1rpx;
    margin-top: 8rpx;
    background: #e5e7eb;
  }
  
  .actions {
    width: 100%;
    display: flex;
    flex-direction: column;
    align-items: center;
    margin-top: 20rpx;
  }
  
  .btn-save {
    width: 100%;
    height: 88rpx;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-bottom: 24rpx;
    padding: 0;
    font-size: 32rpx;
    font-weight: 500;
    color: #fff;
    background: #2185ff;
    border: none;
    border-radius: 999rpx;
  }
  
  .btn-save::after {
    display: none;
  }
  
  .btn-save[disabled] {
    opacity: 0.6;
  }
  
  .skip-link {
    font-size: 26rpx;
    color: #9ca3af;
    padding: 16rpx;
  }
  </style>