liyong
2025-04-23 c1196767c4407de5419b5bc970c3b300fb520905
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
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
<template>
  <view class="login-container">
    <!-- 背景图 -->
    <image src="/static/images/auth/login-bg.svg" mode="aspectFill" class="login-bg" />
 
    <!-- Logo和标题区域 -->
    <view class="header">
      <image src="/static/logo.png" class="logo" />
      <text class="subtitle">专注于构建高效开发的应用解决方案</text>
    </view>
 
    <!-- 登录表单区域 -->
    <view class="login-card">
      <view class="form-wrap">
        <wd-form ref="loginFormRef" :model="loginFormData">
          <!-- 用户名输入框 -->
          <view class="form-item">
            <wd-icon name="user" size="22" color="#165DFF" class="input-icon" />
            <input v-model="loginFormData.username" class="form-input" placeholder="请输入用户名" />
            <wd-icon
              v-if="loginFormData.username"
              name="close-fill"
              size="18"
              color="#9ca3af"
              class="clear-icon"
              @click="loginFormData.username = ''"
            />
          </view>
          <view class="divider"></view>
 
          <!-- 密码输入框 -->
          <view class="form-item">
            <wd-icon name="lock-on" size="22" color="#165DFF" class="input-icon" />
            <input
              v-model="loginFormData.password"
              class="form-input"
              type="password"
              placeholder="请输入密码"
              placeholder-style="color: #9ca3af; font-weight: normal;"
            />
            <wd-icon name="eye-close" size="18" color="#9ca3af" class="eye-icon" />
          </view>
          <view class="divider"></view>
 
          <!-- 登录按钮 -->
          <button
            class="login-btn"
            :disabled="loading"
            :style="loading ? 'opacity: 0.7;' : ''"
            @click="handleLogin"
          >
            登录
          </button>
        </wd-form>
 
        <!-- 微信登录 -->
        <view class="other-login">
          <view class="other-login-title">
            <view class="line"></view>
            <text class="text">其他登录方式</text>
            <view class="line"></view>
          </view>
 
          <view class="wechat-login" @click="handleWechatLogin">
            <view class="wechat-icon-wrapper">
              <image src="/static/icons/weixin.png" class="wechat-icon" />
            </view>
          </view>
        </view>
 
        <!-- 底部协议 -->
        <view class="agreement">
          <text class="text">登录即同意</text>
          <text class="link" @click="navigateToUserAgreement">《用户协议》</text>
          <text class="text">和</text>
          <text class="link" @click="navigateToPrivacy">《隐私政策》</text>
        </view>
      </view>
    </view>
 
    <wd-toast />
  </view>
</template>
 
<script lang="ts" setup>
import { onLoad } from "@dcloudio/uni-app";
import { type LoginFormData } from "@/api/auth";
import { useUserStore } from "@/store/modules/user";
import { useToast } from "wot-design-uni";
import { ref } from "vue";
 
const loginFormRef = ref();
const toast = useToast();
const loading = ref(false);
const userStore = useUserStore();
 
// 登录表单数据
const loginFormData = ref<LoginFormData>({
  username: "31601016",
  // username: "32308027",
  password: "cxjt1234",
});
 
// 获取重定向参数
const redirect = ref("");
onLoad((options) => {
  if (options) {
    redirect.value = options.redirect ? decodeURIComponent(options.redirect) : "/pages/index/index";
  } else {
    redirect.value = "/pages/index/index";
  }
});
 
// 登录处理
const handleLogin = () => {
  if (loading.value) return;
  loading.value = true;
 
  userStore
    .login(loginFormData.value)
    .then(() => userStore.getInfo())
    .then(() => {
      toast.success("登录成功");
      // 检查用户信息是否完整
      if (!userStore.isUserInfoComplete()) {
        // 信息不完整,跳转到完善信息页面
        setTimeout(() => {
          uni.navigateTo({
            url: `/pages/login/complete-profile?redirect=${encodeURIComponent(redirect.value)}`,
          });
        }, 1000);
      } else {
        // 否则直接跳转到重定向页面
        setTimeout(() => {
          uni.reLaunch({
            url: redirect.value,
          });
        }, 1000);
      }
    })
    .catch((error) => {
      toast.error(error?.message || "登录失败");
    })
    .finally(() => {
      loading.value = false;
    });
};
 
