login.vue 11 KB

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