login.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 { ref, reactive, getCurrentInstance } from "vue";
  39. import { onLoad } from "@dcloudio/uni-app";
  40. const prefixIconStyle = "font-size:50rpx";
  41. const placeholderStyle = "font-size:32rpx";
  42. const customStyle = reactive({
  43. "font-size": "40rpx",
  44. height: "90rpx",
  45. });
  46. let { proxy } = getCurrentInstance();
  47. const formData = reactive({
  48. username: "",
  49. password: "",
  50. code: "",
  51. });
  52. const uForm = ref(null);
  53. const isCountingDown = ref(false);
  54. const countdown = ref(60);
  55. const countDownText = ref("获取验证码");
  56. const startCountDown = () => {
  57. isCountingDown.value = true;
  58. countdown.value = 60;
  59. countDownText.value = `${countdown.value}秒重发`;
  60. const timer = setInterval(() => {
  61. countdown.value--;
  62. countDownText.value = `${countdown.value}秒重发`;
  63. if (countdown.value <= 0) {
  64. clearInterval(timer);
  65. isCountingDown.value = false;
  66. countDownText.value = "重新发送";
  67. }
  68. }, 1000);
  69. };
  70. const getVerifyCode = () => {
  71. if (isCountingDown.value) return;
  72. if (!formData.username) return uni.$u.toast("请输入用户名");
  73. if (!formData.password) return uni.$u.toast("请输入密码");
  74. let data = {
  75. username: formData.username,
  76. password: formData.password,
  77. };
  78. // TODO: 调用发送验证码的API
  79. uni.showLoading({
  80. mask: true,
  81. title: "发送中...",
  82. });
  83. uni.$u.http
  84. .post("/app/appUser/sendMobileCode", data)
  85. .then((res) => {
  86. if (res.code == 200) {
  87. uni.showToast({
  88. title: "验证码发送成功,请注意查收",
  89. icon: "none",
  90. });
  91. startCountDown();
  92. } else {
  93. uni.$u.toast(res.msg);
  94. }
  95. })
  96. .finally(() => uni.hideLoading());
  97. };
  98. const handleLogin = () => {
  99. // 实现登录逻辑
  100. if (!formData.username) return uni.$u.toast("请输入用户名");
  101. if (!formData.password) return uni.$u.toast("请输入密码");
  102. // if (!formData.code) return uni.$u.toast('请输入验证码')
  103. // TODO: 调用登录API /app/appUser/login
  104. uni.showLoading({
  105. mask: true,
  106. title: "登录中...",
  107. });
  108. // /actuator/debug/loginNotCode ///app/appUser/login
  109. uni.$u.http
  110. .post("/app/appUser/login", formData)
  111. .then((res) => {
  112. if (res.code == 200) {
  113. uni.setStorageSync("token", res.data);
  114. setTimeout(() => {
  115. getUserInfo();
  116. //获取菜单权限
  117. uni.$u.http.get("/app/appUser/getRouters").then((resp) => {
  118. if (resp.code == 200) {
  119. uni.setStorageSync("menuList", resp.data);
  120. uni.$u.toast("登录成功");
  121. uni.switchTab({
  122. url: "/pages/index/index",
  123. });
  124. uni.$u.ttsModule.speak("书嗨,不辜负每一个爱书的人");
  125. } else {
  126. uni.$u.toast(resp.msg);
  127. }
  128. });
  129. }, 50);
  130. } else {
  131. uni.$u.toast(res.msg);
  132. }
  133. })
  134. .catch((error) => {
  135. uni.showModal({
  136. title: "登录失败",
  137. content: JSON.stringify(error),
  138. showCancel: false,
  139. });
  140. })
  141. .finally(() => uni.hideLoading());
  142. };
  143. //获取登陆人信息并存储本地 /app/appUser/getUserInfoById
  144. function getUserInfo() {
  145. uni.$u.http.get("/app/appUser/getUserInfoById").then((res) => {
  146. if (res.code == 200) {
  147. uni.setStorageSync("userInfo", res.data);
  148. }
  149. });
  150. }
  151. //检测token是否可用
  152. function checkToken() {
  153. // /app/appUser/checkToken
  154. uni.showLoading({
  155. title: "检测登录状态...",
  156. });
  157. uni.$u.http
  158. .get("/app/appUser/checkToken")
  159. .then((res) => {
  160. if (res.code == 200) {
  161. uni.$u.toast("登录成功");
  162. uni.switchTab({
  163. url: "/pages/index/index",
  164. });
  165. } else {
  166. uni.$u.toast("请先登录");
  167. }
  168. })
  169. .finally(() => uni.hideLoading());
  170. }
  171. onLoad(() => {
  172. let token = uni.getStorageSync("token");
  173. if (token) {
  174. checkToken();
  175. }
  176. });
  177. </script>
  178. <style>
  179. page {
  180. background-color: #ffffff;
  181. }
  182. </style>
  183. <style lang="scss" scoped>
  184. .login-container {
  185. padding: 5% 60rpx;
  186. .logo-section {
  187. display: flex;
  188. flex-direction: column;
  189. align-items: center;
  190. }
  191. .logo {
  192. width: 300rpx;
  193. height: 300rpx;
  194. }
  195. .form-section {
  196. margin-top: 40rpx;
  197. }
  198. }
  199. </style>