login.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  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">{{ t('login.appTitle') }}</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. </view>
  75. </template>
  76. <script setup>
  77. import { ref, computed, nextTick, onMounted } from 'vue'
  78. import { onShow } from '@dcloudio/uni-app'
  79. import { useI18n } from 'vue-i18n'
  80. import { useUserStore } from '@/store/index'
  81. import { useLocaleStore } from '@/store/locale'
  82. import request from '@/utils/request.js'
  83. const { t, locale } = useI18n()
  84. const userStore = useUserStore()
  85. const localeStore = useLocaleStore()
  86. const phone = ref('')
  87. const password = ref('')
  88. const showPassword = ref(false)
  89. const isLanguageSwitching = ref(false)
  90. const agreedToTerms = ref(false)
  91. const statusBarHeight = ref(0)
  92. // 语言切换按钮的top位置
  93. const languageSwitcherTop = computed(() => {
  94. return statusBarHeight.value + 50 // 状态栏高度 + 50px间距,确保不被遮挡
  95. })
  96. onMounted(() => {
  97. // 获取系统信息
  98. const systemInfo = uni.getSystemInfoSync()
  99. statusBarHeight.value = systemInfo.statusBarHeight || 0
  100. })
  101. // 当前语言名称
  102. const currentLanguageName = computed(() => {
  103. return localeStore.getCurrentLocaleName()
  104. })
  105. // 页面显示时更新标题
  106. onShow(() => {
  107. // 更新导航栏标题
  108. uni.setNavigationBarTitle({
  109. title: t('login.title')
  110. })
  111. })
  112. // 切换语言 - 优化版本,避免页面重绘
  113. const switchLanguage = async () => {
  114. // 防止频繁切换
  115. if (isLanguageSwitching.value) {
  116. return
  117. }
  118. isLanguageSwitching.value = true
  119. try {
  120. // 直接切换语言,i18n 会自动处理文本更新
  121. localeStore.toggleLocale()
  122. // 使用 nextTick 确保在 DOM 更新后再更新导航栏
  123. await nextTick()
  124. // 异步更新导航栏标题
  125. uni.setNavigationBarTitle({
  126. title: t('login.title')
  127. })
  128. // 显示切换成功提示
  129. uni.showToast({
  130. title: t('common.language.switchSuccess'),
  131. icon: 'success',
  132. duration: 1500
  133. })
  134. } finally {
  135. // 短暂延迟后允许再次切换
  136. setTimeout(() => {
  137. isLanguageSwitching.value = false
  138. }, 300)
  139. }
  140. }
  141. // 验证手机号格式
  142. const isValidPhone = computed(() => {
  143. return /^1[3-9]\d{9}$/.test(phone.value)
  144. })
  145. // 是否可以提交
  146. const canSubmit = computed(() => {
  147. return isValidPhone.value && password.value.length >= 6
  148. })
  149. // 手机号输入处理
  150. const onPhoneInput = (e) => {
  151. // 限制只能输入数字
  152. phone.value = e.detail.value.replace(/[^\d]/g, '')
  153. }
  154. // 切换密码显示
  155. const togglePassword = () => {
  156. showPassword.value = !showPassword.value
  157. }
  158. // 协议勾选变化
  159. const handleAgreementChange = (e) => {
  160. agreedToTerms.value = e.detail.value.length > 0
  161. }
  162. // 查看用户协议
  163. const viewUserAgreement = () => {
  164. uni.navigateTo({
  165. url: '/pages-content/my/aggreement/detail?type=user'
  166. })
  167. }
  168. // 查看隐私协议
  169. const viewPrivacyPolicy = () => {
  170. uni.navigateTo({
  171. url: '/pages-content/my/aggreement/detail?type=privacy'
  172. })
  173. }
  174. // 登录处理
  175. const handleLogin = async () => {
  176. // 验证手机号
  177. if (!isValidPhone.value) {
  178. uni.showToast({
  179. title: t('login.phoneError'),
  180. icon: 'none',
  181. duration: 2000
  182. })
  183. return
  184. }
  185. // 验证密码
  186. if (password.value.length < 6) {
  187. uni.showToast({
  188. title: t('login.passwordError'),
  189. icon: 'none',
  190. duration: 2000
  191. })
  192. return
  193. }
  194. // 验证是否同意协议
  195. if (!agreedToTerms.value) {
  196. uni.showToast({
  197. title: t('login.agreementError'),
  198. icon: 'none',
  199. duration: 2000
  200. })
  201. return
  202. }
  203. try {
  204. uni.showLoading({
  205. title: t('login.loggingIn'),
  206. mask: true
  207. })
  208. // 准备登录参数
  209. const loginContent = JSON.stringify({
  210. phoneNumber: phone.value,
  211. password: password.value
  212. })
  213. // 调用新的登录接口
  214. const res = await request({
  215. url: '/applet/auth/login/password',
  216. method: 'POST',
  217. data: {
  218. clientId: '2f847927afb2b3ebeefc870c13d623f2',
  219. content: loginContent
  220. }
  221. })
  222. // 保存 token
  223. userStore.setToken(res.data.token)
  224. uni.hideLoading()
  225. uni.showToast({
  226. title: t('login.loginSuccess'),
  227. icon: 'success',
  228. duration: 1500
  229. })
  230. // 登录成功后跳转到首页
  231. setTimeout(() => {
  232. uni.navigateTo({
  233. url: '/pages/index'
  234. })
  235. }, 1500)
  236. } catch (error) {
  237. uni.hideLoading()
  238. console.error('登录失败:', error)
  239. }
  240. }
  241. </script>
  242. <style lang="scss" scoped>
  243. .login-container {
  244. min-height: 100vh;
  245. background-color: #f5f5f5;
  246. display: flex;
  247. flex-direction: column;
  248. position: relative;
  249. }
  250. // 语言切换按钮
  251. .language-switcher {
  252. position: fixed;
  253. right: 32rpx;
  254. display: flex;
  255. align-items: center;
  256. background: rgba(255, 255, 255, 0.2);
  257. backdrop-filter: blur(10rpx);
  258. border-radius: 30rpx;
  259. padding: 12rpx 24rpx;
  260. cursor: pointer;
  261. transition: all 0.3s;
  262. gap: 8rpx;
  263. z-index: 100;
  264. &:active {
  265. transform: scale(0.95);
  266. background: rgba(255, 255, 255, 0.3);
  267. }
  268. .language-text {
  269. font-size: 24rpx;
  270. color: rgba(255, 255, 255, 0.7);
  271. font-weight: 500;
  272. transition: all 0.3s;
  273. &.active {
  274. color: #ffffff;
  275. font-weight: 700;
  276. }
  277. }
  278. .language-separator {
  279. font-size: 24rpx;
  280. color: rgba(255, 255, 255, 0.5);
  281. font-weight: 300;
  282. }
  283. }
  284. // 顶部渐变背景
  285. .header-bg {
  286. background: linear-gradient(180deg, #1ec9c9 0%, #1eb8b8 100%);
  287. min-height: 500rpx;
  288. display: flex;
  289. flex-direction: column;
  290. align-items: center;
  291. justify-content: center;
  292. position: relative;
  293. padding: 0 40rpx 60rpx;
  294. .app-title {
  295. font-size: 52rpx;
  296. font-weight: 700;
  297. color: #ffffff;
  298. letter-spacing: 2rpx;
  299. margin-bottom: 16rpx;
  300. text-align: center;
  301. line-height: 1.3;
  302. word-break: break-word;
  303. max-width: 600rpx;
  304. }
  305. .app-subtitle {
  306. font-size: 28rpx;
  307. color: rgba(255, 255, 255, 0.9);
  308. letter-spacing: 2rpx;
  309. text-align: center;
  310. }
  311. }
  312. // 登录卡片
  313. .login-card {
  314. background: #ffffff;
  315. border-radius: 24rpx 24rpx 0 0;
  316. margin-top: -60rpx;
  317. padding: 48rpx 32rpx;
  318. flex: 1;
  319. box-shadow: 0 -4rpx 20rpx rgba(0, 0, 0, 0.08);
  320. position: relative;
  321. z-index: 5;
  322. .card-title {
  323. font-size: 36rpx;
  324. font-weight: 600;
  325. color: #333333;
  326. margin-bottom: 32rpx;
  327. }
  328. }
  329. .form-section {
  330. width: 100%;
  331. }
  332. .form-item {
  333. margin-bottom: 24rpx;
  334. }
  335. .input-wrapper {
  336. display: flex;
  337. align-items: center;
  338. background: #f5f5f5;
  339. border-radius: 16rpx;
  340. padding: 0 24rpx;
  341. height: 96rpx;
  342. transition: all 0.3s;
  343. border: 2rpx solid transparent;
  344. &:focus-within {
  345. background: #fff;
  346. border-color: #1ec9c9;
  347. box-shadow: 0 0 0 4rpx rgba(30, 201, 201, 0.1);
  348. }
  349. }
  350. .input-icon {
  351. width: 40rpx;
  352. height: 40rpx;
  353. margin-right: 16rpx;
  354. opacity: 0.6;
  355. }
  356. .input-field {
  357. flex: 1;
  358. font-size: 28rpx;
  359. color: #333;
  360. &::placeholder {
  361. color: #999;
  362. }
  363. }
  364. .toggle-password {
  365. font-size: 40rpx;
  366. cursor: pointer;
  367. padding: 8rpx;
  368. }
  369. .agreement-section {
  370. margin-top: 32rpx;
  371. margin-bottom: 16rpx;
  372. }
  373. .agreement-label {
  374. display: flex;
  375. align-items: flex-start;
  376. cursor: pointer;
  377. }
  378. .agreement-checkbox {
  379. margin-right: 12rpx;
  380. margin-top: 4rpx;
  381. transform: scale(0.8);
  382. }
  383. .agreement-text {
  384. flex: 1;
  385. display: flex;
  386. flex-wrap: wrap;
  387. align-items: center;
  388. line-height: 1.6;
  389. .prefix {
  390. font-size: 24rpx;
  391. color: #666;
  392. margin-right: 4rpx;
  393. }
  394. .link {
  395. font-size: 24rpx;
  396. color: #1ec9c9;
  397. text-decoration: none;
  398. margin: 0 4rpx;
  399. &:active {
  400. opacity: 0.7;
  401. }
  402. }
  403. }
  404. .login-btn {
  405. width: 100%;
  406. height: 88rpx;
  407. background: linear-gradient(135deg, #1ec9c9 0%, #1eb8b8 100%);
  408. color: #ffffff;
  409. font-size: 32rpx;
  410. font-weight: 600;
  411. border-radius: 16rpx;
  412. border: none;
  413. margin-top: 32rpx;
  414. transition: all 0.3s;
  415. box-shadow: 0 6rpx 20rpx rgba(30, 201, 201, 0.3);
  416. &:active {
  417. opacity: 0.9;
  418. transform: scale(0.98);
  419. }
  420. &::after {
  421. border: none;
  422. }
  423. }
  424. </style>