WXL
9 小时以前 b7b8202e3ecb7f720eefd7a226b2ee8166fc5057
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
386
387
388
389
390
391
392
393
394
395
396
<template>
  <view class="login-container">
    <view class="header">
      <image src="/static/avatar/logo.png" class="logo" />
      <text class="hospital-name">青大附院OPO管理平台</text>
    </view>
 
    <view class="form-container">
      <view class="input-group">
        <uni-icons type="contact" size="24" color="#409EFF" />
        <input v-model="username" placeholder="请输入账号" class="input" />
      </view>
 
      <view class="input-group">
        <uni-icons type="locked" size="24" color="#409EFF" />
        <input
          v-model="password"
          :password="!showPassword"
          placeholder="请输入密码"
          class="input"
        />
        <uni-icons
          :type="showPassword ? 'eye' : 'eye-slash'"
          size="22"
          color="#999"
          @click="showPassword = !showPassword"
        />
      </view>
 
      <button
        class="login-btn"
        :class="{ active: username && password }"
        @click="handleLogin"
        hover-class="button-hover"
      >
        登录
      </button>
 
      <!-- <view class="footer-links">
        <view @click="gotoRegister">注册账号</view>
        <view @click="gotoForgetPassword">忘记密码</view>
      </view> -->
    </view>
  </view>
</template>
 
<script setup>
import { ref } from "vue";
import { onLoad, onShow } from "@dcloudio/uni-app";
import { useUserStore } from "@/stores/user";
import { getToken, setToken } from "@/utils/auth";
import { encrypt } from "@/utils/crypto";
 
let isAutoLogining = false;
let hasAutoLogin = false;
 
const username = ref("");
const password = ref("");
const showPassword = ref(false);
const redirect = ref("");
 
onLoad((options) => {
  console.log("登录页onLoad,完整参数:", options);
  console.log("页面栈:", getCurrentPages());
 
  // ✅ 获取完整URL参数
  const pages = getCurrentPages();
  if (pages.length > 0) {
    const currentPage = pages[pages.length - 1];
    console.log("当前页面对象:", currentPage);
  }
 
  // 处理redirect参数
  if (options.redirect) {
    redirect.value = decodeURIComponent(options.redirect);
  } else if (options.userName && options.passWord) {
    // 如果没有redirect但有SSO参数,尝试从referrer获取
    const launchOptions = uni.getLaunchOptionsSync();
    if (launchOptions.referrerInfo && launchOptions.referrerInfo.extraData) {
      const referrer = launchOptions.referrerInfo.extraData;
      console.log("referrer信息:", referrer);
    }
  }
 
  // 检查是否已有token
  if (getToken()) {
    console.log("已存在token,直接跳转");
    setTimeout(() => {
      navigateToTarget();
    }, 100);
    return;
  }
 
  // ✅ SSO登录:检查所有可能的参数位置
  let ssoUserName = options.userName;
  let ssoPassWord = options.passWord;
 
  // 如果没有从options获取到,尝试从启动参数获取
  if (!ssoUserName || !ssoPassWord) {
    const launchOptions = uni.getLaunchOptionsSync();
    if (launchOptions.query) {
      ssoUserName = ssoUserName || launchOptions.query.userName;
      ssoPassWord = ssoPassWord || launchOptions.query.passWord;
    }
  }
 
  if (ssoUserName && ssoPassWord) {
    console.log("检测到SSO参数,自动登录", { ssoUserName });
 
    // 合并所有页面参数
    const pageParams = { ...options };
    delete pageParams.userName;
    delete pageParams.passWord;
    delete pageParams.redirect;
 
    handleSSOLogin(ssoUserName, ssoPassWord, pageParams);
  } else {
    // 普通登录
    password.value = "";
    username.value = "";
  }
});
 
onShow(() => {
  console.log("登录页onShow");
  if (hasAutoLogin) {
    hasAutoLogin = false;
  }
});
 
// ✅ 改进的SSO登录函数
const handleSSOLogin = async (userName, passWord, pageParams = {}) => {
  if (isAutoLogining) {
    console.log("正在自动登录中,跳过");
    return;
  }
 
  isAutoLogining = true;
  console.log("开始SSO免登:", { userName, pageParams });
 
  uni.showLoading({ title: "自动登录中...", mask: true });
 
  try {
    console.log(11);
 
    // 1. 获取token
    const tokenRes = await uni.$uapi.post("/getToken", {
      userName,
      passWord,
    });
    console.log(tokenRes.data);
 
    uni.hideLoading();
 
    if (!tokenRes.data.token) {
      throw new Error("获取token失败");
    }
 
    console.log("获取到token成功");
 
    // 2. 保存token
    setToken(tokenRes.data.token);
 
    // 3. 获取用户信息
    const userStore = useUserStore();
    const userInfo = await uni.$uapi.get("/getInfo");
    if (userInfo) {
      userStore.setUserInfo(userInfo);
    }
 
    // 4. 构建目标页面URL
    let targetPage = redirect.value;
    if (!targetPage && Object.keys(pageParams).length > 0) {
      // 尝试从原始访问路径构建
      const launchOptions = uni.getLaunchOptionsSync();
      if (launchOptions.path && !launchOptions.path.includes("login/Login")) {
        targetPage = "/" + launchOptions.path;
      }
    }
 
    // 5. 跳转
    hasAutoLogin = true;
    await navigateToTargetPage(targetPage, pageParams);
  } catch (err) {
    uni.hideLoading();
    console.error("SSO免登失败:", err);
    uni.showToast({
      title: "自动登录失败,请手动登录",
      icon: "none",
      duration: 3000,
    });
 
    uni.removeStorageSync("token");
  } finally {
    isAutoLogining = false;
  }
};
 
