login.vue 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <view class="login-container">
  3. <!-- Logo和标题区域 -->
  4. <view class="logo-section">
  5. <image class="logo" src="/static/img/logo.png" mode="aspectFit" />
  6. </view>
  7. <!-- 表单区域 -->
  8. <view class="form-section">
  9. <u--form :model="formData" ref="uForm">
  10. <u-form-item>
  11. <u-input :prefixIconStyle="prefixIconStyle" :placeholder-style="placeholderStyle"
  12. :custom-style="customStyle" v-model="formData.username" placeholder="请输入用户名"
  13. prefixIcon="account" />
  14. </u-form-item>
  15. <u-form-item>
  16. <u-input :prefixIconStyle="prefixIconStyle" :placeholder-style="placeholderStyle"
  17. :custom-style="customStyle" v-model="formData.password" type="password" placeholder="请输入密码"
  18. prefixIcon="lock" />
  19. </u-form-item>
  20. <u-form-item>
  21. <u-input :prefixIconStyle="prefixIconStyle" :placeholder-style="placeholderStyle"
  22. :custom-style="customStyle" v-model="formData.code" placeholder="请输入验证码" prefixIcon="chat">
  23. <template #suffix>
  24. <u-button custom-style="width:200rpx" color="#22ac38" size="small" @click="getVerifyCode"
  25. :disabled="isCountingDown">
  26. {{ countDownText }}
  27. </u-button>
  28. </template>
  29. </u-input>
  30. </u-form-item>
  31. </u--form>
  32. <u-button custom-style="margin-top:5%" type="primary" block @click="handleLogin" color="#22ac38"
  33. shape="circle">登录</u-button>
  34. </view>
  35. </view>
  36. </template>
  37. <script setup>
  38. import {
  39. ref,
  40. reactive,
  41. getCurrentInstance,
  42. } from 'vue'
  43. import {
  44. onLoad
  45. } from '@dcloudio/uni-app'
  46. const prefixIconStyle = "font-size:50rpx"
  47. const placeholderStyle = "font-size:32rpx"
  48. const customStyle = reactive({
  49. "font-size": '40rpx',
  50. height: '90rpx'
  51. })
  52. let {
  53. proxy
  54. } = getCurrentInstance()
  55. const formData = reactive({
  56. username: '',
  57. password: '',
  58. code: ''
  59. })
  60. const uForm = ref(null)
  61. const isCountingDown = ref(false)
  62. const countdown = ref(60)
  63. const countDownText = ref('获取验证码')
  64. const startCountDown = () => {
  65. isCountingDown.value = true
  66. countdown.value = 60
  67. countDownText.value = `${countdown.value}秒重发`
  68. const timer = setInterval(() => {
  69. countdown.value--
  70. countDownText.value = `${countdown.value}秒重发`
  71. if (countdown.value <= 0) {
  72. clearInterval(timer)
  73. isCountingDown.value = false
  74. countDownText.value = '重新发送'
  75. }
  76. }, 1000)
  77. }
  78. const getVerifyCode = () => {
  79. if (isCountingDown.value) return
  80. if (!formData.username) return uni.$u.toast('请输入用户名')
  81. if (!formData.password) return uni.$u.toast('请输入密码')
  82. let data = {
  83. username: formData.username,
  84. password: formData.password
  85. }
  86. // TODO: 调用发送验证码的API
  87. uni.showLoading({
  88. mask: true,
  89. title: "发送中..."
  90. })
  91. uni.$u.http.post('/app/appUser/sendMobileCode', data).then(res => {
  92. if (res.code == 200) {
  93. uni.showToast({
  94. title: '验证码发送成功,请注意查收',
  95. icon: 'none'
  96. })
  97. startCountDown()
  98. } else {
  99. uni.$u.toast(res.msg)
  100. }
  101. }).finally(() => uni.hideLoading())
  102. }
  103. const handleLogin = () => {
  104. // 实现登录逻辑
  105. if (!formData.username) return uni.$u.toast('请输入用户名')
  106. if (!formData.password) return uni.$u.toast('请输入密码')
  107. // if (!formData.code) return uni.$u.toast('请输入验证码')
  108. // TODO: 调用登录API /app/appUser/login
  109. uni.showLoading({
  110. mask: true,
  111. title: "登录中..."
  112. })
  113. // /actuator/debug/loginNotCode ///app/appUser/login
  114. uni.$u.http.post('/actuator/debug/loginNotCode', formData).then(res => {
  115. if (res.code == 200) {
  116. uni.$u.toast('登录成功')
  117. uni.switchTab({
  118. url: "/pages/index/index"
  119. })
  120. console.log(res,'xxxxx')
  121. uni.setStorageSync('token', res.data)
  122. getUserInfo()
  123. uni.$u.ttsModule.speak('书嗨,不辜负每一个爱书的人')
  124. } else {
  125. uni.$u.toast(res.msg)
  126. }
  127. }).finally(() => uni.hideLoading())
  128. }
  129. //获取登陆人信息并存储本地 /app/appUser/getUserInfoById
  130. function getUserInfo(){
  131. uni.$u.http.get('/app/appUser/getUserInfoById').then(res => {
  132. if (res.code == 200) {
  133. uni.setStorageSync('userInfo', res.data)
  134. }
  135. })
  136. }
  137. //检测token是否可用
  138. function checkToken() {
  139. // /app/appUser/checkToken
  140. uni.showLoading({
  141. title: "检测登录状态..."
  142. })
  143. uni.$u.http.get('/app/appUser/checkToken').then(res => {
  144. if (res.code == 200) {
  145. uni.$u.toast('登录成功')
  146. uni.switchTab({
  147. url: "/pages/index/index"
  148. })
  149. } else {
  150. uni.$u.toast('请先登录')
  151. }
  152. }).finally(() => uni.hideLoading())
  153. }
  154. onLoad(() => {
  155. let token = uni.getStorageSync('token')
  156. if (token) {
  157. checkToken()
  158. }
  159. })
  160. </script>
  161. <style>
  162. page {
  163. background-color: #ffffff;
  164. }
  165. </style>
  166. <style lang="scss" scoped>
  167. .login-container {
  168. padding: 5% 60rpx;
  169. .logo-section {
  170. display: flex;
  171. flex-direction: column;
  172. align-items: center;
  173. }
  174. .logo {
  175. width: 300rpx;
  176. height: 300rpx;
  177. }
  178. .form-section {
  179. margin-top: 40rpx;
  180. }
  181. }
  182. </style>