login.vue 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <template>
  2. <view class="u-page">
  3. <view class="u-block">
  4. <view class="u-block__content">
  5. <view class="image"><image src="/static/image/logo.png" mode="aspectFit"></image></view>
  6. <u--form ref="form1" labelPosition="left" :model="modelInfo" labelWidth="10">
  7. <u-form-item prop="username"><u-input v-model="modelInfo.username" @confirm="startLogin" placeholder="请输入用户名"></u-input></u-form-item>
  8. <u-form-item prop="password"><u-input type="password" @confirm="startLogin" v-model="modelInfo.password" placeholder="请输入密码"></u-input></u-form-item>
  9. <u-form-item>
  10. <u-button type="primary" v-if="!isRotate" text="登录" @click="startLogin"></u-button>
  11. <u-button type="primary" v-if="isRotate" loading loadingText="登录中..."></u-button>
  12. </u-form-item>
  13. </u--form>
  14. </view>
  15. </view>
  16. </view>
  17. </template>
  18. <script>
  19. import { log } from "@/utils/base.js";
  20. export default {
  21. data() {
  22. return {
  23. //logoImage: "/pages/login/image/logo.png",
  24. modelInfo: {
  25. username: "",
  26. password: "",
  27. },
  28. userName: "", //用户/电话
  29. password: "", //密码
  30. isRotate: false, //是否加载旋转
  31. isFocus: true, // 是否聚焦
  32. redirect: "",
  33. rules: {
  34. username: {
  35. type: "string",
  36. required: true,
  37. message: "用户名不能为空",
  38. trigger: ["blur", "change"],
  39. },
  40. password: {
  41. type: "string",
  42. required: true,
  43. message: "密码不能为空",
  44. trigger: ["blur", "change"],
  45. },
  46. },
  47. };
  48. },
  49. onReady() {
  50. // 如果需要兼容微信小程序,并且校验规则中含有方法等,只能通过setRules方法设置规则
  51. this.$refs.form1.setRules(this.rules);
  52. },
  53. onLoad(opt) {
  54. this.redirect = opt.redirect ? decodeURIComponent(opt.redirect) : "";
  55. this.$store.commit("menu/setMenus", []);
  56. //log("=====>opt", opt, this.redirect);
  57. },
  58. mounted() {
  59. this.$store.commit("logout");
  60. //this.isLogin();
  61. },
  62. methods: {
  63. isLogin() {
  64. if (this.$store.getters.hasLogin) {
  65. this.$Router.replaceAll("/pages/home/home");
  66. }
  67. },
  68. startLogin(e) {
  69. //登录
  70. this.$store.commit("logout");
  71. if (this.isRotate) {
  72. //判断是否加载中,避免重复点击请求
  73. return false;
  74. }
  75. if (!this.modelInfo.username) {
  76. uni.showToast({
  77. type: "error",
  78. position: "top",
  79. title: "用户名不能为空",
  80. });
  81. return;
  82. }
  83. if (!this.modelInfo.password) {
  84. uni.showToast({
  85. position: "top",
  86. title: "密码不能为空",
  87. });
  88. return;
  89. }
  90. this.isRotate = true;
  91. this.$store
  92. .dispatch("login", {
  93. n: this.modelInfo.username,
  94. p: this.modelInfo.password,
  95. })
  96. .then(res => {
  97. this.isRotate = false;
  98. const path = this.redirect ? this.redirect : "/pages/home/home";
  99. log("登录成功", path, this.$Router);
  100. this.$store.dispatch("menu/getMenus");
  101. uni.showToast({
  102. type: "success",
  103. url: path,
  104. duration: 2000,
  105. title: "登录成功",
  106. });
  107. this.$Router.replaceAll(path);
  108. })
  109. .catch(err => {
  110. this.isRotate = false;
  111. const msg = "用户名或密码不正确";
  112. // uni.showToast({
  113. // type: "none",
  114. // position: "top ",
  115. // title: msg,
  116. // });
  117. uni.$u.toast(msg);
  118. log("登录失败", err);
  119. return;
  120. });
  121. },
  122. },
  123. };
  124. </script>
  125. <style lang="scss">
  126. .image {
  127. width: 100px;
  128. height: 100px;
  129. display: flex;
  130. margin: 0 auto;
  131. border-radius: 50%;
  132. image {
  133. width: 100%;
  134. height: 100%;
  135. }
  136. }
  137. .u-block {
  138. height: calc(100vh - 200px);
  139. display: flex;
  140. align-items: center;
  141. justify-content: center;
  142. &__content {
  143. width: 400%;
  144. }
  145. }
  146. </style>