autoLogin.html 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  6. <title>自动登录</title>
  7. <script src="/js/mp_base/base.js?v=20261127-auth-refresh-v1"></script>
  8. </head>
  9. <body>
  10. <!-- 页面加载状态 -->
  11. <div id="page-loading" class="page-loading">
  12. <div class="loading-content">
  13. <div class="loading-spinner"></div>
  14. <div class="loading-text">正在自动登录...</div>
  15. </div>
  16. </div>
  17. <!-- 主要内容 -->
  18. <div id="app" v-cloak>
  19. <!-- 自动登录状态 -->
  20. <div class="auto-login-container">
  21. <div class="status-icon">
  22. <div v-if="status === 'loading'" class="loading-spinner"></div>
  23. <div v-else-if="status === 'success'" class="success-icon">✓</div>
  24. <div v-else-if="status === 'error'" class="error-icon">✗</div>
  25. </div>
  26. <div class="status-text">{{ statusText }}</div>
  27. <div v-if="status === 'error'" class="retry-section">
  28. <button @click="handleRetry" class="retry-btn">重试</button>
  29. <button @click="handleManualLogin" class="manual-btn">
  30. 手动登录
  31. </button>
  32. </div>
  33. </div>
  34. <!-- Loading 遮罩 -->
  35. <div v-if="showLoading" class="loading-mask">
  36. <div class="loading-content">
  37. <div class="loading-spinner"></div>
  38. <div class="loading-text">{{ loadingText }}</div>
  39. </div>
  40. </div>
  41. <!-- Toast 提示 -->
  42. <div v-if="toast.show" class="toast" :class="toast.type">
  43. {{ toast.message }}
  44. </div>
  45. </div>
  46. <script>
  47. // 获取yhsbToken的函数
  48. function getYhsbToken() {
  49. // 从localStorage获取用户信息
  50. const userInfo = localStorage.getItem("userInfo");
  51. if (userInfo) {
  52. try {
  53. const userData = JSON.parse(userInfo);
  54. return userData.yhsbToken || userData.onlineToken || "";
  55. } catch (e) {
  56. console.error("解析用户信息失败:", e);
  57. }
  58. }
  59. // 从其他可能的存储位置获取
  60. return localStorage.getItem("yhsbToken") || "";
  61. }
  62. // 等待SS框架加载完成
  63. window.SS.ready(function () {
  64. // 使用SS框架的方式创建Vue实例
  65. window.SS.dom.initializeFormApp({
  66. el: "#app",
  67. data() {
  68. return {
  69. status: "loading", // loading, success, error
  70. statusText: "正在自动登录...",
  71. showLoading: false,
  72. loadingText: "",
  73. toast: {
  74. show: false,
  75. message: "",
  76. type: "info",
  77. },
  78. };
  79. },
  80. mounted() {
  81. // 隐藏页面加载状态
  82. const pageLoading = document.getElementById("page-loading");
  83. if (pageLoading) {
  84. pageLoading.style.display = "none";
  85. }
  86. // 初始化页面,使用自定义结果处理
  87. if (typeof initPage === "function") {
  88. initPage("autoLogin", this.handleResult);
  89. }
  90. // 获取URL参数
  91. const urlParams = this.getUrlParams();
  92. console.log("🔗 H5自动登录页面参数:", urlParams);
  93. // 开始自动登录
  94. this.startAutoLogin();
  95. },
  96. methods: {
  97. // 获取URL参数
  98. getUrlParams() {
  99. const params = {};
  100. const urlSearchParams = new URLSearchParams(
  101. window.location.search
  102. );
  103. for (const [key, value] of urlSearchParams) {
  104. params[key] = decodeURIComponent(value);
  105. }
  106. return params;
  107. },
  108. // 处理操作结果(来自小程序的回调)
  109. handleResult(data) {
  110. console.log("🎯 收到小程序回调:", data);
  111. },
  112. // 显示Loading
  113. showLoadingMask(text = "加载中...") {
  114. this.showLoading = true;
  115. this.loadingText = text;
  116. },
  117. // 隐藏Loading
  118. hideLoadingMask() {
  119. this.showLoading = false;
  120. this.loadingText = "";
  121. },
  122. // 开始自动登录
  123. async startAutoLogin() {
  124. try {
  125. this.status = "loading";
  126. this.statusText = "正在自动登录...";
  127. // 从URL参数或localStorage获取yhsbToken进行自动登录
  128. const urlParams = this.getUrlParams();
  129. const yhsbToken = urlParams.yhsbToken || getYhsbToken();
  130. const redirectUrl = urlParams.redirect || "";
  131. const entryScene = urlParams.entryScene || "";
  132. console.log("🔄 开始自动登录,yhsbToken:", yhsbToken);
  133. if (!yhsbToken) {
  134. throw new Error("未找到yhsbToken,无法自动登录");
  135. }
  136. // 统一走公共会话恢复,避免自动登录请求再次触发登录恢复。
  137. const recovery = await window.ensureMiniProgramSessionFresh({
  138. force: true,
  139. forceRecovery: true,
  140. bypassCooldown: true,
  141. page: "autoLogin",
  142. });
  143. if (recovery && recovery.userInfo) {
  144. console.log("✅ 自动登录成功:", recovery.userId || "");
  145. // 构建用户数据(参考登录页面的格式)
  146. const userData = recovery.userInfo;
  147. // 保存用户信息到H5本地存储(和登录页面一样)
  148. h5UserApi.saveUserInfo(userData);
  149. // 准备返回数据给小程序(和登录页面一样,只是多了isAutoLogin标记)
  150. const loginResult = {
  151. success: true,
  152. userInfo: userData,
  153. isAutoLogin: true, // 标记为自动登录,用于区分返回逻辑
  154. redirectUrl,
  155. entryScene,
  156. };
  157. this.status = "success";
  158. this.statusText = "自动登录成功!";
  159. console.log("✅ 自动登录成功,准备返回数据:", loginResult);
  160. // 延迟返回结果
  161. setTimeout(() => {
  162. this.returnLoginResult(loginResult);
  163. }, 1500);
  164. } else {
  165. throw new Error("自动登录响应数据无效");
  166. }
  167. } catch (error) {
  168. console.error("❌ 自动登录失败:", error);
  169. this.status = "error";
  170. const authStatus = error?.authStatus || "";
  171. if (
  172. authStatus === window.AUTH_RECOVERY_STATUS?.TOKEN_INVALID ||
  173. authStatus === window.AUTH_RECOVERY_STATUS?.NO_AUTH
  174. ) {
  175. this.statusText = "登录信息已失效,请手动登录";
  176. } else if (
  177. authStatus === window.AUTH_RECOVERY_STATUS?.USER_MISMATCH
  178. ) {
  179. this.statusText = "账号信息不一致,请重新登录";
  180. } else {
  181. this.statusText =
  182. "系统暂时无法恢复登录,请稍后重试或手动登录";
  183. }
  184. }
  185. },
  186. // 返回登录结果给小程序
  187. returnLoginResult(result) {
  188. console.log("🔄 返回自动登录结果给小程序:", result);
  189. callNative("loginSuccess", "自动登录成功", result);
  190. },
  191. // 重试自动登录
  192. handleRetry() {
  193. console.log("🔄 重试自动登录");
  194. this.startAutoLogin();
  195. },
  196. // 跳转手动登录
  197. handleManualLogin() {
  198. console.log("👤 跳转手动登录");
  199. // 用户明确选择手动登录后才清理持久登录信息。
  200. localStorage.removeItem("userInfo");
  201. localStorage.removeItem("JSESSIONID");
  202. localStorage.removeItem("yhsbToken");
  203. localStorage.removeItem("onlineToken");
  204. document.cookie =
  205. "JSESSIONID=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
  206. window.location.href =
  207. "/page/login.html" + window.location.search;
  208. },
  209. },
  210. });
  211. console.log("✅ 自动登录页面初始化完成");
  212. });
  213. </script>
  214. <style>
  215. /* 防止Vue模板闪烁 */
  216. [v-cloak] {
  217. display: none !important;
  218. }
  219. /* 页面加载状态 */
  220. .page-loading {
  221. position: fixed;
  222. top: 0;
  223. left: 0;
  224. right: 0;
  225. bottom: 0;
  226. background: #f5f5f5;
  227. display: flex;
  228. align-items: center;
  229. justify-content: center;
  230. z-index: 9999;
  231. }
  232. .page-loading .loading-content {
  233. text-align: center;
  234. }
  235. .page-loading .loading-spinner {
  236. width: 40px;
  237. height: 40px;
  238. border: 4px solid #f3f3f3;
  239. border-top: 4px solid #40ac6d;
  240. border-radius: 50%;
  241. animation: page-spin 1s linear infinite;
  242. margin: 0 auto 15px;
  243. }
  244. @keyframes page-spin {
  245. 0% {
  246. transform: rotate(0deg);
  247. }
  248. 100% {
  249. transform: rotate(360deg);
  250. }
  251. }
  252. .page-loading .loading-text {
  253. color: #666;
  254. font-size: 14px;
  255. }
  256. * {
  257. margin: 0;
  258. padding: 0;
  259. box-sizing: border-box;
  260. }
  261. body {
  262. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto,
  263. sans-serif;
  264. background: #f5f5f5;
  265. min-height: 100vh;
  266. display: flex;
  267. align-items: center;
  268. justify-content: center;
  269. }
  270. .auto-login-container {
  271. background: white;
  272. border-radius: 12px;
  273. padding: 40px 30px;
  274. text-align: center;
  275. box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
  276. max-width: 300px;
  277. width: 90%;
  278. }
  279. .status-icon {
  280. margin-bottom: 20px;
  281. }
  282. .loading-spinner {
  283. width: 50px;
  284. height: 50px;
  285. border: 4px solid #f3f3f3;
  286. border-top: 4px solid #40ac6d;
  287. border-radius: 50%;
  288. animation: spin 1s linear infinite;
  289. margin: 0 auto;
  290. }
  291. .success-icon {
  292. width: 50px;
  293. height: 50px;
  294. background: #40ac6d;
  295. border-radius: 50%;
  296. display: flex;
  297. align-items: center;
  298. justify-content: center;
  299. color: white;
  300. font-size: 24px;
  301. font-weight: bold;
  302. margin: 0 auto;
  303. }
  304. .error-icon {
  305. width: 50px;
  306. height: 50px;
  307. background: #dc3545;
  308. border-radius: 50%;
  309. display: flex;
  310. align-items: center;
  311. justify-content: center;
  312. color: white;
  313. font-size: 24px;
  314. font-weight: bold;
  315. margin: 0 auto;
  316. }
  317. @keyframes spin {
  318. 0% {
  319. transform: rotate(0deg);
  320. }
  321. 100% {
  322. transform: rotate(360deg);
  323. }
  324. }
  325. .status-text {
  326. font-size: 16px;
  327. color: #333;
  328. margin-bottom: 20px;
  329. line-height: 1.5;
  330. word-break: keep-all;
  331. }
  332. .retry-section {
  333. display: flex;
  334. gap: 10px;
  335. justify-content: center;
  336. }
  337. .retry-btn,
  338. .manual-btn {
  339. padding: 10px 20px;
  340. border: none;
  341. border-radius: 6px;
  342. font-size: 14px;
  343. cursor: pointer;
  344. transition: all 0.3s ease;
  345. }
  346. .retry-btn {
  347. background: #40ac6d;
  348. color: white;
  349. }
  350. .retry-btn:hover {
  351. background: #369a5a;
  352. }
  353. .manual-btn {
  354. background: #6c757d;
  355. color: white;
  356. }
  357. .manual-btn:hover {
  358. background: #5a6268;
  359. }
  360. /* Loading 遮罩 */
  361. .loading-mask {
  362. position: fixed;
  363. top: 0;
  364. left: 0;
  365. right: 0;
  366. bottom: 0;
  367. background: rgba(0, 0, 0, 0.5);
  368. display: flex;
  369. align-items: center;
  370. justify-content: center;
  371. z-index: 10000;
  372. }
  373. .loading-mask .loading-content {
  374. background: white;
  375. padding: 20px;
  376. border-radius: 8px;
  377. text-align: center;
  378. min-width: 120px;
  379. }
  380. .loading-mask .loading-text {
  381. color: #333;
  382. font-size: 14px;
  383. }
  384. /* Toast 提示 */
  385. .toast {
  386. position: fixed;
  387. top: 50%;
  388. left: 50%;
  389. transform: translate(-50%, -50%);
  390. background: rgba(0, 0, 0, 0.8);
  391. color: white;
  392. padding: 12px 20px;
  393. border-radius: 6px;
  394. font-size: 14px;
  395. z-index: 10001;
  396. max-width: 80%;
  397. text-align: center;
  398. }
  399. .toast.success {
  400. background: rgba(40, 167, 69, 0.9);
  401. }
  402. .toast.error {
  403. background: rgba(220, 53, 69, 0.9);
  404. }
  405. </style>
  406. </body>
  407. </html>