// 普通登录
const handleLogin = async () => {
  try {
    if (!username.value || !password.value) {
      uni.showToast({ title: "请输入账号密码", icon: "none" });
      return;
    }
 
    uni.showLoading({ title: "登录中...", mask: true });
 
    const userStore = useUserStore();
    const encryptedPassword = encrypt(password.value);
    const encryptedUsername = encrypt(username.value);
 
    const loginRes = await uni.$uapi.post("/login", {
      username: encryptedUsername,
      password: encryptedPassword,
    });
 
    if (!loginRes || !loginRes.token) {
      throw new Error("登录失败");
    }
 
    setToken(loginRes.token);
 
    const userInfo = await uni.$uapi.get("/getInfo");
    if (userInfo) {
      userStore.setUserInfo(userInfo);
    }
 
    uni.hideLoading();
    await navigateToTarget();
  } catch (err) {
    uni.hideLoading();
    console.error("登录失败:", err);
    uni.showToast({
      title: err.message || "登录失败",
      icon: "none",
      duration: 3000,
    });
  }
};
 
// ✅ 改进的跳转函数
const navigateToTarget = () => {
  return navigateToTargetPage(redirect.value, {});
};
 
const navigateToTargetPage = (targetPage, pageParams = {}) => {
  return new Promise((resolve) => {
    setTimeout(() => {
      let targetUrl = targetPage || "/pages/index/index";
 
      // 处理页面参数
      const paramKeys = Object.keys(pageParams).filter(
        (key) =>
          pageParams[key] !== undefined &&
          pageParams[key] !== null &&
          key !== "userName" &&
          key !== "passWord" &&
          key !== "redirect",
      );
 
      if (paramKeys.length > 0) {
        const queryStr = paramKeys
          .map((key) => `${key}=${encodeURIComponent(pageParams[key])}`)
          .join("&");
 
        targetUrl = targetUrl.includes("?")
          ? `${targetUrl}&${queryStr}`
          : `${targetUrl}?${queryStr}`;
      }
 
      console.log("最终跳转目标:", targetUrl);
 
      const tabBarPages = [
        "/pages/index/index",
        "/pages/appointment/index",
        "/pages/consultation/index",
        "/pages/my/index",
      ];
 
      const baseUrl = targetUrl.split("?")[0];
      const isTabBar = tabBarPages.includes(baseUrl);
 
      if (isTabBar) {
        uni.switchTab({
          url: baseUrl,
          success: () => resolve(true),
          fail: () => {
            uni.redirectTo({ url: "/pages/index/index" });
            resolve(false);
          },
        });
      } else {
        uni.redirectTo({
          url: targetUrl,
          success: () => resolve(true),
          fail: (err) => {
            console.error("跳转失败:", err);
            // 如果目标页跳转失败,跳首页
            uni.redirectTo({ url: "/pages/index/index" });
            resolve(false);
          },
        });
      }
    }, 300);
  });
};
</script>
 
<style scoped>
.login-container {
  padding: 40rpx;
  background: linear-gradient(to bottom, #e6f7ff, #ffffff);
  height: 100vh;
  box-sizing: border-box;
}
 
.header {
  display: flex;
  flex-direction: column;
  align-items: center;
  margin: 100rpx 0 60rpx;
}
 
.logo {
  width: 160rpx;
  height: 160rpx;
  border-radius: 50%;
  border: 3rpx solid #d6ecff;
  box-shadow: 0 6rpx 18rpx rgba(24, 144, 255, 0.3);
}
 
.hospital-name {
  font-size: 38rpx;
  font-weight: bold;
  color: #1890ff;
  margin-top: 24rpx;
  letter-spacing: 3rpx;
}
 
.form-container {
  background: #ffffff;
  border-radius: 24rpx;
  padding: 50rpx;
  box-shadow: 0 8rpx 24rpx rgba(24, 144, 255, 0.1);
}
 
.input-group {
  display: flex;
  align-items: center;
  flex-direction: row;
  padding: 28rpx 0;
  border-bottom: 1rpx solid #f0f0f0;
  flex-wrap: nowrap;
}
 
.input {
  flex: 1;
  margin: 0 20rpx;
  font-size: 30rpx;
  color: #333;
  background: transparent;
}
 
.login-btn {
  margin-top: 60rpx;
  height: 90rpx;
  line-height: 90rpx;
  border-radius: 50rpx;
  font-size: 34rpx;
  border: none;
  color: white;
  background: #c0dfff;
  transition: all 0.3s ease;
}
 
.login-btn.active {
  background: linear-gradient(to right, #40a9ff, #1890ff);
  box-shadow: 0 4rpx 12rpx rgba(24, 144, 255, 0.4);
}
 
.button-hover {
  opacity: 0.8;
}
 
.footer-links {
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  text-align: center;
  margin-top: 30rpx;
  color: #1890ff;
  font-size: 28rpx;
  width: 100%;
}
</style>