autoLogin.html 14 KB

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