// 微信登录处理
const handleWechatLogin = async () => {
  if (loading.value) return;
  loading.value = true;
 
  try {
    // #ifdef MP-WEIXIN
    // 获取微信登录的临时 code
    const { code } = await uni.login({
      provider: "weixin",
    });
 
    // 调用后端接口进行登录认证
    const result = await userStore.loginByWechat(code);
 
    if (result) {
      // 获取用户信息
      await userStore.getInfo();
      toast.success("登录成功");
 
      // 检查用户信息是否完整
      if (!userStore.isUserInfoComplete()) {
        // 如果信息不完整,跳转到完善信息页面
        setTimeout(() => {
          uni.navigateTo({
            url: `/pages/login/complete-profile?redirect=${encodeURIComponent(redirect.value)}`,
          });
        }, 1000);
      } else {
        // 否则直接跳转到重定向页面
        setTimeout(() => {
          uni.reLaunch({
            url: redirect.value,
          });
        }, 1000);
      }
    }
    // #endif
 
    // #ifndef MP-WEIXIN
    toast.error("当前环境不支持微信登录");
    // #endif
  } catch (error: any) {
    toast.error(error?.message || "微信登录失败");
  } finally {
    loading.value = false;
  }
};
 
// 跳转到用户协议页面
const navigateToUserAgreement = () => {
  uni.navigateTo({
    url: "/pages/mine/user-agreement/index",
  });
};
 
// 跳转到隐私政策页面
const navigateToPrivacy = () => {
  uni.navigateTo({
    url: "/pages/mine/privacy/index",
  });
};
</script>
 
<style lang="scss" scoped>
.login-container {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100vh;
  overflow: hidden;
}
 
.login-bg {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}
 
.header {
  z-index: 2;
  display: flex;
  flex-direction: column;
  align-items: center;
  margin-top: 120rpx;
}
 
.logo {
  width: 180rpx;
  height: 180rpx;
  margin-bottom: 20rpx;
}
 
.title {
  margin-bottom: 10rpx;
  font-size: 48rpx;
  font-weight: bold;
  color: #ffffff;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
}
 
.subtitle {
  font-size: 28rpx;
  color: #ffffff;
  text-align: center;
  text-shadow: 0 2rpx 4rpx rgba(0, 0, 0, 0.1);
}
 
.login-card {
  z-index: 2;
  display: flex;
  flex-direction: column;
  width: 90%;
  margin-top: 80rpx;
  overflow: hidden;
  background-color: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border-radius: 24rpx;
  box-shadow: 0 8rpx 40rpx rgba(0, 0, 0, 0.1);
}
 
.form-wrap {
  padding: 40rpx;
}
 
.form-item {
  position: relative;
  display: flex;
  align-items: center;
  padding: 24rpx 0;
}
 
.input-icon {
  margin-right: 20rpx;
}
 
.form-input {
  flex: 1;
  height: 60rpx;
  font-size: 28rpx;
  line-height: 60rpx;
  color: #333;
}
 
.clear-icon,
.eye-icon {
  padding: 10rpx;
}
 
.divider {
  height: 1px;
  margin: 0;
  background-color: rgba(0, 0, 0, 0.06);
}
 
.login-btn {
  width: 100%;
  height: 90rpx;
  margin-top: 60rpx;
  font-size: 32rpx;
  line-height: 90rpx;
  color: #fff;
  background: linear-gradient(90deg, #165dff, #4080ff);
  border: none;
  border-radius: 45rpx;
  box-shadow: 0 8rpx 20rpx rgba(22, 93, 255, 0.3);
  transition: all 0.3s;
}
 
.login-btn:active {
  box-shadow: 0 4rpx 10rpx rgba(22, 93, 255, 0.2);
  transform: translateY(2rpx);
}
 
.other-login {
  margin-top: 60rpx;
}
 
.other-login-title {
  display: flex;
  align-items: center;
  margin-bottom: 40rpx;
}
 
.line {
  flex: 1;
  height: 1px;
  background-color: rgba(0, 0, 0, 0.08);
}
 
.text {
  padding: 0 30rpx;
  font-size: 26rpx;
  color: #9ca3af;
}
 
.wechat-login {
  display: flex;
  justify-content: center;
  margin-bottom: 30rpx;
}
 
.wechat-icon-wrapper {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 90rpx;
  height: 90rpx;
  background-color: #fff;
  border-radius: 50%;
  box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.08);
}
 
.wechat-icon {
  width: 60rpx;
  height: 60rpx;
}
 
.agreement {
  display: flex;
  justify-content: center;
  margin-top: 30rpx;
  font-size: 24rpx;
}
 
.agreement .text {
  padding: 0 4rpx;
  color: #9ca3af;
}
 
.agreement .link {
  color: #165dff;
}
</style>