login.vue 7.1 KB

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