index.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <script setup lang="ts">
  2. import type { MemberVO } from '@/api/globals'
  3. import router from '@/router'
  4. definePage({
  5. name: 'login',
  6. islogin: false,
  7. style: {
  8. navigationBarTitleText: '登录',
  9. navigationStyle: 'custom',
  10. },
  11. })
  12. // 表单数据
  13. const phone = ref('')
  14. const code = ref('')
  15. // 验证码倒计时
  16. const countdown = ref(0)
  17. const isSending = ref(false)
  18. const { token, redirectName, userInfo } = storeToRefs(useUserStore())
  19. const tabList = ['/pages/order/index', '/pages/voucher/index', '/pages/index/index']
  20. // 发送验证码
  21. async function handleSendCode() {
  22. if (!phone.value) {
  23. useGlobalToast().show('请输入手机号')
  24. return
  25. }
  26. if (!/^1[3-9]\d{9}$/.test(phone.value)) {
  27. useGlobalToast().show('请输入正确的手机号')
  28. return
  29. }
  30. if (isSending.value)
  31. return
  32. isSending.value = true
  33. countdown.value = 60
  34. const timer = setInterval(() => {
  35. countdown.value--
  36. if (countdown.value <= 0) {
  37. clearInterval(timer)
  38. isSending.value = false
  39. }
  40. }, 1000)
  41. // TODO: 调用发送验证码接口
  42. await Apis.general.post_smqjh_auth_api_v1_auth_sms_code({ params: { mobile: phone.value } })
  43. useGlobalToast().show('验证码已发送')
  44. }
  45. // 登录
  46. async function handleLogin() {
  47. if (!phone.value) {
  48. useGlobalToast().show('请输入手机号')
  49. return
  50. }
  51. if (!code.value) {
  52. useGlobalToast().show('请输入验证码')
  53. return
  54. }
  55. uni.showLoading({ mask: true })
  56. try {
  57. const res = await Apis.general.post_smqjh_auth_oauth2_token({ params: { grant_type: 'sms_code', mobile: phone.value, code: code.value } })
  58. uni.hideLoading()
  59. token.value = `Bearer ${res.data.access_token}`
  60. useGlobalToast().show('登录成功')
  61. const user = await Apis.general.get_smqjh_system_app_api_v1_members_me()
  62. userInfo.value = user.data as MemberVO
  63. setTimeout(() => {
  64. if (tabList.includes(redirectName.value)) {
  65. router.pushTab({ path: redirectName.value })
  66. }
  67. else {
  68. router.replace({ path: redirectName.value })
  69. }
  70. }, 1000)
  71. }
  72. catch {
  73. uni.hideLoading()
  74. }
  75. }
  76. </script>
  77. <template>
  78. <view class="login-page min-h-100vh px48rpx">
  79. <!-- 表单区域 -->
  80. <view class="pt292rpx">
  81. <!-- 手机号 -->
  82. <view class="mb48rpx">
  83. <view class="mb24rpx text-32rpx text-#333 font-medium">
  84. 手机号
  85. </view>
  86. <view class="h96rpx flex items-center rounded-48rpx bg-#E5E8D8 px32rpx">
  87. <input
  88. v-model="phone"
  89. type="number"
  90. :maxlength="11"
  91. placeholder="请输入手机号"
  92. placeholder-class="text-#999"
  93. class="flex-1 text-32rpx"
  94. >
  95. </view>
  96. </view>
  97. <!-- 验证码 -->
  98. <view class="mb64rpx">
  99. <view class="mb24rpx text-32rpx text-#333 font-medium">
  100. 验证码
  101. </view>
  102. <view class="h96rpx flex items-center rounded-48rpx bg-#E5E8D8 px32rpx">
  103. <input
  104. v-model="code"
  105. type="tel"
  106. :maxlength="4"
  107. placeholder="请输入验证码"
  108. placeholder-class="text-#999"
  109. class="flex-1 text-32rpx"
  110. >
  111. <view
  112. class="ml20rpx flex-shrink-0 text-28rpx"
  113. :class="[isSending ? 'text-#999' : 'text-#7CB305']"
  114. @click="handleSendCode"
  115. >
  116. {{ isSending ? `${countdown}秒后重新发送` : '获取验证码' }}
  117. </view>
  118. </view>
  119. </view>
  120. <!-- 登录按钮 -->
  121. <wd-button block size="large" @click="handleLogin">
  122. 登录
  123. </wd-button>
  124. </view>
  125. </view>
  126. </template>
  127. <style lang="scss" scoped>
  128. .login-page {
  129. background: linear-gradient( 180deg, #F3FFD1 0%, #FFFFFF 37.42%, #F3FFD1 100%);
  130. }
  131. </style>