login.vue 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. <template>
  2. <view class="login-container">
  3. <!-- 语言切换按钮 -->
  4. <view class="language-switcher" @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. <view class="login-box">
  10. <view class="logo-section">
  11. <text class="app-title">{{ $t('login.appTitle') }}</text>
  12. <text class="app-subtitle">{{ $t('login.welcome') }}</text>
  13. </view>
  14. <view class="form-section">
  15. <view class="form-item">
  16. <view class="input-wrapper">
  17. <image class="input-icon" src="/static/pages/login/phone.png" mode="aspectFit" />
  18. <input
  19. v-model="phone"
  20. type="text"
  21. maxlength="11"
  22. :placeholder="$t('login.phonePlaceholder')"
  23. class="input-field"
  24. @input="onPhoneInput"
  25. />
  26. </view>
  27. </view>
  28. <view class="form-item">
  29. <view class="input-wrapper">
  30. <image class="input-icon" src="/static/pages/login/password.png" mode="aspectFit" />
  31. <input
  32. v-model="password"
  33. :password="!showPassword"
  34. :placeholder="$t('login.passwordPlaceholder')"
  35. class="input-field"
  36. />
  37. <text
  38. class="toggle-password"
  39. @click="togglePassword"
  40. >
  41. {{ showPassword ? '👁' : '👁‍🗨' }}
  42. </text>
  43. </view>
  44. </view>
  45. <!-- 协议勾选 -->
  46. <view class="agreement-section">
  47. <checkbox-group @change="handleAgreementChange">
  48. <label class="agreement-label">
  49. <checkbox
  50. :checked="agreedToTerms"
  51. class="agreement-checkbox"
  52. color="#2E7CF6"
  53. />
  54. <view class="agreement-text">
  55. <text class="prefix">{{ $t('login.agreePrefix') }}</text>
  56. <text class="link" @click.stop="viewUserAgreement">{{ $t('login.userAgreement') }}</text>
  57. <text class="prefix">{{ $t('login.and') }}</text>
  58. <text class="link" @click.stop="viewPrivacyPolicy">{{ $t('login.privacyPolicy') }}</text>
  59. </view>
  60. </label>
  61. </checkbox-group>
  62. </view>
  63. <button
  64. class="login-btn"
  65. @click="handleLogin"
  66. >
  67. {{ $t('login.loginButton') }}
  68. </button>
  69. </view>
  70. </view>
  71. </view>
  72. </template>
  73. <script setup>
  74. import { ref, computed, nextTick } from 'vue'
  75. import { onShow } from '@dcloudio/uni-app'
  76. import { useI18n } from 'vue-i18n'
  77. import { useUserStore } from '@/store/index'
  78. import { useLocaleStore } from '@/store/locale'
  79. import request from '@/utils/request.js'
  80. const { t, locale } = useI18n()
  81. const userStore = useUserStore()
  82. const localeStore = useLocaleStore()
  83. const phone = ref('')
  84. const password = ref('')
  85. const showPassword = ref(false)
  86. const isLanguageSwitching = ref(false)
  87. const agreedToTerms = ref(false)
  88. // 当前语言名称
  89. const currentLanguageName = computed(() => {
  90. return localeStore.getCurrentLocaleName()
  91. })
  92. // 页面显示时更新标题
  93. onShow(() => {
  94. // 更新导航栏标题
  95. uni.setNavigationBarTitle({
  96. title: t('login.title')
  97. })
  98. })
  99. // 切换语言 - 优化版本,避免页面重绘
  100. const switchLanguage = async () => {
  101. // 防止频繁切换
  102. if (isLanguageSwitching.value) {
  103. return
  104. }
  105. isLanguageSwitching.value = true
  106. try {
  107. // 直接切换语言,i18n 会自动处理文本更新
  108. localeStore.toggleLocale()
  109. // 使用 nextTick 确保在 DOM 更新后再更新导航栏
  110. await nextTick()
  111. // 异步更新导航栏标题
  112. uni.setNavigationBarTitle({
  113. title: t('login.title')
  114. })
  115. } finally {
  116. // 短暂延迟后允许再次切换
  117. setTimeout(() => {
  118. isLanguageSwitching.value = false
  119. }, 300)
  120. }
  121. }
  122. // 验证手机号格式
  123. const isValidPhone = computed(() => {
  124. return /^1[3-9]\d{9}$/.test(phone.value)
  125. })
  126. // 是否可以提交
  127. const canSubmit = computed(() => {
  128. return isValidPhone.value && password.value.length >= 6
  129. })
  130. // 手机号输入处理
  131. const onPhoneInput = (e) => {
  132. // 限制只能输入数字
  133. phone.value = e.detail.value.replace(/[^\d]/g, '')
  134. }
  135. // 切换密码显示
  136. const togglePassword = () => {
  137. showPassword.value = !showPassword.value
  138. }
  139. // 协议勾选变化
  140. const handleAgreementChange = (e) => {
  141. agreedToTerms.value = e.detail.value.length > 0
  142. }
  143. // 查看用户协议
  144. const viewUserAgreement = () => {
  145. uni.navigateTo({
  146. url: '/pages-content/my/aggreement/detail?type=user'
  147. })
  148. }
  149. // 查看隐私协议
  150. const viewPrivacyPolicy = () => {
  151. uni.navigateTo({
  152. url: '/pages-content/my/aggreement/detail?type=privacy'
  153. })
  154. }
  155. // 登录处理
  156. const handleLogin = async () => {
  157. // 验证手机号
  158. if (!isValidPhone.value) {
  159. uni.showToast({
  160. title: t('login.phoneError'),
  161. icon: 'none',
  162. duration: 2000
  163. })
  164. return
  165. }
  166. // 验证密码
  167. if (password.value.length < 6) {
  168. uni.showToast({
  169. title: t('login.passwordError'),
  170. icon: 'none',
  171. duration: 2000
  172. })
  173. return
  174. }
  175. // 验证是否同意协议
  176. if (!agreedToTerms.value) {
  177. uni.showToast({
  178. title: t('login.agreementError'),
  179. icon: 'none',
  180. duration: 2000
  181. })
  182. return
  183. }
  184. try {
  185. uni.showLoading({
  186. title: t('login.loggingIn'),
  187. mask: true
  188. })
  189. // 准备登录参数
  190. const loginContent = JSON.stringify({
  191. phoneNumber: phone.value,
  192. password: password.value
  193. })
  194. // 调用新的登录接口
  195. const res = await request({
  196. url: '/applet/auth/login/password',
  197. method: 'POST',
  198. data: {
  199. clientId: '2f847927afb2b3ebeefc870c13d623f2',
  200. content: loginContent
  201. }
  202. })
  203. // 保存 token
  204. userStore.setToken(res.data.token)
  205. uni.hideLoading()
  206. uni.showToast({
  207. title: t('login.loginSuccess'),
  208. icon: 'success',
  209. duration: 1500
  210. })
  211. // 登录成功后跳转到首页
  212. setTimeout(() => {
  213. uni.navigateTo({
  214. url: '/pages/index'
  215. })
  216. }, 1500)
  217. } catch (error) {
  218. uni.hideLoading()
  219. console.error('登录失败:', error)
  220. }
  221. }
  222. </script>
  223. <style lang="scss" scoped>
  224. .login-container {
  225. min-height: 100vh;
  226. background: linear-gradient(135deg, #2E7CF6 0%, #1A5FC7 100%);
  227. display: flex;
  228. flex-direction: column;
  229. align-items: center;
  230. justify-content: flex-start;
  231. padding: 40rpx;
  232. padding-top: 270rpx;
  233. position: relative;
  234. }
  235. .language-switcher {
  236. align-self: flex-end;
  237. margin-bottom: 32rpx;
  238. margin-right: 40rpx;
  239. display: flex;
  240. align-items: center;
  241. background: rgba(255, 255, 255, 0.95);
  242. backdrop-filter: blur(10rpx);
  243. border-radius: 30rpx;
  244. padding: 12rpx 24rpx;
  245. cursor: pointer;
  246. transition: all 0.3s;
  247. box-shadow: 0 4rpx 12rpx rgba(0, 0, 0, 0.15);
  248. gap: 8rpx;
  249. &:active {
  250. transform: scale(0.95);
  251. background: rgba(255, 255, 255, 1);
  252. }
  253. }
  254. .login-box {
  255. width: 100%;
  256. max-width: 600rpx;
  257. background: #ffffff;
  258. border-radius: 32rpx;
  259. padding: 60rpx 40rpx;
  260. box-shadow: 0 20rpx 60rpx rgba(0, 0, 0, 0.15);
  261. }
  262. .language-text {
  263. font-size: 26rpx;
  264. color: #999;
  265. font-weight: 500;
  266. transition: all 0.3s;
  267. &.active {
  268. color: #2E7CF6;
  269. font-weight: 700;
  270. }
  271. }
  272. .language-separator {
  273. font-size: 26rpx;
  274. color: #ccc;
  275. font-weight: 300;
  276. }
  277. .logo-section {
  278. text-align: center;
  279. margin-bottom: 80rpx;
  280. }
  281. .app-title {
  282. display: block;
  283. font-size: 48rpx;
  284. font-weight: bold;
  285. color: #333;
  286. margin-bottom: 16rpx;
  287. }
  288. .app-subtitle {
  289. display: block;
  290. font-size: 28rpx;
  291. color: #999;
  292. }
  293. .form-section {
  294. width: 100%;
  295. }
  296. .form-item {
  297. margin-bottom: 32rpx;
  298. }
  299. .input-wrapper {
  300. display: flex;
  301. align-items: center;
  302. background: #f5f5f5;
  303. border-radius: 16rpx;
  304. padding: 0 24rpx;
  305. height: 96rpx;
  306. transition: all 0.3s;
  307. border: 2rpx solid transparent;
  308. &:focus-within {
  309. background: #fff;
  310. border-color: #2E7CF6;
  311. box-shadow: 0 0 0 4rpx rgba(46, 124, 246, 0.1);
  312. }
  313. }
  314. .input-icon {
  315. width: 40rpx;
  316. height: 40rpx;
  317. margin-right: 16rpx;
  318. opacity: 0.6;
  319. }
  320. .input-field {
  321. flex: 1;
  322. font-size: 32rpx;
  323. color: #333;
  324. &::placeholder {
  325. color: #999;
  326. }
  327. }
  328. .toggle-password {
  329. font-size: 40rpx;
  330. cursor: pointer;
  331. padding: 8rpx;
  332. }
  333. .login-btn {
  334. width: 100%;
  335. height: 96rpx;
  336. background: linear-gradient(135deg, #2E7CF6 0%, #1A5FC7 100%);
  337. color: #ffffff;
  338. font-size: 32rpx;
  339. font-weight: bold;
  340. border-radius: 16rpx;
  341. border: none;
  342. margin-top: 48rpx;
  343. transition: all 0.3s;
  344. &:active {
  345. opacity: 0.8;
  346. transform: scale(0.98);
  347. }
  348. }
  349. .login-btn-disabled {
  350. background: #ccc;
  351. opacity: 0.6;
  352. &:active {
  353. transform: none;
  354. }
  355. }
  356. .agreement-section {
  357. margin-top: 24rpx;
  358. margin-bottom: 8rpx;
  359. }
  360. .agreement-label {
  361. display: flex;
  362. align-items: center;
  363. cursor: pointer;
  364. }
  365. .agreement-checkbox {
  366. margin-right: 12rpx;
  367. transform: scale(0.7);
  368. }
  369. .agreement-text {
  370. flex: 1;
  371. display: flex;
  372. flex-wrap: wrap;
  373. align-items: center;
  374. line-height: 1.6;
  375. .prefix {
  376. font-size: 24rpx;
  377. color: #666;
  378. margin-right: 4rpx;
  379. }
  380. .link {
  381. font-size: 24rpx;
  382. color: #2E7CF6;
  383. text-decoration: none;
  384. margin: 0 4rpx;
  385. &:active {
  386. opacity: 0.7;
  387. }
  388. }
  389. }
  390. </style>