login.vue 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  1. <template>
  2. <view class="login-container">
  3. <!-- 语言切换按钮 -->
  4. <view class="language-switcher" :style="{ top: languageSwitcherTop + 'px' }" @click="switchLanguage">
  5. <text class="language-text" :class="{ active: locale === 'zh-CN' }">中</text>
  6. <text class="language-separator">|</text>
  7. <text class="language-text" :class="{ active: locale === 'en-US' }">EN</text>
  8. </view>
  9. <!-- 顶部渐变背景区域 -->
  10. <view class="header-bg" :style="{ paddingTop: statusBarHeight + 'px' }">
  11. <text class="app-title">{{ getAppName() }}</text>
  12. <text class="app-subtitle">{{ t('login.welcome') }}</text>
  13. </view>
  14. <!-- 登录表单卡片 -->
  15. <view class="login-card">
  16. <view class="card-title">{{ t('login.title') }}</view>
  17. <view class="form-section">
  18. <view class="form-item">
  19. <view class="input-wrapper">
  20. <image class="input-icon" src="/static/pages/login/phone.png" mode="aspectFit" />
  21. <input
  22. v-model="phone"
  23. type="text"
  24. maxlength="11"
  25. :placeholder="t('login.phonePlaceholder')"
  26. class="input-field"
  27. @input="onPhoneInput"
  28. />
  29. </view>
  30. </view>
  31. <view class="form-item">
  32. <view class="input-wrapper">
  33. <image class="input-icon" src="/static/pages/login/password.png" mode="aspectFit" />
  34. <input
  35. v-model="password"
  36. :password="!showPassword"
  37. :placeholder="t('login.passwordPlaceholder')"
  38. class="input-field"
  39. />
  40. <text
  41. class="toggle-password"
  42. @click="togglePassword"
  43. >
  44. {{ showPassword ? '👁' : '👁‍🗨' }}
  45. </text>
  46. </view>
  47. </view>
  48. <!-- 协议勾选 -->
  49. <view class="agreement-section">
  50. <checkbox-group @change="handleAgreementChange">
  51. <label class="agreement-label">
  52. <checkbox
  53. :checked="agreedToTerms"
  54. class="agreement-checkbox"
  55. color="#1EC9C9"
  56. />
  57. <view class="agreement-text">
  58. <text class="prefix">{{ t('login.agreePrefix') }}</text>
  59. <text class="link" @click.stop="viewUserAgreement">{{ t('login.userAgreement') }}</text>
  60. <text class="prefix">{{ t('login.and') }}</text>
  61. <text class="link" @click.stop="viewPrivacyPolicy">{{ t('login.privacyPolicy') }}</text>
  62. </view>
  63. </label>
  64. </checkbox-group>
  65. </view>
  66. <button
  67. class="login-btn"
  68. @click="handleLogin"
  69. >
  70. {{ t('login.loginButton') }}
  71. </button>
  72. </view>
  73. </view>
  74. <!-- 协议弹窗 -->
  75. <view v-if="showAgreementModal" class="modal-overlay" @click="closeAgreementModal">
  76. <view class="modal-content agreement-modal" @click.stop>
  77. <view class="modal-header">
  78. <text class="modal-title">{{ agreementTitle }}</text>
  79. <view class="modal-close" @click="closeAgreementModal">
  80. <text class="close-icon">×</text>
  81. </view>
  82. </view>
  83. <scroll-view scroll-y class="modal-body">
  84. <view class="agreement-content">
  85. <rich-text :nodes="agreementContent"></rich-text>
  86. </view>
  87. </scroll-view>
  88. <view class="modal-footer">
  89. <view class="modal-btn confirm" @click="closeAgreementModal">
  90. <text class="modal-btn-text">我知道了</text>
  91. </view>
  92. </view>
  93. </view>
  94. </view>
  95. </view>
  96. </template>
  97. <script setup>
  98. import { ref, computed, nextTick, onMounted } from 'vue'
  99. import { onShow } from '@dcloudio/uni-app'
  100. import { useI18n } from 'vue-i18n'
  101. import { useUserStore } from '@/store/index'
  102. import { useLocaleStore } from '@/store/locale'
  103. import request from '@/utils/request.js'
  104. import { getAppName } from '@/config/app.js'
  105. import { getAgreementContent } from '@/apis/setting'
  106. const { t, locale } = useI18n()
  107. const userStore = useUserStore()
  108. const localeStore = useLocaleStore()
  109. const phone = ref('')
  110. const password = ref('')
  111. const showPassword = ref(false)
  112. const isLanguageSwitching = ref(false)
  113. const agreedToTerms = ref(false)
  114. const statusBarHeight = ref(0)
  115. // 协议弹窗
  116. const showAgreementModal = ref(false)
  117. const agreementTitle = ref('')
  118. const agreementContent = ref('')
  119. const agreementLoading = ref(false)
  120. // 语言切换按钮的top位置
  121. const languageSwitcherTop = computed(() => {
  122. return statusBarHeight.value + 50 // 状态栏高度 + 50px间距,确保不被遮挡
  123. })
  124. onMounted(() => {
  125. // 获取系统信息
  126. const windowInfo = uni.getWindowInfo()
  127. statusBarHeight.value = windowInfo.statusBarHeight || 0
  128. })
  129. // 当前语言名称
  130. const currentLanguageName = computed(() => {
  131. return localeStore.getCurrentLocaleName()
  132. })
  133. // 页面显示时更新标题
  134. onShow(() => {
  135. // 更新导航栏标题
  136. uni.setNavigationBarTitle({
  137. title: t('login.title')
  138. })
  139. })
  140. // 切换语言 - 优化版本,避免页面重绘
  141. const switchLanguage = async () => {
  142. // 防止频繁切换
  143. if (isLanguageSwitching.value) {
  144. return
  145. }
  146. isLanguageSwitching.value = true
  147. try {
  148. // 直接切换语言,i18n 会自动处理文本更新
  149. localeStore.toggleLocale()
  150. // 使用 nextTick 确保在 DOM 更新后再更新导航栏
  151. await nextTick()
  152. // 异步更新导航栏标题
  153. uni.setNavigationBarTitle({
  154. title: t('login.title')
  155. })
  156. // 显示切换成功提示
  157. uni.showToast({
  158. title: t('common.language.switchSuccess'),
  159. icon: 'success',
  160. duration: 1500
  161. })
  162. } finally {
  163. // 短暂延迟后允许再次切换
  164. setTimeout(() => {
  165. isLanguageSwitching.value = false
  166. }, 300)
  167. }
  168. }
  169. // 验证手机号格式
  170. const isValidPhone = computed(() => {
  171. return /^1[3-9]\d{9}$/.test(phone.value)
  172. })
  173. // 是否可以提交
  174. const canSubmit = computed(() => {
  175. return isValidPhone.value && password.value.length >= 6
  176. })
  177. // 手机号输入处理
  178. const onPhoneInput = (e) => {
  179. // 限制只能输入数字
  180. phone.value = e.detail.value.replace(/[^\d]/g, '')
  181. }
  182. // 切换密码显示
  183. const togglePassword = () => {
  184. showPassword.value = !showPassword.value
  185. }
  186. // 协议勾选变化
  187. const handleAgreementChange = (e) => {
  188. agreedToTerms.value = e.detail.value.length > 0
  189. }
  190. // 查看用户协议
  191. const viewUserAgreement = async () => {
  192. agreementTitle.value = t('login.userAgreement')
  193. showAgreementModal.value = true
  194. await fetchAgreementContent('user')
  195. }
  196. // 查看隐私协议
  197. const viewPrivacyPolicy = async () => {
  198. agreementTitle.value = t('login.privacyPolicy')
  199. showAgreementModal.value = true
  200. await fetchAgreementContent('privacy')
  201. }
  202. // 获取协议内容
  203. const fetchAgreementContent = async (type) => {
  204. try {
  205. agreementLoading.value = true
  206. agreementContent.value = '<p style="text-align: center; color: #999;">加载中...</p>'
  207. // 调用API获取内容
  208. const response = await getAgreementContent(type)
  209. if (response && response.code === 200 && response.data && response.data.content) {
  210. // 解析JSON字符串
  211. try {
  212. const contentObj = JSON.parse(response.data.content)
  213. // 根据当前语言环境选择对应的内容
  214. const currentLocale = locale.value // 'zh-CN' 或 'en-US'
  215. const localeKey = currentLocale.replace('-', '_') // 转换为 'zh_CN' 或 'en_US'
  216. // 优先使用当前语言,如果不存在则使用中文,再不存在则使用任意可用语言
  217. agreementContent.value = contentObj[localeKey] ||
  218. contentObj['zh_CN'] ||
  219. contentObj['en_US'] ||
  220. Object.values(contentObj)[0] ||
  221. ''
  222. } catch (parseError) {
  223. console.error('解析协议内容JSON失败:', parseError)
  224. // 如果解析失败,尝试直接使用原始内容
  225. agreementContent.value = response.data.content
  226. }
  227. } else {
  228. throw new Error(response?.msg || '获取失败')
  229. }
  230. } catch (error) {
  231. console.error('获取协议内容失败:', error)
  232. agreementContent.value = getDefaultContent(type)
  233. } finally {
  234. agreementLoading.value = false
  235. }
  236. }
  237. // 获取默认内容(API失败时的后备方案)
  238. const getDefaultContent = (type) => {
  239. if (type === 'user') {
  240. return `
  241. <div style="padding: 10px; line-height: 1.8; color: #333;">
  242. <p style="text-align: center; color: #999;">协议内容加载失败,请稍后重试。</p>
  243. </div>
  244. `
  245. } else {
  246. return `
  247. <div style="padding: 10px; line-height: 1.8; color: #333;">
  248. <p style="text-align: center; color: #999;">协议内容加载失败,请稍后重试。</p>
  249. </div>
  250. `
  251. }
  252. }
  253. // 关闭协议弹窗
  254. const closeAgreementModal = () => {
  255. showAgreementModal.value = false
  256. agreementContent.value = ''
  257. }
  258. // 登录处理
  259. const handleLogin = async () => {
  260. // 验证手机号
  261. if (!isValidPhone.value) {
  262. uni.showToast({
  263. title: t('login.phoneError'),
  264. icon: 'none',
  265. duration: 2000
  266. })
  267. return
  268. }
  269. // 验证密码
  270. if (password.value.length < 6) {
  271. uni.showToast({
  272. title: t('login.passwordError'),
  273. icon: 'none',
  274. duration: 2000
  275. })
  276. return
  277. }
  278. // 验证是否同意协议
  279. if (!agreedToTerms.value) {
  280. uni.showToast({
  281. title: t('login.agreementError'),
  282. icon: 'none',
  283. duration: 2000
  284. })
  285. return
  286. }
  287. try {
  288. uni.showLoading({
  289. title: t('login.loggingIn'),
  290. mask: true
  291. })
  292. // 准备登录参数
  293. const loginContent = JSON.stringify({
  294. phoneNumber: phone.value,
  295. password: password.value
  296. })
  297. // 调用新的登录接口
  298. const res = await request({
  299. url: '/applet/auth/login/password',
  300. method: 'POST',
  301. data: {
  302. clientId: '2f847927afb2b3ebeefc870c13d623f2',
  303. content: loginContent
  304. }
  305. })
  306. // 保存 token
  307. userStore.setToken(res.data.token)
  308. uni.hideLoading()
  309. uni.showToast({
  310. title: t('login.loginSuccess'),
  311. icon: 'success',
  312. duration: 1500
  313. })
  314. // 登录成功后跳转到首页
  315. setTimeout(() => {
  316. uni.reLaunch({
  317. url: '/pages/home/index'
  318. })
  319. }, 1500)
  320. } catch (error) {
  321. uni.hideLoading()
  322. console.error('登录失败:', error)
  323. }
  324. }
  325. </script>
  326. <style lang="scss" scoped>
  327. .login-container {
  328. min-height: 100vh;
  329. background-color: #f5f5f5;
  330. display: flex;
  331. flex-direction: column;
  332. position: relative;
  333. }
  334. // 语言切换按钮
  335. .language-switcher {
  336. position: fixed;
  337. right: 32rpx;
  338. display: flex;
  339. align-items: center;
  340. background: rgba(255, 255, 255, 0.2);
  341. backdrop-filter: blur(10rpx);
  342. border-radius: 30rpx;
  343. padding: 12rpx 24rpx;
  344. cursor: pointer;
  345. transition: all 0.3s;
  346. gap: 8rpx;
  347. z-index: 100;
  348. &:active {
  349. transform: scale(0.95);
  350. background: rgba(255, 255, 255, 0.3);
  351. }
  352. .language-text {
  353. font-size: 24rpx;
  354. color: rgba(255, 255, 255, 0.7);
  355. font-weight: 500;
  356. transition: all 0.3s;
  357. &.active {
  358. color: #ffffff;
  359. font-weight: 700;
  360. }
  361. }
  362. .language-separator {
  363. font-size: 24rpx;
  364. color: rgba(255, 255, 255, 0.5);
  365. font-weight: 300;
  366. }
  367. }
  368. // 顶部渐变背景
  369. .header-bg {
  370. background: linear-gradient(180deg, #1ec9c9 0%, #1eb8b8 100%);
  371. min-height: 500rpx;
  372. display: flex;
  373. flex-direction: column;
  374. align-items: center;
  375. justify-content: center;
  376. position: relative;
  377. padding: 0 40rpx 60rpx;
  378. .app-title {
  379. font-size: 52rpx;
  380. font-weight: 700;
  381. color: #ffffff;
  382. letter-spacing: 2rpx;
  383. margin-bottom: 16rpx;
  384. text-align: center;
  385. line-height: 1.3;
  386. word-break: break-word;
  387. max-width: 600rpx;
  388. }
  389. .app-subtitle {
  390. font-size: 28rpx;
  391. color: rgba(255, 255, 255, 0.9);
  392. letter-spacing: 2rpx;
  393. text-align: center;
  394. }
  395. }
  396. // 登录卡片
  397. .login-card {
  398. background: #ffffff;
  399. border-radius: 24rpx 24rpx 0 0;
  400. margin-top: -60rpx;
  401. padding: 48rpx 32rpx;
  402. flex: 1;
  403. box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.08);
  404. position: relative;
  405. z-index: 5;
  406. .card-title {
  407. font-size: 36rpx;
  408. font-weight: 600;
  409. color: #333333;
  410. margin-bottom: 32rpx;
  411. }
  412. }
  413. .form-section {
  414. width: 100%;
  415. }
  416. .form-item {
  417. margin-bottom: 24rpx;
  418. }
  419. .input-wrapper {
  420. display: flex;
  421. align-items: center;
  422. background: #f5f5f5;
  423. border-radius: 16rpx;
  424. padding: 0 24rpx;
  425. height: 96rpx;
  426. transition: all 0.3s;
  427. border: 2rpx solid transparent;
  428. &:focus-within {
  429. background: #fff;
  430. border-color: #1ec9c9;
  431. box-shadow: 0 0 0 4rpx rgba(30, 201, 201, 0.1);
  432. }
  433. }
  434. .input-icon {
  435. width: 40rpx;
  436. height: 40rpx;
  437. margin-right: 16rpx;
  438. filter: brightness(0) saturate(100%) invert(68%) sepia(48%) saturate(1234%) hue-rotate(134deg) brightness(91%) contrast(92%);
  439. }
  440. .input-field {
  441. flex: 1;
  442. font-size: 28rpx;
  443. color: #333;
  444. &::placeholder {
  445. color: #999;
  446. }
  447. }
  448. .toggle-password {
  449. font-size: 40rpx;
  450. cursor: pointer;
  451. padding: 8rpx;
  452. }
  453. .agreement-section {
  454. margin-top: 32rpx;
  455. margin-bottom: 16rpx;
  456. }
  457. .agreement-label {
  458. display: flex;
  459. align-items: flex-start;
  460. cursor: pointer;
  461. }
  462. .agreement-checkbox {
  463. margin-right: 12rpx;
  464. margin-top: 4rpx;
  465. transform: scale(0.8);
  466. }
  467. .agreement-text {
  468. flex: 1;
  469. display: flex;
  470. flex-wrap: wrap;
  471. align-items: center;
  472. line-height: 1.6;
  473. .prefix {
  474. font-size: 24rpx;
  475. color: #666;
  476. margin-right: 4rpx;
  477. }
  478. .link {
  479. font-size: 24rpx;
  480. color: #1ec9c9;
  481. text-decoration: none;
  482. margin: 0 4rpx;
  483. &:active {
  484. opacity: 0.7;
  485. }
  486. }
  487. }
  488. .login-btn {
  489. width: 100%;
  490. height: 88rpx;
  491. background: linear-gradient(135deg, #1ec9c9 0%, #1eb8b8 100%);
  492. color: #ffffff;
  493. font-size: 32rpx;
  494. font-weight: 600;
  495. border-radius: 16rpx;
  496. border: none;
  497. margin-top: 32rpx;
  498. transition: all 0.3s;
  499. box-shadow: 0 6rpx 20rpx rgba(30, 201, 201, 0.3);
  500. &:active {
  501. opacity: 0.9;
  502. transform: scale(0.98);
  503. }
  504. &::after {
  505. border: none;
  506. }
  507. }
  508. // 协议弹窗
  509. .modal-overlay {
  510. position: fixed;
  511. top: 0;
  512. left: 0;
  513. right: 0;
  514. bottom: 0;
  515. background: rgba(0, 0, 0, 0.5);
  516. display: flex;
  517. align-items: center;
  518. justify-content: center;
  519. z-index: 1000;
  520. .modal-content {
  521. width: 650rpx;
  522. max-height: 80vh;
  523. background: #ffffff;
  524. border-radius: 24rpx;
  525. overflow: hidden;
  526. display: flex;
  527. flex-direction: column;
  528. &.agreement-modal {
  529. .modal-header {
  530. padding: 32rpx;
  531. border-bottom: 1rpx solid #f5f5f5;
  532. display: flex;
  533. align-items: center;
  534. justify-content: space-between;
  535. .modal-title {
  536. font-size: 34rpx;
  537. font-weight: 600;
  538. color: #333333;
  539. }
  540. .modal-close {
  541. width: 56rpx;
  542. height: 56rpx;
  543. display: flex;
  544. align-items: center;
  545. justify-content: center;
  546. border-radius: 50%;
  547. background: #f5f5f5;
  548. &:active {
  549. background: #e0e0e0;
  550. }
  551. .close-icon {
  552. font-size: 40rpx;
  553. color: #666666;
  554. line-height: 1;
  555. }
  556. }
  557. }
  558. .modal-body {
  559. flex: 1;
  560. padding: 32rpx;
  561. max-height: 60vh;
  562. .agreement-content {
  563. font-size: 28rpx;
  564. color: #333333;
  565. line-height: 1.8;
  566. }
  567. }
  568. .modal-footer {
  569. padding: 24rpx 32rpx;
  570. border-top: 1rpx solid #f5f5f5;
  571. .modal-btn {
  572. height: 80rpx;
  573. display: flex;
  574. align-items: center;
  575. justify-content: center;
  576. border-radius: 40rpx;
  577. &.confirm {
  578. background: linear-gradient(135deg, #1ec9c9 0%, #1eb8b8 100%);
  579. &:active {
  580. opacity: 0.9;
  581. }
  582. .modal-btn-text {
  583. color: #ffffff;
  584. font-size: 30rpx;
  585. font-weight: 600;
  586. }
  587. }
  588. }
  589. }
  590. }
  591. }
  592. }
  593. </style>